我用C ++编写了一个非常简单的计算器程序,它运行正常!但有人可以解释我如何修改此代码,以便它能够一次添加2个以上的数字吗?
所以不要做2 + 2,例如,我希望用户能够做2 + 2 + 2.但是每次我尝试,它只是添加前两个" 2" s并给出4,不管你输入多少+ 2。
以下是代码:
#include <iostream>
using namespace std;
// input function
void Input (float &x, float &y);
float a=1.0, b=1.0, result;
char operation;
int main ()
{
cout << "A simple calculator \n\n";
cin >> a >> operation >> b;
Input (a,b);
cout << result << endl;
system ("pause");
return 0;
}
void Input (float &x, float &y)
{
a = x;
b = y;
switch (operation)
{
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout << "Improper operation. Please input a correct calculation operation: \n";
cin >> a >> operation >> b;
Input (a, b);
}
}
谢谢你们!
答案 0 :(得分:0)
您的代码被认为是您要求输入三个元素:2个数字及其运算符(表示为字符)。
假设你只拍了一枪。如果你想使用更多的运算符和数字,你必须开发一个简单的解析器,它应该继续获取输入,直到找到结束标记,可能在你的情况下结束。
要做到这一点,我会改变你的数据采集和计算方法。你必须有简单的选择:
例如,如果您决定实施第一个选项,则此不完整的代码可能会对您有所帮助:
float Input (float &x, float &y, char operation)
{
float result;
switch (operation)
{
case '+':
result = x + y;
break;
case '-':
result = x - y;
break;
case '*':
result = x * y;
break;
case '/':
result = x / y;
break;
default:
cout << "Improper operation. Please input a correct calculation operation: \n";
}
return result;
}
...
...
string input;
if(!getline( cin, input ) ) return; //Try to read one line.
istringstream lin( input );
float partial = 0.0;
float a;
char op;
while(lin >> op >> a) partial = Input(partial, a, op);
//At this point, partial should contain your final result
答案 1 :(得分:0)
float operate(float a, char operand, float b)
{
float result=0.0;
switch(operand)
{
case '+':
result = (a+b);
break;
case '*':
result = (a*b);
break;
case '/':
result = (a/b);
break;
default:
cout<<"Unknown operand"<<endl;
break;
}
return result;
}
int main()
{
float result=0.0;
char operand;
float a,b;
cout<<"enter a number followed by an operation, followed by a number"<<endl;
while(cin>>a>>operand>>b)
result+=operate(a,operand,b);
cout<<result;
return 0;
}
答案 2 :(得分:0)
解析器解析可以通过以下规则构建的任何语句:
<expression> ::= <term>
| <term> "+" <term>
| <term> "-" <term>
<term> ::= <factor>
| <factor> "*" <factor>
| <factor> "/" <factor>
<factor> ::= <number>
| "(" <expression> ")"
<number> ::= ["-"] ["0" .. "9"]*
这里是代码,它几乎是自我解释,因为它遵循上面定义的规则:
#include <stdio.h>
#include <stdlib.h>
char next;
void getNext(){
next=getchar();
}
int getNum(){ //get a number
int num=0;
if((next<'0')||(next>'9')){
printf("error: expected number; found %c",next);
exit(-1);
}
while((next>='0')&&(next<='9')){
num*=10;
num+=(next-'0');
getNext();
}
return num;
}
int add(int x){
return x+term();
}
int sub(int x){
return x-term();
}
int multiply(int x){
return x*factor();
}
int divide(int x){
return x/factor();
}
int factor(){
int f=0;
if(next=='('){
getNext();
f=expression();
if(next=')')
getNext();
else{
printf("error \")\" expected; found %c",next);
exit(-1);
}
}else{
f=getNum();
}
return f;
}
int term(){
int val;
val=factor();
while((next=='*')||(next=='/')){
char c=next;
getNext();
switch(c){
case '*': val=multiply(val); break;
case '/': val=divide(val); break;
default: printf("error: (*, /) expected; found %c",next);
exit(-1);
}
}
return val;
}
int expression(){
int sign=1;
int val;
if(next=='-'){
sign=-1;
getNext();
}
val=term()*sign;
while((next=='+')||(next=='-')){
char c=next;
getNext();
switch(c){
case '+': val=add(val); break;
case '-': val=sub(val); break;
default: printf("error: (+, -) expected; found %c",next);
exit(-1);
}
}
return val;
}
int main(void) {
getNext();
printf("%d\n", expression());
return 0;
}
示例:
$ gcc main.c -o calc
$ ./calc
1+2+3
6
$ ./calc
(1+2+3)/2
3
$ ./calc
3*(1+2)
9
$ ./calc
-10+20
10
$