69,373
社区成员
发帖
与我相关
我的任务
分享
#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;
}
}
int main()
{
int type;
double op2;
char s[MAXOP];
getchar();//这儿清空下缓冲区的东西就好了
while ((type=getop(s)) != EOF)