创建一个计算器c ++

时间:2014-12-24 03:39:40

标签: c++

我目前的代码是:

#include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;

int main()
{
    char cAgain, type;
    int x, y=0;
    double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
        multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;
    do
    {
        cout << "How many operations would you like to do?" << endl;
        cin >> x;
        if (x <= 0)
        {
            cout << "Error: You must have 1 or more operations." << endl;
        }
        else
        {
            while (x != 0, x--)
            {
                y++;
                cout << "Enter in your "<<y<< " operation. (First Number (+,-,*,/,^) Second Number)" << endl;
                cin >> num1 >> type >> num2;
                switch (type)
                {
                case '+':
                    total = addition(num1, num2);
                    cout << num1 << " + " << num2 << "= " << total<< endl;
                    break;
                case'-':
                    total = subtraction(num1, num2);
                    cout << num1 << " - " << num2 << "= " << total<< endl;
                    break;
                case'/':
                    total = division(num1, num2);
                    cout << num1 << " / " << num2 << "= " << total<< endl;
                    break;
                case'*':
                    total = multiplication(num1, num2);
                    cout << num1 << " * " << num2 << "= " << total << endl;
                    break;
                case'^':
                    total = exponential(num1, num2);
                    cout << num1 << " ^ " << num2 << "= " << total << endl;
                    break;
                }
            }
        }
        cout << "Would you like to run the program again.(Y/N)" << endl;
            cin >> cAgain;
    } while (cAgain == 'Y' || cAgain == 'y');
return 0;
}

double addition(double num1, double num2)
{
    double total;
    total = num1 + num2;
    return (total);
}
double subtraction(double num1, double num2)
{
    double total;
    total = num1 - num2;
    return (total);
}
double division(double num1, double num2)
{
    double total;
    total = num1 / num2;
    return (total);
}
double multiplication(double num1, double num2)
{
    double total;
    total = num1 * num2;
    return (total);
}
double exponential(double num1, double num2)
{
    double total;
    total = pow(num1,num2);
    return (total);
}

目前代码的工作原理也是如此 首先要求
你想做多少次手术?
你输入一个数字说3
然后它会要求您输入您的操作
5 + 5
10个
然后它要求第二个 10 ^ 2个
100个
然后它要求第三个 100-10
90个
然后它询问您是否要重新开始并再次运行该程序。

我试图改变的是5 + 5 ^ 2-10 =它给了我20 基本上我希望用户能够输入整个操作,如5 + 5 ^ 2-10 =,它给我正确的答案。允许用户输入功能并使用&#39; =&#39;并返回开始计算。

3 个答案:

答案 0 :(得分:2)

您可以使用以下技术来解决表达式:

#include<iostream>
#include<cmath>
#include<stdio.h>
#include<cstdlib>
#include<map>
#include<stack>

using namespace std;
int expression_value(string str)
{
    map<char,int>priority;
    priority['^']=3;
    priority['*']=2,priority['/']=2;
    priority['+']=1,priority['-']=1;

    stack<char>op_stack;
    stack<int>val_stack;

    int val=0;
    for(int i=0;str[i];i++)
    {
        if(str[i]>='0'&&str[i]<='9')
          val=val*10+str[i]-'0';
        else
        {
            if(op_stack.empty()) // first operator
            {
                val_stack.push(val);
                op_stack.push(str[i]);
            }
            else if(priority[op_stack.top()] < priority[str[i]]) // current operator is more prior then previous operator. so push it to stack.
            {
                val_stack.push(val);
                op_stack.push(str[i]);
            }
            else // current operator is less prior then previous operator. so calculate previous operators resultant value
            {
                int num1,num2;
                num1=val_stack.top(); val_stack.pop();
                num2=val;
                if(op_stack.top()=='+')
                  //val_stack.push(addition(num1, num2));
                  val_stack.push(num1 + num2);
                else if(op_stack.top()=='-')
                  //val_stack.push(subtraction(num1, num2));
                  val_stack.push(num1 - num2);
                else if(op_stack.top()=='*')
                  //val_stack.push(multiplication(num1, num2));
                  val_stack.push(num1 * num2);
                else if(op_stack.top()=='/')
                  //val_stack.push(division(num1, num2));
                  val_stack.push(num1 / num2);
                else
                  //val_stack.push(exponential(num1, num2));
                  val_stack.push(pow(num1 , num2));
                op_stack.pop(); // as operator's value calculation done, pop it from the stack
                op_stack.push(str[i]); // push the new operator
            }
            val=0;
        }
    }
    val_stack.push(val); // last value

    // calculate remaining operators value
    while(!op_stack.empty())
    {
        int num1,num2;
        num2=val_stack.top(); val_stack.pop();
        num1=val_stack.top(); val_stack.pop();
        if(op_stack.top()=='+')
            //val_stack.push(addition(num1, num2));
            val_stack.push(num1 + num2);
        else if(op_stack.top()=='-')
            //val_stack.push(subtraction(num1, num2));
            val_stack.push(num1 - num2);
        else if(op_stack.top()=='*')
            //val_stack.push(multiplication(num1, num2));
            val_stack.push(num1 * num2);
        else if(op_stack.top()=='/')
            //val_stack.push(division(num1, num2));
            val_stack.push(num1 / num2);
        else
            //val_stack.push(exponential(num1, num2));
            val_stack.push(pow(num1 , num2));
        op_stack.pop();
    }
    return val_stack.top();
}

在这里你必须将整个表达式作为字符串传递 运算符将存储在op_stack中,值存储在val_stack

答案 1 :(得分:0)

你应该实现一个 binary expression tree

我刚修改你的程序以满足你的要求:

#include <iostream>
#include <cstdio>

using namespace std;

char op;

double multiplication()
{
    double product;
    cin>>product;
    while(1)
    {
        cin>>op;
        if(op == '*')
        {
            double temp;
            cin>>temp;
            product *= temp;
        }
        else if(op == '/')
        {
            double temp;
            cin>>temp;
            product /= temp;
        }
        else break;
    }
    return product;
}

double addition()
{
    double sum = multiplication();
    while(op != '=')
    {
        if(op == '+')
            sum += multiplication();
        else if(op == '-')
            sum -= multiplication();
        else
        {
            cout<<"Operator error"<<endl;
            return 0.0;
        }
    }
    return sum;
}

int main()
{
    char cAgain;
    int x;
    do
    {
        cout << "How many operations would you like to do?" << endl;
        cin >> x;
        if (x <= 0)
        {
            cout << "Error: You must have 1 or more operations." << endl;
        }
        else
        {
            while (x != 0, x--)
            {
                double result = addition();
                cout<<result<<endl;
            }
        }
    cout << "Would you like to run the program again.(Y/N)" << endl;
            cin >> cAgain;
    } while (cAgain == 'Y' || cAgain == 'y');

    return 0;
}

答案 2 :(得分:0)

计算一些表达式如a + b,a-b,a ^ b,a * b等很简单。但是如果你想解决这样的表达式:5 + 5 ^ 2-10,我认为你需要学习一些关于Reverse Polish notation的东西。这不是很困难。祝你好运。