Newtopython
诗岑
2021-05-27 17:24
采纳率: 82.8%
浏览 21

【C语言】结构体数据存储问题

做了一个学生成绩的结构体数组,运行的时候发现成绩始终是0.0,好像输入的score数据没有传进去,其他的都正常。源码贴在下面,请大佬纠错!

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
//定义结构体数组
typedef struct {
	char num[20];//学号
	char name[20];//学生姓名
	int score;//分数
}STUDENT;
STUDENT stu[20]; 
//home page
void home_page()
{
	printf("\n\t\t******************************** \n");
	printf("\t\t     学生成绩信息管理系统       \n\n");
	printf("\t\t        1、录入信息            \n\n");
	printf("\t\t        2、浏览信息            \n\n");
	printf("\t\t        3、查询信息            \n\n");
	printf("\t\t        4、修改信息            \n\n");
	printf("\t\t        5、成绩排序            \n\n");
	printf("\t\t        6、删除信息            \n\n");
	printf("\t\t        7、向文件写入数据       \n\n");
	printf("\t\t        8、从文件读出数据       \n\n");
	printf("\t\t        9、退出系统            \n\n");
	printf("\t\t******************************** \n\n");
}
// add information
int add_infor(STUDENT stu[], int n)
{
	int i = 0;
	char sign = 'Y';

	while ((sign == 'Y') || (sign == 'y'))
	{
		printf("\n\t\t\t学号:");
		scanf("\t\t%s", stu[n + i].num);
		printf("\t\t\t姓名:");
		scanf("\t\t%s", stu[n + i].name);
		printf("\t\t\t成绩:");
		scanf("\t\t%d", &stu[n + i].score);

		printf("\n\t\t\t是否继续录入?(Y/N):");
		//清空缓存
		scanf("%*[^\n]") ;
		scanf("%*c");
		sign=getchar();

		i++;
	}

	return (n + i);
}
//browse information
void browse_infor(STUDENT stu[], int n)
{
	int i;

	printf("\n\n");
	printf("\t\t学号\t姓名\t成绩\n\n");

	for (i = 0; i < n; i++)
	{
		printf("\t\t%s\t%s\t %d\n",stu[i].num,stu[i].name,stu[i].score);
	}
}
//find information
void find_infor(STUDENT stu[], int n)
{
	int option;
	int i = 0;
	char search_num[10] = { 0 };
	char search_name[20] = { 0 };

	printf("\n\t\t\t1、按学号查找\n");
	printf("\t\t\t2、按姓名查找\n");
	printf("\n\t\t\t请选择相应的选项:");
	scanf("%d",&option);

	system("cls");

	if (1 == option)
	{
		printf("\n\t\t请输入要查找的学号:");
		scanf("%s", search_num);

		while ((strcmp(stu[i].num, search_num) != 0) && i < n)
		{
			i++;
		}
	}

	if (2 == option)
	{
		printf("\n\t\t请输入要查找的姓名:");
		scanf("%s", search_name);

		while ((strcmp(stu[i].name, search_name) != 0) && i < n)
		{
			i++;
		}
	}

	if (i == n)
	{
		printf("\n\t\t未找到!");
		return;
	}

	printf("\n\t\t学号:%s\n", stu[i].num);
	printf("\t\t姓名:%s\n", stu[i].name);
	printf("\t\t成绩:%d\n", stu[i].score);
}
//modify information
int modify_infor(STUDENT stu[], int n)
{
	int option, choice;
	int i = 0;
	char old_temp[20] = { 0 };
	char new_temp[20] = { 0 };
	float new_score;

	printf("\n\t\t\t1、按学号查找修改\n");
	printf("\t\t\t2、按姓名查找修改\n");
	printf("\n\t\t请输入选项选择功能:");
	scanf("%d", &option);

	system("cls");

	if (1 == option)
	{
		printf("\n\t\t请输入学号:");
		scanf("%s", &old_temp);

		while ((strcmp(stu[i].num, old_temp) != 0) && i < n)
		{
			i++;
		}

		if (i == n)
		{
			printf("\n\t\t未找到!");
		}
	}

	if (2 == option)
	{
		printf("\n\t\t请输入姓名:");
		scanf("%s", &old_temp);

		while ((strcmp(stu[i].name, old_temp) != 0) && i < n)
		{
			i++;
		}

		if (i == n)
		{
			printf("\n\t\t未找到!");
		}
	}

	printf("\n\t\t待修改学生的信息:\n");
	printf("\n\t\t学号:%s\n\t\t姓名:%s\n\t\t成绩:%d\n", stu[i].num, stu[i].name, stu[i].score);

	printf("\n\t\t请选择要修改的信息(1.学号 2.姓名 3.成绩):");
	scanf("%d", &choice);

	if (1 == choice)
	{
		printf("\n\t\t输入修改后的学号:");
		scanf("%s", &new_temp);

		strcpy(stu[i].num, new_temp);
		printf("\t\t修改成功!\n");
	}

	if (2 == choice)
	{
		printf("\n\t\t输入修改后的姓名:");
		scanf("%s", &new_temp);

		strcpy(stu[i].name, new_temp);
		printf("\t\t修改成功!\n");
	}

	if (3 == choice)
	{
		printf("\n\t\t输入修改后的成绩:");
		scanf("%f", &new_score);

		stu[i].score = new_score;
		printf("\t\t修改成功!\n");
	}
}
//sort the score
void sort_score(STUDENT stu[], int n)
{
	int i, j;
	int * p, * q, s;
	char temp[20];

	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - 1 - i; j++)
		{
			if (stu[j].score > stu[j + 1].score)
			{
				strcpy(temp, stu[j].num);
				strcpy(stu[j].num, stu[j + 1].num);
				strcpy(stu[j + 1].num, temp);

				strcpy(temp, stu[j].name);
				strcpy(stu[j].name, stu[j+1].name);
				strcpy(stu[j+1].name,temp);

				p = &stu[j].score;
				q = &stu[j + 1].score;
				s = *p;
				*p = *q;
				*q = s;
			}
		}
	}
}
//delete information
int delete_infor(STUDENT stu[], int n)
{
	char search_num[10] = { 0 };
	char search_name[20] = { 0 };
	int i = 0;
	int j;
	int option;
	
	printf("\n\t\t\t1、按学号查找删除\n");
	printf("\t\t\t2、按姓名查找删除\n");
	printf("\n\t\t\t请输入选项选择功能:");
	scanf("%d", &option);
	
	system("cls");

	if (1 == option)
	{
		printf("\n\t\t请输入要删除的学生的学号:");
		scanf("%s", &search_num);

		while ((strcmp(stu[i].num, search_num)) != 0 && (i < n))
		{
			i++;
		}

		if (i == n)
		{
			printf("\t\t未找到!\n");
			return n;
		}
	}
	
	if (2 == option)
	{
		printf("\n\t\t请输入要删除的学生的姓名:");
		scanf("%s", &search_name);

		while ((strcmp(stu[i].num, search_name) != 0) && (i < n))
		{
			i++;
		}

		if (i == n)
		{
			printf("\t\t未找到!\n");
			return n;
		}
	}

	for (j = i; j < n; j++)
	{
		strcpy(stu[j].num, stu[j + 1].num);
		strcpy(stu[j].name, stu[j + 1].name);
		stu[j].score = stu[j + 1].score;
	}

	printf("\t\t删除成功!\n");
	return (n - 1);
}
//write to file
void writetofile(STUDENT stu[], int n)
{
	FILE* fp;
	int i = 0;
	char filename[20] = { 0 };
	
	printf("\n\t\t\t请输入文件名称:");
	scanf("%s", filename);

	if ((fp = fopen(filename, "w")) == NULL)
	{
		printf("\n\t\t\t打开文件失败!\n");
		return;
	}

	fprintf(fp, "%d\n", n);

	while (i < n)
	{
		fprintf(fp, "%-6s%-10s%d\n", stu[i].num, stu[i].name, stu[i].score);
		i++;
	}

	fclose(fp);

	printf("\n\t\t\t写入文件成功!\n");
}
//read from file
int readfromfile(STUDENT stu[], int n)
{
	FILE* fp;
	int i, num;
	char filename[20];

	i = 0;

	printf("\n\t\t\t请输入文件名称:");
	scanf("%s", filename);

	if ((fp = fopen(filename, "r")) == NULL)
	{
		printf("\n\t\t\t无法打开文件!\n");

		return n;
	}

	fscanf(fp, "%d", &num);

	while (i < num)
	{
		fscanf(fp, "%-5s%-10s%d\n", stu[n + i].num, stu[n + i].name, stu[n + i].score);
		i++;
	}

	n += num;
	fclose(fp);

	printf("\n\t\t\t读出数据成功!\n");

	return n;
}
int main()
{
	int select = 0;
	int n = 0;

	while (1)
	{
		system("cls");
		
		home_page();

		printf("\t\t输入标号选择功能: ");
		scanf("%d", &select);

		switch (select)
		{
			case 1:
				system("cls");
				printf("\n\t\t********录入学生成绩信息********\n");
				n = add_infor(stu, n);
				break;
			case 2:
				system("cls");
				printf("\n\t\t********浏览学生成绩信息********\n");
				browse_infor(stu, n);
				getchar();
				break;			
			case 3:
				system("cls");
				printf("\n\t\t**********查询学生信息**********\n");
				find_infor(stu, n);
				getchar();
				break;
			case 4:
				system("cls");
				printf("\n\t\t*********修改学生信息*********\n");
				n = modify_infor(stu, n);
				getchar();
				break;
			case 5:
				system("cls");
				printf("\n\t\t*********按成绩排序*********\n");
				sort_score(stu, n);
				printf("\n\t\t\t排序成功!");
				getchar();
				break;	
			case 6:
				system("cls");
				printf("\n\t\t*********删除学生信息*********\n");
				n = delete_infor(stu, n);
				getchar();
				break;
			case 7:
				system("cls");
				printf("\n\t\t********向文件写入数据********\n");
				writetofile(stu, n);
				getchar();
				break;
			case 8:
				system("cls");
				printf("\n\t\t********从文件读出数据********\n");
				n = readfromfile(stu, n);
				getchar();
				break;
			default:
				system("cls");
				printf("\n\n\t\t**********谢谢使用!**********\n");
				exit(0);
				break;
		}

		getchar();
	}
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qq_28117531
    Jine_GG 2021-05-27 19:27
    已采纳

    你这个只是 modify_infor函数缺少返回值,导致编译不过,你重新全部编译就可以得出这个问题。改下就好了,应该是之前编译的时候有数据残留导致你编译能够通过。

    点赞 评论
  • qfl_sdu
    qfl_sdu 2021-05-27 19:12

    你的问题具体是什么呢,我这运行输入成绩后,执行2能显示成绩啊。

    另外,你的代码中,modify_infor()和main()函数你声明的类型是int,需要有返回值,需要在最后添加:return 0;

    点赞 评论

相关推荐