#include <stdio.h> #include <string.h> struct data{ int year; int month; int day; }; struct student{ char name[20]; char sex[10]; struct data bir; } stu[30]; main(){ int i = 0,j; while(i>=0){ char str[10]; for(j = 0;;j++){ scanf("%c",&str[j]); if(str[j] == ' '||str[j] == '\n'){ str[j] = '\0'; break; } } if(strcmp(str,"quit") == 0) break; if(strcmp(str,"add") == 0){ scanf("%s",&stu[i].name); scanf("%s",&stu[i].sex); scanf("%d",&stu[i].bir.year); scanf("%d",&stu[i].bir.month); scanf("%d",&stu[i].bir.day); i++; } if(strcmp(str,"name") == 0){ char name[20]; gets(name); for(j = 0;j<i-1;j++){ if(strcmp(stu[j].name,name) == 0) printf("%s %s %d %d %d\n",stu[j].name,stu[j].sex,stu[j].bir.year,stu[j].bir.month,stu[j].bir.day); } } if(strcmp(str,"sex") == 0){ char sex[10]; gets(sex); for(j = 0;j<i-1;j++){ if(strcmp(stu[j].sex,sex) == 0) printf("%s %s %d %d %d\n",stu[j].name,stu[j].sex,stu[j].bir.year,stu[j].bir.month,stu[j].bir.day); } } } return 0; } |
Double click to view unformatted code.