You can use some tricks; You have at least at the beginning of each line an arithmetic operator character eg( "+ - *") And at least one argument follows it. So you can use a variable of type character to get the operator and after its read inside the loop you read the remaining whole line then we use the API strtok
passing to it space (' ').
Inside the loop after reading the operator and the line we switch according to the operator we call strtok
to parse the line and atoi
to convert to integer the results.
Here is a simple program I elaborated for you which I think it works fine:
#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;
}
The file data.txt
content:
+ 88 19
- 29 28
! 4
+ 2 2
The output:
88 + 19 = 107
29 - 28 = 1
4! = 24
2 + 2 = 4