标题:简单计算器结果输出有错,请大神指教
只看楼主
周旺
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-3-2
结帖率:0
 问题点数:0 回复次数:1 
简单计算器结果输出有错,请大神指教
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define max 100
char ex[max];       /*存储后缀表达式*/
void trans();
void compvalue();
void main()
{
    while(1)
    {
        int i;
        trans();
        compvalue();
        printf("如果继续,请按1,其余数字键代表退出:");
        scanf("%d",&i);
        if(i!=1)
            break;
        
    }
}
void trans()
{        /*将算术表达式转化为后缀表达式*/
    char str[max];   /*存储原算术表达式*/
    char stack[max];       /*作为栈使用*/
    char ch;
    int sum,i,j,t,top=0;
    printf("*****************************************\n");
    printf("*****输入一个求值的表达式,以=结束。*****\n");
    printf("*****************************************\n");
    printf("算数表达式:\n");
    i=0;                      /*获取用户输入的表达式*/
    do
    {
        i++;
        scanf("%c",&str[i]);
    }while(str[i]!='=');
    sum=i;
    t=1;
    i=1;
    ch=str[i];
    i++;
    while(ch!='=')
    {
        switch(ch)
        {
        case '(':                 /*判定为左括号*/
            top++;
            stack[top]=ch;
            break;
        case ')':                 /*判定为右括号*/
            while(stack[top]!='(')
            {
                ex[t]=stack[top];
                top--;
                t++;
            }
            top--;
            break;
        case '+':                 /*判定为加减号*/
        case '-':      
            while(top!=0&&stack[top]!='(')
            {
                ex[t]=stack[top];
                top--;
                t++;
            }
            top++;
            stack[top]=ch;
            break;
        case '*':                  /*判定为乘除号*/
        case '/':
            while(stack[top]=='*'||stack[top]=='/')
            {
                ex[t]=stack[top];
                top--;
                t++;
            }
            top++;
            stack[top]=ch;
            break;
        case ' ':
            break;
        default:
            while(ch>='0'&&ch<='9'||ch=='.')
            {                 /*判定为数字*/
                ex[t]=ch;
                t++;
                ch=str[i];
                i++;
            }
            i--;
            ex[t]='=';
            t++;
        }
        ch=str[i];
        i++;
    }
    while(top!=0)
    {
        ex[t]=stack[top];
        t++;
        top--;
    }
    ex[t]='=';
   
    printf("\n");
}
void compvalue()
{                                 /*计算后缀表达式的值*/
    float stack[max],d;                    /*作为栈使用*/
    char ch;
    int t=1,top=0; /*t为ex下标,top为stack下标*/
    int k=0,z=0,i;
    ch=ex[t];t++;
    while(ch!='=')
    {
        switch(ch)
        {
        case '+':
            stack[top-1]=stack[top-1]+stack[top];
            top--;
            break;
        case '-':
            stack[top-1]=stack[top-1]-stack[top];
            top--;
            break;
        case '*':
            stack[top-1]=stack[top-1]*stack[top];
            top--;
            break;
        case '/':
            if(stack[top]!=0)
                stack[top-1]=stack[top-1]/stack[top];
            else{
                printf("t除零错误!\n");
                printf("\n");
                exit(0);                   /*异常退出*/
            }
            top--;
            break;
        default:
            d=0;
            while(ch>='0'&&ch<='9'||ch=='.')
            {
                if(ch!='.')
                {
                    d=10*d+ch-'0';               /*将数字字符转化为对应的数值*/   
               
                    t++;
                }
                if(k==1)
                    z++;
                if(ch=='.')
                {
                    k=1;
                    
                    t++;
                    
                }
                    ch=ex[t];
                    
            
               
                if(z!=0)
                    for(i=0;i<z;i++)
                        d=d/10.0;
                    
            }
            top++;
            stack[top]=d;
        }
        ch=ex[t];
        t++;
    }
    printf("计算结果:%g\n",stack[top]);
    printf("\n");
}
搜索更多相关主题的帖子: void 存储 include 表达式 
2013-03-11 10:14
liqi2009
Rank: 2
等 级:禁止访问
帖 子:71
专家分:15
注 册:2012-2-21
得分:0 
是数据结构,不是VC
2013-03-12 11:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-400397-1-1.html




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

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