字符串或数字是否有标识符?

时间:2016-01-01 21:07:20

标签: c++

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

/*
*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&gt;&gt;时输入一个字符串或一个字符(不是数字变量)应用程序将开始打印您从未插入过的字符: 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;
}

这不是一个真正的工作代码,它只是我的意思的代表,或者我不会让你浪费你的生命:) 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作

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()。数字限制是在新行之前可以采用的最大缓冲区。如果您将其打印出来,它只是一些大数字,以便它可以忽略该输入量。