我正在使用xcode作为我的c ++。它是一个简单的命令行计算器。 这就是我到目前为止所做的:
//
// 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;
}
我有几个请求:
2.检查名为problem2的两条评论。在这些部分继续;并打破;不要正确重启计算器。我想回到while循环的开头,goto应该是不稳定和不好的。
3.你能纠正我的代码吗?我不是专家,整件事情非常糟糕。请告诉我更好的逻辑,使代码更短,更快,更稳定。
谢谢! PS。我是一个12岁的孩子,在互联网上教自己c ++,所以请给我一些松懈,解释一下你和小狗说话的事情。
答案 0 :(得分:1)
你的问题是if (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;
}
现在,如果所有以前的其他条款都被激活,那么最终的其他只会激活。
可选地
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
。它会回到循环的顶部,如果条件为真,则重新开始。
尝试将变量声明到更接近它们的位置。例如,在循环的后期才使用answer和opersym。您可以将它们声明为if (gooy == true)
答案 1 :(得分: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);
}