//复合文字 #include<stdio.h> #define MAXTITL 41 #define MAXAUTL 31 struct book { char title[MAXTITL]; char author[MAXAUTL]; float value; }; int main(void) { struct book readFirst; int score; printf("Enter test score:"); scanf("%d",&score); if(score >= 84) { readFirst = (struct book) {"Crime and Punishment", "Fyodor Dostoyevsky", 9.99 }; } else { readFirst = (struct book) {"Mr.Bouncy's NIce Hat", "Fred Winsome", 5.99 }; } printf("Your assigned reading:/n"); printf("%s by %s $%.2f/n",readFirst.title, readFirst.author,readFirst.value); return 0; } //伸缩数组成员 #include<stdio.h> #include<stdlib.h> struct flex { int count; double average; double scores[]; }; void showFlex(const struct flex * p); int main(void) { struct flex *pf1, *pf2; int n = 5; int i; int tot = 0; /*为结构和数组分配空间*/ pf1 = malloc(sizeof (struct flex) + n * sizeof(double)); pf1->count = n; for(i = 0; i < n; i++) { pf1->scores[i] = 20.0-i; tot += pf1->scores[i]; } pf1->average = tot / n; showFlex(pf1); n = 9; tot = 0; pf2 = malloc(sizeof(struct flex) + n * sizeof(double)); pf2->count = n; for(i = 0; i < n; i++) { pf2->scores[i] = 20.0 - i/2.0; tot += pf2->scores[i]; } pf2->average = tot / n; showFlex(pf2); free(pf1); free(pf2); getch(); return 0; } void showFlex(const struct flex *p) { int i; printf("Scores:"); for(i = 0; i < p->count; i++) { printf("%g",p->scores[i]); } printf("/n Average :%g/n",p->average); } //把结构保存到文件中 #include<stdio.h> #include<stdlib.h> #define MAXTITL 40 #define MAXAUTL 40 #define MAXBKS 10 struct book { char title[MAXTITL]; char author[MAXAUTL]; float value; }; int main(void) { struct book library[MAXBKS]; int count = 0; int index,filecount; FILE * pbooks; int size = sizeof(struct book); if((pbooks = fopen("book.dat","a+b"))==NULL) { fputs("Can't open book.dat file./n",stderr); exit(1); } rewind(pbooks); while(count < MAXBKS && fread(&library[count],size, 1,pbooks) == 1) { if(count = 0) { puts("Current contentd of bool.dat"); } printf("%s by %s $%.2f/n",library[count].title, library[count].author,library[count].value); count++; } filecount = count; if(count == MAXBKS) { fputs("The book.dat file is full./n",stderr); exit(2); } puts("Please add new book title./n"); puts("Please[enter] at the start of a line to stop./n"); while(count < MAXBKS && gets(library[count].title) != NULL && library[count].title[0] != '/0') { puts("Now enter the author."); gets(library[count].author); puts("Now enter the value."); scanf("%f",&library[count++].value); while(getchar() != '/n') { continue; } if(count < MAXBKS) { puts("Enter the next title."); } } if(count > 0) { puts("Here is the list of your books:"); for(index = 0; index < count; index++) { printf("%s by %s: $%.2f/n",library[index].title, library[index].author,library[index].value); } fwrite(&library[filecount],size,count - filecount,pbooks); } else { puts("No books?Too bad./n"); } puts("Bye./n"); fclose(pbooks); getch(); return 0; } //enum #include<stdio.h> #include<string.h> #include<stdbool.h> enum spectrum {red,orange,yellow,green,blue,violet}; const char *colors[] = {"red","orange","yellow","green", "blue","violet"}; #define LEN 30 int main(void) { char choice[LEN]; enum spectrum color; bool colorIsfound = false; puts("Enter a color (empty line to quit):"); while(gets(choice) != NULL && choice[0] != '/0') { for(color = red; color <= violet; color++) { if(strcmp(choice,colors[color]) == 0) { colorIsfound = true; break; } } if(colorIsfound) { switch(color) { case red :puts("Roses are red."); break; case orange:puts("Poppies are orange."); break; case yellow:puts("Sunflowers are yellow."); break; case green :puts("Grass are green."); break; case blue :puts("Bluebells are blue,"); break; case violet:puts("Violets are violet,"); break; } } else { printf("I don't know about the color %s./n",choice); } colorIsfound = false; puts("Next color.please(empty line to quit):"); } puts("Goodbye!"); getch(); return 0; } //函数指针 #include<stdio.h> #include<string.h> #include<ctype.h> char showmenu(void); void eatline(void);//读至行末 void show(void (*fp)(char *),char *str); void ToUpper(char *);//转换为大写 void ToLower(char *);//转换为小写 void Transpose(char *);//大小写转置 void Dummy(char *);//不改变字符串 int main(void) { char line[81]; char copy[81]; char choice; void (*pfun)(char *);/*指向一个函数,该函数接受一个字符指针,没返回值*/ puts("Enter a string(empty line to quit):"); while(gets(line) != NULL &&line[0] != '/0') { while((choice = showmenu()) != 'n') { switch(choice)//switch语句用来设置指针 { case 'u':pfun = ToUpper; break; case 'l':pfun = ToLower; break; case 't':pfun = Transpose; break; case 'o':pfun = Dummy; break; } strcpy(copy,line);//为show()制作一个备份 show(pfun,copy); //使用用户选择的函数 } puts("Enter a string(empty line to quit):"); } puts("Bye"); getch(); return 0; } char showmenu(void) { char ans; puts("Enter menu choice."); puts("u)uppercase l)lowercase"); puts("t)transposed case o)original case"); puts("n)next string"); ans = getchar();//获取用户的响应 ans = tolower(ans);//转换为小写 eatline();// 删除行中剩余部分 while(strchr("ulton",ans) == NULL) { puts("PLease enter a u,l t, o, or n:"); ans = tolower(getchar()); eatline(); } return ans; } void eatline(void) { while(getchar() != '/n') continue; } void ToUpper(char *str) { while(*str) { *str = toupper(*str); str++; } } void ToLower(char *str) { while(*str) { *str = tolower(*str); str++; } } void Transpose(char *str) { while(*str) { if(islower(*str)) *str = toupper(*str); else if(isupper(*str)) *str = tolower(*str); str++; } } void Dummy(char *str) { //不改变字符串 } void show(void(*fp)(char *),char *str) { (*fp)(str);//把用户选择的函数作用于str puts(str);//显示结果 }