是否可以将字符串表达式的结果转换为int?

时间:2019-11-26 19:53:43

标签: c++ string type-conversion int

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

int main()
{
    int a;
    string exp1,exp2,sign2,dec2,dec3;
    char sign;
    float op1=0,op2=0,answer=0,op3=0,exper1=0,exper2=0;
    string dec,clear;

    /*----------------------------------------------section of choices----------------------------------------------*/
    cout<<"Enter the number for desired operation\n"<<endl;                                  
    cout<<"Enter '1' for arithmatic opertions "<<endl;
    cout<<"Enter '2' for increament/decreamenr operations"<<endl;
    cout<<"Enter '3' for logical operations"<<endl;       
    cin>>a;

    /*----------------------------------------------section of Arithmetic operators----------------------------------------------*/
restart:

    switch(a)
    {
    case 1:                                                   
        cout<<"Section of Arithmetic opearators\n";
        cout<<"\n";
        cout<<"enter first operand\n";
        cin>>op1;

        cout<<"Enter operator\n";
        cin>>sign;

        cout<<"Enter second operand\n";
        cin>>op2;

        cout<<"if you want to Start calculation press small 's'\ntype 'end' to end programe\n";
        cin>>dec;

        if(dec=="s")
        {
            goto start;
        }
        else
        {
            goto end;
        }

start: 
        switch (sign)
        {
        case '+':
            answer=op1+op2;
            break;

        case '-':
            answer=op1-op2;
            break;

        case '*':
            answer=op1*op2;
            break;

        case '/':
            answer=op1/op2;
            break;

        case '=':
            answer=op1=op2;
            break;

        case '>':
            answer=op1>op2;
            if (answer==0)
            {
                cout<<"the expression evaluates to fales\n";
            }
            cout<<"the expression evaluates to true\n";

            break;

        case '<':
            answer=op1<op2;
            if (answer==0)
            {
                cout<<"the expression evaluates to fales\n";
            }
            cout<<"the expression evaluates to ture\n";

            break;
        default:
            cout<<"Enter a valid arithmetic operator!\n";
        }
        cout<<"the answer to the operation is "<<answer<<"\n\n\n<<"<<flush;
        cout<<"Press 'c' to clear screen and do more operations\n Enter end to end programe\n";
        cin>>clear;
        if (clear=="c")
        {
            system("cls");
            goto restart;
        }
        exit(0);        

        break;

    /*----------------------------------------------Increment/Decreament operator----------------------------------------------*/

    case 2:                                          
        cout<<"Section of increament decreament operatotrs\n";
        cout<<"Enter The Operand\n";
        cin>>op3;
        cout<<"Enter the operator(+ or -)\n";
        cin>>sign;
        cout<<"if you want to Start calculation press small 's'\ntype 'end' to end programe\n";
        cin>>dec2;

        if(dec2=="s")
        {
            goto start2;
        }
        else
        {
            goto end2;
        }

start2: 
        switch (sign)
        {
        case '+':
            op3=op3+1;
            cout<<"The increamented value is "<<op3<<"\n";
            break;

        case '-':     
            op3=op3-1;
            cout<<"The decreamented value is \n"<<op3<<"\n";
            break;

        default:
            cout<<"enter a valid operator\n\n";
            break;       
        }

        cout<<"Press 'C' to clear screen and do more operations\n Enter end to end programe\n";
        cin>>clear;
        if (clear=="c")
        {
            system("cls");
            goto restart;
        }
        exit(0);

        break;
        /*----------------------------------------------Section of logical statements----------------------------------------------*/
    case 3:                                                      
        cout<<"section of logical statements\n";

        cout<<"Enter first expression in brackets '()'\n";
        cin>>exp1;
        istringstream(exp1)>>exper1; 

        cout<<"Type '&&' or '||' \n";
        cin>>sign2;

        cout<<"Enter second expression in brackets '()'\n";
        cin>>exp2;
        istringstream(exp2)>>exper2;

        cout<<"if you want to Start calculation press small 's'\ntype 'end' to end programe\n";
        cin>>dec3;
        if(dec3=="s")
        {
            goto start3;
        }
        else
        {
            goto end3;
        }

start3: 
        if (sign2=="&&")
        {
            answer=exper1&&exper2;
            if (answer==0)
            {
                cout<<"The experrssion evalutes to false\n";
            }
            else
            {
                cout<<"The experession evaluates to ture\n";
            }
        }
        else
        {
            answer=exper1||exper2;
            if (answer==0)
            {
                cout<<"The experrssion evalutes to false\n";
            }
            else
            {
                cout<<"The experession evaluates to ture\n";
            }            
        }
        cout<<"Press 'C' to clear screen and do more operations\n Enter end to end programe\n";
        cin>>clear;
        if (clear=="c")
        {
            system("cls");
            goto restart;
        }
        exit(0);
        break;
    default:
        cout<<"enter any valid option\n";
     }
end:
end2:
end3: 
    return 0;
}

我正在制作一个简单的程序,该程序从用户那里获取两个关系表达式,并使用逻辑运算符对它们进行求值。我使用sstream将字符串转换为int,但我认为我做错了,因为它仅将int评估为0.我只是想获取字符串的评估以及它的答案(无论将0还是1存储到int数据中)类型。

0 个答案:

没有答案