我正在为我的 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;
}
我有几个要求:
- 首先,似乎只有除法起作用。检查 main 的第一部分,它要求操作并确认它。它不想为 +、- 或 x 工作,而只为 d
2.检查名为problem2的两条评论。在这些部分继续;并打破;不要正确重启计算器。我想回到 while 循环的开头,并且 goto 应该是不稳定和坏的。
3.你能更正我的代码吗?我不是专家,整个事情做得很肮脏。请向我展示更好的逻辑,以使代码更短、更快、更稳定。
谢谢!附言。我是一个 12 岁的孩子,在网上自学 C++,所以请放慢我的脚步,解释一下你在和一只小狗说话的方式。