gpt4 book ai didi

c++ - 是否有字符串或数字的标识符?

转载 作者:行者123 更新时间:2023-11-28 02:14:10 25 4
gpt4 key购买 nike

我最近开发了一个计算器,这是代码:

/*
*All 4 operations + percentage finder + Raise to power and more....
* by Ulisse
* ulissebenedennti@outlook.com
* 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。现在您了解了它是如何工作的,让我们来看看问题……是否有数字或字符的标识符?当您在 cin >> (不是数字变量)时键入字符串或字符时,应用程序将开始打印出您从未插入的字符: Input

I think like this(console output) will be printed out(until you dont close the process.

因此,当您为变量键入无效输入并使其执行另一条指令时,我想防止应用程序失败,这就是我的意思:

if(anumbervariable != number || anumbervariable == string){
cout << "invalid input" << endl;
}

这不是一个真正的工作代码,它只是我的意思的一种表示,否则我不会来这里让你浪费你的生命:)提前致谢。

最佳答案

你可以做如下的事情

int getNumber(){
int x;
cin >> x;
while(cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "invalid input"<<endl;
cin >> x;
}
return x;
}

如果你想逐个字符地做事,C++ 有一个 isalpha() 函数,所以你可以使用 !isalpha()。数字限制是在新行之前可以使用的最大缓冲区。如果您将其打印出来,它只是一些大数字,因此它可以忽略该输入量。

关于c++ - 是否有字符串或数字的标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com