简单计算器帮助(c++)

Simple Calculator Help (C++)

本文关键字:c++ 帮助 计算器 简单      更新时间:2023-10-16

我用c++写了一个非常简单的计算器程序,它工作得很好!但是有人能解释一下我如何修改这个代码,使它能够一次添加两个以上的数字吗?

例如,我希望用户能够做2 + 2 + 2,而不是2 + 2。但每次我尝试,它只是把前两个"2"加起来,结果是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 nn";
    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);
    }
}

谢谢你们了!

您的代码被认为是这样,所以您要求输入三个元素: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

注意:我没有测试用例。比如除以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;
}

我几周前写了一个小解析器,你可以研究我的代码,也许(希望)你会发现一些有用的东西。
它不是c++,而是c,所以你的编译器应该能够编译它。

解析器解析任何可以按照以下规则构建的语句:

<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("%dn", 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
$