本文共 23335 字,大约阅读时间需要 77 分钟。
#include#include #define MAXLINE 1024int getchars(char s[],int max){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < max - 1) s[l++] = c; s[l] = '\0'; return l;}int strindex(char s[], char t[]){ int sl, tl; int i, j, k; int c; sl = strlen(s); tl = strlen(t); i = sl - tl; while (i >= 0) { for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; ++j, ++k) ; if (k > 0 && t[k] == '\0') return i; --i; } return -1;}int main(){ char src[MAXLINE]; char ptrn[MAXLINE]; printf("Please Enter: \n"); while(getchars(ptrn,MAXLINE)==0) ; printf("The rightmost index is: %d\n",strindex(src,ptrn)); return 0;}
#include#include #define MAXLINE 1024int getchars(char s[], int max){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < max - 1) s[l++] = c; s[l] = '\0'; return l;}double atof(char s[]){ double val, power, base, epower; int i, sign, exponent; for (i = 0; isspace(s[i]); i++) /* skip white space */ ; sign = (s[i] == '-') ? -1 : 1; if (s[i] == '+' || s[i] == '-') i++; for (val = 0.0; isdigit(s[i]); i++) val = 10.0 * val + (s[i] - '0'); if (s[i] == '.') i++; for (power = 1.0; isdigit(s[i]); i++) { val = 10.0 * val + (s[i] - '0'); power *= 10; } epower = 1; if (s[i] == 'e' || s[i] == 'E') { i++; base = s[i] == '-' ? 0.1 : 10.0; if (s[i] == '-' || s[i] == '+') i++; for (exponent = 0; isdigit(s[i]); i++) { exponent = 10 * exponent + (s[i] - '0'); } while (exponent-- > 0) epower *= base; } return sign * val / power * epower;}int main(void){ char s[MAXLINE]; printf("Enter a number:\n"); while(getchars(s,MAXLINE)==0) ; printf("The number is %lf\n",atof(s)); return 0;}
#include#include #include #define MAXLINE 1024#define MAXOP 100#define NUMBER '0'int getop(char []);void push(double);double pop(void);int main(void){ 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 '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); 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("= %g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0;}#define MAXVAL 100 /* maximum depth of val stack */int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//* push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}#include int getch(void);void ungetch(int);/* getop: get next character or numeric operand */int getop(char s[]){ int i, c, c2; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; if (!isdigit(c) && c != '.' && c != '-') return c; /* not a number */ i = 0; if (c == '-') { c2 = getch(); if (c2 != EOF) ungetch(c2); if (!isdigit(c2) && c2 != '.') return c; } if (isdigit(c) || c == '-') /* collect integer part */ while (isdigit(s[++i] = c = getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE]; /* buffer for ungetch */int bufp = 0; /* next free position in buf */int getch(void) /* get a (possibly pushed-back) character */{ return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c) /* push character back on input */{ if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c;}
#include#include #include #define MAXLINE 1024#define MAXOP 100#define NUMBER '0'int getop(char []);void push(double);double pop();void printtop();void duplicate();void swap();void clear();int main(void){ push(114.514); push(1919.810); duplicate(); printtop(); swap(); clear(); return 0;}#define MAXVAL 100 /* maximum depth of val stack */int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//* push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}/* printtop: print the top element of the stack */void printtop(void){ if (sp > 0) printf("%g\n", val[sp - 1]); else printf("error: stack empty\n");}/* duplicate: duplicate the top element of the stack */void duplicate(void){ double top = pop(); push(top); push(top);}/* swap: swap the top two elements of the stack */void swap(void){ double top1 = pop(); double top2 = pop(); push(top1); push(top2);}/* clear: clear stack */void clear(void){ sp = 0;}
#include#include #include #define MAXLINE 1024#define MAXOP 100#define NUMBER '0'#define MATHLIB '1'int getop(char []);void push(double);double pop();int main(void){ int type; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case MATHLIB: if (strcmp(s, "sin") == 0) push(sin(pop())); else if (strcmp(s, "cos") == 0) push(cos(pop())); else if (strcmp(s, "pow") == 0) { op2 = pop(); push(pow(pop(), op2)); } else printf("error: unknown command %s\n", s); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); 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("= %g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0;}#define MAXVAL 100 /* maximum depth of val stack */int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//* push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}#include int getch(void);void ungetch(int);/* getop: get next character or numeric operand */int getop(char s[]){ int i, c, c2; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; i = 0; if (isalpha(c)) { while (isalpha(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return MATHLIB; } if (!isdigit(c) && c != '.' && c != '-') return c; /* not a number */ if (c == '-') { c2 = getch(); if (c2 != EOF) ungetch(c2); if (!isdigit(c2) && c2 != '.') return c; } if (isdigit(c) || c == '-') /* collect integer part */ while (isdigit(s[++i] = c = getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE]; /* buffer for ungetch */int bufp = 0; /* next free position in buf */int getch(void) /* get a (possibly pushed-back) character */{ return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c) /* push character back on input */{ if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c;}
#include#include #include #include #include #define MAXLINE 1024#define MAXOP 100#define NUMBER '0'#define MATHLIB '1'#define VARIABLE '2'int getop(char []);void push(double);double pop(void);double vars[26];double last;int main(void){ int type, v; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case MATHLIB: if (strcmp(s, "sin") == 0) push(sin(pop())); else if (strcmp(s, "cos") == 0) push(cos(pop())); else if (strcmp(s, "pow") == 0) { op2 = pop(); push(pow(pop(), op2)); } else if (strcmp(s, "last") == 0) { printf("the most recently printed value is %g\n", last); } else printf("error: unknown command %s\n", s); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); 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 VARIABLE: v = tolower(s[0]); last = vars[v - 'a'] = pop(); push(last); break; case '\n': printf("= %g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0;}#define MAXVAL 100 /* maximum depth of val stack */int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//* push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}#include int getch(void);void ungetch(int);/* getop: get next character or numeric operand */int getop(char s[]){ int i, c, c2; while ((s[0] = c = getch()) == ' ' || c == '\t') ; s[1] = '\0'; i = 0; if (isalpha(c)) { while (isalpha(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return MATHLIB; } if (!isdigit(c) && c != '.' && c != '-') { if (c == '=') { if (isalpha(c2 = getch())) { s[0] = c2; return VARIABLE; } else if (c2 != EOF) { ungetch(c2); } } return c; /* not a number */ } if (c == '-') { c2 = getch(); if (c2 != EOF) ungetch(c2); if (!isdigit(c2) && c2 != '.') return c; } if (isdigit(c) || c == '-') /* collect integer part */ while (isdigit(s[++i] = c = getch())) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = getch())) ; s[i] = '\0'; if (c != EOF) ungetch(c); return NUMBER;}#define BUFSIZE 100char buf[BUFSIZE]; /* buffer for ungetch */int bufp = 0; /* next free position in buf */int getch(void) /* get a (possibly pushed-back) character */{ return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c) /* push character back on input */{ if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c;}
#include#include int getch();void ungetch(int);void ungets(char s[]);int main(void){ ungets("testring"); putchar(getch()); putchar(getch()); putchar(getch()); putchar(getch()); putchar(getch()); putchar(getch()); putchar(getch()); putchar(getch()); return 0;}#define BUFSIZE 100char buf[BUFSIZE]; /* buffer for ungetch */int bufp = 0; /* next free position in buf */int getch(void) /* get a (possibly pushed-back) character */{ return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c) /* push character back on input */{ if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else buf[bufp++] = c;}void ungets(char s[]){ int l = strlen(s); while (l) ungetch(s[--l]);}
#includeint getch();void ungetch(int);int main(void){ ungetch('a'); ungetch('t'); putchar(getch()); return 0;}int buf = -1; /* buffer for ungetch */int getch(void) /* get a (possibly pushed-back) character */{ int b = buf; if (b > -1) { buf = -1; return b; } return getchar();}void ungetch(int c) /* push character back on input */{ buf = c;}
#includeint getch();void ungetch(int);int main(void){ ungetch('t'); ungetch(EOF); putchar(getch()); return 0;}#define BUFSIZE 100char buf[BUFSIZE]; /* buffer for ungetch */int bufp = 0; /* next free position in buf */int getch(void) /* get a (possibly pushed-back) character */{ return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c) /* push character back on input */{ if (bufp >= BUFSIZE) printf("ungetch: too many characters\n"); else if (c != EOF) buf[bufp++] = c;}
#include#include #include #include #include #define MAXLINE 1024#define MAXOP 100#define NUMBER '0'#define MATHLIB '1'#define VARIABLE '2'int get_line(char line[],int limit);char line[MAXLINE];int linep = 0;int getop(char []);void push(double);double pop();double vars[26];double last;int main(void){ int type, v; double op2; char s[MAXOP]; while (get_line(line, MAXLINE) > 0) { linep = 0; while ((type = getop(s)) != '\0') { switch (type) { case NUMBER: push(atof(s)); break; case MATHLIB: if (strcmp(s, "sin") == 0) push(sin(pop())); else if (strcmp(s, "cos") == 0) push(cos(pop())); else if (strcmp(s, "pow") == 0) { op2 = pop(); push(pow(pop(), op2)); } else if (strcmp(s, "last") == 0) { printf("the most recently printed value is %g\n", last); } else printf("error: unknown command %s\n", s); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); 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 VARIABLE: v = tolower(s[0]); last = vars[v - 'a'] = pop(); push(last); break; case '\n': printf("= %g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } } return 0;}#define MAXVAL 100 /* maximum depth of val stack */int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//* push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}#include /* getop: get next character or numeric operand */int getop(char s[]){ int i, c, c2; while ((s[0] = c = line[linep++]) == ' ' || c == '\t') ; s[1] = '\0'; i = 0; if (isalpha(c)) { while (isalpha(s[++i] = c = line[linep++])) ; s[i] = '\0'; if (c != '\0') --linep; return MATHLIB; } if (!isdigit(c) && c != '.' && c != '-') { if (c == '=') { if (isalpha(c2 = line[linep++])) { s[0] = c2; return VARIABLE; } else if (c2 != '\0') { --linep; } } return c; /* not a number */ } if (c == '-') { c2 = line[linep++]; if (c2 != '\0') --linep; if (!isdigit(c2) && c2 != '.') return c; } if (isdigit(c) || c == '-') /* collect integer part */ while (isdigit(s[++i] = c = line[linep++])) ; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = line[linep++])) ; s[i] = '\0'; if (c != '\0') --linep; return NUMBER;}int get_line(char s[], int max){ int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < max - 1) s[l++] = c; if (c == '\n') s[l++] = c; s[l] = '\0'; return l;}
#include#include #include #include #include #define MAXLINE 1024#define MAXOP 100#define NUMBER '0'#define MATHLIB '1'#define VARIABLE '2'int getop(char []);void push(double);double pop();double vars[26];double last;int main(void){ int type, v; double op2; char s[MAXOP]; while ((type = getop(s)) != EOF) { switch (type) { case NUMBER: push(atof(s)); break; case MATHLIB: if (strcmp(s, "sin") == 0) push(sin(pop())); else if (strcmp(s, "cos") == 0) push(cos(pop())); else if (strcmp(s, "pow") == 0) { op2 = pop(); push(pow(pop(), op2)); } else if (strcmp(s, "last") == 0) { printf("the most recently printed value is %g\n", last); } else printf("error: unknown command %s\n", s); break; case '+': push(pop() + pop()); break; case '*': push(pop() * pop()); break; case '-': op2 = pop(); push(pop() - op2); 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 VARIABLE: v = tolower(s[0]); last = vars[v - 'a'] = pop(); push(last); break; case '\n': printf("= %g\n", pop()); break; default: printf("error: unknown command %s\n", s); break; } } return 0;}#define MAXVAL 100 /* maximum depth of val stack */int sp = 0; /* next free stack position */double val[MAXVAL]; /* value stack *//* push: push f onto value stack */void push(double f){ if (sp < MAXVAL) val[sp++] = f; else printf("error: stack full, can't push %g\n", f);}/* pop: pop and return top value from stack */double pop(void){ if (sp > 0) return val[--sp]; else { printf("error: stack empty\n"); return 0.0; }}#include int getch(void);/* getop: get next character or numeric operand */int getop(char s[]){ static int buf = EOF; int i, c, c2; while ((s[0] = c = (buf == EOF ? getch() : buf)) == ' ' || c == '\t') buf = EOF; buf = EOF; s[1] = '\0'; i = 0; if (isalpha(c)) { while (isalpha(s[++i] = c = (buf == EOF ? getch() : buf))) buf = EOF; buf = EOF; s[i] = '\0'; if (c != EOF) buf = c; return MATHLIB; } if (!isdigit(c) && c != '.' && c != '-') { if (c == '=') { c2 = (buf == EOF ? getch() : buf); buf = EOF; if (isalpha(c2)) { s[0] = c2; return VARIABLE; } else if (c2 != EOF) { buf = c2; } } return c; /* not a number */ } if (c == '-') { c2 = (buf == EOF ? getch() : buf); buf = EOF; if (c2 != EOF) buf = c2; if (!isdigit(c2) && c2 != '.') return c; } if (isdigit(c) || c == '-') /* collect integer part */ while (isdigit(s[++i] = c = (buf == EOF ? getch() : buf))) { buf = EOF; } buf = EOF; if (c == '.') /* collect fraction part */ while (isdigit(s[++i] = c = (buf == EOF ? getch() : buf))) buf = EOF; buf = EOF; s[i] = '\0'; if (c != EOF) buf = c; return NUMBER;}int getch(void) /* get a (possibly pushed-back) character */{ return getchar();}
#include#include #include #define MAXLINE 1024void itoa(int n,char s[]);void itoaR(int n,char s[]);int main(void){ char s[MAXLINE]; itoa(INT_MIN, s); printf("%d ---> %s.\n", INT_MIN, s); return 0;}void itoa(int n, char s[]){ s[0] = '\0'; itoaR(n, s);}void itoaR(int n, char s[]){ int remainder, l; if (n / 10) itoa(n / 10, s); l = strlen(s); if (n / 10 == 0 && n < 0) s[l++] = '-'; remainder = n % 10; s[l++] = ((n < 0) ? -remainder : remainder) + '0'; s[l++] = '\0';}
#include#include #define MAXLINE 1024int getchars(char line[],int max);void reverse(char s[]){ reverseR(s,0,strlen(s)-1);}void reverseR(char s[],int i,int j){ int tmp; tmp = s[i]; s[i] = s[j]; s[j] = tmp; if(i
#include#include #define swap(t,x,y) {t tmp;tmp=x;x=y;y=tmp;}int main(){ int a=114; int b=514; printf("Before swap, a=%d,b=%d\n",a,b); swap(int,a,b) printf("After swap, a=%d,b=%d\n",a,b);}
转载地址:http://tfwai.baihongyu.com/