make a c++ calculator program This is what i\'ve so far, but it only works for 1
ID: 3722495 • Letter: M
Question
make a c++ calculator program
This is what i've so far, but it only works for 1 line
and i don't know how to square numbers
#include
using namespace std;
int main(){
int numbers;
int sum=0;
int sum1=0;
char opr='+' || '-' ;
int sumb;
while (cin>>sumb){
while(cin>>opr>>numbers){
if (opr=='+'){
sum=sum+numbers;
}
else if (opr=='-'){
sum1=sum1-numbers;
}
else if (opr=='^'){
}
}
sumb+=sum+sum1;
cout<
}
return 0;
}
info:
Write an even better calculator program calc3.cpp that can understand squared numbers. We are going to use a simplified notation X to mean X2. For example, 10 7 -51 should mean 102+7-512 Example: When reading input file formulas.txt 5A; 1000 6A - 5A+ 1; the program should report: $ ./calc3 formulas·txt 25 1012 A hint: To take into account don't add or subtract new numbers right away after reading them. Instead, remember the number, read the next operator and if it is a ,square the remembered number, then add or subtract it.Explanation / Answer
/* program */
#include<iostream>
#include<fstream>
using namespace std;
int main(int argc, char** argv)
{
char old_opr='+',new_opr;
int number,ans=0;
ifstream file;
file.open(argv[1]);
while(file>>number)
{
file>>new_opr;
if(new_opr=='^')
{
number*=number;
file>>new_opr;
}
if(old_opr=='+')
{
ans+=number;
}
else if(old_opr=='-')
{
ans-=number;
}
if(new_opr==';')
{
cout<<ans<<endl;
ans=0;
new_opr='+';
}
old_opr=new_opr;
}
return 0;
}
/* output
formula.txt file
5^;
1000+6^-5^+1;
after running
./a.out formula.txt
25
1012
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.