Я недавно разработал калькулятор, это код:Есть ли идентификаторы для строк или цифр?
/*
*All 4 operations + percentage finder + Raise to power and more....
* by Ulisse
* [email protected]
* Feel free to take some parts of this code an put them
* in yours, but do not take all the code and change/delete
* the comments to take the credit, trust me, it doesn't
* gives the satisfaction you expect.
*/
#include <iostream> //For cin and cout
#include <iomanip> //For setprecision()
#include <windows.h> //For SetconsoleTitle()
#include <stdlib.h> //For system()
#include <cmath> //For pow()
#include <cctype> //For isdigit()
using namespace std;
int main(){
reset:
system("cls"); //Screen cleaner
system("color 0f");
SetConsoleTitle("Calculator by Ulisse");//Setting window title
char op; //Start of the variables declaration
double a, b, ra;
string p, ms, d, me, e;
p = " + ";
ms = " - ";
d = " : ";
me = " x ";
e = " = "; //End of the variable declaration
cout << "Type now '?' for help page, or another character to continue." << endl;
cin >> op;
if (op == '?'){
help:
system("cls");
cout << "Write the whole operation.\nEXAMPLE: 2^3 \n OUTPUT: 2^3 = 8"<< endl;
cout << "(+) Sum between a and b\n(-) Subtraction between a and b" << endl;
cout << "(^) Raise to power\n(%)finds the a% of b\n(x or *)Multiplicate a by b" << endl;
cout << "(: or /) Divide a by b" << endl;
system("pause");
system("cls");
goto start;
}
else{
system("cls");
while(1){
start:
cout << "CALC> ";
cin >> a;
cin >> op;
cin >> b;
//The four operations
if (op == '+'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << p << b << e << a + b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '-'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << ms << b << e << a - b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '*' || op == 'x'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << me << b << e << a * b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '/' || op == ':'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << d << b << e << a/b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '%'){
cout << "RESULT" << endl;
cout << setprecision(999) << "The " << a << "% of " << b << " is " << b/100 * a << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '^'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << "^" << b << " = " << pow (a, b) << endl;
cout << "________________________________________________________________________________" << endl;
}
//Some useful functions
if (op == 'c'){
system("cls");
}
if (op == '?'){
system("cls");
goto help;
}
if (op == 'r'){
goto reset;
}
if (op == 'b'){
system("color 0c");
Beep(400,500);
cout << "CLOSING, ARE YOU SURE?(y/n)";
system("color 0c");
cin >> op;
if(op == 'y'){
cout << "Closing..." << endl;
system("cls");
system("color 0f");
system("pause");
break;
}
if(op == 'n'){
goto start;
}
}
if (op == '<'){
if (a < b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " TRUE " << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " FALSE " << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '>'){
if (a > b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '='){
if (a == b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
}
}
}
Вот как это работает: Вы пишете число, то оператор (например, +, - плюс другие функции ...) и это делает операция между двумя введенными вами цифрами зависит от того, что является типизированным оператором, поэтому, если вы наберете 4 + 3, он выведет 4 + 3 = 7. Теперь, когда вы понимаете, как это работает, давайте перейдем к qyestion ... Есть ли отступы для числа или символа? При вводе sttring или символа при CIN >> (а не переменное число) приложение начнет печать из символов, которые вы ни разу не вставленные: Input
I think like this(console output) will be printed out(until you dont close the process.
Так что я хотел бы, чтобы предотвратить Applicatin за то, когда вы вводите неверный вход для переменного и делаем его выполняет другую команду, вот что я имею в виду:
if(anumbervariable != number || anumbervariable == string){
cout << "invalid input" << endl;
}
Это не реальный рабочий код, это только представление о том, что я имею в виду, или Я бы не стал заставлять тебя тратить впустую :) Спасибо заранее.
Этот вопрос немного исходит из ошибочного предположения: когда вход не соответствует типу переменной, переменная _not changed_! Поэтому вы не можете проверить 'anumbervariable'. Кроме того, на практике все биты ** интегрального ** численного значения используются для представления значения. Однако для чисел с плавающей запятой существует 'std :: isnan'. Это сокращение от «Не является числом». – MSalters
Вы можете разделить «' cout << "RESULT" << endl; ':" из вашего оператора 'switch' до перед оператором' switch', поскольку он встречается в каждом случае. –
Вы действительно хотите 999 для 'setprecision'? Вы можете использовать числовые ограничения из [этого примера в CppReference.com] (http://en.cppreference.com/w/cpp/io/manip/setprecision). –