| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 634 人关注过本帖
标题:计算器源代码改错(应该是主函数那里的问题)
只看楼主 加入收藏
lilylis
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-19
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
计算器源代码改错(应该是主函数那里的问题)
#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
#include<math.h>
#include<string.h>
#define N 25  //最大表达式长度
#define OP 1   //OP为运算符,值为1
#define NUM 0   //NUM为数字,值为0
#define MAXSIZE 100
typedef char datatype;
typedef struct{
  datatype a[MAXSIZE];
  int top;
  }sequence_stack;
void init(sequence_stack *st)//栈初始化
{
    st->top=0;
}
int empty(sequence_stack st)//判断栈是否为空
{
    return(st.top?0:1);
}
datatype read(sequence_stack st)//读栈顶结点值
{
    if(empty(st))
    {
        printf("\n栈是空的!");exit(1);
    }
   else return st.a[st.top-1];
}
void push(sequence_stack *st,datatype x)//进栈
{
    if(st->top==MAXSIZE)
    {
        printf("\nThe sequence stack is full!");exit(1);
    }
    st->a[st->top]=x;
    st->top++;
}
void pop(sequence_stack *st)//出栈
{
    if(st->top==0)
    {
        printf("\nThe sequence stack is empty!");exit(1);
        st->top--;
    }
}
int match_kuohao(char c[])//判断表达式括号是否匹配
{
    int i=0;
    sequence_stack s;
    init(&s);
    while(c[i]!='#')
    {
        switch(c[i])
        {
            case'{':
            case'[':
            case'(':push(&s,c[i]);break;
            case'}':if(!empty(s)&&read(s)=='{')
                     {
                         pop(&s);break;
                     }
                     else return 0;
            case']':if(!empty(s)&&read(s)=='[')
                     {
                         pop(&s);break;
                     }
                     else return 0;
            case')':if(!empty(s)&&read(s)=='(')
                     {
                         pop(&s);break;
                     }
                     else return 0;
        }
        i++;
    }
    return(empty(s));
}
double readnumber(char f[],int *i)//将数字字符串转变成相应的数
{
    double x=0.0;
    int k=0;
    while(f[*i]>='0'&&f[*i]<='9')
    {
        x=x*10+(f[*i]-'0');
        (*i)++;
    }
    if(f[*i]=='.')
    {
        (*i)++;
        while(f[*i]>='0'&&f[*i]<='9')
        {
           x=x*10+(f[*i]-'0');
           (*i)++;k++;
        }
    }
    while (k!=0)
    {
        x=x/10.0;
        k=k-1;
    }
    return(x);
}
double qiuzhi(char f[])//求一个后缀表达式的值
{
    double obst[100];
    int top=0;
    int i=0;
    double x1,x2;
    while(f[i]!='#')
    {
         if (f[i]>='0'&&f[i]<='9')
         {
            obst[top]=readnumber(f,&i);top++;
         }
         else if(f[i]==' ') i++;
         else if(f[i]=='+')
         {
             x2=obst[--top];
             x1=obst[--top];
             obst[top]=x1+x2;top++;
             i++;
         }
         else if (f[i]=='-')
         {
             x2=obst[--top];
             x1=obst[--top];
             obst[top]=x1-x2;top++;
             i++;
         }
         else if (f[i]=='*')
         {
             x2=obst[--top];
             x1=obst[--top];
             obst[top]=x1*x2;top++;
             i++;
         }
         else if (f[i]=='/')
         {
             x2=obst[--top];
             x1=obst[--top];
             obst[top]=x1/x2;top++;
             i++;
         }
    }
    return obst[0];
}
int is_op(char op)//判断一个字符是否为操作符
{
    switch(op)
    {
        case '+':
        case '-':
        case '*':
        case '/':return 1;
        default:return 0;
    }
}
int youxianji(char op)//求运算符的优先级
{
    switch(op)
    {
       case '#':return -1;
       case '(':return 0;
       case '+':
       case '-':return 1;
       case '*':
       case '/':return 2;
       default:return -1;
    }
}
void z_h(char e[],char f[])//将一个中缀表达式e转换为它等价的后缀表达式
{ int i=0;
    int j=0;
    char opst[100];
    int top,t;
    top=0;
    opst[100]='#';top++;
    while(e[i]!='#')
    {
        if((e[i]>='0'&&e[i]<='9')||e[i]=='.')
        f[j++]=e[i];
        else if(e[i]=='(')
          {
              opst[top]=e[i];top++;
          }
          else if(e[i]==')')
          {
              t=top-1;
              while (opst[t]!='(')
                     {
                         f[j++]=opst[--top];t=top-1;
                     }
                     top--;
          }
          else if(is_op(e[i]))
          {
              f[j++]=' ';
              while (youxianji(opst[top-1])>=youxianji(e[i]))
              f[j++]=opst[--top];
              opst[top]=e[i];top++;
          }
          i++;
    }
    while (top) f[j++]=opst[--top];
}
void main()
{
    system("cls");
    system("color 9F");
    sequence_stack s;
    init(&s);
    char f[100];
    char e[100];
    datatype ch;
    double x;
    int k=0;int i;int j;
    printf("请输入表达式:");
    for(i=0;(ch=getchar())!='\n'&&k<=N;i++)
    {
        e[i]=ch;
        push(&s,ch);
        k++;
    }
    e[i+1]='#';
    printf("显示表达式:");
    for(i=0;e[i]!='#';i++)
    {printf("%f",e[i]);}
    z_h(e,f);
   readnumber(f,&j);
  x=qiuzhi(f);
  printf("表达式的结果为:",x);
}

搜索更多相关主题的帖子: 计算器 include return 表达式 
2013-04-24 22:10
lilylis
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-19
收藏
得分:0 
跪求各位大神的指点,小女子不甚感激!!!
2013-04-24 22:12
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:20 
你的栈写了,没用到、、、
程序代码:
void z_h(char e[],char f[])//将一个中缀表达式e转换为它等价的后缀表达式
{
    int i=0;
    int j=0;
    char opst[100];
    int top,t;
    top=0;
//  opst[100]='#';        //第二处
    memset(opst, '#', sizeof(opst));    //应该用memset填充
    top++;
    while(e[i]!='#')
    {
        if((e[i]>='0'&&e[i]<='9')||e[i]=='.')
            f[j++]=e[i];
        else if(e[i]=='(')
        {
            opst[top]=e[i];top++;
        }
        else if(e[i]==')')
        {
            t=top-1;
            while (opst[t]!='(')
            {
                f[j++]=opst[--top];t=top-1;
            }
            top--;
        }
        else if(is_op(e[i]))
        {
            f[j++]=' ';
            while (youxianji(opst[top-1])>=youxianji(e[i]))
                f[j++]=opst[--top];
            opst[top]=e[i];top++;
        }
        i++;
    }
    while (top) f[j++]=opst[--top];
}
void main()
{
    system("cls");
    system("color 9F");
    sequence_stack s;
    init(&s);
    char f[100];
    char e[100];
    datatype ch;
    double x;
    int k=0;int i;
    printf("请输入表达式:");
    for(i=0;(ch=getchar())!='\n'&&k<=N;i++)
    {
        e[i]=ch;
        push(&s,ch);
        k++;
    }
    e[i+1]='#';
    printf("显示表达式:");
    for(i=0;e[i]!='#';i++)
    {
        printf("%c",e[i]);    //第一处错误
    }
    z_h(e,f);

//    readnumber(f,&j);    貌似多余啊
    x=qiuzhi(f);
    printf("\n表达式的结果为:%f\n",x);    //这里竟然少了%f,细心啊
}


[fly]存在即是合理[/fly]
2013-04-25 13:01
tremere
Rank: 6Rank: 6
来 自:火星
等 级:侠之大者
帖 子:223
专家分:432
注 册:2013-3-11
收藏
得分:0 
菜鸟,来学习的。

极品菜鸟,来学习啦,啦啦啦啦啦啦啦。。。
2013-04-25 13:16
lilylis
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-19
收藏
得分:0 
回复 3楼 azzbcc
嗯,谢谢,是我粗心了。可是改了那些地方还是运行不出来啊。嗯,能再帮我看看吗?
2013-04-25 17:54
快速回复:计算器源代码改错(应该是主函数那里的问题)
数据加载中...
 
   



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

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