CPP第五版 课后题
星海
posted @ 2011年2月05日 21:55
in C代码
, 1698 阅读
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define MAXNUM 19 /* 球员数 */ #define MAXNAMECH 20 /* 名字最大字符树 */ #define MAXCHAR 80 /* 棒球队一行中最多字符数 */ struct nine { char first[MAXNAMECH]; char last[MAXNAMECH]; int gp; /* 出场次数 */ int ab; /* 击中数 */ int tob; /* 走垒数 */ int rbi; /* 跑点数 */ double sucrate; /* 成功率 */ }; /*----------------------------------------------------------------------------- * 思路:用一个临时字符串存放棒球队信息文件中一行的字符数据 * 调用sscanf将字符转换为struct结构中相应数据项 *-----------------------------------------------------------------------------*/ int main(void) { char line[MAXCHAR]; int num; int tmpgp, tmpab, tmptob, tmprbi; /* 临时存放结构数据 */ struct nine temp[MAXNUM]; memset(temp, 0, sizeof(struct nine) * MAXNUM); FILE *fp; /* input-file pointer */ char *fp_file_name = "nines.dat"; /* input-file name */ fp = fopen(fp_file_name, "r"); if (fp == NULL) { fprintf(stderr, "couldn't open file '%s'; %s\n", fp_file_name, strerror(errno)); exit(EXIT_FAILURE); } while (fgets(line, MAXCHAR - 1, fp) != NULL) { sscanf(line, "%d", &num); sscanf(line, "%*d %[^ ] %[^ ] %d %d %d %d", temp[num].first, temp[num].last, &tmpgp, &tmpab, &tmptob, &tmprbi); temp[num].gp += tmpgp; temp[num].ab += tmpab; temp[num].tob += tmptob; temp[num].rbi += tmprbi; } puts("号码 1stname lastname 上场次数 击中数 走垒数 跑点数 成功率\n"); for (num = 0; num < MAXNUM; num++) { if (temp[num].gp != 0) temp[num].sucrate = (float)temp[num].ab / temp[num].gp; printf ("%-4d %-8s %-8s %-8d %-6d %-6d %-6d %.2f\n", num, temp[num].first, temp[num].last, temp[num].gp, temp[num].ab, temp[num].tob, temp[num].rbi, temp[num].sucrate); } if (fclose(fp) == EOF) { /* close input file */ fprintf(stderr, "couldn't close file '%s'; %s\n", fp_file_name, strerror(errno)); exit(EXIT_FAILURE); } return 0; }
#include <stdio.h> #include <string.h> #include <ctype.h> #define ID_MASK 0xFF #define SIZE_MASK 0x7F00 #define LEFT 0x00000 #define CENTER 0x08000 #define RIGHT 0x10000 #define ALIGN_MASK 0x18000 #define REGULAR 0x00000 #define BOLD 0x20000 #define ITALIC 0x40000 #define UNDERLINE 0x80000 #define STYLE_MASK 0xE0000 #define SIZE_SHIFT 8 typedef unsigned long font; char do_menu(font * f); char get_choice(const char *); void show_menu(void); void show_font(font f); void eatline(void); void get_id(font * f); void get_size(font * f); void get_align(font * f); int main(void) { font sample = 1 | (12 << SIZE_SHIFT) | LEFT | ITALIC; while (do_menu(&sample) != 'q') continue; puts("Bye!"); return 0; } char do_menu(font * f) { char response; show_font(*f); show_menu(); response = get_choice("fsabiuq"); switch (response) { case 'f': get_id(f); break; case 's': get_size(f); break; case 'a': get_align(f); break; case 'b': *f ^= BOLD; break; case 'i': *f ^= ITALIC; break; case 'u': *f ^= UNDERLINE; break; case 'q': break; default: fprintf(stderr, "menu problem\n"); } return response; } char get_choice(const char *str) { char ch; ch = getchar(); ch = tolower(ch); eatline(); while (strchr(str, ch) == NULL) { printf("Please enter one of the following: %s\n", str); ch = tolower(getchar()); eatline(); } return ch; } void eatline(void) { while (getchar() != '\n') continue; } void show_menu(void) { puts("f)change font s)change size a)change alignment"); puts("b)toggle bold i)toggle italic u)toggle underline"); puts("q)quit"); } void show_font(font f) { printf("\n%4s %4s %9s %3s %3s %3s\n", "ID", "SIZE", "ALIGNMENT", "B", "I", "U"); printf("%4ld %4ld", f & ID_MASK, (f & SIZE_MASK) >> SIZE_SHIFT); switch (f & ALIGN_MASK) { case LEFT: printf("%7s", "left"); break; case RIGHT: printf("%7s", "right"); break; case CENTER: printf("%7s", "center"); break; default: printf("%7s", "unknown"); break; } printf("%8s %3s %3s\n\n", (f & BOLD) == BOLD ? "on" : "off", (f & ITALIC) == ITALIC ? "on" : "off", (f & UNDERLINE) == UNDERLINE ? "on" : "off"); } void get_id(font * f) { int id; printf("Enter font ID (0-255): "); scanf("%d", &id); id = id & ID_MASK; *f |= id; eatline(); } void get_size(font * f) { int size; printf("Enter font size (0-127): "); scanf("%d", &size); *f |= (size << SIZE_SHIFT) & SIZE_MASK; eatline(); } void get_align(font * f) { puts("Select alignment:"); puts("l)left c)center r)right"); switch (get_choice("lcr")) { case 'l': *f &= ~ALIGN_MASK; *f |= LEFT; break; case 'c': *f &= ~ALIGN_MASK; *f |= CENTER; break; case 'r': *f &= ~ALIGN_MASK; *f |= RIGHT; break; default: fprintf(stderr, "alignment problem\n"); } }