我有一个文件,我正在读取数据,数据是格式的
[功能] [number1] [number2],其中[number2]是可选的!
例如。
+ 88 19
- 29 28
! 4
+ 2 2
上面的输出:
107
1
当代码格式为[function] [number1] [number2]时,我的代码工作得很好但是当代码中不存在[number2]时我的代码失败了。
到目前为止我的代码:
我正在使用的图书馆:
iostream的
fstream的
cstdlib
串
...
while (infile >> function >> number1>> number2)
{
switch (function)
{
case '+':
addition(number1, number2);
break;
case '-':
subtraction(number1, number2);
break;
case '!':
factorial(number1);
break;
...
如何只读取[number1]并在下一次读取IF [number2]时跳回[功能]不存在。
感谢任何帮助,
谢谢!
答案 0 :(得分:0)
你应该逐行阅读文件。
然后,您应该按' '
解析每一行。您可以使用布尔变量,并在到达第二个空格时将其设为true
。如果你到达字符串的末尾并且布尔值仍然是假的,那么你没有任何数字2。
答案 1 :(得分:0)
这将是适应您的方法的一种方式,但对输入的假设较少。请注意,此代码特定于您选择的格式,并且100%信任您正在阅读的文件是正确的。正如人们在评论中提到的那样,阅读这条线并对其进行解析将更加可靠,并且可能是您应该考虑的未来努力。然而,这只是通过一些修改而完全遵循您当前的方法。
const bool isUnary(char function)
{
if (function == '!') return true;
// Add further unary functions here as if/switch
return false
}
int main()
{
// Only read function here
while (infile >> function)
{
// Prepare your numbers
int number1{}, number2{};
// If the function is Unary (IE: Takes one parameter)
if (isUnary(function))
{
infile >> number1; // Read the one number
}
// Otherwise read both numbers
else
{
infile >> number1;
infile >> number2;
}
// Then switch on function as you otherwise would
switch (function)
{
case '+':
addition(number1, number2);
break;
case '-':
subtraction(number1, number2);
break;
case '!':
factorial(number1);
break;
}
}
return 0;
}
答案 2 :(得分:0)
你可以使用一些技巧;你至少在每一行的开头都有一个算术运算符,例如(" + - *")并且它后面至少有一个参数。因此,您可以使用类型字符的变量来获取操作符,并在读取循环内部后读取剩余的整行,然后我们使用API strtok
传递给它空间('')
在读取运算符和我们根据运算符切换的行之后的循环中,我们调用strtok
来解析行,atoi
将结果转换为整数。
这是一个我为你详细阐述的简单程序,我觉得它运作正常:
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
//using namespace std;
// some forward declarations
int Add(int, int = 0); // as long as you said the second parameter is optional
int Sub(int, int = 0);
int Mult(int, int = 0);
int Div(int, int = 0);
int Fact(int);
int main(){
std::ifstream in("data.txt");
std::string sLine;
char op;
int param1, param2;
while(in >> op){ // getting the first character from the file which is considered to be the operator
std::getline(in, sLine); // after reading the operator above we read the remaining whole line containing 1 or 2 integer values.
switch(op){
case '+':{
// some parsing and converting here
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
// calling relevant functions and printing
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Add(param1, param2) << std::endl;
}
}
break;
case '-':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Sub(param1, param2) << std::endl;
}
}
break;
case '*':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Mult(param1, param2) << std::endl;
}
}
break;
case '/':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
param2 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << " " << op << " "
<< param2 << " = " <<
Div(param1, param2) << std::endl;
}
}
break;
case '!':{
char* token = strtok((char*)&sLine[0], " ");
while(token){
param1 = atoi(token);
token = strtok(NULL, " ");
std::cout << param1 << op << " = " <<
Fact(param1) << std::endl;
}
}
break;
}
}
in.close(); // graceful close
std::cout << std::endl;
std::cin.get();
return 0;
}
int Add (int a, int b) { return a + b;}
int Sub (int a, int b) { return a - b;}
int Mult(int a, int b) { return a * b;}
int Div (int a, int b) { return a / b;}
int Fact(int a) {
int tmp(a - 1);
while( tmp)
a *= tmp--;
return a;
}
文件data.txt
内容:
+ 88 19
- 29 28
! 4
+ 2 2
输出:
88 + 19 = 107
29 - 28 = 1
4! = 24
2 + 2 = 4