| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2524 人关注过本帖
标题:利用栈求表达式的值
只看楼主 加入收藏
文思旋风
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-6-12
收藏
 问题点数:0 回复次数:2 
利用栈求表达式的值
要求:建立试题库文件,随机产生n个题目;题目涉及加减乘除,带括弧的混合运算;随时可以退出;保留历史分数,能回顾历史,给出与历史分数比较后的评价
搜索更多相关主题的帖子: 历史 表达式 
2012-06-13 20:07
文思旋风
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-6-12
收藏
得分:0 
#include<iostream>
#include"conio.h"
#define maxsize 100
#include "ctype.h"
typedef char datatype;
typedef struct
{
    datatype stack[maxsize];
    int top;
} seqstack;
#define STACK_INIT_SIZE    100
 template <class T>
 class stack
 {
 private:
          T*base;
          T *top;
          int stacksize;
 public:
     stack()
     {
         InitStack(seqstack *);
     }
     ~stack()
     {
         if()
         {
             delete[]base;
         }
     }
 void InitStack(seqstack *s);           // 创建栈
 int  noempty(seqstack s)                              
 void Push(seqstack *s);                            // 向栈中放入数据[压栈]
 void Pop(seqstack *s,datatype *d);                            // 从栈中取数据[弹栈]
int  GetTop(seqstack s,datatype *d);                                // 查看栈顶数据
 };

template <class T>
 void stack<T>:: InitStack(seqstack *s)                            // 创建栈
 {
     s->top=0;
 }

template <class T>
int stack<T>:: noempty(seqstack s)
{
    if(s.top<=0) return 0;
    else return 1;
}
 
template <class T>
 void stack<T>:: Push(seqstack *s, datatype x)                        // 向栈中放入数据[压栈]
 {     if(s->top>=maxsize)
    {
        cout<<"堆栈已满无法插入!"<<endl;
        return 0;
    }
    else
    {
        s->stack[s->top]=x;
        s->top++;
        return 1;
    }
 }

 template <class T>
 void stack<T>:: Pop(seqstack *s,datatype *d)                        // 从栈中取数据[弹栈]
 {      if(s->top<=0)
    {
        cout<<"堆栈已空无数据元素出栈!"<<endl;
        return 0;
    }
    else
    {
        s->top--;
        *d=s->stack[s->top];
        return 1;
    }
 }

 template <class T>
 T stack<T>::GetTop(seqstack s,datatype *d)                                // 查看栈顶数据           
 {
     if(s.top<=0)
    {
        cout<<"堆栈已空!"<<endl;
        return 0;
    }
    else
    {
        *d=s.stack[s.top-1];
        return 1;
    }
 }

void expiscorrect(char exp[], int n)
{
    seqstack mystack;
    int i;
    char c;

    InitStack(&mystack);
    for(i=0;i<n;i++)
    {
        if(exp[i]=='(')
            Push(&mystack, exp[i]);

        else if((exp[i]==')') && notempty(mystack) && GetTop(mystack,&c) && c=='(')
            Pop(&mystack,&c);
        else if((exp[i]==')') && !notempty(mystack))
        {
            cout<<"右括号多余左括号!"<<endl;
            return ;
        }
    }

    if(notempty(mystack))
        cout<<"左括号多余右括号!"<<endl;
    else
        cout<<"左右括号匹配正确!"<<endl;
}


  static float ans[50];
  static int count=0;
  static int right=0;
  static float yourresult[50];

int postexp(char exp[])
{
    seqstack mystack;
    datatype x1,x2;
    int i;
    int j=0,ls;
    int top=0;
    int isFalse=0;
    int isRead=0;
    char oprator = ' ';
    InitStack(&mystack);
    for(i=0;exp[i]!='#';i++)
    {
        if(isdigit(exp[i]))
        {
            top=(int)(exp[i]-48);
            top*=10;
        }
        else
        {
            if (isRead == 0)
            {
                isRead++;
                top/=10;
                Push(&mystack,top);
                oprator = exp[i];
                top=0;
            }
            else if (isRead == 1)
            {
                Push(&mystack,top);
                if (isRead == 1)
                {
                    isRead++;
                    Pop(&mystack,&x2);
                }
                stackpop(&mystack,&x1);
                ls=x1;x1=x2;x2=ls;
                x1/=10;
                switch(oprator)
                {
                case '+': {x1+=x2;break;}
                case '-': {x1-=x2;break;}
                case '*': {x1*=x2;break;}
                case '/':
                    {
                    if(x2==0.0)
                    {
                        cout<<"除数为0错!"<<endl;
                        isFalse=1;
                    }
                    else
                    {
                        x1/=x2;
                    }
                    };break;
                }
                oprator = exp[i];
                x2=x1;
                top=0;
            }
        }
    }
            if (isRead == 0)
            {
                isRead++;
                top/=10;
                Push(&mystack,top);
                oprator = exp[i];
                top=0;
            }
            else if (isRead == 1)
            {
                Push(&mystack,top);
                if (isRead == 1)
                {
                    isRead++;
                    Pop(&mystack,&x2);
                }
                Pop(&mystack,&x1);
                ls=x1;x1=x2;x2=ls;
                x1/=10;
                switch(oprator)
                {
                case '+': {x1+=x2;break;}
                case '-': {x1-=x2;break;}
                case '*': {x1*=x2;break;}
                case '/':
                    {
                    if(x2==0.0)
                    {
                        cout<<"除数为0错!"<<;
                        isFalse=1;
                    }
                    else
                    {
                        x1/=x2;
                    }
                    };break;
                }
                oprator = exp[i];
                x2=x1;
                top=0;
            }
    if (isFalse == 1) return -1;
        ans[count] = x1;
           cout<<"请输入您计算的结果:"<<endl;
        cin>>"%f">>&yourresult[count];
        if(ans[count]==yourresult[count]) right++;
         count++;
}

 int main()
 {
     int i=0;
  char exp[100];
   
    cout<<"   ------计算表达式------\n使用说明:"<<endl;
    cout<<" 1.请输入要计算的表达式,以#结尾,例如1+2#"<<endl;
    cout<<" 2.结束计算请输入符号$"<<endl;
    cout<<" 3.在表达式中可以运算括号,中括号[]和大括号{}统一使用小括号代替()!"<<endl;
    cout<<" 4.程序输入格式错误时,将会自动结束程序,请注意格式正确!"<<endl;
    cout<<"请输入要计算的表达式,以#结尾。结束程序输入$。"<<endl;
    cin>>"%s">>exp[i];
    while(exp[i]!='$')
      {
        expiscorrect(&exp[i],i);
            postexp(&exp[i]);
                                 
        cout<<"-------------------------------------------\n"<<endl;
        cout<<"请输入下一个表达式,以#结尾。结束程序输入$。\n"<<endl;   
        cin>>"%s">>exp[i];
                                 
        }



    cout<<"\n\n      ******您的成绩单*******\n"<<endl;
    cout<<"----------------------------------------\n"<<endl;
    cout<<" 序 号     正确答案    您的答案   结 论\n"<<endl;

    for(i=0;i<count;i++)
    {
            cout<<"表达式%d     %.2f        %.2f      %s\n"<<
           i+1<<ans[i],yourresult[i],ans[i]==yourresult[i]?"正确!":"错误!");
    }
    cout<<"----------------------------------------\n"<<endl;
    cout<<"解答题目总共为%d道,其中\n"<<count<<endl;
    cout<<"正确解答%d道题,错误解答%d道题。\n      *****最后得分为%d分!*****\n"<< right<<count-right<<right*100/count<<endl;
    cout<<"----------------------------------------\n"<<endl;
    return 0;
 }
2012-06-17 14:08
文思旋风
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-6-12
收藏
得分:0 
#include "string.h"
#include "stdio.h"
#include"conio.h"
#define maxsize 100
#include "ctype.h"
typedef char datatype;
typedef struct
{
    datatype stack[maxsize];
    int top;
} seqstack;

void stackinitiate(seqstack *s)
{
    s->top=0;
}

int stacknotempty(seqstack s)
{
    if(s.top<=0) return 0;
    else return 1;
}

int stackpush(seqstack *s, datatype x)
{
    if(s->top>=maxsize)
    {
        printf("堆栈已满无法插入!\n");
        return 0;
    }
    else
    {
        s->stack[s->top]=x;
        s->top++;
        return 1;
    }
}

int stackpop(seqstack *s,datatype *d)
{
    if(s->top<=0)
    {
        printf("堆栈已空无数据元素出栈!\n");
        return 0;
    }
    else
    {
        s->top--;
        *d=s->stack[s->top];
        return 1;
    }
}

int stacktop(seqstack s,datatype *d)
{
    if(s.top<=0)
    {
        printf("堆栈已空!\n");
        return 0;
    }
    else
    {
        *d=s.stack[s.top-1];
        return 1;
    }
}

void expiscorrect(char exp[], int n)
{
    seqstack mystack;
    int i;
    char c;

    stackinitiate(&mystack);
    for(i=0;i<n;i++)
    {
        if(exp[i]=='(')
            stackpush(&mystack, exp[i]);

        else if((exp[i]==')') && stacknotempty(mystack) && stacktop(mystack,&c) && c=='(')
            stackpop(&mystack,&c);
        else if((exp[i]==')') && !stacknotempty(mystack))
        {
            printf("右括号多余左括号!\n");
            return ;
        }
    }

    if(stacknotempty(mystack))
        printf("左括号多余右括号!\n");
    else
        printf("左右括号匹配正确!\n");
}


  static float ans[50];
  static int count=0;
  static int right=0;
  static float yourresult[50];

int postexp(char exp[])
{
    seqstack mystack;
    datatype x1,x2;
    int i;
    int j=0,ls;
    int top=0;
    int isFalse=0;
    int isRead=0;
    char oprator = ' ';
    stackinitiate(&mystack);
    for(i=0;exp[i]!='#';i++)
    {
        if(isdigit(exp[i]))
        {
            top=(int)(exp[i]-48);
            top*=10;
        }
        else
        {
            if (isRead == 0)
            {
                isRead++;
                top/=10;
                stackpush(&mystack,top);
                oprator = exp[i];
                top=0;
            }
            else if (isRead == 1)
            {
                stackpush(&mystack,top);
                if (isRead == 1)
                {
                    isRead++;
                    stackpop(&mystack,&x2);
                }
                stackpop(&mystack,&x1);
                ls=x1;x1=x2;x2=ls;
                x1/=10;
                switch(oprator)
                {
                case '+': {x1+=x2;break;}
                case '-': {x1-=x2;break;}
                case '*': {x1*=x2;break;}
                case '/':
                    {
                    if(x2==0.0)
                    {
                        printf("除数为0错!\n");
                        isFalse=1;
                    }
                    else
                    {
                        x1/=x2;
                    }
                    };break;
                }
                oprator = exp[i];
                x2=x1;
                top=0;
            }
        }
    }
            if (isRead == 0)
            {
                isRead++;
                top/=10;
                stackpush(&mystack,top);
                oprator = exp[i];
                top=0;
            }
            else if (isRead == 1)
            {
                stackpush(&mystack,top);
                if (isRead == 1)
                {
                    isRead++;
                    stackpop(&mystack,&x2);
                }
                stackpop(&mystack,&x1);
                ls=x1;x1=x2;x2=ls;
                x1/=10;
                switch(oprator)
                {
                case '+': {x1+=x2;break;}
                case '-': {x1-=x2;break;}
                case '*': {x1*=x2;break;}
                case '/':
                    {
                    if(x2==0.0)
                    {
                        printf("除数为0错!\n");
                        isFalse=1;
                    }
                    else
                    {
                        x1/=x2;
                    }
                    };break;
                }
                oprator = exp[i];
                x2=x1;
                top=0;
            }
    if (isFalse == 1) return -1;
        ans[count] = x1;
           printf("请输入您计算的结果:");
        scanf("%f",&yourresult[count]);
        if(ans[count]==yourresult[count]) right++;
         count++;
         getchar();
}

int main()
{
    int i=0;
  char exp[100];
   
  printf("   ------计算表达式------\n使用说明:\n");
    printf(" 1.请输入要计算的表达式,以#结尾,例如1+2#\n");
    printf(" 2.结束计算请输入符号$\n");
    printf(" 3.在表达式中可以运算括号,中括号[]和大括号{}统一使用小括号代替()!\n");
    printf(" 4.程序输入格式错误时,将会自动结束程序,请注意格式正确!\n");
    printf("请输入要计算的表达式,以#结尾。结束程序输入$。\n");
   // c=getchar();



                            scanf("%s",exp);
                           while(exp[i]!='$')
                              {
                                 expiscorrect(&exp[i],i);
                                 postexp(&exp[i]);
                                 
                         printf("-------------------------------------------\n");
                                printf("请输入下一个表达式,以#结尾。结束程序输入$。\n");   
                                scanf("%s",exp);
                               // c=getchar();      
                              }



    printf("\n\n      ******您的成绩单*******\n");
    printf("----------------------------------------\n");
    printf(" 序 号     正确答案    您的答案   结 论\n");

    for(i=0;i<count;i++)
    {
            printf("表达式%d     %.2f        %.2f      %s\n",
           i+1,ans[i],yourresult[i],ans[i]==yourresult[i]?"正确!":"错误!");
    }
    printf("----------------------------------------\n");
    printf("解答题目总共为%d道,其中\n",count);
    printf("正确解答%d道题,错误解答%d道题。\n      *****最后得分为%d分!*****\n",
           right,count-right,right*100/count);
    printf("----------------------------------------\n");
   getchar();
    getchar();
    return 0;
}
2012-06-17 14:14
快速回复:利用栈求表达式的值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018206 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved