一个简单的加减乘除程序

发布于 2024-11-01 08:47:28 字数 2600 浏览 0 评论 0原文

一个简单的C++计算器程序,用于计算加法、减法、乘法和加法...

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //input declarations as doubles for total and counter
    double total = 0, counter =0;
    //input declarations sign and Q as character
    char sign, Q = 0;
    //input declaration value as double
    double value;



        //A do..while will loop forever (or until we hit the break statement)
        do 
        {
            //The current value is 0.
            cout << "Result :"<<" "<< total << '\n';

            //Please enter an operation
            cout << "Please enter an operation and number : ";
        cin >> sign;

        //If the operation is Q, the program will end.
        if (sign != 'Q')

        cin >> value;
            cin.ignore();


            // If the value input is <=0, you can't divide anything by zero. 
            if (value <= 0)
            {
                cout << "Unknown Operator " << sign <<'\n' ;
            }

            //Otherwise procede with the calulator program
            else
            {
                //If the operation is equal to '+', then the total is added.
                if (sign == '+')
                { 
                    total += value;
                }

                // If the operation is equal to '-', then the value is subtracted from the previous number input. 
                else
                {
                    if (sign == '-')
                    {
                        total -= value;
                    }

                    // If the operation is equal to '*', then the value is multiplied to the previous number input. 
                    else
                    {
                        if (sign == '*')
                        {
                            total *= value;
                        }

                        // If the operation is equal to '/', then the value is divided by the previous number input.
                        else 
                        {
                            if ((sign == '/')&&(value != 0))
                            {
                                total /= value;
                            }
                        }
                    }
                }
            }
        }



            //While the operation is not equal to 'Q', the program will run.
            while (sign != 'Q');



        return 0;
}

上述程序的编码没有错误,但如果我按“Q”退出,它将不停地显示最后的结果。 .一遍又一遍,一遍又一遍。 。 。无论如何,任何人都知道如何在程序中添加平方根。 。

A simple C++ calculator program to calculate addition, subtraction, multiplication and addition...

#include <iostream>
#include <string>
using namespace std;

int main()
{
    //input declarations as doubles for total and counter
    double total = 0, counter =0;
    //input declarations sign and Q as character
    char sign, Q = 0;
    //input declaration value as double
    double value;



        //A do..while will loop forever (or until we hit the break statement)
        do 
        {
            //The current value is 0.
            cout << "Result :"<<" "<< total << '\n';

            //Please enter an operation
            cout << "Please enter an operation and number : ";
        cin >> sign;

        //If the operation is Q, the program will end.
        if (sign != 'Q')

        cin >> value;
            cin.ignore();


            // If the value input is <=0, you can't divide anything by zero. 
            if (value <= 0)
            {
                cout << "Unknown Operator " << sign <<'\n' ;
            }

            //Otherwise procede with the calulator program
            else
            {
                //If the operation is equal to '+', then the total is added.
                if (sign == '+')
                { 
                    total += value;
                }

                // If the operation is equal to '-', then the value is subtracted from the previous number input. 
                else
                {
                    if (sign == '-')
                    {
                        total -= value;
                    }

                    // If the operation is equal to '*', then the value is multiplied to the previous number input. 
                    else
                    {
                        if (sign == '*')
                        {
                            total *= value;
                        }

                        // If the operation is equal to '/', then the value is divided by the previous number input.
                        else 
                        {
                            if ((sign == '/')&&(value != 0))
                            {
                                total /= value;
                            }
                        }
                    }
                }
            }
        }



            //While the operation is not equal to 'Q', the program will run.
            while (sign != 'Q');



        return 0;
}

The coding for the above program has no error but if i press "Q" to quit,it will display the last result non-stop. .Over and over and over and over again. . . Anyway,anyone know how to add square root to the program. .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尬尬 2024-11-08 08:47:28

if (sign != 'Q') ... 替换为 if (sign == 'Q') break;

  • 这将修复许多可能的错误。
  • 通过使用更少的缩进和大括号,这将使您的代码更具可读性。

编辑:正如有人提到的,你可能也应该检查小写。 (如果(符号=='Q'||符号=='q'))。

Replace if (sign != 'Q') ... by if (sign == 'Q') break;

  • This will fix numerous possible errors.
  • This will make your code more readable by using less indentation and braces.

EDIT: as someone mentionned, you should probably be checking lowercase too. (if (sign == 'Q' || sign == 'q')).

苏辞 2024-11-08 08:47:28

作为一个简单的练习,我重构了您的程序以使其更简单。我不保证它有效,但它应该为您提供一个良好的基础:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  //input declarations as doubles for total and counter
  double total = 0, counter =0;
  //input declarations sign and Q as character
  char sign = 0;
  //input declaration value as double
  double value;

  //A do..while will loop forever (or until we hit the break statement)
  do 
  {
    //The current value is 0.
    cout << "Current result: " << total << '\n';

    //Please enter an operation
    cout << "Please enter an operation (or Q to quit) and number: ";
    cin >> sign;

    //If the operation is Q, the program will end.
    if (sign == 'Q' || sign == 'q') {
      cout << "See you!\n";
      break;
    }

    // If the input cannot be transformed into a double
    // abort loop and try again
    if (!(cin >> value)) {
      cerr << "Invalid value entered, try again\n";
      continue;
    }

    switch(sign)
    {
    case '+': total += value; break;
    case '-': total -= value; break;
    case '*': total *= value; break;
    case '/':
      if (value == 0) {
        cerr << "Divide by 0 prevented!\n";
      } else {
        total /= value; break;
      }
    default:
      cerr << "Unknown sign: " << sign << "\n";
    }

  } while (true);

  return 0;
}

要点:

  • 尽可能避免缩进,它们会妨碍可读性(更喜欢继续/中断),
  • 尽可能使用开关(char 和数字)
  • 在必要时检查错误:从用户输入到 double 的转换可能会失败,可能会输入 0

As a simple exercise I have refactored your program to make it simpler. I don't promise it works, but it should give you a nice foundation to build on:

#include <iostream>
#include <string>
using namespace std;

int main()
{
  //input declarations as doubles for total and counter
  double total = 0, counter =0;
  //input declarations sign and Q as character
  char sign = 0;
  //input declaration value as double
  double value;

  //A do..while will loop forever (or until we hit the break statement)
  do 
  {
    //The current value is 0.
    cout << "Current result: " << total << '\n';

    //Please enter an operation
    cout << "Please enter an operation (or Q to quit) and number: ";
    cin >> sign;

    //If the operation is Q, the program will end.
    if (sign == 'Q' || sign == 'q') {
      cout << "See you!\n";
      break;
    }

    // If the input cannot be transformed into a double
    // abort loop and try again
    if (!(cin >> value)) {
      cerr << "Invalid value entered, try again\n";
      continue;
    }

    switch(sign)
    {
    case '+': total += value; break;
    case '-': total -= value; break;
    case '*': total *= value; break;
    case '/':
      if (value == 0) {
        cerr << "Divide by 0 prevented!\n";
      } else {
        total /= value; break;
      }
    default:
      cerr << "Unknown sign: " << sign << "\n";
    }

  } while (true);

  return 0;
}

The main points:

  • avoid indentation when possible, they hamper readability (prefer continue/break)
  • use a switch when possible (it's possible for char and numbers)
  • check for errors when necessary: the conversion from the user input to the double could fail, 0 might be entered
好久不见√ 2024-11-08 08:47:28
#include <math.h>
double result = sqrt (value);
#include <math.h>
double result = sqrt (value);
〃温暖了心ぐ 2024-11-08 08:47:28

对于平方根,
#include <数学>
双 r=sqrt(e);

关于您的其他问题:请更好地缩进您的代码;-)

您确定没有忘记在此行后面放置一个块吗?
if (sign != 'Q')

最简单的方法,在单步模式下使用调试器,您就会明白!

For square root,
#include <math>
double r=sqrt(e);

Concerning your other issue : please indent your code better ;-)

Are you sure you didn't forget to put a block after this line ?
if (sign != 'Q')

Simplest way, use a debugger in step mode and you'll understand!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文