user-0 提问: 用c语言编写

回复:
user: user-1
content: 无聊写了下，仅供参考   #include <string.h> #include <stdio.h> #include <stdlib.h>  #define TEST_CLASS_MAX_NUM (100) #define TEST_STUDENT_MAX_NUM (100)  typedef struct{ 	int iStuID; 	char strStuName[20]; 	char strSex[8]; }T_student;  typedef struct{ 	int iClassID; 	char strClassName[20]; 	int iStuCnt; 	T_student tStu[TEST_STUDENT_MAX_NUM]; } T_class;  typedef struct{ 	int iClassCnt; 	T_class tClass[TEST_CLASS_MAX_NUM]; }T_classList;  static T_classList gs_tClassList;  void func_showMenu() { 	printf("\n\n  同学录管理系统\n"); 	printf("\t1.显示班级列表\n"); 	printf("\t2.增加班级\n"); 	printf("\t3.删除班级\n"); 	printf("\t4.班级同学录入\n"); 	printf("\t5.班级同学删除\n"); 	printf("\t6.显示指定班级同学录\n"); 	printf("\t7.退出\n"); 	printf("\t请输入您的操作选项:"); 	return; }  void func_initClass() { 	int iReadSize = 0; 	FILE *fp = fopen("tongxunlu.dat","rb"); 	if(NULL == fp) 	{ 		printf("tongxunlu.dat is null\n"); 		return; 	} 	 	int iClassCnt = 0; 	 	T_class tClass; 	 	while(1) 	{ 		iReadSize = 0; 		memset(&tClass,0,sizeof(T_class)); 		iReadSize = fread(&tClass,sizeof(T_class),1,fp); 		if(iReadSize <= 0) 		{ 			break; 		}		 		 		memcpy(&(gs_tClassList.tClass[gs_tClassList.iClassCnt]),&tClass,sizeof(T_class)); 		 		gs_tClassList.iClassCnt++; 	} 	 	fclose(fp); 	return; }  void func_showClass() { 	printf("班级总数为%d\n",gs_tClassList.iClassCnt); 	 	int i = 0; 	T_class *ptClass = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID > 0) 		{ 			printf("班号:%d 班级名称:%s 班级人数:%d\n",ptClass->iClassID,ptClass->strClassName,ptClass->iStuCnt); 		} 	} 	 	return; }  int func_insertClass() { 	T_class tClass; 	memset(&tClass,0,sizeof(T_class)); 	 	printf("请输入班号:"); 	scanf("%d",&(tClass.iClassID)); 	 	if(tClass.iClassID <= 0) 	{ 		printf("iClassID = %d,return\n",tClass.iClassID); 		return -1; 	} 	 	printf("请输入班级名称:"); 	scanf("%s",tClass.strClassName); 	 	int i = 0; 	T_class *ptClass = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID == tClass.iClassID) 		{			 			printf("此班号%d的班级已存在,请修改\n",tClass.iClassID); 			return -1; 		} 	} 	 	ptClass = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID == 0) 		{			 			memcpy(ptClass,&tClass,sizeof(T_class)); 				 			gs_tClassList.iClassCnt++; 			return 0; 		} 	} 	 	return -1; }  void func_saveFile() { 	int iReadSize = 0; 	FILE *fp = fopen("tongxunlu.dat","wb"); 	if(NULL == fp) 	{ 		printf("tongxunlu.dat不存在\n"); 		return; 	} 	 	int i = 0; 	T_class *ptClass = NULL; 	for(i = 0;i < gs_tClassList.iClassCnt;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID > 0) 		{ 			fwrite(ptClass,sizeof(T_class),1,fp); 		} 	} 	 	fclose(fp); 	 	printf("修改成功\n"); 	 	return; }  int func_deleteClass() { 	int iClassID = 0; 	 	printf("请输入您要删除的班号:"); 	scanf("%d",&iClassID); 	if(iClassID <= 0) 	{ 		printf("iClassID = %d,return\n",iClassID); 		return -1; 	} 	 	int i = 0; 	T_class *ptClass = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID == iClassID) 		{			 			memset(ptClass,0,sizeof(T_class)); 				 			gs_tClassList.iClassCnt--; 			return 0; 		} 	} 	 	return -1; }  int func_insertStudent() {	 	T_student tStu; 	memset(&tStu,0,sizeof(T_student)); 	 	printf("请输入您添加学生的学号:"); 	scanf("%d",&(tStu.iStuID)); 	 	printf("请输入您添加学生的姓名:"); 	scanf("%s",&(tStu.strStuName)); 	 	printf("请输入您添加学生的性别:"); 	scanf("%s",&(tStu.strSex)); 	 	int iClassID = 0;	 	printf("请输入您添加学生的班号:"); 	scanf("%d",&iClassID);	 	 	if(iClassID <= 0 || tStu.iStuID <= 0) 	{ 		printf("iClassID = %d,tStu.iStuID=%d,return\n",iClassID,tStu.iStuID); 		return -1; 	} 	 	int i = 0; 	T_class *ptClass = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID == iClassID) 		{			 			break; 		} 	} 	 	if(i == TEST_CLASS_MAX_NUM) 	{ 		printf("此班号%d的班级不存在\n",iClassID); 		return -1; 	} 	 	T_student *ptStu = NULL; 	for(i = 0;i < TEST_STUDENT_MAX_NUM;i++) 	{ 		ptStu = &(ptClass->tStu[i]); 		if(ptStu->iStuID == tStu.iStuID) 		{ 			printf("此学号的学生已存在,请修改\n"); 			return -1; 		} 	} 	 	ptStu = NULL; 	for(i = 0;i < TEST_STUDENT_MAX_NUM;i++) 	{ 		ptStu = &(ptClass->tStu[i]); 		if(ptStu->iStuID == 0) 		{ 			memcpy(ptStu,&tStu,sizeof(T_student)); 			ptClass->iStuCnt++; 			return 0; 		} 	} 	 	return -1; }  int func_deleteStudent() { 	int iStuID = 0; 	printf("请输入您删除学生的学号:"); 	scanf("%d",&iStuID); 	 	if(iStuID <= 0) 	{ 		printf("iStuID=%d,return\n",iStuID); 		return -1; 	} 	 	int i = 0;	 	int j = 0; 	T_class *ptClass = NULL; 	T_student *ptStu = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID == 0 || ptClass->iStuCnt <= 0) 		{ 			continue; 		} 		 		for(j=0;j<TEST_STUDENT_MAX_NUM;j++) 		{ 			ptStu = &(ptClass->tStu[j]); 			if(ptStu->iStuID == iStuID) 			{ 				memset(ptStu,0,sizeof(T_student)); 				ptClass->iStuCnt--; 				return 0; 			} 		} 	} 		 	return -1; }  void func_showStudent() { 	int iClassID = 0; 	printf("请输入您要显示的班号:"); 	scanf("%d",&iClassID); 	 	if(iClassID <= 0) 	{ 		printf("iClassID = %d,return\n",iClassID); 		return; 	} 	 	int i = 0;	 	int j = 0; 	T_class *ptClass = NULL; 	T_student *ptStu = NULL; 	for(i = 0;i < TEST_CLASS_MAX_NUM;i++) 	{ 		ptClass = &gs_tClassList.tClass[i]; 		if(ptClass->iClassID == iClassID) 		{ 			for(j=0;j<TEST_STUDENT_MAX_NUM;j++) 			{ 				ptStu = &(ptClass->tStu[j]); 				if(ptStu->iStuID == 0) 				{ 					continue; 				} 				printf("学号:%d 姓名:%s 性别:%s 班号:%d\n",ptStu->iStuID,ptStu->strStuName,ptStu->strSex,ptClass->iClassID); 			} 			break; 		} 		 	} 	 	return; }  int main() { 	int iSelect = 0; 	memset(&gs_tClassList,0,sizeof(gs_tClassList)); 	func_initClass(); 	 	int iRet = -1; 	 	do{ 		iRet = -1; 		func_showMenu(); 		scanf("%d",&iSelect); 		if(iSelect <1 || iSelect > 7) 		{ 			printf("您的输入有误.\n"); 			getchar(); 			continue; 		} 		switch(iSelect) 		{ 			case 1: 				func_showClass(); 				continue; 			 			case 2: 				iRet = func_insertClass(); 				break; 				 			case 3: 				iRet = func_deleteClass(); 				break; 			 			case 4: 				iRet = func_insertStudent(); 				break; 			 			case 5: 				iRet = func_deleteStudent(); 				break; 			 			case 6: 				func_showStudent(); 				continue; 				 			case 7: 				break; 			 			default: 				printf("您的输入有误.\n"); 				continue; 		} 		if(iRet == 0) 		{ 			func_saveFile(); 		} 	}while(iSelect != 7); 		 	return 0; }