繁体   English   中英

C++ 程序,用于开发基于文本的计算器,您可以在其中进行加法、减法、乘法和除法

[英]C++ program to develop a text based calculator where you can do addition, subtraction, multiplication and division

所以我遇到的问题是,当我运行程序时,它要求第一个输入,但在我给操作员之后,output 屏幕刚刚停止,我不得不强制退出 output 屏幕。 我正在使用while循环让用户输入任意数量的数字,当他们输入=符号时,它应该结束程序并打印output。

//  C++ program to develop a simple text based calculator where you can do addition, subtraction, multiplication and division.
#include <iostream>
using namespace std;
class calci
{
        private:
            char op; // variable for the operator
            double no;   // variable for getting input numbers    
            double out; // for the output
        
        public:
            void input(char ,double,double);    //to accept input values as well as calculate the output
            
            void output();      //to print the output
};
void calci :: input(char op,double no,double out)
{   op= '+';        //given as a default value for op so that the while loop can start
    
    while(op != '=')    // Used just to make the user input as many numbers as they want untill they give a '=' sign
    {
        std::cout <<"Enter the first number:"<<endl;
        std::cin >>no;
        std::cout <<"\nEnter the operation to be performed with this number";
        std::cout <<"\nEnter + for Additon \nEnter - for Subtraction \nEnter * for multiplication \nEnter / for division \nEnter = to produce the output"<<endl;
        std::cin >>op;
        while(op != '=')
        {
            switch(op)
            {
                case '+' :
                    out=out+no;
                    break;
                case '-' :
                    out=out-no;
                    break;
                case '*':
                    out=out*no;
                    break;
                case '/':
                    out=out/no;
                    break;
                default:
                    break;
            }
        }
    }
    
}
void calci :: output()
{ 
    cout<<"the final answer is"<<out;
    
}
int main() 
{   
    calci c;
    std::cout << "Hello!"<<endl;
    c.input('+',2.0,2.0);
    c.output();
    return 0;
    
}

我已经修复了程序,删除了 class。 我只使用了一个 function。

//A C++ program to develop a simple calculator where you can do addition, subtraction, multiplication and division with as many numbers as you want.


#include <iostream>
using namespace std;

int main()
{
    char op;    //variable to accept operators
    double no,n1;   //variables to accept input numbers
    double out;     //variable to calculate the output
    cout<<"Hello!"<<endl;
    cout<<"Enter the first number: ";
    cin>>n1;
    out=n1;
    while(op != '=')
    {   std::cout << "\nEnter the operation to be performed ";
        std::cout << "\nEnter \"+\" for Additon \nEnter \"-\" for Subtraction \nEnter \"*\" for multiplication \nEnter \"/\" for division \nEnter \"=\"  to end calculation"<< endl;
        cin>>op;
        if(op == '=')
            goto printing;
        else
        {
            std::cout << "Enter the next number:" << endl;
            std::cin >> no;
            switch(op)
                {
                    case '+' :
                        out = out + no;
                        break;
                    case '-' :
                        out = out - no;
                        break;
                    case '*':
                        out = out * no;
                        break;
                    case '/':
                        out = out / no;
                        break;
                    default:
                        break;
                }
          }
    }
    printing:
      cout<<"The final answer is ="<<out;
    return 0;
}

while do 循环是一个无限循环。 switch-case 语句中的断点结束了 switch-case。 它们不会影响 while 循环。 解决该问题的一种可能性是移动 'std::cin >>op;' 在内循环中。

void calci::input(char op, double no)
{   
    out = 0.0;
    std::cout << "Enter the first number:" << endl;
    std::cin >> no;
    std::cout << "\nEnter the operation to be performed with this number";
    std::cout << "\nEnter + for Additon \nEnter - for Subtraction \nEnter * for multiplication \nEnter / for division \nEnter = to produce the output"<< endl;
    
    while(op != '=')
    {
        std::cin >>op;
        switch(op)
        {
            case '+' :
                out=out+no;
                break;
            case '-' :
                out=out-no;
                break;
            case '*':
                out=out*no;
                break;
            case '/':
                out=out/no;
                break;
            default:
                break;               
        }
    }
}

还有很多问题

  1. output 的计算结果不正确。 因为你在输入 function 中使用了 out 参数。 看一下变量的 scope
  2. calci 中的 out 变量未初始化。 您的程序有未定义的行为
  3. 输入的 op 参数不影响计算,因为第一行覆盖了参数
  4. 外部 while 循环不影响计算。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM