⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smsystem.c

📁 Linux环境下用C语言实现的学生管理系统
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <unistd.h>#include <stdio.h>#include <curses.h>#include <stdlib.h>#include <string.h>#define NAME_LEN 32 //the max length of the name #define MAIN_X 13 // the start x coordinate of the main menu#define MAIN_Y 40 // the start y coordinate of the main menu#define MENU_ITEM_SUM 5 //the sum of the menu itemtypedef struct lesson{ //the struct of the lesson that the student have	int chinese_score;	int english_score;	int math_score;	}LESSON;typedef struct student{ //the struct to describe one student	long sno;  //the NO of the student	char name[NAME_LEN];	int age;	char sex;	LESSON lesson; //	struct student *next;	}STUDENT;STUDENT *student=NULL; //store student info recordint record_sum=0;//record sumint record_pos=0;//record_positionint cur_pos=0;//current cursor positionint scr_sum=6;//a screen can show six recordint my_init_color(){  //initial color	if(!has_colors()){ //test the local meschine whether supports the color attribute		endwin();		fprintf(stderr,"Error -no color support on this terminal\n");		return 0;	}	if(start_color()!=OK){//initial the color		endwin();		fprintf(stderr,"Error-could not initial colors\n");		return 0;	}	return 1;}void initial(){ //initial attribute	initscr();	clear();	cbreak();	nonl();	intrflush(stdscr,FALSE);	noecho();	keypad(stdscr,TRUE);	my_init_color();		//set color pair including foreground color and background color	init_pair(1,COLOR_RED,COLOR_BLUE);	init_pair(2,COLOR_WHITE,COLOR_BLACK);	init_pair(3,COLOR_RED,COLOR_WHITE);	init_pair(4,COLOR_BLACK,COLOR_WHITE);	init_pair(5,COLOR_RED,COLOR_BLUE);	init_pair(6,COLOR_CYAN,COLOR_BLACK);	init_pair(7,COLOR_RED,COLOR_BLUE);}int mainGUI(){	int key; 	initial();	//create main framework		attrset(A_BOLD|COLOR_PAIR(1));	mvprintw(5,15,"%s","******************** Student Management System *****************");	mvprintw(25,15,"%s","****************************************************************");	int i=0;	for(i=5;i<=25;i++){		mvaddch(i,15,'*');		mvaddch(i,79,'*');	}	attrset(COLOR_PAIR(1));	mvprintw(MAIN_X-2,MAIN_Y-3,"%s","Welcome to use this system");	attroff(A_BOLD);	char menu[5][21]={"  Add New Student  "," Del Student Info  ","Modify Student Info","Search Student Info","       Quit        "}; //set menu item	attrset(COLOR_PAIR(3));//the first menu item is selected by default	mvprintw(MAIN_X+0,MAIN_Y,"%s",menu[0]);	attrset(COLOR_PAIR(4));	mvprintw(MAIN_X+1,MAIN_Y,"%s",menu[1]);	mvprintw(MAIN_X+2,MAIN_Y,"%s",menu[2]);	mvprintw(MAIN_X+3,MAIN_Y,"%s",menu[3]);	mvprintw(MAIN_X+4,MAIN_Y,"%s",menu[4]);	move(MAIN_X,MAIN_Y);	key=0; //store the current position of the cursor	refresh();		int ch;	do{		ch=getch();		switch(ch){		case KEY_UP: //up key 			attrset(COLOR_PAIR(4));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			key=(key-1+MENU_ITEM_SUM)%MENU_ITEM_SUM;			attrset(COLOR_PAIR(3));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			move(MAIN_X+key,MAIN_Y);			break;			case KEY_DOWN: //down key			attrset(COLOR_PAIR(4));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			key=(key+1)%MENU_ITEM_SUM;			attrset(COLOR_PAIR(3));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			move(MAIN_X+key,MAIN_Y);			break;			case KEY_RIGHT: //the same function with the key KEY_UP			attrset(COLOR_PAIR(4));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			key=(key+1)%MENU_ITEM_SUM;			attrset(COLOR_PAIR(3));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			move(MAIN_X+key,MAIN_Y);			break;			case KEY_LEFT://the same function with the key KEY_DOWN			attrset(COLOR_PAIR(4));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			key=(key-1+MENU_ITEM_SUM)%MENU_ITEM_SUM;			attrset(COLOR_PAIR(3));			mvprintw(MAIN_X+key,MAIN_Y,menu[key]);			move(MAIN_X+key,MAIN_Y);			break;			default:			break;		}		refresh();	}while(ch!='\r');		return key;	}void createStudentInfo(){	char again='y';	touchwin(stdscr);		echo();	FILE *fp;	fp=fopen("student.dat","a"); //the file to store the data	do{	clear();	//create the framework	attrset(A_BOLD|COLOR_PAIR(4));	mvprintw(3,15,"%s","******************** Student Management System *****************");	mvprintw(24,15,"%s","****************************************************************");	int i=0;  //for the loop counter	for(i=3;i<=24;i++){		mvaddch(i,15,'*');		mvaddch(i,79,'*');	}	attrset(COLOR_PAIR(2));	mvprintw(5,37,"%s","Please input a new student info");	attrset(COLOR_PAIR(3));	mvprintw(7,30,"%s","SNO:");	mvprintw(9,30,"%s","Name:");	mvprintw(11,30,"%s","Age:");	mvprintw(13,30,"%s","Sex(m/f):");	mvprintw(15,30,"%s","Chinese_Score:");	mvprintw(16,30,"%s","English_Score:");	mvprintw(17,30,"%s","Math_Score:");	refresh();	//allocate the memory to store the data	STUDENT *ptr=(STUDENT *)malloc(sizeof(STUDENT));/*	ptr->next=NULL;	if(student_ptr==NULL)		student_ptr=ptr;	else{		ptr->next=student_ptr->next;		student_ptr->next=ptr;	}*/	attrset(COLOR_PAIR(4));		move(7,34);  //read student NO	while(!scanw("%ld",&ptr->sno)||ptr->sno>999999||ptr->sno<1){ //test the input NO.		mvprintw(19,30,"%s","Input data format error!");		move(7,34);		clrtoeol();		mvaddch(7,79,'*');		move(7,34);		refresh();	}		move(19,29);	clrtoeol();	mvaddch(19,79,'*');	refresh();	move(9,35); //read name	scanw("%s",ptr->name);	move(11,34);   	 //read age	while(!scanw("%d",&ptr->age)){		mvprintw(19,30,"%s","Input data format error!");		move(11,34);		clrtoeol();		mvaddch(11,79,'*');		move(11,34);		refresh();	}	move(19,29);	clrtoeol();	mvaddch(19,79,'*');	refresh();    	move(13,39);  //read gender of the student	scanw("%c",&ptr->sex);	while(ptr->sex!='f'&&ptr->sex!='m'){		mvprintw(19,30,"%s","Input data format error!");			move(13,39);		clrtoeol();		mvaddch(13,79,'*');		move(13,39);		refresh();		scanw("%c",&ptr->sex);	}	move(19,29);	clrtoeol();	mvaddch(19,79,'*');	refresh();		move(15,44);  //read chinese score	int score;	while(!scanw("%d",&score)){		mvprintw(19,30,"%s","Input data format error!");		move(15,44);		clrtoeol();		mvaddch(15,79,'*');		move(15,44);		refresh();		}	ptr->lesson.chinese_score=score;	move(19,29);	clrtoeol();		mvaddch(19,79,'*');	refresh();		move(16,44); //read english score	while(!scanw("%d",&score)){		mvprintw(19,30,"%s","Input data format error!");		move(16,44);		clrtoeol();		mvaddch(16,79,'*');		move(16,44);		refresh();	}	ptr->lesson.english_score=score;	move(19,29);	clrtoeol();	mvaddch(19,79,'*');	refresh();		move(17,41);  //read math score	while(!scanw("%d",&score)){		mvprintw(19,30,"%s","Input data format error!");		move(17,41);		clrtoeol();		mvaddch(17,79,'*');		move(17,41);		refresh();	}	ptr->lesson.math_score=score;	move(19,29);	clrtoeol();	mvaddch(19,79,'*');		//write the data to file		fwrite(ptr,sizeof(STUDENT),1,fp);		mvprintw(20,30,"%s","Continue to add new student(y/n):");	scanw("%c",&again);	refresh();	}while(again=='y'||again=='Y');	noecho();	fclose(fp);	return;}void readStudentRecord(){ //read all the student info from the file	FILE *fp;	int alloc_num=1;	//read data from file	fp=fopen("student.dat","r");	record_sum=0;	while(fread(&student[record_sum],sizeof(STUDENT),1,fp)){		record_sum++;		if(record_sum>20*alloc_num){			student=(STUDENT *)realloc(student,sizeof(STUDENT)*20*alloc_num);			alloc_num++;		}	}	fclose(fp);	}void displayInfo(int i){//dispaly one record info	//show the selected record information	attrset(A_BOLD|COLOR_PAIR(6));	mvprintw(21,23,"the student whose SNO is %ld has information as show.",student[i].sno);	mvprintw(9,34,"%ld",student[i].sno);	mvprintw(11,35,"%s",student[i].name);	mvprintw(13,34,"%d",student[i].age);	mvprintw(15,39,"%c",student[i].sex);	mvprintw(17,44,"%d",student[i].lesson.chinese_score);	mvprintw(18,44,"%d",student[i].lesson.english_score);	mvprintw(19,41,"%d",student[i].lesson.math_score);}void setGUI(char *str,int start_pos){	clear();	attrset(A_BOLD|COLOR_PAIR(4)); //set fond and color attribute  	//create main framework	mvprintw(5,15,"******************** Student Management System *****************");	mvprintw(24,15,"****************************************************************");	int i=0;	for(i=5;i<=24;i++){		mvaddch(i,15,'*');		mvaddch(i,79,'*');	}	attrset(COLOR_PAIR(2));	mvprintw(7,37,"%s",str);	attrset(COLOR_PAIR(3));	mvprintw(9,30,"%s","SNO:");	mvprintw(11,30,"%s","Name:");	mvprintw(13,30,"%s","Age:");	mvprintw(15,30,"%s","Sex(m/f):");	mvprintw(17,30,"%s","Chinese_Score:");	mvprintw(18,30,"%s","English_Score:");	mvprintw(19,30,"%s","Math_Score:");	//create choice sno framework	attrset(COLOR_PAIR(5));	mvprintw( 9,61," Choice SNO: ");	mvprintw(10,61," (Use u or d)");	mvprintw(11,61,"*************");	mvprintw(18,61,"*************");	for(i=0;i<6;i++)		mvprintw(12+i,61,"*           *");	for(i=0;i<6&&i<record_sum;i++){		mvprintw(12+i,61,"* ");		mvprintw(12+i,63,"%-9ld",student[start_pos+i].sno);		mvprintw(12+i,72," *");		}	//if have record show the current record	if(record_sum>0){		attrset(COLOR_PAIR(6));		mvprintw(12+cur_pos,61,"* ");		mvprintw(12+cur_pos,63,"%-9ld",student[record_pos].sno);		mvprintw(12+cur_pos,72," *");		displayInfo(record_pos);	}	//show the sum of the record		attrset(COLOR_PAIR(4));	mvprintw(19,61," The sum:%-4d",record_sum);	move(12+cur_pos,62);	attroff(A_BOLD);	noecho();	refresh();}void up(char *str){ //reaction to the upper key	int i,pos;	//the sum of record less than the num of one screen can show	if(record_sum==0){		mvprintw(20,25,"Have no record!");	}	//when the sum of the recordless than the sum of one screen can show				else if(record_sum<=scr_sum){  					record_pos=(record_pos-1+record_sum)%record_sum;		cur_pos=record_pos;		setGUI(str,0);	}			else{		if(cur_pos==0){ //when curosr position on the top 			if(record_pos==0)			{				record_pos=record_sum-1;				cur_pos=5;  //set cursor position				setGUI(str,record_sum-scr_sum);			}			else{				record_pos--;				setGUI(str,record_pos);				}		}		else{//cursor position is not 0

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -