Write a c++ program that reads arithmetic equations from an inputfile, say input
ID: 3610495 • Letter: W
Question
Write a c++ program that reads arithmetic equations from an inputfile, say input.txt, and produces the calculation resultsto an output file, say output.txt. Suppose only basicarithmetic equations, namely + - * /, are considered, and eachequation involves only two operands. Operands and operator areseparated by blank space. The format for input.txt shouldlook like this:
98 + 3
98 - 3
98 * 3
98 / 3
After calculation, the written contents of output.txtshould look like this:
98
+ 3
----------
101
=============================
98
- 3
----------
95
=============================
98
* 3
----------
294
=============================
98
/ 3
----------
32.6667
=============================
Explanation / Answer
//create a file namedinputfile.txt and copy and paste youreqation// //if problem=sloved thenrate=best#include<iostream.h> #include<conio.h> #include<fstream.h> #include<string> using std::string;
main() { ifstream readFile; ofstream writeFile("output.txt");
int answer; char operand1[10]; char _operator[2]; char operand2[5]; readFile.open("input.txt"); if(!readFile){ cout<<"File is not Here";} else{
do{ readFile>>operand1>>_operator>>operand2;
int f_operand=atoi(operand1); int s_operand=atoi(operand2); int opr_asci=*_operator;
if(opr_asci==43) { answer=f_operand+s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<answer; writeFile<<" ===================== "; } else if(opr_asci==45) { answer=f_operand-s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<answer; writeFile<<" ===================== "; } else if(opr_asci==42) { answer=f_operand*s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<answer; writeFile<<" ===================== "; } else if(opr_asci==47) { //answer=f_operand/s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<f_operand*1.00/s_operand*1.00; writeFile<<" ===================== "; } else { //cout<<_operator; //cout<<" WRONGOPERTOR: "; } } while(!readFile.eof());
} cout<<"Program completed: ok"; getch(); }
//create a file namedinputfile.txt and copy and paste youreqation// //if problem=sloved thenrate=best
#include<iostream.h> #include<conio.h> #include<fstream.h> #include<string> using std::string;
main() { ifstream readFile; ofstream writeFile("output.txt");
int answer; char operand1[10]; char _operator[2]; char operand2[5]; readFile.open("input.txt"); if(!readFile){ cout<<"File is not Here";} else{
do{ readFile>>operand1>>_operator>>operand2;
int f_operand=atoi(operand1); int s_operand=atoi(operand2); int opr_asci=*_operator;
if(opr_asci==43) { answer=f_operand+s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<answer; writeFile<<" ===================== "; } else if(opr_asci==45) { answer=f_operand-s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<answer; writeFile<<" ===================== "; } else if(opr_asci==42) { answer=f_operand*s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<answer; writeFile<<" ===================== "; } else if(opr_asci==47) { //answer=f_operand/s_operand; writeFile<<" "<<f_operand<<endl; writeFile<<_operator<<" "<<s_operand<<endl; writeFile<<"--------------------- "; writeFile<<" "<<f_operand*1.00/s_operand*1.00; writeFile<<" ===================== "; } else { //cout<<_operator; //cout<<" WRONGOPERTOR: "; } } while(!readFile.eof());
} cout<<"Program completed: ok"; getch(); }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.