m0_55711330
Nuyoah.-
2021-06-23 11:05
采纳率: 100%
浏览 72

求完整版答案,不要有错误。急急

C语言课设大一,能在VS环境中运行,写一份算法流程图和功能模块图,每行代码后尽量多加些注释,通俗易懂,主要使用结构体 数组 文件读写 排序算法,不要使用太复杂的知识点

要求看图

 

  • 点赞
  • 收藏

3条回答 默认 最新

  • qfl_sdu
    qfl_sdu 2021-06-23 20:17
    已采纳

    完整代码及原始数据文件如下,如有帮助,请采纳一下,谢谢。

    原始数据文件(a.txt):

    学号  姓名     英语     C语言     高等数学  大学物理   体育
    1     张三      77        89        78        78        80
    2     李四      88        44        85        33        55
    3     杨潇      68        67        85        68        91
    4     韦一笑    78        67        85        68        95
    5     周芷若    88        100       85        90        93
    6     包不同    66        56        45        68        89
    7     段誉      76        67        85        68        70
    8     王语嫣    56        67        85        68        77
    9     杨过      82        56        85        68        99
    10    小龙女    81        67        85        68        92
    11    虚竹      87        100       85        90        88

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //定义学生的最大数量
    #define MAXNMB 50
    //定义课程数量
    #define NMBKC 5
    struct Student
    {
    	int number;      //学号
    	char name[20];   //姓名
    	int score[NMBKC];//保存课程成绩
    	float avg;       //平均分
    	int mc;          //名次
    };
    //显示信息
    void Display(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i;
    	char* xh ="学号";
    	char* xm ="姓名";
    	printf("%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		printf("%-10s",kecheng[i]);
    	printf("\n");
    	for (i = 0;i< nmb;i++)
    	{
    		printf("%-6d%-10s%-10d%-10d%-10d%-10d%-10d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4]);
    	}
    }
    //解析数据
    int ParserStr(char* buf,char out[NMBKC][20])
    {
    	int i = 0;
    	int j = 0,m = 0;;
    	char prech = 'a';
    	//去掉开头的空格
    	while(buf[i] == ' ')
    		i++;
    	while(buf[i] != '\0')
    	{
    		if (buf[i] == '\n' && prech != ' ')
    		{
    			out[j][m] = '\0';
    			//printf("%d  %s\n",j,out[j]);
    			prech = ' ';
    			m = 0;
    			j++;
    			i++;
    		}else if(buf[i] == ' ' && prech != ' ')
    		{
    			out[j][m] = '\0';
    			//printf("%d  %s\n",j,out[j]);
    			prech = ' ';
    			m = 0;
    			j++;
    			i++;
    		}else if (buf[i] == ' ' && prech == ' ')
    		{
    			i++;
    		}else
    		{
    			out[j][m] = buf[i];
    			prech = buf[i];
    			m++;
    			i++;
    		}
    	}
    	if (j== NMBKC+2)
    	{
    		return 1;
    	}else
    		return 0;
    }
    
    //读取文件
    void ReadFile(struct Student stu[],int *nmb,char kecheng[][20])
    {
    	int i = 0,j = 0;
    	char buf[100] = {0};
    	char tmp[NMBKC+2][20] = {0};
    	int t;
    	FILE* fp;
    	errno_t err;
    	if ((err = fopen_s(&fp,"a.txt","r")) != 0)
    	{
    		printf("文件读取失败\n");
    		return ;
    	}
    	//读取文件
    	//第一行数据
    	fgets(buf,100,fp);
    	//解析数据
    	ParserStr(buf,tmp); //解析出课程名称
    	for (j =0;j < NMBKC;j++)
    	{
    		strcpy_s(kecheng[j],20,tmp[j+2]);
    	}
    	while(!feof(fp))
    	{
    		memset(buf,0,100);
    		fgets(buf,100,fp); //读取一行
    		//避免最后一行直接结束
    		if(buf[strlen(buf)-1] != '\n')
    			buf[strlen(buf)] = ' ';
    		memset(tmp,0,sizeof(tmp));
    		if(ParserStr(buf,tmp))
    		{
    			stu[i].number = atoi(tmp[0]);
    			strcpy_s(stu[i].name,20,tmp[1]);
    			stu[i].avg = 0.0;
    			for(t = 0;t < NMBKC;t++)
    			{
    				stu[i].score[t] = atoi(tmp[2+t]);
    				stu[i].avg += stu[i].score[t];
    			}
    			stu[i].avg /= NMBKC;
    			i++;
    		}
    	}
    	fclose(fp);
    	*nmb = i;
    	//printf("总学生数:%d\n",i);
    }
    
    //按平均分排名
    void SortByavg(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i,j;
    	errno_t err;
    	struct Student tmp;
    	char* xh ="学号";
    	char* xm ="姓名";
    	char* pjf ="平均分";
    	char* pm="排名";
    	FILE* fp;
    	printf("%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		printf("%-10s",kecheng[i]);
    	printf("%-10s%-10s\n",pjf,pm);
    	for (i = 0;i< nmb-1;i++)
    	{
    		for (j = 0;j<nmb-1-i;j++)
    		{
    			if(stu[j].avg < stu[j+1].avg)
    			{
    				tmp = stu[j];
    				stu[j] = stu[j+1];
    				stu[j+1] = tmp;
    			}
    		}
    	}
    	//屏幕显示
    	j = 1;
    	for (i = 0;i<nmb;i++)
    	{
    		if (i == 0)
    		{
    			stu[i].mc = 1;
    			printf("%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,j);
    		}else
    		{
    			if(stu[i].avg != stu[i-1].avg)
    				j++;
    			stu[i].mc = j;
    			printf("%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,j);
    		}
    	}
    	//写文件
    	if ((err = fopen_s(&fp,"file2.txt","w")) != 0)
    	{
    		printf("文件打开失败\n");
    		return;
    	}
    	//屏幕显示
    	fprintf(fp,"%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		fprintf(fp,"%-10s",kecheng[i]);
    	fprintf(fp,"%-10s%-10s\n",pjf,pm);
    	j = 1;
    	for (i = 0;i<nmb;i++)
    	{
    		if (i == 0)
    		{
    			fprintf(fp,"%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,j);
    		}else
    		{
    			if(stu[i].avg != stu[i-1].avg)
    				j++;
    			fprintf(fp,"%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,j);
    		}
    	}
    	fclose(fp);
    }
    //按学号排名
    void SortByxh(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i,j;
    	struct Student tmp;
    	errno_t err;
    	char* xh ="学号";
    	char* xm ="姓名";
    	char* pjf ="平均分";
    	char* pm="排名";
    	FILE* fp;
    
    	//写文件
    	printf("%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		printf("%-10s",kecheng[i]);
    	printf("%-10s%-10s\n",pjf,pm);
    	for (i = 0;i< nmb-1;i++)
    	{
    		for (j = 0;j<nmb-1-i;j++)
    		{
    			if(stu[j].number > stu[j+1].number)
    			{
    				tmp = stu[j];
    				stu[j] = stu[j+1];
    				stu[j+1] = tmp;
    			}
    		}
    	}
    	//屏幕显示
    	for (i = 0;i<nmb;i++)
    	{
    		printf("%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,stu[i].mc);
    	}
    	//写文件
    	if ((err = fopen_s(&fp,"file1.txt","w")) != 0)
    	{
    		printf("文件打开失败\n");
    		return;
    	}
    	//写文件
    	fprintf(fp,"%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		fprintf(fp,"%-10s",kecheng[i]);
    	fprintf(fp,"%-10s%-10s\n",pjf,pm);
    	for (i = 0;i<nmb;i++)
    	{
    		fprintf(fp,"%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,stu[i].mc);
    	}
    	fclose(fp);
    }
    //统计每门课程的平均分
    void TongjiKcpjf(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i,j;
    	errno_t err;
    	FILE* fp;
    	float avg[NMBKC];  //保存每门课程的平均分
    	int a[5][NMBKC];   //保存每个分数段的数量
    	char* d0 = "分数段";
    	char* ds[] = {"<60","60-69","70-79","80-89",">=90"};
    	char* pjf = "平均分";
    	//计算课程平均分
    	for (i=0;i<NMBKC;i++)
    	{
    		avg[i] = 0.0;
    		for (j = 0;j< nmb;j++)
    			avg[i] += stu[j].score[i];
    		avg[i] /= nmb;
    	}
    	//初始化数量
    	for (i=0;i<5;i++)
    	{
    		for(j=0;j<NMBKC;j++)
    			a[i][j] = 0;
    	}
    	//统计各分数段数量
    	for (i=0;i<nmb;i++)
    	{
    		for(j=0;j<NMBKC;j++)
    		{
    			if(stu[i].score[j]< 60)
    				a[0][j] += 1;
    			else if(stu[i].score[j]>= 60 && stu[i].score[j]<= 69)
    				a[1][j] +=1;
    			else if(stu[i].score[j]>= 70 && stu[i].score[j]<= 79)
    				a[2][j] +=1;
    			else if(stu[i].score[j]>= 80 && stu[i].score[j]<= 89)
    				a[3][j] +=1;
    			else
    				a[4][j] +=1;
    		}
    	}
    	//打印
    	printf("%-6s  ",d0);
    	for(i=0;i<NMBKC;i++)
    		printf("%-10s",kecheng[i]);
    	printf("\n");
    	for (i = 0;i< 5;i++)
    	{
    		printf("%-6s  ",ds[i]);
    		for(j=0;j<NMBKC;j++)
    			printf("%-10d",a[i][j]);
    		printf("\n");
    	}
    
    	printf("%-6s  ",pjf);
    	for (i=0;i<NMBKC;i++)
    	{
    		printf("%.2f     ",avg[i]);
    	}
    	printf("\n");
    	//写文件
    	if ((err = fopen_s(&fp,"file3.txt","w")) != 0)
    	{
    		printf("文件打开失败\n");
    		return;
    	}
    	//写
    	fprintf(fp,"%-6s  ",d0);
    	for(i=0;i<NMBKC;i++)
    		fprintf(fp,"%-10s",kecheng[i]);
    	fprintf(fp,"\n");
    	for (i = 0;i< 5;i++)
    	{
    		fprintf(fp,"%-6s  ",ds[i]);
    		for(j=0;j<NMBKC;j++)
    			fprintf(fp,"%-10d",a[i][j]);
    		fprintf(fp,"\n");
    	}
    
    	fprintf(fp,"%-6s  ",pjf);
    	for (i=0;i<NMBKC;i++)
    	{
    		fprintf(fp,"%.2f     ",avg[i]);
    	}
    	fprintf(fp,"\n");
    	fclose(fp);
    
    }
    //打印成绩条
    void DisplayCjt(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i,j;
    	errno_t err;
    	struct Student tmp;
    	char* xh ="学号";
    	char* xm ="姓名";
    	char* pjf ="平均分";
    	char* pm="排名";
    	FILE* fp;
    
    	for (i = 0;i< nmb-1;i++)
    	{
    		for (j = 0;j<nmb-1-i;j++)
    		{
    			if(stu[j].number > stu[j+1].number)
    			{
    				tmp = stu[j];
    				stu[j] = stu[j+1];
    				stu[j+1] = tmp;
    			}
    		}
    	}
    	//屏幕显示
    	for (i = 0;i<nmb;i++)
    	{
    		printf("%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,stu[i].mc);
    	}
    	//写文件
    	if ((err = fopen_s(&fp,"file4.txt","w")) != 0)
    	{
    		printf("文件打开失败\n");
    		return;
    	}
    	//写文件
    	fprintf(fp,"%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		fprintf(fp,"%-10s",kecheng[i]);
    	fprintf(fp,"%-10s%-10s\n",pjf,pm);
    	for (i = 0;i<nmb;i++)
    	{
    		fprintf(fp,"%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,stu[i].mc);
    	}
    	fclose(fp);
    }
    
    //打印所有不及格信息
    void DisplayBjg(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i,j;
    	FILE* fp;
    	errno_t err;
    	int bw = 0;
    	char buf[100]={0};
    	char tmp[10];
    	if((err = fopen_s(&fp,"file5.txt","w")) != 0)
    	{
    		printf("文件打开失败\n");
    		return ;
    	}
    	for (i = 0;i< nmb;i++)
    	{
    		bw = 0;
    		memset(buf,0,100);
    		for (j = 0;j< NMBKC;j++)
    		{
    			if(stu[i].score[j]< 60)
    			{
    				memset(tmp,0,10);
    				strcat_s(buf,100,kecheng[j]);
    				strcat_s(buf,100,"(");
    				_itoa_s(stu[i].score[j],tmp,10,10);
    				strcat_s(buf,100,tmp);
    				strcat_s(buf,100,")  ");
    				bw = 1;
    			}
    		}
    		if(bw)
    		{
    			printf("%d  %s  %s\n",stu[i].number,stu[i].name,buf);
    			fprintf(fp,"%d  %s  %s\n",stu[i].number,stu[i].name,buf);
    		}
    	}
    	fclose(fp);
    }
    //判断是否是优等生
    int isYds(struct Student st)
    {
    	int i;
    	int tt = 0;
    	int flag1 = 0,flag2 = 0,flag3 = 1;
    	if (st.avg > 90)
    	{
    		flag1 = 1;
    	}
    	for (i=0;i<NMBKC;i++)
    	{
    		if(st.score[i] == 100 && st.avg > 85)
    			flag1 = 1;
    		if(st.score[i] > 95)
    			tt++;
    		if(st.score[i]< 60)
    			flag3 = 0;
    	}
    	if(tt>=2 && st.avg > 85)
    		flag1 = 1;
    	if(st.mc <=3 )
    		flag2 = 1;
    	if (flag1 && flag2 && flag3)
    	{
    		return 1;
    	}
    	return 0;
    }
    //打印优等生
    void DisplayYds(struct Student stu[],int nmb,char kecheng[][20])
    {
    	int i;
    	errno_t err;
    	FILE* fp;
    	char* xh ="学号";
    	char* xm ="姓名";
    	char* pjf ="平均分";
    	char* pm="排名";
    	if((err = fopen_s(&fp,"file6.txt","w")) != 0)
    	{
    		printf("文件打开失败\n");
    		return ;
    	}
    	//终端输出
    	printf("%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		printf("%-10s",kecheng[i]);
    	printf("%-10s%-10s\n",pjf,pm);
    
    	//写文件
    	fprintf(fp,"%-6s%-8s",xh,xm);
    	for(i=0;i<NMBKC;i++)
    		fprintf(fp,"%-10s",kecheng[i]);
    	fprintf(fp,"%-10s%-10s\n",pjf,pm);
    	for (i=0;i<nmb;i++)
    	{
    		if(isYds(stu[i]))
    		{
    			printf("%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,stu[i].mc);
    			fprintf(fp,"%-6d%-10s%-10d%-10d%-10d%-10d%-10d%.2f%5d\n",stu[i].number,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],stu[i].avg,stu[i].mc);
    		}
    	}
    	fclose(fp);
    }
    int main()
    {
    	struct Student stu[MAXNMB];
    	int nmb;  //实际学生人数,从文件中读取
    	char kc[NMBKC][20] = {0};  //保存课程名称
    	//1.从文件中读取数据
    	printf("-------------------1.读入原始数据并显示---------------------\n");
    	ReadFile(stu,&nmb,kc);
    	Display(stu,nmb,kc);
    	//2.排序
    	printf("-------------------2.1根据平均分排序并显示------------------\n");
    	SortByavg(stu,nmb,kc); //现根据平均分排序
    	printf("-------------------2.2根据学号排序并显示--------------------\n");
    	SortByxh(stu,nmb,kc);  //再根据学号排序,因为首选需要根据平均分排序才能得到名次
    	//3.统计课程平均分
    	printf("-------------------3.统计课程平均分并显示--------------------\n");
    	TongjiKcpjf(stu,nmb,kc);
    	//4.打印成绩条
    	printf("-------------------4.打印成绩条------------------------------\n");
    	DisplayCjt(stu,nmb,kc);
    	//5.打印不及格成绩
    	printf("-------------------5.打印不及格成绩---------------------------\n");
    	DisplayBjg(stu,nmb,kc);
    	//6.打印优等生
    	printf("-------------------6.打印优等生-------------------------------\n");
    	DisplayYds(stu,nmb,kc);
    	getchar();
    	getchar();
    	return 0;
    }
    
    点赞 评论
  • SoftwareTeacher
    SoftwareTeacher 2021-06-23 11:14

    请提供你的初始思路吧, 这样大家好帮助你。 

    点赞 评论
  • caixukun1father
    caixukun1father 2021-06-23 14:52

    你是要完整代码还是读写文件有问题

    点赞 评论

相关推荐