You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++读取TXT文件指令实现简易计算器问题求助:jmp指令无法正常工作

Fixing the JMP Instruction in Your Simple Calculator

Hey there! Let's get that jmp instruction working properly. The core issue with your current code is that you're reading the file line-by-line in a loop—you can't easily jump around because the file stream only moves forward sequentially. Instead, we need to first store all your instructions in a list, then use an index to navigate through them freely.

Here's the step-by-step fix:

  1. Store all instructions first
    We'll load every line of your input file into a vector of pairs (each pair holds the command and its number). This lets us access any instruction directly using an index, instead of being tied to the file's read order.

  2. Use a program counter (PC) to track execution position
    Replace your i counter with a pc variable that represents the index of the current instruction we're executing. When we hit a jmp, we just adjust this index to jump to the target line.

  3. Add loop protection
    Without tracking which instructions we've already run, we could end up in an infinite loop (e.g., a jmp that sends us back to a previous instruction). A simple boolean vector will let us mark instructions as visited to avoid this.

Modified Code

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>

using namespace std;

int main() {
    ifstream file("my.txt");
    vector<pair<string, int>> instructions;
    string cmd;
    int num;

    // Load all instructions into memory first
    if (file.is_open()) {
        while (file >> cmd >> num) {
            instructions.emplace_back(cmd, num);
        }
        file.close();
    } else {
        cout << "Error opening file!" << endl;
        cin.get();
        return 1;
    }

    int var = 0;
    int pc = 0; // Tracks the current instruction index
    vector<bool> visited(instructions.size(), false); // Prevent infinite loops

    while (pc < instructions.size() && !visited[pc]) {
        visited[pc] = true;
        auto& current_instr = instructions[pc];
        cmd = current_instr.first;
        num = current_instr.second;

        cout << "Command: " << cmd << " Number: " << num << " ---- PC: " << pc << " // Var: " << var << endl;

        if (cmd == "acc") {
            var += num;
            pc++; // Move to next instruction after acc
        } else if (cmd == "jmp") {
            pc += num; // Jump directly by adjusting the program counter
        } else if (cmd == "nop") {
            pc++; // Do nothing, just move to next instruction
        }
    }

    cout << "\nFinal accumulator value: " << var << endl;
    cin.get();
    return 0;
}

Key Changes Explained

  • Instruction Storage: By loading all commands into a vector, we break free from the file stream's sequential read limitation—now we can jump to any line instantly.
  • Program Counter: The pc variable is our navigation tool. For jmp +262, we just add 262 to pc to jump forward 262 instructions. For acc and nop, we increment pc by 1 to move to the next line.
  • Loop Prevention: The visited array ensures we don't re-run the same instruction twice, which stops the program from getting stuck in an infinite loop (a common edge case with jmp instructions).

Give this code a try—your jmp instructions should now work as expected, and you'll also avoid accidental infinite loops!

内容的提问来源于stack exchange,提问作者Moblie Games

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 07:17:38