四则运算改进版

需求分析:

  随机生成四则运算公式,包括括号并能对题目进行回答,在对所答题目进行判断对错,如果答错了显示正确答案。

代码设计:

  后缀表达式:

  

void houzhuibds(char str[100])//后缀表达式 
{
    
    stack<char>s1;
    stack<Number>s2;
    int i=0,j=0;
    for(i=0;str[i]!='';i++)
    {
        if(str[i]>='0'&&str[i]<='9')
        {
            Number num;
            num.a=0;
            num.b=0;
            while(str[i]<='9'&&str[i]>='0')
            num.a=(str[i++]-'0')+num.a*10;
            s2.push(num);    
            i--;
        }
        else
        {
            if(str[i]==')')
            {
                while(s1.top()!='(')
                {
                    Number num;
                    num.b=1;
                    num.a=s1.top();
                    s2.push(num);
                    s1.pop();
                }
                s1.pop();
            }
            else if(s1.empty()||s1.top()=='('||str[i]=='(')
            {
                s1.push(str[i]);
            }
            else
            {
                if((str[i]=='*'||str[i]=='/')&&(s1.top()=='+'||s1.top()=='-'))
                s1.push(str[i]);
                else
                {
                    Number num;
                    num.b=1;
                    num.a=s1.top();
                    s2.push(num);
                    s1.pop();
                    i--;
                }
            }            
        }
    }
    while(!s1.empty())
    {
        Number num;
        num.b=1;
        num.a=s1.top();
        s2.push(num);
        s1.pop();
    }
    while(!s2.empty())
    {
        s3.push(s2.top());
        s2.pop();
    }
}

  求值运算:

double qiuzhi()
{
    stack<double>s4;
    while(!s3.empty())
    {
        Number c1=s3.top();
        s3.pop();
        if(c1.b==0)
        s4.push(c1.a);
        else
        {
            double c2=s4.top();
            s4.pop();
            double c3=s4.top();
            s4.pop();
            double c4;
            switch((int)c1.a)
            {
                case '+':c4=c3+c2;break;
                case '-':c4=c3-c2;break;
                case '*':c4=c3*c2;break;
                case '/':c4=c3/c2;break;
            }
            s4.push(c4);
        }
    }
    return s4.top();
}

  判定数字后元素:

void aftersz(char str[100],int t,int n)//数字后面是+-/*(0-4)  右括号(5-9)
{
    int i=rand()%10;
    if(i<=4)
    {
        int j=rand()%4;
        switch(j)
        {
            case 0:str[t]='+';break;
            case 1:str[t]='-';break;
            case 2:str[t]='*';break;
            case 3:str[t]='/';break;
        }
        afterfh(str,++t,n);
    }
    else//右括号 
    {
        if(y>0&&(str[t-2]!='('||str[t-3]!='('&&(str[t-2]<='0'&&str[t-2]>='9')))
        {
            str[t]=')';
            y--;
            aftersz(str,++t,n);
        }
        else
        aftersz(str,t,n);   
    }    
    
}

  判定符号后元素:

void afterfh(char str[100],int t,int n)//+-*/之后 
{
        int p=rand()%10;
        if(p>=3)//数字 
        {
            int num=rand()%100;
            if(num>=10)
            {
                int a=num%10;
                str[t++]=num/10+'0';
                str[t]=a+'0';
            }
            else
            str[t]='0'+num;
            n++;
            if(n==4)
            {
                str[++t]='';
                return ;    
            }
            aftersz(str,++t,n);
        }
        else//左括号 
        {
            str[t]='(';
            y++;
            afterfh(str,++t,n);
        }
}

  主函数:

int main()
{
    char str[100];
    int s=0;
    for(int i=0;i<20;i++)
    {
        int t=0;
        int n=0;
        afterfh(str,t,n); 
        int len=strlen(str);
        while(y)
        {
            if(str[len-2]=='(')
            {
                str[len-2]=str[len-1];
                len--;
            }
            else if(str[len-3]=='(')
            {
                str[len-3]=str[len-2];
                str[len-2]=str[len-1];
                len--;
            }
            else
            str[len++]=')';    
            y--;
        }
        str[len]='';
        houzhuibds(str);
        double ans=qiuzhi();
        for(int i=0;str[i]!='';i++)
        printf("%c",str[i]);
        printf("=
?");
        double s;
        scanf("%lf",&s);
        if(abs(s-ans)<0.01)
        {
            s++;
            printf("答对了,你真是天才
");
        }
        else
        printf("再想想吧,答案似乎是%.2lf喔!
",ans);
    }
    printf("你一共答对%d道题,共20道题。
",s);
    return 0;
}

运行结果:

 将随机出的题和答案打印到硬盘中代码为:

    FILE *fp;
    fp=fopen("D:\2.txt","w");
    char str[100];
    char s[100];
    int z=0;
    while(1)
    {
        int k=0,flag=0;
        scanf("%s",&s);
        while(s[k]!='')
        {
            if(s[k]>='0'&&s[k]<='9')
            z=z*10+s[k]-'0';
            else
            {
                flag=1;
                break;
            }
            k++;
        }
        if(flag==1)
        else
        break;
    }
    getchar();
    fprintf(fp,"%25s",str);
        fprintf(fp,"=                   %.2lf
",ans);

结果为:

ssh:git@git.coding.net:ziyoujay/sizeyunsuan1.git

我的合作者:王森

原文地址:https://www.cnblogs.com/ziyoujay/p/5870279.html