计算器程序,头文件和变量摆放位置不懂,求教

i945800687 2012-02-14 10:43:13
第一种是正确的

#include <stdio.h>
#include <math.h>

#define MAXOP 100
#define NUMBER '0'

int getop(char s[]);
void push(double f);
double pop();

int main()
{
int type;
double op2;
char s[MAXOP];

while ((type=getop(s)) != EOF)
{
switch (type)
{
case NUMBER:
{
push(atof(s));
break;
}
case '+':
{
push(pop() + pop());
break;
}
case '-':
{
op2 = pop();
push(pop() - op2);
break;
}
case '*':
{
push(pop() * pop());
break;
}
case '/':
{
op2 = pop();
if (op2 != 0.0)
{
push(pop() / op2);
}
else
{
printf("error: zero divisor\n");
}
break;
}

case '%':
{
op2 = pop();
if (op2 != 0.0)
{
push(fmod(pop(), op2));
}
else
{
printf("error: zero divisor\n");
}
break;
}
case '\n':
{
printf("\t%.8g\n", pop());
break;
}
default:
{
printf("error: unknown command %s\n", s);
break;
}
}
}

return 0;
}

#define MAXVAL 100

int sp=0;
double val[MAXVAL];

void push(double f)
{
if (sp < MAXVAL)
{
val[sp++] = f;
}
else
{
printf("error: stack full, can't push %g\n", f);
}
}

double pop()
{
if (sp > 0)
{
return val[--sp];
}
else
{
printf("error: stack empty\n");

return 0.0;
}
}

#include <ctype.h>

#define MAXLINE 100

int getline(char line[], int lim);

int li=0;
char line[MAXLINE];

int getop(char s[])
{
int c, i;

if (line[li] == '\0')
{
if (getline(line, MAXLINE) == 0)
{
return EOF;
}
else
{
li = 0;
}
}

while ((s[0]=c=line[li++]) == ' ' || c == '\t')
{
;
}
s[1] = '\0';

if (!isdigit(c) && c!='.')
{
return c;
}
i = 0;
if (isdigit(c))
{
while (isdigit(s[++i] = c = line[li++]))
{
;
}
}

if (c == '.')
{
while (isdigit(s[++i] = c = line[li++]))
{
;
}
}
s[i] = '\0';
--li;

return NUMBER;
}

int getline(char s[], int lim)
{
int c, i;

i = 0;

while (--lim>0 && (c=getchar())!=EOF && c!='\n')
{
s[i++] = c;
}
if (c == '\n')
{
s[i++] = c;
}
s[i] = '\0';

return i;
}


第二种编译通过,运行报错

#include <stdio.h>
#include <math.h>
#include <ctype.h>

#define MAXLINE 100
#define MAXOP 100
#define MAXVAL 100
#define NUMBER '0'

int getline(char line[], int lim);
int getop(char s[]);
void push(double f);
double pop();

int main()
{
int type;
double op2;
char s[MAXOP];

while ((type=getop(s)) != EOF)
{
switch (type)
{
case NUMBER:
{
push(atof(s));
break;
}
case '+':
{
push(pop() + pop());
break;
}
case '-':
{
op2 = pop();
push(pop() - op2);
break;
}
case '*':
{
push(pop() * pop());
break;
}
case '/':
{
op2 = pop();
if (op2 != 0.0)
{
push(pop() / op2);
}
else
{
printf("error: zero divisor\n");
}
break;
}

case '%':
{
op2 = pop();
if (op2 != 0.0)
{
push(fmod(pop(), op2));
}
else
{
printf("error: zero divisor\n");
}
break;
}
case '\n':
{
printf("\t%.8g\n", pop());
break;
}
default:
{
printf("error: unknown command %s\n", s);
break;
}
}
}

return 0;
}

int getline(char s[], int lim)
{
int c, i;

i = 0;

while (--lim>0 && (c=getchar())!=EOF && c!='\n')
{
s[i++] = c;
}
if (c == '\n')
{
s[i++] = c;
}
s[i] = '\0';

return i;
}

int getop(char s[])
{
int c, i, li;
char line[MAXLINE];

li = 0;

if (line[li] == '\0')
{
if (getline(line, MAXLINE) == 0)
{
return EOF;
}
else
{
li = 0;
}
}

while ((s[0]=c=line[li++]) == ' ' || c == '\t')
{
;
}
s[1] = '\0';

if (!isdigit(c) && c!='.')
{
return c;
}
i = 0;
if (isdigit(c))
{
while (isdigit(s[++i] = c = line[li++]))
{
;
}
}

if (c == '.')
{
while (isdigit(s[++i] = c = line[li++]))
{
;
}
}
s[i] = '\0';
--li;

return NUMBER;
}

void push(double f)
{
int sp;
double val[MAXVAL];

sp = 0;

if (sp < MAXVAL)
{
val[sp++] = f;
}
else
{
printf("error: stack full, can't push %g\n", f);
}
}

double pop()
{
int sp;
double val[MAXVAL];

sp = 0;

if (sp > 0)
{
return val[--sp];
}
else
{
printf("error: stack empty\n");

return 0.0;
}
}


书上的格式是第一种,但没有讲头文件和变量为什么要这样摆放
求教
...全文
158 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
面包大师 2012-02-15
  • 打赏
  • 举报
回复
当然还记得要。。。结贴,呵呵
面包大师 2012-02-15
  • 打赏
  • 举报
回复
可以,分析这样的程序只要看懂算法就好了,不要去纠结,编程的规范是在平时一步步养成的,多看点书,多查阅点资料,多上论坛问问题,从基础开始,
i945800687 2012-02-15
  • 打赏
  • 举报
回复
-3 -4

头文件和变量摆放位置不一样
i945800687 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 downmooner 的回复:]
是你很多变量没有初始化,导致断言报错。
[/Quote]

[Quote=引用 2 楼 czh3642210 的回复:]
//这儿清空下缓冲区的东西就好了
[/Quote]

断言和缓冲区的知识看什么书了解呢?
我可不可以先直接分析第一个程序,记住头文件、变量位置顺序,以后懂了再来看
justkk 2012-02-15
  • 打赏
  • 举报
回复
没看到什么区别
AnYidan 2012-02-15
  • 打赏
  • 举报
回复
两个程序一样吗?

assert wrong
i945800687 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 czh3642210 的回复:]
可以,分析这样的程序只要看懂算法就好了,不要去纠结,编程的规范是在平时一步步养成的,多看点书,多查阅点资料,多上论坛问问题,从基础开始,
[/Quote]

嗯,那我先记到算了
taoyh2002 2012-02-15
  • 打赏
  • 举报
回复
头文件和变量在哪儿放不重要,只要放在使用之前就行
面包大师 2012-02-14
  • 打赏
  • 举报
回复
定义在哪儿都没什么的,只是预编译的时候会有点影响
你改成下边这个样子应该就好了
int main()
{
int type;
double op2;
char s[MAXOP];
getchar();//这儿清空下缓冲区的东西就好了
while ((type=getop(s)) != EOF)
downmooner 2012-02-14
  • 打赏
  • 举报
回复
是你很多变量没有初始化,导致断言报错。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧