#include <stdio.h> /*C语言 结构体的应用*/ typedef struct { char bookname[40]; //书名 char author[20]; //作者 float price; //价格 } BOOK; int main(int argc, char* argv[]) { BOOK b1, b2, b; scanf("%s %s %f", b1.bookname, b1.author, &b1.price); scanf("%s %s %f", b2.bookname, b2.author, &b2.price); if(b1.price>b2.price) b = b1; else b = b2; printf("价格较高的书的书名:%s\n", b.bookname); return 0; } #include <stdio.h> #include <string.h> /*C语言 结构体的应用*/ /*C语言 查询结构体中的信息*/ typedef struct { char name[40]; //动物名 int num; //动物数量 } ANIMAL; int main(int argc, char* argv[]) { ANIMAL cc[10] = {{"大象", 3}, {"猴子", 52}, {"斑马", 6}, {"羚羊",11},{"老虎",4},{"狮子",7},{"黑熊",2}, {"猩猩",6},{"狼",17},{"豹",4}}; int zs, i; int flag; char temp_name[40]; int n; zs = 0; for( i=0; i<sizeof(cc)/sizeof(cc[0]); i++ ) //累加动物数量 zs = zs+cc[i].num; printf("动物园动物的总数量:%d\n", zs); for( i=0; i<sizeof(cc)/sizeof(cc[0]); i++ ) //输出动物数量小于5的信息 if( cc[i].num < 5 ) printf("%s %d\n", cc[i].name, cc[i].num); printf("输入动物名称:"); scanf("%s", temp_name); flag = -1; for( i=0; i<sizeof(cc)/sizeof(cc[0]); i++ ) if( strcmp(cc[i].name, temp_name)==0 ){ flag=i; break; } //查找到相应动物,记下数组编号 if( flag!=-1 ) printf("该动物的数量:%d\n",cc[flag].num); else printf("没有找到此动物\n"); return 0; } #include <stdio.h> #include <string.h> typedef struct { char name[10]; int num; } ANIMAL ; main() { ANIMAL cc[10]={{"大象",3},{"猴子",52},{"斑马",6},{"羚羊",11},{"老虎",4}, {"狮子",7},{"黑熊",2},{"猩猩",6},{"狼",17},{"豹",4}}; int i,im; im=0; for(i=1;i<10;i++) if(cc[i].num>cc[im].num)im=i; printf("数量最多的动物的名称与数量:%s %d\n",cc[im].name, cc[im].num); } #include <stdio.h> /*C语言 求员工平均工资*/ typedef struct { char xm[10]; int jbgz; int jj; int zc; int sfgz; } ZHIGONG; int main(int argc, char* argv[]) { ZHIGONG zg[4]={{"zhao",650,210,170,0},{"qian",570,260,180,0}, {"sun",708,190,150,0},{"li",490,240,160,0} }; int i; ZHIGONG *p; int aver; for( i=0; i<sizeof(zg)/sizeof(zg[0]); i++ ) zg[i].sfgz = zg[i].jbgz+zg[i].jj-zg[i].zc; //计算实发工资 aver = 0; for( p=zg; p<zg+sizeof(zg)/sizeof(zg[0]); p++ ) aver = aver+p->sfgz; //计算平均工资 aver = aver/(sizeof(zg)/sizeof(zg[0])); printf("平均工资是:%d\n", aver); return 0; } #include <stdio.h> /*C语言 结构体与指针操作*/ typedef struct { char name[40]; int num; } ANIMAL; int main(int argc, char* argv[]) { ANIMAL cc[10]={{"大象",3},{"猴子",52},{"斑马",6},{"羚羊",11},{"老虎",4}, {"狮子",7},{"黑熊",2},{"猩猩",6},{"狼",17},{"豹",4}}; ANIMAL *p; for( p=cc; p<cc+sizeof(cc)/sizeof(cc[0]); p++ ) if( p->num>=10 && p->num<=20 ) printf("%s %d\n", p->name, p->num); printf("C语言 结构体与指针操作\n"); return 0; }