C Language--Course Management Information System

Project requirements

Course management system
Function: Assume that there are n courses, and each course has course number, course name, total hours, credits, semester
and other information. Try to design a course management system so that it can provide the following functions:
1. Enter, modify and delete course information;
2. Browse by course name after sorting;
3. Search by course and credit;

Function call description

insert image description here

1. Set the structure

code show as below:

struct info
{
    
    
int number;//课程编号
char name[100];//课程名称
int hours;//总学时
int credit;//学分
int semester;// 开课学期
bool flag;//数据有效标记,
};

This structure is used to save data. The integer variable number stores the course number; the 100-byte character array name stores the course name; the integer variable hours stores the total hours; the integer variable credit stores the credits; The type variable semester stores the semester; the Boolean integer variable flag stores the valid flag of the data.

There are also the following structures that implement functions:

struct info temp;//临时中转
struct info a[20];//数据存储
struct info newdata; //modify 中,修改的数据存储
struct info original;//del 中,删除数据的判据

2. Main function:

code show as below:

int main(void)//true
{
    
    
	long long t = 0;
	FILE* fp;
	if ((fp = fopen("info.dat", "rb+")) != NULL)
	{
    
    
		fpos_t pos;//定义pos
		fgetpos(fp, &pos);//获取文件指针,写入pos
		fseek(fp, 0, SEEK_END);//文件指针指向末尾
		t = ftell(fp);//获取文件指针到文件头部的字节大小
		fsetpos(fp, &pos);//文件指针还原
		fclose(fp);//释放文件
	}
	n = t / size;
	//以上实现了有效数据条数n的获取
	printf("有效数据:%d条\n", n);
	char c;//选项代码
	while (1)
	{
    
    
		printf("\n\n\n\n");
		printf(" student course management system \n");
		printf("-----------------------------------------------------------------");
		printf("\n\n");
		printf(" 1 录入信息 2 浏览所有信息(排序后) 3 查询信息(名称) \n");
		printf(" 4 修改信息 5 删除信息 6 查询信息(学分) \n");
		printf(" 0 exit system \n");
		printf(" \n\n");
		printf("------------------------------------------------------------------");
		printf("\n please select:");
		c = getchar();
		switch (c)
		{
    
    
			case '0'://exit system
			{
    
    
				return 0;
			}
			case '1':
			{
    
    
				input();//1 录入信息
				getchar();
				break;
			}
			case '2':
			{
    
    
				look();//2 浏览所有信息(排序后)
				getchar();
				break;
			}
			case '3':
			{
    
    
				QueryName();//3 查询信息(名称)
				getchar();
				break;
			}
			case '4':
			{
    
    
				modify();//4 修改信息
				getchar();
				break;
			}
			case '5':
			{
    
    
				getchar();
				del();//5 删除信息
				getchar();//防止功能失效
				break;
			}
			case '6':
			{
    
    
				QueryCredit();//6 查询信息(学分)
				getchar();
				break;
			}
			default:
			{
    
    
				getchar();
				printf("\ninput error,please try again:\n");
				break;
			}
		} 
	}
	return 0;
}
  • The prototype declaration of the function: int main(void)
  • Function function and parameter description This function is the main function of the whole program, which is used to display the main menu and interact with the user, and call different functions to realize specific functions
    according to the user's choice .
    This function has no return value and no parameters are passed.
  • Description of key variables used in the function
  1. Int size: Used to indicate the number of bytes occupied by a piece of data, that is, the space occupied by the struct info type.
  2. long long t: used to indicate the size of the file, and determine the value of valid data n together with int size.
  3. char c: Used to store the user's option number.
  4. int n: the number of bars used to store valid data

3. Enter information

code show as below:

void input(void)//1 录入信息 true
{
    
    
	char ch;
	while (1)
	{
    
    
		printf("课程编号");
		scanf("%d", &temp.number);
		printf("课程名称");
		scanf("%s", &temp.name);
		printf("总学时");
		scanf("%d", &temp.hours);
		printf("学分");
		scanf("%d", &temp.credit);
		printf("开课学期");
		scanf("%d", &temp.semester);
		temp.flag = 1;
		n++;//有效数据增加1条
		copy3(n - 1);//由temp中写入数组中
		getchar();//接受换行符
		printf("是否继续录入?[y/n]\n");
		ch = getchar();
		if (ch == 'n' || ch == 'N')
		break;
	}
		writeToFile();//统一写入文件info.dat
}

4. Browsing information

void look(void)//2 浏览所有信息(排序后) true 
{
    
    
	load();
	//sort
	for (int i = 0; i < n - 1; i++) {
    
    
	int k = i;// 最小元素的下标
	for (int j = i + 1; j < n; j++)
	if (strcmp(a[k].name, a[j].name) > 0)//从小到大排序
	k = j;//如果a[k]>a[j],即有元素比我们认为的最小元素更小,那就更新最小元素的下标
	if (k != i)
	{
    
    //等效于三步交换法
		copy1(i);
		copy2(i, k);
		copy3(k);
	} 
}
display_header();//表头
display_all();//内容
display_end();//表尾
}

5. Query information

  1. query name
void QueryName(void)//3 查询信息(名称)true
{
    
    
	char ch;
	char CName[100];//用于存储课程的名称
	while (1)
	{
    
    
		printf("请输入课程名称:");
		scanf("%s", CName);
		getchar();
		load();
		display_header();
		for (int i = 0; i < n; i++)
		{
    
    
		// printf("%d\n",i);
		if (strcmp(a[i].name, CName) == 0)
		{
    
    
			printf("ture.\n");
			copy1(i);
			display();
		} 
	}
	display_end();
	printf("是否继续查询?[y/n]\n");
	scanf("%c", &ch);
	if (ch == 'n' || ch == 'N')
		break;
	} 
}
  1. Query credits
void QueryCredit(void)//6 查询信息(学分) true
{
    
    
	char ch;
	int mark;//用于存储课程的学分
	while (1)
	{
    
    
		printf("请输入课程学分:");
		scanf("%d", &mark);
		getchar();
		load();
		display_header();
		for (int i = 0; i < n; i++)
		{
    
    
		if (mark == a[i].credit)
		{
    
    
			copy1(i);
			display();
		} 
	}
	display_end();
	printf("是否继续查询?[y/n]\n");
	ch = getchar();
	if (ch == 'n' || ch == 'N')
		break;
	} 
}

6. Modify information

void modify(void)//4 修改信息
{
    
    
	struct info newdata;//modify中修改的数据存储
	int num;
	load();//读原文件info.dat
	look();
	printf("用课程编号来确定修改的数据行:");
	scanf("%d", &num);
	getchar();
	printf("\n\n");
	printf(" student course info \n");
	printf("------------------------------------------------------------------\n");
	printf("你想修改什么?\n");
	printf("1.课程名称 2.总学时 3.学分\n");
	printf("4.开课学期\n");
	printf("------------------------------------------------------------------");
	printf("\n please select:");
	char c = getchar();
	while (c > '4' || c < '1')
	{
    
    
		printf("\n输入错误,重试");
		c = getchar();
	}
	getchar();
	switch (c)
	{
    
    
		case '1'://1.课程名称
	{
    
    
	printf("请输入新名称:");
	gets(newdata.name);
	for (int i = 0; i < n; i++)
		if (a[i].number == num)
			strcpy(a[i].name, newdata.name);
	break;
	}
	case '2'://2.总学时
	{
    
    
		printf("请输入新总学时:");
		scanf("%d", &newdata.hours);
		for (int i = 0; i < n; i++)
			if (a[i].number == num)
				a[i].hours = newdata.hours;
	break;
	}
	case '3'://3.学分
	{
    
    
		printf("请输入新学分:");
		scanf("%d", &newdata.credit);
		for (int i = 0; i < n; i++)
			if (a[i].number == num)
			a[i].credit = newdata.credit;
	break;
	}
	case '4'://4.开课学期
	{
    
    
		printf("请输入新开课学期:");
		scanf("%d", &newdata.semester);
		for (int i = 0; i < n; i++)
			if (a[i].number == num)
				a[i].semester = newdata.semester;
	break;
	}
	}
writeToFile();
}

7. Delete information

void del(void)//5删除信息
{
    
    
	struct info original;//删除数据的判据
	long long t = 0;
	FILE* ftemp;
	load();//读原文件info.dat
	printf("\n\n");
	printf(" student course info \n");
	printf("------------------------------------------------------------------\n");
	printf("你想通过哪种方式来查找需要删除的记录?\n");
	printf("1.课程编号 2.课程名称 3.总学时\n");
	printf("4.学分 5.开课学期\n");
	printf("-----------------------------------------------------------------");
	printf("\n please select:");
	char c = getchar();
	while (c > '5' || c < '1')
	{
    
    
		printf("\n输入错误,重试");
		c = getchar();
	}
	getchar();
	switch (c)
	{
    
    
		case '1'://1.课程编号
		{
    
    
			printf("请输入课程编号:");
			scanf("%d", &original.number);
			for (int i = 0; i < n; i++)
				if (a[i].number == original.number)
					a[i].flag = false;
			break;
		}
		case '2'://2.课程名称
		{
    
    
			printf("请输入课程名称:");
			gets(original.name);
			for (int i = 0; i < n; i++)
				if (strcmp(a[i].name, original.name) == 0)
					a[i].flag = false;
			break;
		}
		case '3'://3.总学时
		{
    
    
			printf("请输入总学时:");
			scanf("%d", &original.hours);
			for (int i = 0; i < n; i++)
				if (a[i].hours == original.hours)
					a[i].flag = false;
			break;
		}
		case '4'://4.学分
		{
    
    
			printf("请输入学分:");
			scanf("%d", &original.credit);
			for (int i = 0; i < n; i++)
				if (a[i].credit == original.credit)
					a[i].flag = false;
			break;
		}
		case '5'://5.开课学期
		{
    
    
			printf("请输入开课学期:");
			scanf("%d", &original.semester);
			for (int i = 0; i < n; i++)
				if (a[i].semester == original.semester)
					a[i].flag = false;
			break;
		}}
	if ((ftemp = fopen("temporary.dat", "wb")) == NULL)//写打开临时文件temporary.dat
	{
    
    
		printf("Open the file failure...\n");
		exit(EXIT_SUCCESS);
	}
	for (int i = 0; i < n; i++)
		if (a[i].flag == false)
			continue;
		else
			fwrite(&a[i], size, 1, ftemp);//不是则将这写入临时文件temporary.dat
			fpos_t pos;//定义pos
			fgetpos(ftemp, &pos);//获取文件指针,写入pos
			fseek(ftemp, 0, SEEK_END);//文件指针指向末尾
			t = ftell(ftemp);//获取文件指针到文件头部的字节大小
			fsetpos(ftemp, &pos);//文件指针还原
			fclose(ftemp);
			n = t / size;//因为文件的变动,更新n的值
			
	int back1 = remove("info.dat");
	int back2 = rename("temporary.dat", "info.dat");//将临时文件名改为原文件名
	if (!back1 && !back2)
		printf("成功!\n");
	else printf("%d %d\n失败!\n", back1, back2);
}

8. Operations on files

void writeToFile(void)//true
{
    
    
	FILE* fp;
	if ((fp = fopen("info.dat", "wb")) == NULL)
	{
    
    
		printf("无法打开此文件\n");
		exit(EXIT_SUCCESS);
	}
	for (int i = 0; i < n; i++)
	{
    
     
		if (fwrite(&a[i], size, 1, fp) != 1)
			{
    
    
				printf("file write error.\n");
			} 
	}
	fclose(fp);
}

Code running detection and results

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_56935264/article/details/125121708