lalalala


私信TA

用户名:zhangshuo

访问量:152259

签 名:

像狗一样的学习,像绅士一样地玩耍。

等  级
排  名 6
经  验 30186
参赛次数 10
文章发表 201
年  龄 12
在职情况 学生
学  校 芜湖市第十一中学
专  业

  自我简介:

今日懒惰流下的口水,将会成为明日里伤心的泪水。

解题思路:





注意事项:





参考代码:

一开始编了个程序,不知咋的指针越界。正确程序:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
char st[1001],a[1000];
string st1;
int ans1,ans2,b[1000],atop,btop,num[1000],n;
int pow1(int x,int y)//计算幂次方 {    
    int hh=x;    
    for (int i=2;i<=y;++i)
    {
        x*=hh;
        x%=32767;
    }    return x;
}
    void js()//四个运算(运算内容为数字栈的顶上两个数和运算符栈的顶上的运算符) {    
    if (a[atop]=='+') b[btop-1]+=b[btop];    
    if (a[atop]=='-') b[btop-1]-=b[btop];    
    if (a[atop]=='*') b[btop-1]*=b[btop];    
    if (a[atop]=='^') b[btop-1]=pow1(b[btop-1],b[btop]);
    b[btop]=0;
    --btop;
    b[btop]%=32767;
    a[atop]='\0';
    --atop;
}
    void doit(){    
    int i,x;
    atop=0;//符号 
    btop=0;//数字
    gets(st); 
    for (i=0;i<=strlen(st)-1;++i)
    {    //    
        if (st[i]==' ')    continue;
        if (st[i]=='(')    a[++atop]='(';//左括号则符号入栈 
        if (st[i]==')'&&atop>=1)//右括号则清空左到右括号间的内容
        {            
        while (a[atop]!='('&&atop>=1)
                js();
            a[atop]='\0';
            --atop;
        }        
        if (st[i]=='+'||st[i]=='-'||st[i]=='*'||st[i]=='^')//若是运算符且该运算符的运算优先级比前一个低就把前一个先算出来 
        {            
         while (num[st[i]]<=num[a[atop]]&&atop>=1)
                js();
            a[++atop]=st[i];
        }        
            if (st[i]=='a')
            b[++btop]=3;//将a代入一个数(3) 
        if (st[i]>=48&&st[i]<=57)//可能数字是多位数 
        {            
            int x=0;            
            while  (st[i]>=48&&st[i]<=57)
            {
                x=x*10+st[i]-48;
                ++i;
            }
            --i;
            b[++btop]=x;
        }
    }    
        while (atop!=0&&a[atop]!='('&&a[atop]!=')')//将未运算完的内容运算完 
        js();
        ans2=(b[1]+32767)%32767;
}
    int main()
    {    
    int x,i;
    num['^']=3;
    num['*']=2;
    num['+']=num['-']=1;//num表示运算符优先级 
    doit();
    ans1=ans2;//ans1表示题干表达式的结果,ans2表示选项表达式的结果 
    cin>>n;getchar();//如果少了getchar本地过不了 
    for (i=1;i<=n;++i)
    {
        doit();        
        if (ans1==ans2)            
        cout<<char(i+64);
    }
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区