?? stu_info_7.8.c
字號:
/***************************************************
*工程名:學生信息管理系統
*作者:Min Hu
*日期:2008-6-10----2008-7-9
****************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
#include <ctype.h>
#include <io.h>
#include <direct.h>
#define VERSION " Ver 1.0 "
#define AUTHER " Auther: Min Hu "
#define EMAIL " email:humin1102@126.com"
#define TITLE " Student Information Manager System "
#define WELCOME " Welcome to you! "
#define MESSAGE " Message "
#define MENULIST " Menu List "
#define ADDINFO " Add student's info "
#define SORTDATA " Sort data "
#define PRINTDATA " Print student's report "
#define SAVE " Save data to file "
#define LOAD " Load data from file "
#define SEARCH " Search students "
#define UPDATE " Update students Information"
#define STATSCORE " Stat Score "
#define MAXCOUNT 100
//全局變量
char ch;
int count=0;
int sno[MAXCOUNT]={0};
float total[MAXCOUNT];
double avg[MAXCOUNT];
int save_flag=0; //記錄保存點
struct students
{
char sno[4];
char name[21];
int age;
float score[5];
};
struct LINK
{
struct students studate;
struct LINK *next;
};
typedef struct students STU;
typedef struct LINK INFO;
INFO *head=NULL,*last=NULL;
INFO stu_link;
STU data,stu[100];
/*用戶自定義函數*/
void InsertData(STU studate);
void Gotoxy(x,y);
void Getxy(int *x,int *y);
void SetColor(int FontColor,int BackGroundColor) ;
void Time();
void Welcome();
void MakeMenuInfo();
int JuZhong(dest,n);
void MenuList();
void ClearMessage();
void InputError(int x,int y,char ch);
void AddInfo();
int GetString(char buffer[],int maxlen);
void Left_Right(char temp[]);
void AddSno();
void AddName();
void AddAge();
void AddScore();
void PrintData();
void SortData();
void Sort_Print_Message();
void SortSnoAsc();
void SortSnoDesc();
void SortNameAsc();
void SortNameDesc();
void SortTotalAsc();
void SortTotalDesc();
void Exit_Save();
void Exit_System();
void SearchData();
void SearchSno();
void SearchName();
void UpdateData(int num,int x,int y);
void DeleteData(int num,int x,int y);
void SaveData();
char *Getfilename(char fname[100],char file[20]);
void LocalData();
void Stat();
void StatScore(int num);
void StatTotal();
/*****************************************************************
***函數名:InsertData 光標函數
***參數:INFO stu_info
***功能:插入數據
***返回值:無
*****************************************************************/
void InsertData(STU studate)
{
INFO *p;
p=NULL;
p=(INFO *)malloc(sizeof(INFO));
if(p==NULL)
{
printf("Error!Out of memory!");
exit(0);
}
p->studate=studate;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
last->next=p;
}
last=p;
}
/*****************************************************************
***函數名:Gotoxy 光標函數
***參數:int x, int y
***功能:獲取光標位置
***返回值:無
*****************************************************************/
void Gotoxy(int x, int y)
{
COORD pos = {x,y};
HANDLE hOutPut = GetStdHandle(STD_OUTPUT_HANDLE);// 獲取句柄
SetConsoleCursorPosition(hOutPut, pos); //設置句柄
}
/*****************************************************************
***函數名:Getxy 光標函數
***參數:int *x, int *y
***功能:記錄當前位置坐標
***返回值:無
*****************************************************************/
void Getxy(int *x,int *y)
{
HANDLE hOutPut; // 獲取句柄
CONSOLE_SCREEN_BUFFER_INFO Stu_Info;
COORD pos;
hOutPut= GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOutPut, &Stu_Info);
pos = Stu_Info.dwCursorPosition;
*x = pos.X;
*y = pos.Y;
}
/*****************************************************************
***函數名:SetColor
***參數:int FontColor,int BackGroundColor
***功能:設置VC控制臺字體和背景顏色
***返回值:無
*****************************************************************/
void SetColor(int FontColor,int BackGroundColor) //可以接受0/1/2個參數
{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,(unsigned short)(FontColor|BackGroundColor));
}
/*****************************************************************
***函數名:Time
***參數:無
***功能:顯示系統動態時間
***返回值:無
*****************************************************************/
void Time()
{
time_t t=time(NULL);
Gotoxy(53,24);
puts(asctime(localtime(&t)));
}
/*****************************************************************
***函數名:Welcome
***參數:無
***功能:歡迎界面
***返回值:無
*****************************************************************/
void Welcome()
{
int i;
Gotoxy(23,5);
printf(TITLE);
Gotoxy(28,7);
printf(EMAIL);
Gotoxy(32,8);
printf(AUTHER);
Gotoxy(35,9);
printf(VERSION);
Gotoxy(32,11);
printf(WELCOME);
Gotoxy(27,13);
printf("Loading");
for(i=0;i<15;i++)
{
Sleep(60);
printf(".");
}
}
/*****************************************************************
***函數名:MakeMenuInfo
***參數:無
***功能:制作菜單信息
***返回值:無
*****************************************************************/
void MakeMenuInfo()
{
int i;
Gotoxy(23,1);
printf(TITLE);
Gotoxy(21,2);
for(i=1;i<=40;i++)
printf("*");
Gotoxy(0,4);
for(i=1;i<=80;i++)
printf("=");
Gotoxy(0,18);
for(i=1;i<=80;i++)
printf("=");
//MESSAGE居中顯示
Gotoxy(35,18);
printf(MESSAGE);
Gotoxy(0,23);
for(i=1;i<=80;i++)
printf("=");
Gotoxy(2,24);
printf(VERSION);
Gotoxy(23,24);
printf(AUTHER);
Time();
Gotoxy(0,5);
}
/*****************************************************************
***函數名:JuZhong
***參數:char * dest[],int n
***功能:實現菜單項居中顯示
***返回值:無
*****************************************************************/
int JuZhong(char * dest[],int n)
{
int max=0;
int s,i,t;
for(i=0;i<n;i++)
{
t=strlen(dest[i]);
max=max > t ? max : t;
}
s=(80-max)/2;
return s;
}
/*****************************************************************
***函數名:ClearMessage
***參數:無
***功能:清除消息
***返回值:無
*****************************************************************/
void ClearMessage()
{
fflush(stdin);
Gotoxy(0,19);
printf("%*s",80," ");
}
/*****************************************************************
***函數名:InputError
***參數:int x,int y,char ch(x,y坐標)
***功能:對輸入不符合條件進行處理,打印相應信息
***返回值:無
*****************************************************************/
void InputError(int x,int y,char ch)
{
if(ch>='a' && ch<='z' || ch>='A' && ch<='Z')
{
ClearMessage();
Gotoxy(0,19);
printf("Error illegal char!Please enter again!");
}
else if((ch>='0' && ch<='9'))
{
ClearMessage();
Gotoxy(0,19);
printf("Error illegal digit!Please enter again!");
}
else if(ch==13)
{
ClearMessage();
Gotoxy(0,19);
printf("Error null!Please enter again!");
}
else
{
ClearMessage();
Gotoxy(0,19);
printf("Error input illegal!Please enter again!");
}
getch();
Gotoxy(x+1,y);
printf("\b \b");
Gotoxy(x+1,y);
}
/*****************************************************************
***函數名:GetString
***參數:char buffer[],int maxlen
***功能:實現字符串輸入,接收到的字符串放入指定的字符串buffer中
***說明:該函數限定輸入字符個數最大不超過maxLen個,當輸入已滿
maxLen個時,客戶輸入無任務響應也沒有回顯,但可以用
"Backspace"鍵退格修改
***返回值:返回輸入字符串的長度
*****************************************************************/
int GetString(char buffer[],int maxlen)
{
int i=0,len;
char ch;
do
{
ch=getch();
if(ch==27) //ESC->27
{
break;
}
if(ch==13) // <CR>->13
{
buffer[i]='\0';
break;
}
if(ch==8 && i>0) //退格
{
printf("\b \b");
--i;
if(i==0)
continue;
}
if(i<maxlen && ch>=32)
{
buffer[i++]=ch;
printf("%c",ch);
}
}while(1);
len=strlen(buffer);
return len;
}
/*****************************************************************
***函數名:Left_Right
***參數:char temp[]
***功能:去除左右空格
***返回值:無
*****************************************************************/
void Left_Right(char temp[]) //去除左右空格
{
int i,len;
i=0;
while(temp[i]) //左空格
{
if(temp[i]!=' ')
break;
i++;
}
strcpy(temp,temp+i);
len=strlen(temp);
for(i=len - 1;i>=0;i--) //右空格
{
if(temp[i]!=' ')
{
temp[i+1]='\0';
break;
}
}
}
/*****************************************************************
***函數名:MenuList
***參數:無
***功能:顯示菜單項
***返回值:無
*****************************************************************/
void MenuList()
{
int i,s,x,y;
char ch;
char *menu[]=
{ "1. Add student's info",
"2. Sort data",
"3. Print student's report",
"4. Save data to file",
"5. Load data from file",
"6. Search students",
"7. Stat Score",
"0. Exit"
};
system("cls");
Gotoxy(35,3);
printf(MENULIST);
MakeMenuInfo();
printf("\n");
s=JuZhong(menu,8); //菜單項--居中顯示
for(i=0;i<=7;i++) //打印菜單項
printf("%*s%s\n",s," ",menu[i]);
Gotoxy(0,0);
Gotoxy(27,15);
printf("Please Make a choices[0-7]:");
Getxy(&x,&y);
do
{
ClearMessage();
Gotoxy(0,19);
printf("Please Input 0-7 digit. <example:1>");
Gotoxy(x,y);
ch=getche();
getch();
switch(ch)
{
case '1':AddInfo();break;
case '2':SortData();break;
case '3':PrintData();break;
case '4':SaveData();break;
case '5':LocalData();break;
case '6':SearchData();break;
case '7':Stat();break;
case '0':Exit_Save();break;
default:InputError(x,y,ch);break;
}
}while(!(ch>='0' && ch<='7'));
}
/*****************************************************************
***函數名:AddInfo
***參數:無
***功能:用戶添加信息
***返回值:無
*****************************************************************/
void AddInfo()
{
int i,x,y;
char *AddInfo[]=
{ " Sno *:",
" Name *:",
" Age *:",
" Chinese *:",
" English *:",
" Math *:",
" Physics *:",
" Chemistry *:"
};
if(count>100)
{
ClearMessage();
Gotoxy(0,19);
printf("The data is full!Pleased key to continue ...");
getch();
return;
}
do
{
system("cls");
Gotoxy(30,3);
printf(ADDINFO);
MakeMenuInfo();
Gotoxy(0,0);
for(i=0;i<=7;i++)
{
Gotoxy(20,7+i);
printf("%s\n",AddInfo[i]);
}
fflush(stdin);
AddSno();
AddName();
AddAge();
AddScore();
do
{
ClearMessage();
Gotoxy(0,19);
printf("Add Student's information Success ! Are you continue(Y/N)?");
Getxy(&x,&y);
Gotoxy(x,y);
ch=getche();
getch();
if(ch=='Y' || ch=='y' || ch=='N' || ch=='n')
{
break;
}
else
{
ClearMessage();
Gotoxy(0,19);
printf("Error!Please enter again.");
getch();
Gotoxy(x+1,y);printf("\b \b");
continue;
}
}while(1);
stu[count]=data;
total[count]=stu[count].score[0]+stu[count].score[1]+stu[count].score[2]+stu[count].score[3]+stu[count].score[4];
avg[count]=(float)total[count]/5.0;
/*
InsertData(stu_link.studate);
total[count]=stu_link.studate.score[0]+stu_link.studate.score[1]+stu_link.studate.score[2]+stu_link.studate.score[3]+stu_link.studate.score[4];
avg[count]=(float)total[count]/5.0;
*/
count++;
save_flag=1;
}while(ch=='Y' || ch=='y');
}
/*****************************************************************
***函數名:AddSno
***參數:無
***功能:實現學號自動增長
***返回值:無
*****************************************************************/
void AddSno() //學號
{
int i;
char tempsno[5];
Gotoxy(40,7);
for(i=1;i<=MAXCOUNT;i++)
{
if(sno[i-1]==0)
{
sno[i-1]=i;
sprintf(tempsno,"%03d",i);
puts(tempsno);
break;
}
}
strcpy(data.sno,tempsno);
//strcpy(stu_link.studate.sno,tempsno);
}
/*****************************************************************
***函數名:AddName
***參數:無
***功能:去除左右空格,判斷非法字符
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -