|
#include<stdio.h>
#include<conio.h>
#include <Windows.h>
#include <stdlib.h>
#include<string.h>
#define SLEN 10
#ifndef MAXSTU
#define MAXSTU 160
#define _alpha 161 //_alpha用于检测sencount值是否被改变,需大于最大学生数
#endif
//NR(x)得到功能菜单的字符串数量
#define NR(x) (sizeof(x)/sizeof(x[0]))
#define UP 72
#define DOWN 80
#define ENTER 13
#define ESC 27
#define TITLE "学生信息管理系统"
#define _MSDFER "================"
int get_userinput(int *index, int size);
void paixv(struct student *students, int);
void addstustream(struct student *,int *,HANDLE);
struct student _max(struct student *stu, int len);
struct student _min(struct student *stu, int len);
double average(struct student *students, int len);
void print(struct student *students, int, HANDLE);
static bool cleartext(int x,int y,int ym,int anmont);
void destopshow(const struct student *students,int count);
void showmenu(HANDLE hOut, char **menu, int size, int index);
int findcount(struct student *students, int len, char *dname);
int counter_stu(struct student *students, int len, int floor, int last);
struct student findname(struct student *students, int len, char *dname);
char *menu[] =
{
"0-最高学生",
"1-最低学生",
"2-平均成绩",
"3-成绩浏览",
"4-成绩查找"
};
char *mainmenu[]= //菜单
{
"0--管理员",
"1--学 生 ",
"2--退 出 "
};
char *sonmenu[]=
{
"0--学生人数",
"1--及格人数",
"2--成绩添加",
"3--修改成绩",
"4--成绩浏览",
"5--清空成绩",
};
struct student
{
char *name;
char *xuehao;
int grade;
};
//初始化结构体数组
/*{
徐静 11 100
suny 9 49
昂波 1 56
贝尔 12 67
澄江 2 68
秀奥 13 66
劳拉 6 80
贾斯 16 40
Kona 8 85
绍全 14 91
黎万 15 89
李帅 18 59
芳杰 14 81
阿三 7 84
阿四 9 98
收到 3 76
阿五 5 59
昂达 17 72
};
*/
struct student students[MAXSTU];
//控制台光标位置变量
COORD pos = { 0, 0 };
COORD ps = { 0, 0 };
COORD pose= { 0, 0 };
void main()
{
int count=0,SonIndex=0;
int index = 0,_index=0;
char charname[5];
char temp1[SLEN];
char temp2[SLEN];
FILE *fp;
int mirrorcount;
volatile bool supllcoedect=false;
fp=fopen("C:\\Users\\Administrator\\Desktop\\students.txt", "r");
if(fp!=NULL)
{
count=0;
while((fscanf(fp,"%s %s %d",temp1,temp2,&students[count].grade))!=EOF)
{ //通过文件students.txt更新结构体内容
struct student *current=&students[count];
current->name=(char *)malloc(sizeof(char)*(strlen(temp1)+1));
strcpy(current->name,temp1);
current->xuehao=(char *)malloc(sizeof(char)*(strlen(temp2)+1));
strcpy(current->xuehao,temp2);
count++;
}
}
else
{
fp=fopen("C:\\Users\\Administrator\\Desktop\\students.txt","w");
}
fclose(fp);
//HANDLE h获取句柄
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTitleA(TITLE);//控制台的名称
system("color 70");
double _average;
struct student mer;
struct student srr;
struct student stu;
while(true)
{
ps.X = 80;
ps.Y = 0;
supllcoedect=false;
SetConsoleCursorPosition(hOut, ps);
SetConsoleTextAttribute(hOut,(FOREGROUND_BLUE & FOREGROUND_GREEN & FOREGROUND_RED) | (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED));
printf("上,下,ENTER选择,ESC退出");
showmenu(hOut, mainmenu, NR(mainmenu), index);
int _srt=get_userinput(&index, NR(mainmenu));
if (_srt == ESC)
{
system("cls");
exit(0);
}
else if (_srt == ENTER)
{
system("cls");
switch (index)
{
case 0:
{
ps.X = 80;
ps.Y = 0;
SetConsoleCursorPosition(hOut, ps);
SetConsoleTextAttribute(hOut,(FOREGROUND_BLUE & FOREGROUND_GREEN & FOREGROUND_RED) | (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED));
printf("上,下,ENTER选择,ESC返回上一级");
while(true)
{
showmenu(hOut, sonmenu, NR(sonmenu), SonIndex);
int set=get_userinput(&SonIndex, NR(sonmenu));
if(set==ESC)
{
break;
}
else if(set==ENTER)
{
switch(SonIndex)
{
case 0:
{
ps.X = 57;
ps.Y = 13;
SetConsoleCursorPosition(hOut, ps);
printf("学生人数为:%d人\n",count);
break;
}
case 1:
{
ps.X = 57;
ps.Y = 14;
SetConsoleCursorPosition(hOut, ps);
printf("及格人数:%d人\n", counter_stu(students, count, 150, 60));
break;
}
case 5:
{//清空成绩
fp=fopen("C:\\Users\\Administrator\\Desktop\\students.txt", "w");
if(fp!=NULL)
{
count=0;
while((fscanf(fp,"%s %s %d",temp1,temp2,&students[count].grade))!=EOF)
{
struct student *current=&students[count];
current->name=(char *)malloc(sizeof(char)*(strlen(temp1)+1));
strcpy(current->name,temp1);
current->xuehao=(char *)malloc(sizeof(char)*(strlen(temp2)+1));
strcpy(current->xuehao,temp2);
count++;
}
ps.X = 57;
ps.Y = 12;
SetConsoleCursorPosition(hOut, ps);
printf("Clear score success!\n");
}
fclose(fp);
break;
}
case 2:
{
addstustream(&students[count++],&count,hOut);
destopshow(students,count);
if(supllcoedect==true)
{
paixv(students,count);
print(students, count, hOut);
supllcoedect=true;
}
break;
}
case 4:
{
paixv(students,count);
print(students, count, hOut);
supllcoedect=true;
break;
}
case 3:
{
ps.X = 36;
ps.Y = 15;
SetConsoleCursorPosition(hOut, ps);
printf("Please enter the name or student id of the student whose grades need to be modified");
ps.X=57;
ps.Y=16;
SetConsoleCursorPosition(hOut, ps);
printf(" ");
SetConsoleCursorPosition(hOut, ps);
printf("%c",' ');
while((scanf("%s",charname))==1)
{
mirrorcount=findcount(students,count,charname);
if(mirrorcount!=_alpha)
{
struct student *prev=&students[mirrorcount];
ps.X=36;
SetConsoleCursorPosition(hOut, ps);
printf("#%s #%s #%d",prev->name,prev->xuehao,prev->grade);
ps.X=57;
ps.Y=17;
SetConsoleCursorPosition(hOut, ps);
printf("Please enter the student\'s name! ");
ps.X=57;
ps.Y=18;
SetConsoleCursorPosition(hOut, ps);
printf("%c",' ');
scanf("%s",temp1);
SetConsoleCursorPosition(hOut, ps);
printf("Now enter the student\'s number! ");
ps.X=57;
ps.Y=19;
SetConsoleCursorPosition(hOut, ps);
printf("%c",' ');
scanf("%s",temp2);
SetConsoleCursorPosition(hOut, ps);
printf("Now enter the student\'s grade! ");
ps.X=57;
ps.Y=20;
SetConsoleCursorPosition(hOut, ps);
printf("%c",' ');
scanf("%d",&prev->grade);
prev->name=(char *)malloc(sizeof(char)*(strlen(temp1)+1));
prev->xuehao=(char *)malloc(sizeof(char)*(strlen(temp2)+1));
strcpy(prev->xuehao,temp2);
strcpy(prev->name,temp1);
cleartext(36,15,21,84);
ps.X=57;
ps.Y=15;
SetConsoleCursorPosition(hOut, ps);
printf("Modification complete!");
destopshow(students,count);
if(supllcoedect==true)
{
paixv(students,count);
print(students, count, hOut);
supllcoedect=true;
}
}
else
{
SetConsoleCursorPosition(hOut, ps);
printf("no find! please enter to try again! \n");
ps.Y++;
SetConsoleCursorPosition(hOut, ps);
printf("Any key to continue!");
getch();
cleartext(36,15,21,84);
}
break;
}
}
}//switch(sonindex)
}
}//while(true)
system("cls");
break;
}
case 1:
{
ps.X = 80;
ps.Y = 0;
SetConsoleCursorPosition(hOut, ps);
SetConsoleTextAttribute(hOut, (FOREGROUND_BLUE & FOREGROUND_GREEN & FOREGROUND_RED) | (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED));
printf("上,下,ENTER选择,ESC返回上一级");
while(true)
{
showmenu(hOut, menu, NR(menu),_index);
int ret=get_userinput(&_index, NR(menu));
if(ret==ESC)
{
break;
}
else if(ret==ENTER)
{
switch(_index)
{
case 0:
{
if(count==0)
{
ps.X = 57;
ps.Y = 11;
SetConsoleCursorPosition(hOut, ps);
printf("error!No students\n");
}
else
{
stu = _max(students, count);
ps.X = 57;
ps.Y = 11;
SetConsoleCursorPosition(hOut, ps);
printf("最高是第%2s号%4s 成绩:%2d\n", stu.xuehao, stu.name, stu.grade);
}
break;
}
case 1:
{
if(count==0)
{
ps.X = 57;
ps.Y = 12;
SetConsoleCursorPosition(hOut, ps);
printf("error!No students\n");
}
else
{
srr = _min(students, count);
ps.X = 57;
ps.Y = 12;
SetConsoleCursorPosition(hOut, ps);
printf("最低是第%2s号%4s 成绩:%2d\n", srr.xuehao, srr.name, srr.grade);
}
break;
}
case 2:
{
if(count==0)
{
ps.X = 57;
ps.Y = 13;
SetConsoleCursorPosition(hOut, ps);
printf("error!No students\n");
}
else
{
_average = average(students, count);
ps.X = 57;
ps.Y = 13;
SetConsoleCursorPosition(hOut, ps);
printf("平均分:%3.2lf", _average);
}
break;
}
case 3:
{
paixv(students,count);
print(students, count, hOut);
break;
}
case 4:
{
ps.X = 57;
ps.Y = 15;
SetConsoleCursorPosition(hOut, ps);
printf("please input your 'name' or 'student number':");
ps.Y++;
SetConsoleCursorPosition(hOut,ps);
printf(" ");
SetConsoleCursorPosition(hOut,ps);
printf("%c",' ');
while(scanf("%s", charname)==1)
{
ps.X = 57;
ps.Y = 15;
mer = findname(students, count, charname);
ps.Y++;
if(mer.name!=NULL)
{
SetConsoleCursorPosition(hOut, ps);
printf("姓名:%s 学号:%s 成绩:%d ", mer.name, mer.xuehao, mer.grade);
}
else
{
SetConsoleCursorPosition(hOut, ps);
printf("no find! please enter to try again! ");
}
break;
}
}//case 4:
}//switch(bool)
}//else if(bool)
}//while(true)
system("cls");
break;
}
case 2:
exit(0);
}
}
}
}
//更改index,返回键值
int get_userinput(int *index, int size)//size=功能子菜单的字符串数目
{
int ch = _getch();
switch (ch)
{
//上
case UP: if (*index > 0) *index -= 1; break;
//下
case DOWN:if (*index < size - 1) *index += 1; break;
//回车
case ENTER:
return ENTER;
break;
//ESC
case ESC:
return ESC;
break;
}
return ENTER || ESC;
}
//利用index显示菜单
void showmenu(HANDLE hOut, char **menu, int size, int index)
{ //设置颜色
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED));
pos.X = 38;
pos.Y = 2;
SetConsoleCursorPosition(hOut, pos);//设置位置
printf("%s", TITLE);
pos.X = 38;
pos.Y = 3;//显示菜单的非活动部分
SetConsoleCursorPosition(hOut, pos);
printf("%s", _MSDFER);
//while(true),即每次按键循环size次。
for (int i = 0; i < size; i++)
{
if (i == index)
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED));//红前景,白背景
pos.X = 40;
pos.Y = 4 + i; //用特殊颜色打印index对应的字符串
SetConsoleCursorPosition(hOut, pos);
printf("%s", menu[i]);
}
else
{ //黑前景,白背景
SetConsoleTextAttribute(hOut, (FOREGROUND_RED & FOREGROUND_GREEN & FOREGROUND_BLUE) | (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED));
pos.X = 40;
pos.Y = 4 + i;//把非index对应的还原普通色
SetConsoleCursorPosition(hOut, pos);
printf("%s", menu[i]);
}
}
fflush(stdout);
//流是一种理想化输出输入所映射的数据流,每次调用_getch(),使每次按键while循环调用一次fflush()改变指明字符串的颜色
}
//最大值函数
struct student _max(struct student *stu, int len)
{
struct student _max = stu[0];
for (int i = 0; i < len; i++)
{
if (stu[i].grade > _max.grade)
{
_max = stu[i];
}
}
return _max;
}
//最小值函数
struct student _min(struct student *stu, int len)
{
struct student _min = stu[0];
for (int i = 0; i < len; i++)
{
if (stu[i].grade < _min.grade)
{
_min = stu[i];
}
}
return _min;
}
//排序结构体数组冒泡算法
void paixv(struct student *students, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
if (students[i].grade > students[j].grade)
{
struct student stu = students[i];
students[i] = students[j];
students[j] = stu;
}
}
}
}
//求某个区间的成绩的学生数目
int counter_stu(struct student *students, int len, int floor, int last)
{
int counter_stu = 0;
for (int i = 0; i < len; i++)
{
if (students[i].grade<floor && students[i].grade>last)
counter_stu++;
}
return counter_stu;
}
//求平均数
double average(struct student *students, int len)
{
double ave;
int zonghe = 0;
for (int i = 0; i < len; i++)
{
zonghe = students[i].grade + zonghe;
}
ave = zonghe / len;
return ave;
}
//实现查找名字与分数
struct student findname(struct student *students, int len, char *dname)
{
struct student snu;
snu.name=NULL;
for (int i = 0; i < len; i++)
{
if (strstr(students[i].name, dname) != NULL)
//sonst char *str2 strstr(const *str1 ,const *sonstr2);
snu = students[i];
}
for (int g = 0; g < len; g++)
{
if(strcmp(students[g].xuehao, dname) == 0)
snu=students[g];
}
return snu;
}
//实现查找名字与分数,不过返回整数mirrorcount
int findcount(struct student *students, int len, char *dname)
{
int sencount=_alpha;
for (int i = 0; i < len; i++)
{
if (strstr(students[i].name, dname) != NULL)
//sonst char *str2 strstr(const *str1 ,const *sonstr2);
sencount = i;
}
for (int g = 0; g < len; g++)
{
if (strcmp(students[g].xuehao, dname) == 0)
sencount = g;
}
return sencount;
}
//打印结构数组
void print(struct student *students, int len, HANDLE hOut)
{
pose.Y = 0;
SetConsoleCursorPosition(hOut, pose);
for (int i = 0; i < len; i++)
{
printf("学号: %2s 姓名: %4s 成绩: %3d\n", students[i].xuehao, students[i].name, students[i].grade);
}
}
//成绩添加
void addstustream(struct student *pst,int *count,HANDLE h)
{
COORD ps;
char temp[SLEN];
h=GetStdHandle(STD_OUTPUT_HANDLE);//HANDLE h获取句柄
ps.X = 57;
ps.Y = 15;
SetConsoleCursorPosition(h, ps);
printf("Please enter the student\'s name! ");
ps.X=57;
ps.Y=16;
SetConsoleCursorPosition(h, ps);
printf("%c",' ');
scanf("%s",temp);
pst->name=(char *)malloc(sizeof(char)*(strlen(temp)+1));
strcpy(pst->name,temp);
ps.X = 57;
ps.Y = 16;
SetConsoleCursorPosition(h, ps);
printf("Now enter the student\'s number! ");
ps.X=57;
ps.Y=17;
SetConsoleCursorPosition(h, ps);
printf("%c",' ');
pst->xuehao=(char *)malloc(sizeof(char)*(strlen(temp)+1));
scanf("%s",temp);
strcpy(pst->xuehao,temp);
ps.X = 57;
ps.Y = 17;
SetConsoleCursorPosition(h, ps);
printf("Now enter the student\'s grade! ");
ps.X=57;
ps.Y=18;
SetConsoleCursorPosition(h, ps);
printf("%c",' ');
scanf("%d",&pst->grade);
cleartext(57,15,19,63);
ps.X=57;
ps.Y = 15;
SetConsoleCursorPosition(h, ps);
printf("Finished! or chice an amount to continue add grades!");
}
//无用清除文字区域
static bool cleartext(int x,int y,int ym,int anmont)
{
pos.X=x;
pos.Y=y;
HANDLE hu=GetStdHandle(STD_OUTPUT_HANDLE);
for(int coedect=y;coedect<ym;coedect++)
{
for(int code=0;code<anmont;code++,pos.X+=1)
{
SetConsoleCursorPosition(hu,pos);
printf("%c",' ');
}
pos.X=x;
pos.Y++;
}
return true;
}
//创建桌面文件students.txt
void destopshow(const struct student *students,int count)
{
FILE *fp;
fp=fopen("C:\\Users\\Administrator\\Desktop\\students.txt","w+");
rewind(fp);
for(int i=0;i<count;i++)
{
fprintf(fp,"%s %s %d\n",students[i].name,students[i].xuehao,students[i].grade);
}
fclose(fp);
} |
-
上一篇: 今日一天!驱动下的进程枚举下一篇: 用VC6绘制心型
|