【发布时间】:2021-01-05 01:13:04
【问题描述】:
我需要编写一个程序,给出大正整数的算术运算结果。有 4 种基本运算需要考虑:加法 (+)、减法 (-)、乘法 (*) 和整数除法 (/)。标准输入的第一行包含一个整数 Z,它确定在接下来的行中定义的测试数量。每个测试占用标准输入的一行并包含一个算术动作的记录,即由动作运算符分隔的两个数字字符串(没有额外的空格)。数字序列的长度不超过 256 个字符。这就是我写到这一刻的内容:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int Z;
string operacja(256,'\0');
cin >> Z;
vector <string> operacje;
for (int i = 0; i < Z; i++)
{
cin >> operacja;
operacje.push_back(operacja);
}
cout << "" << endl;
for(auto it = begin(operacje); it != end(operacje); ++it)
{
if (find_if(operacje.begin(), operacje.end(), [](const string& str) { return str.find("+") != std::string::npos; }) != operacje.end())
{
cout << "+" << endl;
}
else if (find_if(operacje.begin(), operacje.end(), [](const string& str) { return str.find("-") != std::string::npos; }) != operacje.end())
{
cout << "-" << endl;
}
else if (find_if(operacje.begin(), operacje.end(), [](const string& str) { return str.find("*") != std::string::npos; }) != operacje.end())
{
cout << "*" << endl;
}
else if (find_if(operacje.begin(), operacje.end(), [](const string& str) { return str.find("/") != std::string::npos; }) != operacje.end())
{
cout << "/" << endl;
}
}
return 0;
}
当我运行代码时,我得到了这个:
3
124/5
678-7
8/454545
-
-
-
我应该得到这个:
3
124/5
678-7
8/454545
/
-
/
有人可以帮我解决这个问题吗?
【问题讨论】:
标签: c++ vector foreach substring