Last active
December 18, 2015 22:39
-
-
Save sunnylost/5856004 to your computer and use it in GitHub Desktop.
TCPL 习题集 第五章
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//部分程序来自于上一章节 | |
//getfloat:将字符串转换成 float | |
#include <stdio.h> | |
#include <ctype.h> | |
#include "calc.h" | |
int getch(void); | |
void ungetch(int); | |
int getfloat(double *); | |
int getfloat(double *d) { | |
int c, sign; | |
double power = 10.0; | |
while(isspace(c = getch())) | |
; | |
if(!isdigit(c) && c != '-' && c != '+' && c != EOF && c != '.') { | |
ungetch(c); | |
return 0; | |
} | |
sign = (c == '-') ? -1 : 1; | |
if(c == '+' || c == '-') | |
c = getch(); | |
for(*d = 0.0; isdigit(c); c = getch()) { | |
*d = 10 * *d + (c - '0'); | |
} | |
if(c == '.') { | |
c = getch(); | |
for(; isdigit(c); c = getch()) { | |
*d += (c - '0') / power; | |
power *= 10.0; | |
} | |
} | |
*d *= sign; | |
if(c != EOF) | |
ungetch(c); | |
return c; | |
} | |
int main(){ | |
double d = 0.0; | |
printf("Return = %g\n", getfloat(&d)); | |
printf("Result = %.12g\n", d); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//将字符串 t 拼接到 s 后面 | |
#include <stdio.h> | |
void strcat(char*, char*); | |
void strcat(char *s, char *t) { | |
while(*s++); | |
*--s; | |
while(*s++ = *t++); | |
} | |
int main(){ | |
char t[] = "0123456789"; | |
char s[20] = "abc"; | |
strcat(s, t); | |
printf("%s\n", s); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//strend(s, t),如果字符串 t 出现在字符串 s 的尾部,该函数返回 1,否则返回 0 | |
#include <stdio.h> | |
int strend(char*, char*); | |
int strend(char *s, char *t) { | |
int l = 0; | |
while(*s++); | |
while(*t++) | |
++l; | |
*--(--s); | |
*--(--t); | |
while(l--) | |
if(*t-- != *s--) | |
return 0; | |
return 1; | |
} | |
int main(){ | |
char t[] = "0123456789abc"; | |
char s[] = "abc"; | |
printf("%d\n", strend(t, s)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
static char daytab[2][13] = { | |
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, | |
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} | |
}; | |
int day_of_year(int year, int month, int day) { | |
if(year <= 0 || month <= 0 || month > 12) return 0; | |
int i, leap; | |
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; | |
for(i = 1; i < month; i++) | |
day += *(*daytab + leap) + i; | |
return day; | |
} | |
void month_day(int year, int yearday, int *pmonth, int *pday) { | |
int i, leap; | |
leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0; | |
for(i = 1; yearday > daytab[leap][i]; i++) | |
yearday -= *(*daytab + leap) + i; | |
*pmonth = i; | |
*pday = yearday; | |
} | |
int main(){ | |
printf("%d\n", day_of_year(2013, 1, 25)); | |
int a, b; | |
month_day(2013, 50, &a, &b); | |
printf("month = %d day = %d\n", a, b); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment