回 帖 发 新 帖 刷新版面

主题:[讨论]第77此编程比赛结果


   本题和经典的题目还是有些不同的,增加了 { [ 的情况,要写出这题的文法还是有点困难。这是我第一次主持比赛,过程中有激动,也有失落。收获很大。希望以后有更多的高手来主持编程比赛。

   liuwenhan 的编程思路完全和常规的 “算符优先法” 不相同,并且他的热情也很高,总共提
交了三次代码。

   我对liuwenhan 的代码进行简单修改后 进行了测试。
 
   我找到两组测试没有通过,看 liuwenhan 能不能改进。

   2*(1+2)4     输出:2
  1+28*【1+1)    输出:58


   从提交的代码来看,只有 liuwenhan 提交的最全,写得最好!所以,下次的编程比赛就请liuwenhan 主持了。

   大家对 liuwenhan 的编程热情 和 智慧 鼓掌!

回复列表 (共5个回复)

沙发

鼓掌!~~

板凳


o ,wo xiang xiang a

3 楼

#include<iostream>
#include<cctype>
#include<cmath>
#include<stdio.h>
#include<string.h>
using namespace std;
void badshow();
void highlevel(const char*);
double result(double a,double b,char c);
bool done(const char*,int);
void suanfa(const char*);
void init(const char *);
int top=-1;//for level
int top1=-1;//for sign
int top2=0;//for number
double* number;//store number
char* sign;//store sign
static int i=-1;//piont to the shizi array
int* level;//store the level
int mark;//the current level
int* aim;
int first;
int wrong;//
int nummark;//new
int main()
{    
    char *shizi;
    wrong=0;
    first=0;
    mark=25;//>0
    nummark=0;//new
    number=new double[100];
    sign=new char[20];
    level=new int[20];
    aim=new int[10];
    shizi=new char[100];
    level[0]=-4;
    //cin.unsetf(ios::skipws);
    while(cin>>shizi && shizi!="quit")
    {    
        suanfa(shizi);
        if(wrong!=1) break;
        init(shizi);
    }
    cout<<"the final result is "<<number[0];
    return 0;

}
void highlevel(const char* shizi)
{    
    if(wrong==1) return;
    if(mark==-1 && top1!=-1)
    {
        badshow();
        return;
    }    
    double a,b;
    if(mark==0)
        if(sign[top]!=shizi[i] && (top==0 || top==-1)) 
        {
            badshow();
            return;
        }
        else if(level[top]==0)
        {
            if(sign[top]!=shizi[i])
            {
                cout<<"The position of "<<i+1<<" or "<<aim[top1]<<" is wrong"<<endl;
                wrong=1;
            }
            top--;
            top1--;
            return;
        }    
    if(top==-1)
    {        
        level[++top]=mark;
        sign[top]=shizi[i];
        
    }
    else if(mark<=level[top])
    {            

        if(top>=0)
        {    
            a=number[top2--];
            number[top2+1]=0.0;
            b=number[top2];
            //cout<<"After the functions b is "<<b<<",a is "<<a<<".And there are "<<top2+1<<" numbers in array currently"<<endl;//check the problem
            number[top2]=result(b,a,sign[top--]);
            if(mark==-1 && top2==0) return;    

        }        
        highlevel(shizi);                
                    
    }
    else
    { 
        level[++top]=mark;    
        sign[top]=shizi[i];
    }
    
    return;
}

4 楼

void suanfa(const char* shizi)
{    
    if(wrong==1) return;
    int flag=0;//mark :behind the number isn't '{','[','('
    char sign1;
    for(i=i+1;i<=strlen(shizi);i++)
    {
        if(i==strlen(shizi)) 
        {    
    
            mark=-1;
            //cout<<top1;
            if(top1!=-1)
            {
                cout<<"The position of "<<aim[top1]-1<<" is wrong."<<endl;
                wrong=1;            
            }
            if(top2==0) return;            
            //cout<<number[top2]<<endl<<top2<<endl;//check
            //cout<<number[top2-1]<<endl<<top2-1;//check
            highlevel(shizi);
            return;
        }
        if(isalpha(shizi[i]))
        {        
            cout<<"The position of "<<i+1<<" is character"<<endl;
            wrong=1;
            return;
        }        
        else if(isdigit(shizi[i]))
        {       
            if(first==0)
            {
                first=1;
                flag=1;
        
            }      
            if(nummark==1)
            {
                 badshow();
                return;
            }            
            if(flag==0)
                top2++;           
            number[top2]=number[top2]*10+(double)shizi[i]-48.0;
            flag=1;
            nummark=0;
        }

        else if(shizi[i]=='.')
        {
            if(nummark==1)
            {
                 badshow();
                return;
            }
            done(shizi,flag);
            continue;
            
        }

        else
        { 
            
            nummark=0;

            if(shizi[i]=='{' || shizi[i]=='(' || shizi[i]=='[')
            {    
                    
                if(flag==1)
                {     
                    badshow();
                    return;            
                }
                if(shizi[i]=='{')
                sign1='}';
                else if(shizi[i]=='[')
                sign1=']';
                else sign1=')';
                            
                sign[++top]=sign1;
                aim[++top1]=i+1;
                mark=0;
                level[top]=mark;
                suanfa(shizi);            
                
            }
            else if(shizi[i]=='^')
            {            
                mark=3;
                highlevel(shizi);

            }

            else if(shizi[i]=='*' || shizi[i]=='/')
            {
                mark=2; 
                highlevel(shizi);
            }

            else if(shizi[i]=='+' || shizi[i]=='-')
            {
                mark=1; 
                highlevel(shizi);

            }
            else if(shizi[i]=='}' || shizi[i]==')' || shizi[i]==']')
            {
                mark=0;
                highlevel(shizi);
                nummark=1;
                return;
                                
            }
            else
            {                
                badshow();
                return;                                
            }

            flag=0;
            
        }
    }



}            

5 楼

bool done(const char* shizi,int b)
{
    double j=0;
    if(b!=1)
    {
        badshow();
        return false;
    }
    else if(isalpha(shizi[++i]))
    {
        badshow();
        return false;
    }
    else
    {
        for(i;i<strlen(shizi);i++)
        {    
            if(isdigit(shizi[i]))
            {
                j++;    
                number[top2]=double(shizi[i]-48)/pow(10.0,j)+number[top2];
            }
            else 
            {
                i--;
                return false;
            }
        
        }
        i--;
        return true;
    }

}

double result(double a,double b,char c)
{
    switch(c)
    {
        case '^':
            return pow(a,b);
        case '*':
            return a*b;
        case '/':
            return a/b;
        case '+':
            return a+b;
        case '-':
            return a-b;

    
    }



}
void init(const char* shizi)
{
    top=-1;
    top1=-1;
    top2=0;
    first=0;
    i=-1;
    level[0]=-4;
    wrong=0;
    nummark=0;
    mark=25;//mark>0
    delete []number;
    delete []level;
    delete []sign;
    delete []aim;
    delete []shizi;
    number=new double[100];
    sign=new char[20];
    level=new int[20];
    aim=new int[10];
    shizi=new char[100];
    cout<<"Please re-input:";

}
void badshow()
{
    cout<<"The position of "<<i+1<<" is wrong"<<endl;
    wrong=1;
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册