读书人

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

发布时间: 2012-03-09 16:54:56 作者: rapoo

计算器程序,头文件和变量摆放位置不懂,求教
第一种是正确的

C/C++ code
#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 100int 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 100int 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;}


第二种编译通过,运行报错
C/C++ code
#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;    }} 



书上的格式是第一种,但没有讲头文件和变量为什么要这样摆放
求教

[解决办法]
是你很多变量没有初始化,导致断言报错。
[解决办法]
两个程序一样吗?

assert wrong
[解决办法]
没看到什么区别
[解决办法]
可以,分析这样的程序只要看懂算法就好了,不要去纠结,编程的规范是在平时一步步养成的,多看点书,多查阅点资料,多上论坛问问题,从基础开始,
[解决办法]
头文件和变量在哪儿放不重要,只要放在使用之前就行

读书人网 >C语言

热点推荐