Skip to content

Instantly share code, notes, and snippets.

@sunnylost
Last active December 18, 2015 19:00
Show Gist options
  • Save sunnylost/5830034 to your computer and use it in GitHub Desktop.
Save sunnylost/5830034 to your computer and use it in GitHub Desktop.
TCPL 习题集 第四章
#include <stdio.h>
char s[100];
int i = 0;
void itoa(int);
void itoa(int d) {
int tmp;
if(d == 0) {
for(int j = 0, k = i - 1; j < k; j++, k--) {
tmp = s[j];
s[j] = s[k];
s[k] = tmp;
}
s[i] = '\0';
printf("%s\n", s);
} else {
s[i++] = d % 10 + '0';
itoa(d /= 10);
}
}
int main() {
itoa(523123);
}
//由字符串转为数字,支持科学表示法
#include <stdio.h>
double atof(char[]);
void skipSpace(char[]);
double getE(char[], int);
int isspace(char);
int isdigit(char);
int main() {
char s[] = "12.133e12.f";
printf("%f\n", atof(s));
}
double val, power;
int i, sign;
double atof(char s[]) {
skipSpace(s);
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;
}
double e = getE(s, i);
return sign * val * e / power;
}
void skipSpace(char s[]) {
for(i = 0; isspace(s[i]); i++)
;
}
double getE(char s[], int i) {
int powerSign;
int num = 0;
double e = 1.0;
if(s[i] == 'e' || s[i] == 'E')
i++;
if(s[i] == '+' || s[i] == '-') {
powerSign = s[i] == '-' ? -1 : 1;
i++;
} else {
powerSign = 1;
}
while(isdigit(s[i])) {
num = 10 * num + (s[i] - '0');
i++;
}
for(i = 0; i < num; i++) {
if(powerSign > 0) {
e *= 10.0;
} else {
e /= 10.0;
}
}
return e;
}
int isspace(char c) {
return (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
}
int isdigit(char c) {
return (c >= '0' && c <= '9') ? 1 : 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAXOP 100
#define NUMBER '0'
#define MAXVAL 100
#define BUFSIZE 100
int getop(char[]);
int getch();
void ungetch(int);
void push(double);
double pop();
int isdigit(char);
int sp = 0;
double val[MAXVAL];
char buf[BUFSIZE];
int bufp = 0;
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;
}
}
int getch() {
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) {
if(bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
int getop(char s[]) {
int i = 0, c, n;
while((s[0] = c = getch()) == ' ' || c == '\t');
s[1] = '\0';
if(c == '-') {
n = getch();
if(!isdigit(n) && n != '.') {
ungetch(n);
return c;
} else {
s[i++] = c;
c = s[i] = n;
}
} else if(!isdigit(c) && c != '.' && c != '-')
return c;
if(isdigit(c))
while(isdigit(s[++i] = c = getch()));
if(c == '.')
while(isdigit(s[++i] = c = getch()));
s[i] = '\0';
if(c != EOF)
ungetch(c);
return NUMBER;
}
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((int)pop() % (int)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 isdigit(char c) {
return (c >= '0' && c <= '9') ? 1 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment