| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 531 人关注过本帖
标题:你们试一下我的运行结果,为什么是这样的?
只看楼主 加入收藏
易水寒、
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-10-23
结帖率:100%
收藏
已结贴  问题点数:18 回复次数:3 
你们试一下我的运行结果,为什么是这样的?
我想问  问题出在哪了啦?怎么没有运行结果
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
char op;
char level;
}opNode;
typedef union
{
    opNode opNode;
    double value;
    }Node;
    typedef struct
    {
        char leixing;
        Node* a[100];
        int top;
        }Stack;
        Stack* init_sta()
         {
             int i;
             Stack* s=(Stack*)malloc(sizeof(Stack));
             s->top=0;
             for(i=0;i<100;i++)
             s->a[i]=NULL;
             return s;
         }
         char* substr(char* s,int n1,int n2)
         {
             char* ss=(char*)malloc(sizeof(char)*(n2-n1+2));
             int i,j=0;
             for(i=n1;i<n2;i++)
             {
                 ss[j++]=s[i];
                 }
                 ss[j]='\0';
                 return ss;
         }
         int empty(Stack*ss)
         {
             return ss->top;
         }
         Node*Top(Stack* ss)
         {
             if(ss->top<=0)
             {
                 return 0;
             }
             else
             {
                 return ss->a[ss->top-1];
             }
         }
         Node*Pop(Stack* ss)
         {
             if(ss->top<=0)
             {
                 return 0;
             }
             else
             {
                 ss->top--;
                 return ss->a[ss->top];
             }
         }
         void Push(Stack *p,Node*n)
         {
             Node*temp=(Node*)malloc(sizeof(Node));
             if(p->top>=100)
             {
             printf("错误,栈为满!\n");
             free(temp);
             }
             else
             {
                 if(p->leixing==1)
                 {
                     temp->opNode.level=n->opNode.level;
                     temp->opNode.op=n->opNode.op;
                 }
                 else
                 temp->value=n->value;
                 p->a[p->top]=temp;
                 p->top=p->top+1;
             }
         }
     double calc(char op,double m,double n)
     {
         switch(op)
         {
         case'+':return n+m;
         case'-':return n-m;
         case'*':return n*m;
         case'/':return n/m;
         case'%':return (int)n%(int)m;
         }
         return 0;
     }
     void math(char* exp)
     {
         Node*top;
         Node*temp=(Node*)malloc(sizeof(Node));
         Node*tempn=(Node*)malloc(sizeof(Node));
         Node*tempm;
         char c;
         char*temps="";
         unsigned int index=0,tempindex;
         double a,b;
         Stack*s1=init_sta();
         Stack*s2=init_sta();
         s1->leixing=2;
         s2->leixing=1;
         while(index<strlen(exp))
         {
             c=exp[index];
              if(c>='0'&&c<='9'||c=='.')
              {
                  tempindex=index+1;
                  while(tempindex<strlen(exp)&&(exp[tempindex]>='0'&&exp[tempindex]<='9'||exp[tempindex]=='.'))
                  {
                      tempindex++;
                  }
              if(tempindex==strlen(exp)-1)
              {
                  temps=substr(exp,index,index+1);
              }
              else
              {
                  temps=substr(exp,index,tempindex);
              }
              index=tempindex;
              temp->value=atof(temps);
              Push(s1,temp);
              }
              if(c=='+'||c=='-')
              {
                  tempn->opNode.op=c;
                  tempn->opNode.level=1;
                  top=Top(s2);
                  while(empty(s2)>0&&tempn->opNode.level<=top->opNode.level)
                  {
                      tempn=Pop(s2);
                      a=Pop(s1)->value;
                      b=Pop(s1)->value;
                      temp->value=calc(tempm->opNode.op,a,b);
                      Push(s1,temp);
                      top=Top(s2);
                  }
                  Push(s2,tempn);
                  index++;
              }
     if(c=='(')
     {
         tempn->opNode.op=c;
         tempn->opNode.level=0;
         Push(s2,tempn);
         index++;
     }
     if(c==')')
     {
     top=Top(s2);
     while(empty(s2)>0&&empty(s1)>0&&top->opNode.op!='(')
     {
         tempn=Pop(s2);
         a=Pop(s1)->value;
         b=Pop(s1)->value;
         temp->value=calc(tempn->opNode.op,a,b);
         Push(s1,temp);
         top=Top(s2);
     }
     Pop(s2);
     index++;
         }
     }     
     while(empty(s1)>0&&empty(s2)>0)
     {
     tempm=Pop(s2);
         a=Pop(s1)->value;
         b=Pop(s1)->value;
         temp->value=calc(tempm->opNode.op,a,b);
         Push(s1,temp);
         }
    sprintf(exp,"%1f",Pop(s1)->value);
     }
     void display(char* value)
     {
         char *temps="";
         int i=0;
         for(i=strlen(value)-1;i>=0;i--)
         {
             if(value[i]!='0'&&value[i]!='.')
             break;
         if(value[i]=='.')
         {
             i--;
             break;
         }
     }
     temps=substr(value,0,i+1);
     printf("The Value is \"%s\".",temps);
     }
     void main()
     {
     char*calcstring=(char*)malloc(sizeof(char)*100);
    strcpy(calcstring,"3.01*(4.23-2.16)-9%2");
     printf("This string is %s\n",calcstring);
     math(calcstring);
     display(calcstring);
     getch();
     }
     
搜索更多相关主题的帖子: include double 
2014-10-29 17:34
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:18 
malloc申请的空间是要free的,否则早晚会出问题。


[ 本帖最后由 azzbcc 于 2014-10-29 20:46 编辑 ]


[fly]存在即是合理[/fly]
2014-10-29 20:42
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
math函数中没有对 * % / 的处理,造成死循环


[fly]存在即是合理[/fly]
2014-10-29 21:21
易水寒、
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-10-23
收藏
得分:0 
回复 2 楼 azzbcc
有什么建设性修改方法么?
2014-10-29 21:59
快速回复:你们试一下我的运行结果,为什么是这样的?
数据加载中...
 
   



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

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