| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:运行有问题
只看楼主 加入收藏
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
结帖率:92.86%
收藏
已结贴  问题点数:20 回复次数:9 
运行有问题
#include<stdio.h>
#include<stdlib.h>
#define stackmaxsize 100
#define stackincert 10
typedef struct
{
 float *top;
 float *base;   
 int stacksize;
}Stack;
int stackinit_num(Stack *s)
{
 s->base=(float*)malloc(stackmaxsize*sizeof(float));
 if(s->base==NULL) exit(1);
 s->top=s->base;   
 s->stacksize=stackmaxsize;
 return 0;
}
int stackinit_flag(Stack *p)
{
 p->base=(char*)malloc(stackmaxsize*sizeof(char));
 if(p->base==NULL) exit(1);
 p->top=p->base;   
 p->stacksize=stackmaxsize;
 return 0;
}
int push_num(Stack *s,float x)
{
     if(s->top-s->base>=s->stacksize)
     {
      s->base=(float*)realloc(s->base,(s->stacksize+stackincert)*sizeof(float));   
      if(s->base==NULL) exit(1);
      s->top=s->base+s->stacksize;
      s->stacksize+=stackincert;   
     }
     *(s->top++)=x;
     return 0;
}
int push_flag(Stack *w,char x)
{
     if(w->top-w->base>=w->stacksize)
     {
      w->base=(char*)realloc(w->base,(w->stacksize+stackincert)*sizeof(char));   
      if(w->base==NULL) exit(1);
      w->top=w->base+w->stacksize;
      w->stacksize+=stackincert;   
     }
     *(w->top++)=x;
     return 0;
}
int pop_num(Stack *s_num,float *x)
{
 if(s_num->top==s_num->base) exit(1);
 *x=*(--s_num->top);   
    return 0;
}
int pop_flag(Stack *s_flag,char *x)
{
 if(s_flag->top==s_flag->base) exit(1);
 *x=*(--s_flag->top);   
    return 0;
}
int gettop_flag(Stack s,char *x)
{
 if(s.top==s.base) exit(1);
 *x=*(--s.top);   
 return 0;
}
int compare(char *p1,char *p2)
{
 if(*p1=='+'||*p1=='-')
   {
       if(*p2=='+'||*p2=='-'||*p2==')'||*p2=='#') return 1;
       else return -1;
       }   
else if(*p1=='*'||*p1=='/')
   {
       if(*p2=='(') return -1;
       else return 1;
   }
 else  if(*p1=='(')
   {
        if(*p2==')') return 0;
        else if(*p2=='#') {printf("表达式错误");exit(1);}
           else return -1;
   }
  else if(*p1==')')
    {
     if(*p2=='(') {printf("表达式错误");exit(1);}
     else return 1;   
    }
  else  if(*p1=='#')
         {
          if(*p2==')') {printf("表达式错误");exit(1);}
          else if(*p2=='=') return 0;   
             else return -1;
         }
         else {printf("表达式错误");exit(1);}
}
int function(float a,char ch,float b,float *y)
{
 if(ch='+') *y=a+b;
 else if(ch=='-') *y=a-b;
 else if(ch=='*') *y=a*b;
 else if(ch='/') *y=a/b;
 else {printf("error");exit(1);}
 return 0;   
}

int main()
{
 Stack s_num,s_flag;
 char ch1,ch2;
 float a,b,c;
 stackinit_flag(&s_flag);
 push_flag(&s_flag,'#');
 stackinit_num(&s_num);
 ch1=getchar();
 gettop_flag(s_flag,&ch2);
 while(ch1!='#'||ch2!='#')
 {
   if(ch1!='+'&&ch1!='-'&&ch1!='*'&&ch1!='/')
     {push_num(&s_num,ch1);ch1=getchar();}
     
     else
     {
         
      switch(compare(&ch2,&ch1))
      {
      case -1: push_flag(&s_flag,ch1); break;
      case 0: pop_flag(&s_flag,&ch2);break;
      case 1: pop_flag(&s_flag,&ch2);pop_num(&s_num,&a);
               pop_num(&s_num,&b); function(a,ch2,b,&c);
               push_num(&s_num,c); break;
       default: printf("error");break;
      }
     }
     gettop_flag(s_flag,&ch2);
     ch1=getchar();
}   

printf("%f",*(s_num.base));
return 0;   
}


//计算简单表达式的值
搜索更多相关主题的帖子: include return 
2011-12-29 23:38
liao06550107
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:111
专家分:696
注 册:2011-10-2
收藏
得分:20 
发现如下问题:
1、定义一个(浮点型)栈结构,但是又出来个(字符型)栈运用;实在要想用的话至少还要定义一个(字符型)栈结构
typedef struct
{
    char *base, *top;
    int stacksize;
}CharStack;

原先
typedef struct
{
    float *base;
    float *top;
    int stacksize;
}NumStack;

[ 本帖最后由 liao06550107 于 2011-12-30 00:29 编辑 ]

听不同的音乐,看不同的书,游历不同的城市,邂逅不同的人,走的多了,站的高了,自然就看的远了。
2011-12-30 00:21
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
以下是引用liao06550107在2011-12-30 00:21:43的发言:

发现如下问题:
1、定义一个(浮点型)栈结构,但是又出来个(字符型)栈运用;实在要想用的话至少还要定义一个(字符型)栈结构
typedef struct
{
    char *base, *top;
    int stacksize;
}CharStack;
 
原先
typedef struct
{
    float *base;
    float *top;
    int stacksize;
}NumStack;
2011-12-30 00:42
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
回复 3楼 xdh0817
#include<stdio.h>
#include<stdlib.h>
#define stackmaxsize 100
#define stackincert 10
typedef struct
{
 float *top;
 float *base;   
 int stacksize;
}Stack2;
typedef struct
{
 char *top;
 char *base;   
 int stacksize;
}Stack1;
int stackinit_num(Stack2 *s)
{
 s->base=(float*)malloc(stackmaxsize*sizeof(float));
 if(s->base==NULL) exit(1);
 s->top=s->base;   
 s->stacksize=stackmaxsize;
 return 0;
}
int stackinit_flag(Stack1 *p)
{
 p->base=(char*)malloc(stackmaxsize*sizeof(char));
 if(p->base==NULL) exit(1);
 p->top=p->base;   
 p->stacksize=stackmaxsize;
 return 0;
}
int push_num(Stack2 *s,float x)
{
     if(s->top-s->base>=s->stacksize)
     {
      s->base=(float*)realloc(s->base,(s->stacksize+stackincert)*sizeof(float));   
      if(s->base==NULL) exit(1);
      s->top=s->base+s->stacksize;
      s->stacksize+=stackincert;   
     }
     *(s->top++)=x;
     return 0;
}
int push_flag(Stack1 *w,char x)
{
     if(w->top-w->base>=w->stacksize)
     {
      w->base=(char*)realloc(w->base,(w->stacksize+stackincert)*sizeof(char));   
      if(w->base==NULL) exit(1);
      w->top=w->base+w->stacksize;
      w->stacksize+=stackincert;   
     }
     *(w->top++)=x;
     return 0;
}
int pop_num(Stack2 *s_num,float *x)
{
 if(s_num->top==s_num->base) exit(1);
 *x=*(--s_num->top);   
    return 0;
}
int pop_flag(Stack1 *s_flag,char *x)
{
 if(s_flag->top==s_flag->base) exit(1);
 *x=*(--s_flag->top);   
    return 0;
}
int gettop_flag(Stack1 s,char *x)
{
 if(s.top==s.base) exit(1);
 *x=*(--s.top);   
 return 0;
}
int compare(char *p1,char *p2)
{
 if(*p1=='+'||*p1=='-')
   {
       if(*p2=='+'||*p2=='-'||*p2==')'||*p2=='#') return 1;
       else return -1;
       }   
else if(*p1=='*'||*p1=='/')
   {
       if(*p2=='(') return -1;
       else return 1;
   }
 else  if(*p1=='(')
   {
        if(*p2==')') return 0;
        else if(*p2=='#') {printf("表达式错误");exit(1);}
           else return -1;
   }
  else if(*p1==')')
    {
     if(*p2=='(') {printf("表达式错误");exit(1);}
     else return 1;   
    }
  else  if(*p1=='#')
         {
          if(*p2==')') {printf("表达式错误");exit(1);}
          else if(*p2=='=') return 0;   
             else return -1;
         }
         else {printf("表达式错误");exit(1);}
}
int function(float a,char ch,float b,float *y)
{
 if(ch='+') *y=a+b;
 else if(ch=='-') *y=a-b;
 else if(ch=='*') *y=a*b;
 else if(ch='/') *y=a/b;
 else {printf("error");exit(1);}
 return 0;   
}

int main()
{
 Stack1 s_flag;
 Stack2  s_num;
 char ch1,ch2;
 float a,b,c;
 stackinit_flag(&s_flag);
 push_flag(&s_flag,'#');
 stackinit_num(&s_num);
 ch1=getchar();
 gettop_flag(s_flag,&ch2);
 while(ch1!='#')
 {
   if(ch1!='+'&&ch1!='-'&&ch1!='*'&&ch1!='/')
     {push_num(&s_num,ch1);ch1=getchar();}
     
     else
     {
         
      switch(compare(&ch2,&ch1))
      {
      case -1: push_flag(&s_flag,ch1); ch1=getchar();break;
      case 0: pop_flag(&s_flag,&ch2);ch1=getchar();break;
      case 1: pop_flag(&s_flag,&ch2);pop_num(&s_num,&a);
               pop_num(&s_num,&b); function(a,ch2,b,&c);
               push_num(&s_num,c); break;
       default: printf("error");break;
      }
     }
     gettop_flag(s_flag,&ch2);
     
}   

printf("%f",*(s_num.base));
return 0;   
}

//谢谢你喽~~
改了一下,编译通过了,但是输出还是不对,
2011-12-30 00:44
liao06550107
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:111
专家分:696
注 册:2011-10-2
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define stackmaxsize 100
#define stackincert 10

typedef struct
{
    float *top;
    float *base;    
    int stacksize;
}NumStack;

typedef struct
{
    char *top;
    char *base;
    int stacksize;
}CharStack;

int stackinit_num(NumStack *s) 
{
    s->base = (float*)malloc(stackmaxsize*sizeof(float));
    if(s->base == NULL) 
        exit(1);
    s->top = s->base;    
    s->stacksize = stackmaxsize;
    return 0;
}

int stackinit_flag(CharStack *p)
{
    p->base=(char*)malloc(stackmaxsize*sizeof(char)); 
    if(p->base==NULL) exit(1);
    p->top=p->base;    
    p->stacksize=stackmaxsize;
    return 0;
}
int push_num(NumStack *s,float x)
{
     if(s->top-s->base>=s->stacksize)
     {
          s->base=(float*)realloc(s->base,(s->stacksize+stackincert)*sizeof(float));    
          if(s->base==NULL) exit(1);
          s->top=s->base+s->stacksize;
          s->stacksize+=stackincert;    
     }
     *(s->top++)=x;
     return 0;
}

int push_flag(CharStack *w,char x)
{
     if(w->top-w->base>=w->stacksize)
     {
          w->base=(char*)realloc(w->base,(w->stacksize+stackincert)*sizeof(char));    
          if(w->base==NULL) exit(1);
          w->top=w->base+w->stacksize;
          w->stacksize+=stackincert;    
     }
     *(w->top++)=x;
     return 0;
}

int pop_num(NumStack *s_num,float *x)
{
    if(s_num->top==s_num->base) 
        exit(1);
    *x=*(--s_num->top);    
        return 0;
}

int pop_flag(CharStack *s_flag,char *x)
{
    if(s_flag->top==s_flag->base)
        exit(1);
    *x=*(--s_flag->top);    
        return 0;
}
    
int gettop_flag(CharStack s,char *x)
{
    if(s.top==s.base) 
        exit(1);
    *x=*(--s.top);    
    return 0;
}

int compare(char *p1,char *p2)
{
    if(*p1=='+'||*p1=='-')
       {
           if(*p2=='+'||*p2=='-'||*p2==')'||*p2=='#')
               return 1;
           else 
               return -1;
        }    
    else if(*p1=='*'||*p1=='/')
       {
           if(*p2=='(') 
               return -1;
           else 
               return 1;
       }
    else  if(*p1=='(') 
       {
            if(*p2==')') 
                return 0;
            else if(*p2=='#') 
            {
                printf("表达式错误");
                exit(1);
            }
               else 
                   return -1;
       }
      else if(*p1==')')
        {
         if(*p2=='(') 
         {
             printf("表达式错误");
             exit(1);
         }
         else 
             return 1;    
        }
      else  if(*p1=='#') 
             {
                  if(*p2==')') {printf("表达式错误");exit(1);}
                  else if(*p2=='=') return 0;    
                     else return -1;
             }
             else 
             {
                 printf("表达式错误");
                 exit(1);
             }
}

int function(float a,char ch,float b,float *y)
{
    if(ch == '+') //判断==
    *y=a+b;
    else if(ch == '-') *y=a-b;
    else if(ch == '*') *y=a*b;
    else if(ch == '/') *y=a/b;
    else 
    {
        printf("error");
        exit(1);
    }
    return 0;    
}



int main()
{
    NumStack s_num;
    CharStack s_flag;
    char ch1,ch2;
    float a,b,c;
    char z[20];
    int i;
    stackinit_flag(&s_flag);
    push_flag(&s_flag,'#');
    stackinit_num(&s_num);
    printf("请输入表达式,例如:(3+4)*5#\n");
    ch1=getchar();
    gettop_flag(s_flag, &ch2);
    while(ch1!='#'||ch2!='#')
    {
       if(ch1!='+'&&ch1!='-'&&ch1!='*'&&ch1!='/' && ch1!='(' &&ch1!=')' &&ch1!='#') //加了ch1!='(' &&ch1!=')'&&ch1!='#'
         {
           i = 0;
           do                          
           {
                z[i++] = ch1;                         //多位数的算术运算例如:(3433+5643)*4532
                ch1 = getchar();                      //do..while()目的把多位数如3433存入字符数组
           }while(ch1>='0' && ch1 <='9' || ch1=='.'); 
            z[i] = '\0';  //字符串结束标志
           push_num(&s_num, atof(z)); //把字符串转换为浮点型数字然后存入数字栈中
       }
     
       else 
         {
         
          switch(compare(&ch2,&ch1))
          {
              case -1: push_flag(&s_flag,ch1);
                       ch1=getchar(); //添加ch1=getchar();
                       break;
              case 0: pop_flag(&s_flag,&ch2);
                      ch1=getchar(); //添加ch1=getchar();    
                      break;
              case 1: pop_flag(&s_flag,&ch2);   //case 1:中ch1已经取得一个大于栈中的字符所以ch1保留该字符
                      pop_num(&s_num,&a);        //而字符栈中弹出一个字符进行运算,运算结果存入数字栈中
                      pop_num(&s_num,&b);        //再从字符栈中取出一个字符ch2与前面保存的ch1比较
                      function(a,ch2,b,&c);        //若ch1<ch2,则case -1:ch1进字符栈,若ch1=ch2,则case 0:ch2出栈(目的是脱括号)
                      push_num(&s_num,c);        //若ch1>ch2,则case 1: 重复上面
                      break;
               default: printf("error");break;  //上面数字运算顺序出错了,function(b, ch2, a, &c);
          }
         }
         gettop_flag(s_flag,&ch2);
        //去掉了 ch1=getchar(); 
}    

printf("%f\n",*(s_num.base));
return 0;    
}
收到的鲜花
  • xdh08172011-12-30 16:34 送鲜花  5朵   附言:不好意思点错了,没给你评上专家分 在这里 ...

听不同的音乐,看不同的书,游历不同的城市,邂逅不同的人,走的多了,站的高了,自然就看的远了。
2011-12-30 14:16
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
以下是引用liao06550107在2011-12-30 14:16:40的发言:

#include
#include
#define stackmaxsize 100
#define stackincert 10
 
typedef struct
{
    float *top;
    float *base;     
    int stacksize;
}NumStack;
 
typedef struct
{
    char *top;
    char *base;
    int stacksize;
}CharStack;
 
int stackinit_num(NumStack *s)  
{
    s->base = (float*)malloc(stackmaxsize*sizeof(float));
    if(s->base == NULL)  
        exit(1);
    s->top = s->base;     
    s->stacksize = stackmaxsize;
    return 0;
}
 
int stackinit_flag(CharStack *p)
{
    p->base=(char*)malloc(stackmaxsize*sizeof(char));  
    if(p->base==NULL) exit(1);
    p->top=p->base;     
    p->stacksize=stackmaxsize;
    return 0;
}
int push_num(NumStack *s,float x)
{
     if(s->top-s->base>=s->stacksize)
     {
          s->base=(float*)realloc(s->base,(s->stacksize+stackincert)*sizeof(float));     
          if(s->base==NULL) exit(1);
          s->top=s->base+s->stacksize;
          s->stacksize+=stackincert;     
     }
     *(s->top++)=x;
     return 0;
}
 
int push_flag(CharStack *w,char x)
{
     if(w->top-w->base>=w->stacksize)
     {
          w->base=(char*)realloc(w->base,(w->stacksize+stackincert)*sizeof(char));     
          if(w->base==NULL) exit(1);
          w->top=w->base+w->stacksize;
          w->stacksize+=stackincert;     
     }
     *(w->top++)=x;
     return 0;
}
 
int pop_num(NumStack *s_num,float *x)
{
    if(s_num->top==s_num->base)  
        exit(1);
    *x=*(--s_num->top);     
        return 0;
}
 
int pop_flag(CharStack *s_flag,char *x)
{
    if(s_flag->top==s_flag->base)
        exit(1);
    *x=*(--s_flag->top);     
        return 0;
}
     
int gettop_flag(CharStack s,char *x)
{
    if(s.top==s.base)  
        exit(1);
    *x=*(--s.top);     
    return 0;
}
 
int compare(char *p1,char *p2)
{
    if(*p1=='+'||*p1=='-')
       {
           if(*p2=='+'||*p2=='-'||*p2==')'||*p2=='#')
               return 1;
           else  
               return -1;
        }     
    else if(*p1=='*'||*p1=='/')
       {
           if(*p2=='(')  
               return -1;
           else  
               return 1;
       }
    else  if(*p1=='(')  
       {
            if(*p2==')')  
                return 0;
            else if(*p2=='#')  
            {
                printf("表达式错误");
                exit(1);
            }
               else  
                   return -1;
       }
      else if(*p1==')')
        {
         if(*p2=='(')  
         {
             printf("表达式错误");
             exit(1);
         }
         else  
             return 1;     
        }
      else  if(*p1=='#')  
             {
                  if(*p2==')') {printf("表达式错误");exit(1);}
                  else if(*p2=='=') return 0;     
                     else return -1;
             }
             else  
             {
                 printf("表达式错误");
                 exit(1);
             }
}
 
int function(float a,char ch,float b,float *y)
{
    if(ch == '+') //判断==
    *y=a+b;
    else if(ch == '-') *y=a-b;
    else if(ch == '*') *y=a*b;
    else if(ch == '/') *y=a/b;
    else  
    {
        printf("error");
        exit(1);
    }
    return 0;     
}
 
 
 
int main()
{
    NumStack s_num;
    CharStack s_flag;
    char ch1,ch2;
    float a,b,c;
    char z[20];
    int i;
    stackinit_flag(&s_flag);
    push_flag(&s_flag,'#');
    stackinit_num(&s_num);
    printf("请输入表达式,例如:(3+4)*5#\n");
    ch1=getchar();
    gettop_flag(s_flag, &ch2);
    while(ch1!='#'||ch2!='#')
    {
       if(ch1!='+'&&ch1!='-'&&ch1!='*'&&ch1!='/' && ch1!='(' &&ch1!=')' &&ch1!='#') //加了ch1!='(' &&ch1!=')'&&ch1!='#'
         {
           i = 0;
           do                           
           {
                z = ch1;                         //多位数的算术运算例如:(3433+5643)*4532
                ch1 = getchar();                      //do..while()目的把多位数如3433存入字符数组
           }while(ch1>='0' && ch1 <='9' || ch1=='.');  
            z = '\0';  //字符串结束标志
           push_num(&s_num, atof(z)); //把字符串转换为浮点型数字然后存入数字栈中
       }
      
       else  
         {
         
          switch(compare(&ch2,&ch1))
          {
              case -1: push_flag(&s_flag,ch1);
                       ch1=getchar(); //添加ch1=getchar();
                       break;
              case 0: pop_flag(&s_flag,&ch2);
                      ch1=getchar(); //添加ch1=getchar();     
                      break;
              case 1: pop_flag(&s_flag,&ch2);   //case 1:中ch1已经取得一个大于栈中的字符所以ch1保留该字符
                      pop_num(&s_num,&a);        //而字符栈中弹出一个字符进行运算,运算结果存入数字栈中
                      pop_num(&s_num,&b);        //再从字符栈中取出一个字符ch2与前面保存的ch1比较
                      function(a,ch2,b,&c);        //若ch1


感谢啊~~
虽然还有点错误~
晚上我再研究~~
2011-12-30 16:32
liao06550107
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:111
专家分:696
注 册:2011-10-2
收藏
得分:0 
在VC6.0++中编译没有问题!!!
收到的鲜花
  • xdh08172011-12-30 18:29 送鲜花  5朵   附言:~~

听不同的音乐,看不同的书,游历不同的城市,邂逅不同的人,走的多了,站的高了,自然就看的远了。
2011-12-30 16:40
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
以下是引用liao06550107在2011-12-30 16:40:54的发言:

在VC6.0++中编译没有问题!!!
编译没问题,计算结果还差点儿~
2011-12-30 18:29
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
回复 5楼 liao06550107


ok了,在计算function时参数位置反了,atof好强啊~~
2011-12-30 20:34
xdh0817
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:193
专家分:195
注 册:2011-10-20
收藏
得分:0 
以下是引用liao06550107在2011-12-30 16:40:54的发言:

在VC6.0++中编译没有问题!!!
再问一下,用'\0'表示结束,其实‘\0'后面的东西还是存在的,那么atof(z)以后最好是把z清空一次吧?memset(z,0,20)
2011-12-30 21:02
快速回复:运行有问题
数据加载中...
 
   



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

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