在C ++中查找字符并执行与该字符相关的命令 [英] Finding a character and doing a command associated with that character in C++

查看:38
本文介绍了在C ++中查找字符并执行与该字符相关的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有以下代码,基本上我输入了一个文件,并且文件上带有某些命令.例如,文件可能看起来像这样:

I have this following code here and basically I input a file and the file has certain commands on it. For instance, the file may look like this:

s
u 1
u 2
u 3
o 
o
o

每个字符都有一个关联的命令.

Each of these characters has an associated command.

  • S :创建一个空堆栈.
  • u :将一个值压入堆栈.
  • o 从堆栈中弹出一个值,并将其写入输出文件中.
  • S: Creates an empty stack.
  • u: Pushes a value onto the stack.
  • o pops a value off the stack and writes it onto an output file.

我在下面编写了以下代码,但是不确定如何执行 u o 命令.

I have written the following code below but am unsure on how to proceed for the u and o commands.

void Stack::solution(const char *input_path, const char *output_path) {

    string line; 
    ifstream myfile (input_path);
    if (myfile.is_open()) {
        while (myfile.good()) {
            getline (myfile,line);
            while (!myfile.eof()) {
                if (line.find("c") == line.npos) {
                    cout << line << endl;
                }
                if (line.find("s")) {
                    Stack();
                }
                if (line.find("u")) {
                    // need to figure out this part
                }
            }
            
        } 
    }
}

预期输出为:

3
2
1

推荐答案

C ++标准库具有

The C++ standard library has a std::stack class. I'm assuming Stack is a custom class for a school assignment?

您已经知道如何从输入文件中读取行.您可以使用 std :: istringstream 进行解析这些行.

You already know how to read lines from the input file. You can use std::istringstream to parse those lines.

尝试这样的事情:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stack> // <-- remove if you're not allowed to use std::stack
using namespace std;

void Stack::solution(const char *input_path, const char *output_path) {

    ifstream inputFile (input_path);
    ofstream outputFile (output_path);

    if (!inputFile.is_open() || !outputFile.is_open()) {
        return;
    }

    stack<int> stk; // or: Stack stk;
    string line; 

    while (getline (inputFile, line)) {
        istringstream iss (line);
        char cmd;

        if (!(iss >> cmd)) {
            continue;
        }

        switch (cmd) {
            case 's': {
                stk = stack<int>(); // or: stk = Stack();
                // alternatively, if using std::stack:
                // stack<int>().swap(stk);
                break;
            }

            case 'u': {
                int value;
                if (iss >> value) { 
                    stk.push(value);
                }
                break;
            }

            case 'o': {
                if (!stk.empty()) {
                    outputFile << stk.top() << '\n';
                    stk.pop();
                    // or: if your custom Stack has a pop() that returns the popped value:
                    // outputFile << stk.pop() << '\n';
                }
                break;
            }
        }
    }
}

演示

这篇关于在C ++中查找字符并执行与该字符相关的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆