Enter the plus or minus formula for positive integers less than 100 on the keyboard. Write a program to output the computation result string ., Keyboard Input integer
Question description (50 points): Enter the plus or minus formula of positive integers less than 100 on the keyboard. Please write a program to output the computation result string. The format of the input string is "operand 1 operator operand 2", and "operand" and "operator" are separated by a space. Note: 1. the operand is a positive integer and computing result overflow is not required. 2. If the input formula format is incorrect, the output result is "0 ". Required implementation function: void arithmetic (const char * pInputStr, long lInputLen, char * pOutputStr); [input] pInputStr: input string lInputLen: input string length [Output] pOutputStr: Output string, the space has been opened up, and the length is equal to the input string. [note] You only need to complete this function algorithm, and there is no IO input/output sample input in the middle: 4 + 7 output: 11 input: 4-7 output:-3 input: 9 + 7 output: 0 note: format error my program:
#include<iostream>#include<string>using namespace std;int main(){string s;string s1,s2,oper;getline(cin,s);int a=s.find(" ");s1=s.substr(0,a);s.erase(0,a+1); int b=s.find(" ");oper=s.substr(0,b);s.erase(0,b+1);s2=s;int c=atoi(s1.c_str());int d=atoi(s2.c_str());if(oper=="+")cout<<c+d<<endl;else if(oper=="-")cout<<c-d<<endl;elsecout<<0<<endl;return 0;}
Note that in VC6.0, The getline () function requires two carriage returns to be input. It may be a bug in VC6.0. You can press enter once in VS2010.