1
void main( ) char c; /* int c; */ int sum=0; while ((c = getchar())!= EOF) if(isdigit(c) ) sum += (c-'0'); printf("%d\n", sum); main()int i,sum=0; for(i=0;i<100;++i)sum+=i;printf("%d\n",sum); main( ) int i,sum=0; for (i=0; i<100; ++i) sum+=i; printf("%d\n",sum); main( ) int i, sum = 0; for (i = 0; i < 100; ++i) sum += i; printf("%d\n", sum); 2
void bar(void) int i, j; for (i=0; i<1000; ++i) for (j=0;j<10000; ++j); void foo(void) int i, j; for (i=0 ; i<1000; ++i) for (j=0 ; j<1000; ++j); main ( ) bar( ); foo( ); 3
#include <stdio.h> main() #include <stdio.h> char str[10], *p=str; gets(p); printf("%s\n", p); main() char *p; gets(p); printf("%s\n", p); 4
#define MAX_STACK 256 void push(double val); double pop(void); double surface_sp(void); #include <stdio.h> #include stack.h double stack[max_stack]; int sp=max_stack; double pop(void) * sp 5 + void push ( int ) 3 5
double surface_sp() CC = gcc CFLAGS = -O.c.o : $(CC) $(CFLAGS) -c $< TAR = stack OBJ = stack.o SRC = stack.c #define DEBUG main(void) HDR = stack.h $(TAR) : $(OBJ) $(HDR) int i; $(CC) $(CFLAGS) $(OBJ) -o $(TAR) -lm double x; #ifdef DEBUG x = pop( ); $(TAR):$(OBJ) $(HDR) for (i=0; i<257 ; ++i ) push( i ); #endif push ( 3 ); printf( %f\n, surface_sp( ) ); x = pop ( ); printf( %f\n, x); printf( %d\n, sp ); 6
7
int lex(char *buf) char tmp[max_stack]; int c; double value; main(void) char *buf; int c; while ( (c = getchar()) == ' t' c == ' ') ; if( c == '.' isdigit(c) ) else if ( isop(c) ) buf = (char *)calloc(sizeof(char),max_stack); while( (c = lex(buf))!= NULL ) if ( c == NUMBER ) printf("number %s n", buf); if ( c == OP ) printf("operator %s n", buf); else if ( c == ' n') return(null); else perror("unresolved character"); exit(1); int isop(char op) 8
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "stack.h" char *rev_pol(void) char *buf, *out; static char tmp[max_stack]; int c; if ( ( buf = (char *)calloc(sizeof(char),max_stack) ) == NULL ) perror("memory is not available"); exit(1); if ( ( out = (char *)calloc(sizeof(char),,max_stack) ) == NULL ) perror("memory is not available"); exit(1); while( (c = lex(buf))!= NULL ) if ( c == NUMBER ) if ( c == OP ) while( sp!= MAX_STACK ) strcat(out," 0"); strcpy(tmp,out); free(buf); free(out); return(tmp); 9
int level(char c) int priority; return( priority ); lex.c stack.c rpol.c #ifdef DEBUG main(void) char *pol; int c; pol=(char *)malloc(max_stack); for(;;) pol=rev_pol(); printf( %s\n, pol); if ( (c=getchar()) == EOF) break; else ungetc(c,stdin); #endif pop() push() surface_sp() rev_pol() main() token isop() lex() level.c level() 10
char *token; char *get_token(char *eq) static char *buf=null, *begin_buf, *end_buf; if (buf == NULL) begin_buf= buf = (char *)malloc(strlen(eq)+1); strcpy(buf, eq); end_buf = buf + strlen(buf); if (buf < end_buf) if (*token == NULL) free(begin_buf); token = buf = NULL; else free(begin_buf); token = buf = NULL; return (token); 11
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "stack.h" extern char *token; main(void) char *eq="2 3 + 5 *"; double tmp; extern char *token; double polish(char *eq) double tmp; while( get_token(eq)!= NULL ) if(isdigit(*token)) else if(*token == '+') else if(*token == '-') else if(*token == '*') else if(*token == '/') else if(*token == '^') else if(*token == '%') while( get_token(eq)!= NULL ) if(isdigit(*token)) printf("%f n", atof(token)); else printf("%c n", *token); else perror("undefined operation"); exit(1); if(query_sp()!= MAX_STACK-1) printf("sp=%d n", query_sp()); perror("bad expression"); exit(1); return pop(); 12
main(void) char *pol=null; int c; double x; pol = (char *)malloc(max_stack); pop() for(;;) pol=rev_pol(); push() x = polish(pol); surface_sp() printf("%g\n", x); if ( (c = getchar()) == EOF) break; else ungetc(c,stdin); free(pol); pol.c polish() token.c get_token() main() token stack.c rpol.c rev_pol() token lex.c isop() lex() level.c level() 13
14