-2

I want to create a little "calculator" but I dont know how can I create.

My problem is, I have an input file (.txt) with codes:

acc +40

acc -14

nop +386

jmp +262

acc -4

nop +25

... the "acc" adds the number to my variable

the "jmp" is jump to the line (jmp +500 jump foward 500 line)

the "nop" dont do anything

and here is my code but not working (the acc is okay, but the jmp is not)

ifstream file("my.txt");
    string cmd;
    int num;

    int var= 0;
    int i = 0;

    if(file.is_open())
    {
        while (file >> cmd >> num)
        {

            cout << "Var" << var<< endl;

            cout << "Command: " << cmd << "   Number: " << num<< " ----" << i <<" //  Var: " << var<< endl;
            ++i;
            if(cmd == "acc")
            {
                var= var+ num;
            }

            if(cmd == "jmp")
            {
                ;
            }

        }

        file.close();

    }else {
        cout << "error"<<  endl;
        cin.get();
}
  • 3
    That's not really a calculator, that's a simple (assembly-like) language. – Some programmer dude Aug 30 '21 at 15:57
  • As for your problem, since your "language" is line-based, use `std::getline` to read *all* lines into a string, and use `std::istringstream` to parse each line. Then for `jmp` you can use a loop using `std::getline` to read all lines. At least for forward jumping. If you want backward jumping as well, I recommend you read all lines into a vector of strings, then iterate over the vector using indexes. For jumping, use the operand to modify the current index. – Some programmer dude Aug 30 '21 at 16:00
  • IMHO, you should have a table of or . If the instruction matches an entry, then dereference the pointer to execute a function that can parse and execute the instruction. Create the table with one entry and get it working; then backup. Add another instruction to the table, get it working, then backup. Repeat until all instructions are working. – Thomas Matthews Aug 30 '21 at 18:01
  • Why does `NOP` have a parameter? In the assembly languages that I have seen, the `nop` instruction doesn't have a parameter. – Thomas Matthews Aug 30 '21 at 18:02
  • 1
    @ThomasMatthews Letting `nop` have an operand makes parsing much simpler, as all commands/instructions could be parsed using a single function. At least that's my guess about the rationale for it. – Some programmer dude Aug 30 '21 at 18:10

1 Answers1

-1

This is a sample code. I hope everything here is in order. I did what Some programmer dude told you. Using vectors, you read all the lines into them and then just iterate.

#include <fstream>
#include <iostream>

#include <string> //addition
#include <vector> //addition

using namespace std;

int main() {
    ifstream file("my.txt");
        string cmd;
        int num;

        int var= 0;
        int i = 0;
        
      string my_string;//addition
      vector<int> numbers;//addition
      vector<string> commands;//addition

        if(file.is_open())
        {
        /*this while function reads every line of the file and writes command to the vector of strings named "commands" and the number to the vector of integers named "numbers".*/
            while (getline(file, my_string))//while you can read line from "file", read it and put in into string called "my_string".
            {
            cmd = my_string;
            cmd.resize(3);//leaves only first three characters of the string.
            commands.push_back(cmd);//adds this "cmd" string to vector "commands"  
            
            my_string.erase(0,4);//erases characters from 0 to 4, inclusive, from the string "my_string". So erases first 4 characters, so our command and the space after.
            numbers.push_back(stoi(my_string));//adds my_string, converted to int, to vector "numbers". Stoi() converts string to int.
            ++i;
            }
          
          for(i = 0; i < commands.size(); i++)
          {                          
            cout << "Var " << var << endl;
              if(commands[i] == "acc")
              {
                //remember about "+=", it's quicker this way :)
                var += numbers[i];
              }
            cout << "Command: " << commands[i] << "   Number: " << numbers[i] << " ----" << i <<" //  Var: " << var << endl;
            if(commands[i] == "jmp")
              {
                i+= numbers[i];
              }
               
          }
            file.close();

        }else {
            cout << "error"<<  endl;
            cin.get();
    }
}

Sorry in advance for any formatting issues. My first answer on stackoverflow...

Flower
  • 1
  • 1