C实例No.9|学生成绩小系统项目实战

展示

文档文件
在这里插入图片描述
功能实现和通讯录小系统项目相似,实现序号、名字、性别、年龄以及语数英3课的成绩。录入成绩会不断执行,当序号输入为0时结束录入学生成绩并返回到初始选择的页面。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码

主函数main.c

#include "student.h"

int menu_select();

int main(int argc, const char *argv[])
{
	link l;

	l = (link)malloc(sizeof(NODE));
	if(!l)
	{
		printf("\n malloc memory failure!\n");
		return -1;
	}
	l->next = NULL;

	while(1)
	{
		int tmp = menu_select();
		switch(tmp)
		{
		case 1:
			input(l);
			break;
		case 2:
			del(l);
			break;
		case 3:
			list(l);
			break;
		case 4:
			search(l);
			break;
		case 5:
			save(l);
			break;
		case 6:
			load(l);
			break;
		case 7:
			exit(0);
		//defult :
		//	break;
		}
	}
	return 0;
}

int menu_select()
{
	int i;
	printf("\n\n\t************student list***********\n");
	printf("\t|*        1、input record         *|\n");
	printf("\t|*        2、delete record        *|\n");
	printf("\t|*        3、list record          *|\n");
	printf("\t|*        4、search record        *|\n");
	printf("\t|*        5、save record          *|\n");
	printf("\t|*        6、load record          *|\n");
	printf("\t|*        7、quit                 *|\n");
	printf("\t***********************************\n\n\n");

	do
	{
		printf("\n\t input your choice:");
		scanf("%d",&i);
	}while(i <= 0 || i > 7);

	return i;
}



student.h文件

#ifndef _STUDENT_H_
#define _STUDENT_H_

#include <stdio.h>
#include <stdlib.h>

struct student
{
	int num;              /*学号*/
	char name[15];        /*姓名*/
	char sex[2];          /*性别*/
	int age;              /*年龄*/
	double score[3];      /*三门课的分数*/
	double sum;           /*总分*/
	double ave;           /*平均分*/
};

typedef struct node
{
	struct student data;
	struct node *next;
}NODE,*link;

void input(link l);
void save(link l);
void load(link l);
void del(link l);
void list(link l);
void display(NODE *p);
void search(link l);
#endif 

student.c文件

#include "student.h"

void input(link l)
{
	//int i;
	link p,q = l;

	while(1)
	{
		p = (NODE *)malloc(sizeof(NODE));
		if(!p)
		{
			printf("memory malloc failure\n");
			return ;
		}

		printf("\tinput number:");
		scanf("%d",&p->data.num);
		if(p->data.num == 0)
			break;


		for(;q->next!= NULL;q = q->next)
		{
			if(q->data.num == p->data.num)
			{
				printf("\tthe number has exited,please input again:");
				scanf("%d",&p->data.num);
			}
		}
		printf("\tinput name:");
		scanf("%s",p->data.name);

		printf("\tinput sex w/m:");
		scanf("%s",p->data.sex);

		printf("\tinput age:");
		scanf("%d",&p->data.age);

		printf("\tplease input Chinese Math English score:\n");
		int i;
		for(i = 0;i < 3; i++)
		{
			printf("\t");
			scanf("%lf",&p->data.score[i]);
		}

		p->data.sum = p->data.score[0] + p->data.score[1] + p->data.score[2];
		p->data.ave = p->data.sum / 3;

		q->next = NULL;
		q->next = p;
		p = q;
	}
	return;
}
void del(link l)
{
	int num;
	NODE *p,*q = l;
	p = q->next;

	printf("\tplease input the student number you want to delete:");
	scanf("%d",&num);

	while(p)
	{
		if(num == p->data.num)
		{
			q->next = p->next;
			free(p);
			printf("\tdelete sucessfuly!\n");
			break;
		}
		else
		{
			q = p;
			p = q->next;
		}
	}

	if(NULL == p)
		printf("can not find the student you want to delete!");

	return;
}
void list(link l)
{
	NODE *p = l->next;

	if(NULL == p)
	{
		printf("no student record\n");
	}

	while(p != NULL)
	{
		display(p);
		p = p->next;
	}
	return;
}

void display(NODE *p)
{
	printf("\tstudent information\n");
	printf("\tnumber: %d\n",p->data.num);
	printf("\tname: %s\n",p->data.name);
	printf("\tsex: %s\n",p->data.sex);
	printf("\tChinese: %lf\n",p->data.score[0]);
	printf("\tMath: %lf\n",p->data.score[1]);
	printf("\tEnglist: %lf\n",p->data.score[2]);
	printf("\tsum: %lf\n",p->data.sum);
	printf("\tave: %lf\n",p->data.ave);
}

void search(link l)
{
	int num;
	NODE *p = l->next;

	printf("\tinput number of the student:");
	scanf("%d",&num);

	while(p)
	{
		if(p->data.num == num)
		{
			display(p);
			break;
		}
		p = p->next;
	}

	if(NULL == p)
	{
		printf("\tnot search\n");
	}

	return;
}
void save(link l)
{
	NODE *p = l->next;
	FILE *fp;

	fp = fopen("studentlist.txt","w");
	if(!fp)
	{
		perror("can not open file");
		exit(1);
	}

	printf("\n\tsaving file\n");

	while(p)
	{
		fwrite(p,sizeof(NODE),1,fp);
		p = p->next;
	}
	fclose(fp);
	return;
}
void load(link l)
{
	NODE *p,*r = l;
	FILE *fp = NULL;

	fp = fopen("studentlist.txt","r");
	if(!fp)
	{
		perror("can not open file");
		exit(1);
	}

	printf("\n\tloading file\n");
	while(!feof(fp))
	{
		p = (NODE *)malloc(sizeof(NODE));
		if(!p)
		{
			printf("memory malloc failure\n");
			return ;
		}

		if(fread(p,sizeof(NODE),1,fp) != 1)
			break;
		else
		{
			p->next = NULL;
			r->next = p;
			r = p;
		}
	}
	fclose(fp);

	return;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值