A-C/1.c
xtaodada 84e2158bd6 更新 '1.c'
Signed-off-by: xtaodada <xtao@xtaolink.cn>
2022-03-06 02:22:25 -05:00

120 lines
2.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define MAX 100
#define MAX_1 100
typedef struct singer{
int num; // 序号
char name[10]; // 姓名
char xb[10]; // 性别
float aver; // 平均分
int score[MAX_1]; // 原始成绩
}SING;
void inarray(SING sing[MAX], int *pnum){
// 输入歌手信息:序号、姓名、性别
int n=0;
printf("输入歌手人数:");
scanf("%d", &n);
printf("请输入%d个歌手的信息\n",n);
printf("序号\t姓名\t性别\n");
for(int i=0;i<n;i++){
scanf("%d%s%s",&sing[i].num,sing[i].name,sing[i].xb);
}
*pnum = n;
}
void outarrry(SING sing[MAX], int n){
// 输出歌手信息:序号、姓名、性别
printf("序号\t姓名\t性别\n");
for(int i=0;i<n;i++){
printf("%d\t%s\t%s\n",sing[i].num,sing[i].name,sing[i].xb);
}
}
void insert(SING sing[MAX], int *pnum){
// 新增歌手信息:序号、姓名、性别(增加到最后)
int n=*pnum;
printf("请输入第%d个歌手的信息\n",n);
printf("序号\t姓名\t性别\n");
scanf("%d%s%s",&sing[n].num,sing[n].name,sing[n].xb);
*pnum=n+1;
}
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(){
SING sing[MAX];
int num=0,num_=0;
int choice;
while(1){
// 1.菜单
printf("\t1.输入歌手信息\n");
printf("\t2.输出歌手信息\n");
printf("\t3.新增一个歌手\n");
printf("\t4.查找歌手信息\n");
printf("\t5.修改歌手信息\n");
printf("\t6.删除歌手信息\n");
printf("\t7.评委评分\n");
printf("\t8.输出歌手成绩\n");
printf("\t9.保存歌手信息\n");
printf("\t10.加载歌手信息\n");
printf("\t0.退出\n");
// 2.输入
scanf("%d", &choice);
// 3.退出
if(choice==0) break;
// 4.调用子函数功能
switch (choice) {
case 1:
inarray(sing,&num);
break;
case 2:
outarrry(sing,num);
break;
case 3:
insert(sing,&num);
break;
case 9:
save(sing,num,num_);
break;
case 10:
load(sing,&num,&num_);
break;
default:
printf("调用输入错误函数\n");
break;
}
// 5.清屏
printf("按任意键继续...\n");
getch();
system("cls");
}
printf("感谢使用,再见!\n");
return 0;
}