1

我正在为我的 c++ 使用 xcode。它是一个简单的命令行计算器。这是我到目前为止所拥有的:

//
//  main.cpp
//  test
//
//  Created by Henry Bernard Margulies on 8/21/13.
//  Copyright (c) 2013 Henry Bernard Margulies. All rights reserved.
//
#include <iostream>
#include <string>

using namespace std;

int main()
{
    bool loopy = true;  
    cout << "\nCalculator\n";       
    while (loopy == true)
    {
            bool gooy;          
            double answ;            // answer
            double fn;             // first number
            double sn;             // second number
            string opersym;     // operation symbol
            string oper;        // operation
            string more;        // rerun the program or not
            cout << endl << "Operation please (+, - , x or d): ";  //Problem1
            cin >> oper;                                        
            if (oper == "+")                    //makes sure operation is viable
            {
                gooy = true;
            }
            if (oper == "-")
            {
                gooy = true;
            }
            if (oper == "x")
            {
                gooy = true;
            }
            if (oper == "d")
            {
                gooy = true;
            }                                   //does the above
            else                
            {
                cout << endl << "Enter a real operation";       //complains if oper not viable
                gooy = false;
                continue;
            }
            if (gooy == true)                      
                cout << endl << "First number please: ";        
                if(!(cin >> fn))                                //makes sure it is a number
                {
                    cerr  << endl << "Enter a number next time, please try again"; //complaint
                    gooy = false;
                    loopy = true;
                    break;                            //Problem2
                }
                if (gooy == true)     
                {
                    cout << endl << "Next number: ";                                    
                    if(!(cin >> sn))                        
                    {
                        cerr  << endl << "Enter a number next time, please try again";
                        gooy = false;
                        loopy = true;
                        break;                  //Problem2                       
                    }
                    if (gooy == true)
                    {
                        opersym = oper;
                        if (oper == "+")
                            answ = fn + sn;
                        if (oper == "-")
                            answ = fn - sn;
                        if (oper == "x")
                            answ = fn * sn;
                        if (oper == "d")
                        {
                            opersym = "÷";
                            answ = fn / sn;
                        }
                        cout << endl << "You entered: " << fn << " " << opersym << " " << sn << ". And it equals " << answ;
                        cout << endl << "Want more? y/n: ";
                        cin >> more;
                        if (more == "n")
                        {
                            cout << endl << "Okay, I'm not wanted. Shutting down. :(";
                            return(0);
                        }
                        if (more == "y")
                        {   
                            cout << endl << "Back to work!";
                        }
                        else
                        {
                            cout << endl << "Since you can not be bothered to type it right, I'll take it as a no. :(";
                            return(0);
                        }
                    }
                }

    }
    return 0;
}

我有几个要求:

  1. 首先,似乎只有除法起作用。检查 main 的第一部分,它要求操作并确认它。它不想为 +、- 或 x 工作,而只为 d

2.检查名为problem2的两条评论。在这些部分继续;并打破;不要正确重启计算器。我想回到 while 循环的开头,并且 goto 应该是不稳定和坏的。

3.你能更正我的代码吗?我不是专家,整个事情做得很肮脏。请向我展示更好的逻辑,以使代码更短、更快、更稳定。

谢谢!附言。我是一个 12 岁的孩子,在网上自学 C++,所以请放慢我的脚步,解释一下你在和一只小狗说话的方式。

4

2 回答 2

1

你的问题是 else afterif (oper == "d")如果操作不是 d,else 子句将激活,即使之前选择了一个操作。试试这个。

if (oper == "+")
{
    gooy = true;
}
else if (oper == "-")
{
    gooy = true;
}
else if (oper == "x")
{
    gooy = true;
}
else if (oper == "d")
{
    gooy = true;
}
else                
{
    cout << endl << "Enter a real operation";       //complains if oper not viable
    gooy = false;
    continue;
}

现在,如果所有先前的 else 子句都已激活,则最后的 else 将仅激活。

或者

if (oper == "+" || oper == "-" || oper == "x" || oper == "d")
{
    gooy = true;
}
else                
{
    cout << endl << "Enter a real operation";       //complains if oper not viable
    gooy = false;
    continue;
}

break退出它所在的循环。试试吧continue。它回到循环的顶部,如果条件为真,则重新开始。

尝试在更接近使用它们的位置声明变量。例如 answ 和 opersym 直到循环的后期才使用。您可以将它们声明为 if 语句块的本地if (gooy == true)

于 2013-08-22T03:46:23.827 回答
0

首先,祝你学习C++好运。我相信你很快就会学会它:)这是一个基本的计算器。它不理想,但更短。

#include <iostream>
#include <string>

int main()
{
    using namespace std; //use namespace only within the scope of main()

    //ask user to choose operation
    string operation = "";//good to initialize local variable. otherwise C++ assigns them garbage values
    while(operation != "+" && operation != "-" && operation != "*" && operation != "/")
    {
        cout << "Please enter a mathematical operation. Options are: + or - or * or /" << endl;
        cin >> operation;
    }
    cout << "You entered " <<  operation << endl << endl;

    //ask user to enter two numbers
    double number1 = 0, number2 = 0, result = 0;
    bool success = false;//true if calculation carried out successfully

    //keep looping till calculation carried out successfully
    while(success!=true)
    {
        cout << "Please enter the first number: " << endl;
        cin >> number1;
        cout << "Please enter the second number: " << endl;
        cin >> number2;

        if(operation == "+") result = number1 + number2;
        else if(operation == "-") result = number1 - number2;
        else if(operation == "*") result = number1*number2;
        else if(operation == "/" && number2 != 0) result = number1/number2;
        else 
        {
            cout << "Please enter non-zero value for number2 since carrying out division" << endl;
            continue;
        }
        success = true;
        cout << "Result is: " << number1 << " " << operation << " " << number2 << " = " << result << endl;
    }
    return(0);
}
于 2013-08-22T04:01:11.120 回答