#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXINT 10
#define INTEGER '0'
int getint(char []);
void push(int);
int pop(void);
int main(void)
{
int type, sum;
int count = 0;
char s[MAXINT];
while ((type = getint(s)) != EOF) {
++count;
switch (type) {
case INTEGER:
push(atoi(s));
break;
case '\n':
sum = 0;
while (--count)
sum += pop();
printf("\tsum = %d\n", sum);
break;
default:
printf("error: unknown input\n");
break;
}
}
return 0;
}
#define LEVEL 100
int sp = 0;
int stack[LEVEL];
void push(int val)
{
if (sp < LEVEL)
stack[sp++] = val;
else
printf("error: stack full, can't push %d\n", val);
}
int pop(void)
{
if (sp > 0)
return stack[--sp];
else {
printf("error: stack empty\n");
return 0;
}
}
int getch(void);
void ungetch(int);
int getint(char s[])
{
int c, i;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '-' && c != '+')
return c;
i = 0;
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return INTEGER;
}
#define BUFSIZE 10
int bufp = 0;
int buf[BUFSIZE];
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp < BUFSIZE)
buf[bufp++] = c;
else
printf("error: too many characters\n");
}
[ 本帖最后由 ditg 于 2014-8-28 19:47 编辑 ]
#include <stdlib.h>
#include <ctype.h>
#define MAXINT 10
#define INTEGER '0'
int getint(char []);
void push(int);
int pop(void);
int main(void)
{
int type, sum;
int count = 0;
char s[MAXINT];
while ((type = getint(s)) != EOF) {
++count;
switch (type) {
case INTEGER:
push(atoi(s));
break;
case '\n':
sum = 0;
while (--count)
sum += pop();
printf("\tsum = %d\n", sum);
break;
default:
printf("error: unknown input\n");
break;
}
}
return 0;
}
#define LEVEL 100
int sp = 0;
int stack[LEVEL];
void push(int val)
{
if (sp < LEVEL)
stack[sp++] = val;
else
printf("error: stack full, can't push %d\n", val);
}
int pop(void)
{
if (sp > 0)
return stack[--sp];
else {
printf("error: stack empty\n");
return 0;
}
}
int getch(void);
void ungetch(int);
int getint(char s[])
{
int c, i;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '-' && c != '+')
return c;
i = 0;
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return INTEGER;
}
#define BUFSIZE 10
int bufp = 0;
int buf[BUFSIZE];
int getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if (bufp < BUFSIZE)
buf[bufp++] = c;
else
printf("error: too many characters\n");
}
[ 本帖最后由 ditg 于 2014-8-28 19:47 编辑 ]
梦想拥有一台龙芯3A-4000