How to resolve the algorithm Execute HQ9+ step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Execute HQ9+ step by step in the D programming language

Table of Contents

Problem Statement

Implement a   HQ9+   interpreter or compiler.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Execute HQ9+ step by step in the D programming language

Source code in the d programming language

import std.stdio, std.string;

void main(in string[] args) {
    if (args.length != 2 ||
        args[1].length != args[1].countchars("hHqQ9+")) {
        writeln("Not valid HQ9+ code.");
        return;
    }

    ulong accumulator;
    foreach (immutable c; args[1]) {
        final switch(c) {
            case 'Q', 'q':
                writeln(args[1]);
                break;
            case 'H', 'h':
                writeln("Hello, world!");
                break;
            case '9':
                int bottles = 99;
                while (bottles > 1) {
                    writeln(bottles, " bottles of beer on the wall,");
                    writeln(bottles, " bottles of beer.");
                    writeln("Take one down, pass it around,");
                    if (--bottles > 1)
                        writeln(bottles,
                                " bottles of beer on the wall.\n");
                }
                writeln("1 bottle of beer on the wall.\n");
                break;
            case '+':
                accumulator++;
                break;
        }
    }
}


  

You may also check:How to resolve the algorithm History variables step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Entropy step by step in the Julia programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Transact-SQL programming language
You may also check:How to resolve the algorithm Nonoblock step by step in the Ruby programming language
You may also check:How to resolve the algorithm Power set step by step in the Burlesque programming language