第一版

 

作者:小代码

 

时间:2013年8月2日17:30:22

 

IDE:VC6.0

 

功能:

 

 

一、输入:
 1、尾部追加
 2、追加多个

二、输出所有信息

三、返回序号 为 n 的学生的信息

四、最高分、最低分、平均分

五、排序
 1、按序号排序
 2、按成绩排序

六、写入文件

 

 

代码:

 

StuMain.c

 

 

#include<stdio.h>

#include<stdlib.h>

#include "Student.h"





int main( void )

{

	

	void Menu( void );//菜单提示



	int order;



	pMAN head = Init();



	Menu();

	printf("学生信息管理系统:");

	int or = scanf("%d",&order);



	while ( true )

	{

		if ( 0 == or )

		{

			continue;

		}



		switch ( order )

		{

		case 1:

			{

				append( head );

				break;

			}

		case 2:

			{

				printf("信息全部为 0 时,输入结束.\n");

				input( head );

				break;

			}

		case 3:

			{

				show( head );

				break;

			}

		case 4:

			{

				int xu;

				printf("输入待查序号:");

				scanf("%d",&xu);

				pSTU temp = getIndex( head, xu );

				if ( NULL != temp )

				{

					printf("%-3d| 学号:%-10s姓名:%-15s年龄:%d\t成绩:%.2lf\n",temp->xu,temp->num,temp->name,temp->age,temp->score);

				}

				

				break;

			}

		case 5:

			{

				printf("%.2lf\n",maxScore( head ) );

				break;

			}

		case 6:

			{

				printf("%.2lf\n",minScore( head ) );

				break;

			}

		case 7:

			{

				printf("%.2lf\n",avrScore( head ) );

				break;

			}

		case 8:

			{

				sortOfXu( head );

				break;

			}

		case 9:

			{

				sortOfScore( head );

				break;

			}

		case 10:

			{

				double seach;

				pSTU temp = NULL;

				printf("输入待查询成绩:");

				scanf("%lf",&seach);

				temp = seachOfScore( head, seach );

				if ( NULL != temp )

				{

					printf("%-3d| 学号:%-10s姓名:%-15s年龄:%d\t成绩:%.2lf\n",temp->xu,temp->num,temp->name,temp->age,temp->score);

				}

				else

				{

					printf("无此学生信息!\n");

				}

				break;

			}

		case 11:

			{

				writeFile( head );

				break;

			}

		case -1:

			{

				getchar();

				char ch;				

				printf("是否退出系统?Y/N");

				ch = getchar();

				if ( 'Y' == ch || 'y' == ch )

				{

					printf("已退出系统...\n");

					exit(0);

				}				

				break;

			}

		default :

			{

				printf("输入的命令无效,请重新输入!\n");

				break;

			}

		}



		printf("学生信息管理系统:");

		or = scanf("%d",&order);

	}

	



	printf("\n\nHello World!\n");

	return 0;

}





//菜单提示

void Menu( void )

{

	char one[] = "追加";

	char two[] = "追加多个";

	char three[] = "输出所有";

	char four[] = "返回 n ";

	char five[] = "最高分";

	char six[] = "最低分";

	char seven[] = "平均分";

	char eight[] = "序号排序";

	char nine[] = "成绩排序";

	char ten[] = "查询成绩";

	char eleven[] = "写入文件";

	char quit[] = "退出系统";



	printf("\t1-%-15s\t2-s%-15s\n",one,two);

	printf("\t3-%-15s\t4-s%-15s\n",three,four);

	printf("\t5-%-15s\t6-s%-15s\n",five,six);

	printf("\t7-%-15s\t8-s%-15s\n",seven,eight);

	printf("\t9-%-15s\t10-s%-15s\n",nine,ten);

	printf("\t11-%-15s\t-1%-15s\n",eleven,quit);

	

}


 

 

 

Student.h

 

struct Student

{

	char num[15];//学号

	char name[20];//姓名

	int age;//年龄

	double score;//成绩



	int xu;//序号

	struct Student * next;

};



typedef struct Student STU;

typedef struct Student * pSTU;



struct StuMan

{

	pSTU first;//头结点

	pSTU last;//尾结点

	int len;//总信息数

};



typedef struct StuMan MAN;

typedef struct StuMan * pMAN;





pMAN Init( void );//初始化

bool isEmpty( pMAN head );//是否为空

void append( pMAN head );//追加

void input( pMAN head );//追加多个

void show( pMAN head );//输出所有

pSTU getIndex( pMAN head, int n );//返回序号为 n 的学生信息

double maxScore( pMAN head );//最高分

double minScore( pMAN head );//最低分

double avrScore( pMAN head );//平均分

void sortOfXu( pMAN head );//按序号排序

void sortOfScore( pMAN head );//按成绩排序

pSTU seachOfScore( pMAN head, double seachScore );//按成绩查询

void writeFile( pMAN head );//写入文件


 

 

 

Student.c

 

#include<stdio.h>

#include<stdlib.h>

#include"Student.h"



//初始化

pMAN Init( void )

{

	pMAN head = (pMAN)malloc(sizeof(MAN));



	if ( NULL == head )

	{

		printf("初始化失败!\n");

	}



	head->first = NULL;

	head->last = NULL;

	head->len = 0;



	return head;

}



//是否为空

bool isEmpty( pMAN head )

{

	if ( 0 == head->len )

	{

		return true;

	}

	else

	{

		return false;

	}

}



//追加

void append( pMAN head )

{

	pSTU temp = (pSTU)malloc(sizeof(STU));

	scanf("%s%s%d%lf",temp->num,temp->name,&temp->age,&temp->score);

	temp->next = NULL;



	if ( true == isEmpty( head ) )//如果信息为空,则在第一个位置追加

	{

		head->last = temp;

		head->first = temp;

		head->len++;

		

	}

	else//信息不为空,则在最后一个位置追加

	{

		head->last->next = temp;

		head->last = temp;

		head->len++;

		

	}



	head->last->xu = head->len;

}





//追加多个

void input( pMAN head )

{

	if ( true == isEmpty( head ) )//如果信息为空,则先在第一个位置追加一个

	{

		append( head );

	}

	

	//不为空,或已经在第一个位置追加一个,在尾部追加

	

	pSTU temp = (pSTU)malloc(sizeof(STU));

	

	scanf("%s%s%d%lf",temp->num,temp->name,&temp->age,&temp->score);

	temp->next = NULL;

		

	while ( '0' != temp->num[0] )

	{

		head->last->next = temp;

		head->last = temp;

		head->len++;

		temp->xu = head->len;



		temp = (pSTU)malloc(sizeof(STU));

		scanf("%s%s%d%lf",temp->num,temp->name,&temp->age,&temp->score);

		temp->next = NULL;



	}

	

}



//输出所有

void show( pMAN head )

{

	if ( true == isEmpty( head ) )//信息为空,则退出

	{

		printf("信息为空!\n");

		return ;

	}



	pSTU temp = head->first;



	while ( NULL != temp )

	{

		printf("%-3d| 学号:%-10s姓名:%-15s年龄:%d\t成绩:%.2lf\n",temp->xu,temp->num,temp->name,temp->age,temp->score);

		temp = temp->next;

	}

}



//返回序号为 n 的学生信息

pSTU getIndex( pMAN head, int n )

{

	pSTU temp = NULL;



	//如果序号不在合理范围内,则给出提示,并退出函数

	if ( n < 1 )

	{

		printf("序号应大于 1!\n");

		return temp;

	}

	

	if ( n > head->len )

	{

		printf("序号应不大于 %d!\n",head->len);

		return temp;

	}



	temp = head->first;



	while ( true )

	{

		if ( n == temp->xu )

		{

			break;

		}



		temp = temp->next;

	}



	return temp;

}





//最高分

double maxScore( pMAN head )

{

	double max = -1;

	int i = 1;

	pSTU temp = head->first;



	if ( true == isEmpty( head ) )

	{

		printf("信息为空!\n");

		return max;

	}



	max = temp->score;

	temp = temp->next;

	



	for ( i = 2; i <= head->len; i++ )

	{

		max = ( temp->score >= max ) ? temp->score : max;

		temp = temp->next;

		

	}



	return max;

}





//最低分

double minScore( pMAN head )

{

	double min = -1;

	int i = 1;

	pSTU temp = head->first;

	

	if ( true == isEmpty( head ) )

	{

		printf("信息为空!\n");

		return min;

	}

	

	min = temp->score;

	temp = temp->next;



	for ( i = 2; i <= head->len; i++ )

	{

		min = ( temp->score <= min ) ? temp->score : min;

	}

	

	return min;



}





//平均分

double avrScore( pMAN head )

{

	double ave = 0;

	int i = 1;

	pSTU temp = head->first;



	if ( true == isEmpty( head ) )

	{

		printf("信息为空!\n");

		return ave;

	}



	for ( i = 1; i <= head->len; i++ )

	{

		ave += temp->score;

		temp = temp->next;

	}



	ave = ave / head->len;



	return ave;

}







// 按序号排序

void sortOfXu( pMAN head )

{

	if ( true == isEmpty( head ) )

	{

		printf("信息为空!\n");

		return ;

	}

	

	pSTU * pSort = (pSTU*)malloc(sizeof(pSTU)*head->len);//指针数组

	int i = 1;

	int j = 1;

	pSTU temp = NULL;

	

	for ( i = 0, temp = head->first; i < head->len; i++, temp = temp->next )//保存各条信息指针

	{

		pSort[i] = temp;

	}

	

	// 	for ( i = 0; i < head->len; i++ )

	// 	{

	// 		printf("%-3d| 学号:%-10s姓名:%-15s年龄:%d\t成绩:%.2lf\n",pSort[i]->xu,pSort[i]->num,pSort[i]->name,pSort[i]->age,pSort[i]->score);

	// 	}

	

	

	for ( i = 0; i < head->len - 1; i++ )//指针排序

	{

		for ( j = i + 1; j < head->len; j++)

		{

			if ( pSort[i]->xu > pSort[j]->xu )

			{

				temp = pSort[i];

				pSort[i] = pSort[j];

				pSort[j] = temp;

			}

			

		}

	}

	

	//指针复位

	head->first = pSort[0];

	head->last = pSort[head->len - 1];

	head->last->next = NULL;

	for ( i = 0; i < head->len - 1; i++ )

	{

		pSort[i]->next = pSort[i+1];

	}





}





//按成绩排序

void sortOfScore( pMAN head )

{

	if ( true == isEmpty( head ) )

	{

		printf("信息为空!\n");

		return ;

	}

	

	pSTU * pSort = (pSTU*)malloc(sizeof(pSTU)*head->len);//指针数组

	int i = 1;

	int j = 1;

	pSTU temp = NULL;



	for ( i = 0, temp = head->first; i < head->len; i++, temp = temp->next )//保存各条信息指针

	{

		pSort[i] = temp;

	}



// 	for ( i = 0; i < head->len; i++ )

// 	{

// 		printf("%-3d| 学号:%-10s姓名:%-15s年龄:%d\t成绩:%.2lf\n",pSort[i]->xu,pSort[i]->num,pSort[i]->name,pSort[i]->age,pSort[i]->score);

// 	}





	for ( i = 0; i < head->len - 1; i++ )//指针排序

	{

		for ( j = i + 1; j < head->len; j++)

		{

			if ( pSort[i]->score > pSort[j]->score )

			{

				temp = pSort[i];

				pSort[i] = pSort[j];

				pSort[j] = temp;

			}

			

		}

	}



	//指针复位

	head->first = pSort[0];

	head->last = pSort[head->len - 1];

	head->last->next = NULL;

	for ( i = 0; i < head->len - 1; i++ )

	{

		pSort[i]->next = pSort[i+1];

	}





}



//按成绩查询

pSTU seachOfScore( pMAN head, double seachScore )

{

	pSTU seach = head->first;

	



	if ( true == isEmpty( head ) )

	{

		printf("信息为空!\n");

		return seach;

	}



	while ( NULL != seach )

	{

		if ( seachScore == seach->score )

		{

			break;

		}



		seach = seach->next;

	}



	return seach;



}

//写入文件

void writeFile( pMAN head )

{

	char *str = (char*)malloc(sizeof(char)*25);

	pSTU temp = NULL;

	printf("输入文件名称:");

	scanf("%s",str);

	int i = 1;

	FILE *fp;

	if ( NULL == ( fp = fopen( str, "w" ) ) )

	{

		printf("打开文件失败!\n");

		return;

	}



	temp = head->first;



	while ( i <= head->len )

	{

		fprintf( fp, "%-3d| 学号:%-10s姓名:%-15s年龄:%d\t成绩:%.2lf\n",temp->xu,temp->num,temp->name,temp->age,temp->score);

		temp = temp->next;

		i++;

	}





}


 

 

 

 

 

 

你可能感兴趣的:(第一版)