| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 394 人关注过本帖
标题:为什么程序进不了循环,
取消只看楼主 加入收藏
NeQhk
Rank: 2
等 级:论坛游民
威 望:1
帖 子:164
专家分:85
注 册:2014-7-19
结帖率:69.23%
收藏
 问题点数:0 回复次数:0 
为什么程序进不了循环,
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<ctype.h>
#define M 600
#define increase 10
typedef double ElemType;

typedef struct node{
    ElemType *base;
    ElemType *top;
    int stacksize;
}stack;

void Initstack(stack *s)
{
    s->base=(ElemType*)malloc(sizeof(ElemType));
    if(!s->base)
    {
        exit(0);
    }
    s->base=s->top;
    s->stacksize=M;
}

void push(stack *s,ElemType e)
{
  if(s->top - s->base ==s->stacksize)
  {
    s->base=(ElemType*)realloc(s->base,(s->stacksize+increase)*sizeof(ElemType));
    if(!s->base)
    {
        exit(0);
    }
    s->top=s->base+s->stacksize;
    s->stacksize=s->stacksize+increase;
  }
  *(s->top)=e;
     s->top++;
 
}
void pop(stack *s,ElemType *e)
{
  if(s->top==s->base)
  {
     return ;
  }
  *e=*--(s->top);
}

void clear(stack *s)
{
  s->top=s->base;
}
void destroy(stack *s)
{
    free(s->base);
    s->base=s->top=NULL;
    s->stacksize=0;
}
int stacklen(stack s)
{
  return (s.top-s.base);
}
int main()
{
  stack s;
  int i,j=0,k=0,len,n;
  char c;
  double d,e;
  char ch[100],str[100];
  Initstack(&s);
  /*while(c=getchar())
   {
       if(c!='\n')
     {
         ch[j++]=c;//保存在数组ch【i】中
         len++;//len为输入的长度
     }
       else if(c=='\n')
           break;
       else
           ;
   }*/
  scanf("%d",&n);
  for(j=0;j<n;j++)
  {
     scanf("%c",&ch[j]);
  }
  i=0;
  while(ch[i]!='\n')///————————————————————这个循环进不去运行,直接跳过了,这是求逆波兰计算器的代码
  {
       while(isdigit(ch[i]))
       {
          str[k++]=ch[i];//str缓冲区
          str[k]='\0';
          i++;
          if(!isdigit(ch[i]))
          {
              d=atof(str);
              push(&s,d);
              k=0;
              break;
          }
       }
       switch(ch[i])
       {
       case '+':
           pop(&s,&e);
           pop(&s,&d);
           push(&s,d+e);
           break;
       case '-':
           pop(&s,&e);
           pop(&s,&d);
           push(&s,d-e);
           break;
       case '*':
           pop(&s,&e);
           pop(&s,&d);
           push(&s,d*e);
           break;
       case '/':
           pop(&s,&e);
           pop(&s,&d);
           if(e!=0)
           {
             push(&s,d/e);
           }
           else
           {
              printf("wrong");
           }
           break;
       }
        i++;
  }
  pop(&s,&d);
  printf("%d",d);
  return 0;
}
搜索更多相关主题的帖子: double include increase 
2015-04-25 18:46
快速回复:为什么程序进不了循环,
数据加载中...
 
   



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

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