更新 '1.c'

Signed-off-by: xtaodada <xtao@xtaolink.cn>
This commit is contained in:
xtaodada 2022-03-06 02:22:25 -05:00
parent 4bef6527e5
commit 84e2158bd6

43
1.c
View File

@ -4,13 +4,15 @@
#define MAX 100 #define MAX 100
#define MAX_1 100 #define MAX_1 100
typedef struct singer{ typedef struct singer{
int num; int num; // 序号
char name[10]; char name[10]; // 姓名
char xb[10]; char xb[10]; // 性别
int score[MAX_1]; float aver; // 平均分
int score[MAX_1]; // 原始成绩
}SING; }SING;
void inarray(SING sing[MAX], int *pnum){ void inarray(SING sing[MAX], int *pnum){
// 输入歌手信息:序号、姓名、性别
int n=0; int n=0;
printf("输入歌手人数:"); printf("输入歌手人数:");
scanf("%d", &n); scanf("%d", &n);
@ -22,25 +24,48 @@ void inarray(SING sing[MAX], int *pnum){
*pnum = n; *pnum = n;
} }
void outarrry(SING sing[MAX], int n){ void outarrry(SING sing[MAX], int n){
// 输出歌手信息:序号、姓名、性别
printf("序号\t姓名\t性别\n"); printf("序号\t姓名\t性别\n");
for(int i=0;i<n;i++){ for(int i=0;i<n;i++){
printf("%d\t%s\t%s\n",sing[i].num,sing[i].name,sing[i].xb); printf("%d\t%s\t%s\n",sing[i].num,sing[i].name,sing[i].xb);
} }
} }
void insert(SING sing[MAX], int *pnum){ void insert(SING sing[MAX], int *pnum){
// 新增歌手信息:序号、姓名、性别(增加到最后)
int n=*pnum; int n=*pnum;
printf("请输入第%d个歌手的信息\n",n); printf("请输入第%d个歌手的信息\n",n);
printf("序号\t姓名\t性别\n"); printf("序号\t姓名\t性别\n");
scanf("%d%s%s",&sing[n].num,sing[n].name,sing[n].xb); scanf("%d%s%s",&sing[n].num,sing[n].name,sing[n].xb);
*pnum=n+1; *pnum=n+1;
} }
int search_num(SING sing[MAX], int num, int x){ void save(SING sing[MAX], int num, int num_){
// 保存数据到文件
FILE *fp;
char filename[10];
printf("请输入文件名:");
scanf("%s", filename);
// 打开文件
if((fp=fopen(filename, "w"))==NULL){
printf("无法打开该文件,可能是文件名有误!\n");
return;
}
// 写入文件
fprintf(fp, "当前共有%d个歌手%d个评委\n",num,num_);
fprintf(fp, "序号\t姓名\t性别\t成绩\n");
for(int i=0;i<num;i++){
fprintf(fp,"%d\t%s\t%s",sing[i].num,sing[i].name,sing[i].xb);
for(int n=0;n<num_;n++){
fprintf(fp,"\t%d",sing[i].score[n]);
}
fprintf(fp,"\n");
}
} }
int main(){ int main(){
SING sing[MAX]; SING sing[MAX];
int num=0; int num=0,num_=0;
int choice; int choice;
while(1){ while(1){
@ -75,9 +100,11 @@ int main(){
insert(sing,&num); insert(sing,&num);
break; break;
case 9: case 9:
save(sing,num); save(sing,num,num_);
break;
case 10:
load(sing,&num,&num_);
break; break;
case
default: default:
printf("调用输入错误函数\n"); printf("调用输入错误函数\n");
break; break;