接下来几篇文章是对前面C语言实战项目学校管理系统的实现
- 这篇实现学生逻辑
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getch.h>
#include "student.h"
#include "teacher.h"
#include "system.h"
int Student_on_len = 0;
int Student_off_len = 0;
Student* G_student = NULL;
Student* G_student_off = NULL;
Student* G_student_cp = NULL;
static int code(int);
static double S_score[9];
char Plaintext[50]={};
void encryption(int choose,char* scc_secret)
{
int i = 0, devi = 2;
int count=0;
while(1)
{
if(choose==1)
{
count=strlen(scc_secret);
for(i=0;i<count;i++)
{
Plaintext[i]=scc_secret[i]+i+devi;
}
Plaintext[i]='\0';
}
break;
}
}
void first_code(int k, char* key)
{
int num = 0;
char secret[25] = {};
while(k==0)
{
printf("请输入新密码\n");
while(1)
{
fgets_t(secret,25);
num = strlen(secret);
if(num>11 || num<3)
{
printf("请重新输入3-11位密码:\n");
}
else
{
break;
}
}
encryption(1,secret);
strcpy(key,Plaintext);
k = strcmp("123",secret);
}
}
int sort(double score, int k)
{
int s = 1;
for(int i = 0; i<Student_on_len+Student_off_len; i++)
{
if(G_student[i].score[k]>score && G_student[i].id)
{
s++;
}
}
return s;
}
int score_s(void)
{
double sum[3]={},max[3]= {},min[3]={200,200,200};
int k = 0;
for(int i=0;i<Student_on_len+Student_off_len;i++)
{
for(int j=0; j<3; j++)
{
if(G_student[i].score[j]>max[j] && G_student[i].id)
{
max[j] = G_student[i].score[j];
}
if(G_student[i].score[j]<min[j] && G_student[i].id)
{
min[j] = G_student[i].score[j];
}
sum[j] += G_student[i].score[j];
}
if(G_student[i].id) k++;
}
for(int i=0;i<3;i++)
{
sum[i] /= k;
}
S_score[0]=sum[0],S_score[1]=sum[1],S_score[2]=sum[2];
S_score[3]=max[0],S_score[4]=max[1],S_score[5]=max[2];
S_score[6]=min[0],S_score[7]=min[1],S_score[8]=min[2];
FILE* fwp = fopen("sort_score","w");
if(fwp == NULL)
{
printf("系统繁忙,请稍后..\n");
return 1;
}
fprintf(fwp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf",
S_score[0],S_score[1],S_score[2],S_score[3],S_score[4],S_score[5],S_score[6],S_score[7],S_score[8]);
fclose(fwp);
fwp = NULL;
return 0;
}
void cp_student(void)
{
G_student_cp = (Student*)calloc(sizeof(Student),Student_on_len);
int i = 0,cnt = 0;
for(i=0;i<STU_ON_MAX;i++)
{
if(G_student[i].id)
{
memcpy(&G_student_cp[cnt],&G_student[i],sizeof(Student));
cnt++;
}
if(cnt == Student_on_len)
{
break;
}
}
}
int compare1(const void *s1, const void *s2)
{
Student* ss1 = (Student*)s1;
Student* ss2 = (Student*)s2;
if(ss1->score[0]>ss2->score[0])
{
return -1;
}else if(ss1->score[0]<ss2->score[0])
{
return 1;
}else
{
return 0;
}
}
int compare2(const void *s1, const void *s2)
{
Student* ss1 = (Student*)s1;
Student* ss2 = (Student*)s2;
if(ss1->score[1]>ss2->score[1])
{
return -1;
}else if(ss1->score[1]<ss2->score[1])
{
return 1;
}else
{
return 0;
}
}
int compare3(const void *s1, const void *s2)
{
Student* ss1 = (Student*)s1;
Student* ss2 = (Student*)s2;
if(ss1->score[2]>ss2->score[2])
{
return -1;
}else if(ss1->score[2]<ss2->score[2])
{
return 1;
}else
{
return 0;
}
}
void qsort_score(int i)
{
if(i != 0 && i!=1&&i!=2)
{
return;
}
if(i==0)
{
qsort(G_student_cp,Student_on_len,sizeof(Student),compare1);
}
if(i==1)
{
qsort(G_student_cp,Student_on_len,sizeof(Student),compare2);
}
if(i==2)
{
qsort(G_student_cp,Student_on_len,sizeof(Student),compare3);
}
}
void show_score(int std_id)
{
system("clear");
int fail_f = score_s();
if(fail_f)
{
return;
}
double stu_score[9] = {};
FILE* frp = fopen("sort_score","r");
if(frp == NULL)
{
printf("系统繁忙,请稍后..\n");
return;
}
fscanf(frp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf",&stu_score[0],&stu_score[1],&stu_score[2],
&stu_score[3],&stu_score[4],&stu_score[5],&stu_score[6],&stu_score[7],&stu_score[8]);
printf("\t姓名:%s\t学号:%d\n",G_student[std_id].name,G_student[std_id].id);
printf("语文:%.1lf 排名:%d 科目平均分:%.1lf 最高分:%.1lf 最低分:%.1lf\n",
G_student[std_id].score[0],sort(G_student[std_id].score[0],0),stu_score[0],
stu_score[3],stu_score[6]);
printf("数学:%.1lf 排名:%d 科目平均分:%.1lf 最高分:%.1lf 最低分:%.1lf\n",
G_student[std_id].score[1],sort(G_student[std_id].score[1],1),stu_score[1],
stu_score[4],stu_score[7]);
printf("英语:%.1lf 排名:%d 科目平均分:%.1lf 最高分:%.1lf 最低分:%.1lf\n",
G_student[std_id].score[2],sort(G_student[std_id].score[2],2),stu_score[2],
stu_score[5],stu_score[8]);
fclose(frp);
frp = NULL;
printf("输入任意键继续..");
BUFF_CLEAR;
getchar();
}
void show_score1()
{
cp_student();
int choose = 0;
printf("0.语文 1.数学 2.英语\n");
printf("请输入你想看的科目:");
int num = 0;
for(;;)
{
num = scanf("%d", &choose);
if(num == 0 || (choose != 0 && choose != 1 && choose != 2))
{
BUFF_CLEAR;
printf("输入违法,请重试\n");
}
else
{
break;
}
}
qsort_score(choose);
system("clear");
for(int i=0;i<Student_on_len;i++)
{
printf("\t%.1lf\n", G_student_cp[i].score[choose]);
}
free(G_student_cp);
G_student_cp = NULL;
BUFF_CLEAR;
printf("输入任意键继续..");
getchar();
}
void show_mes(int std_id)
{
system("clear");
printf("姓名: %s\n性别: %c\n学号:%d\n",
G_student[std_id].name,G_student[std_id].sex,G_student[std_id].id);
printf("输入任意键继续..");
BUFF_CLEAR;
getchar();
}
void change_sec(int std_id)
{
system("clear");
first_code(0,G_student[std_id].secret);
printf("修改成功!\n");
printf("输入任意键继续..");
getchar();
}
void main_student(int stu_id)
{
system("clear");
int std_id = find_student(stu_id);
if(code(std_id))
{
return;
}
int choice = 0;
while(1)
{
BUFF_CLEAR;
system("clear");
printf("--------欢迎进入学生系统--------\n\n");
printf("请选择您需要的功能\n");
printf("1.查看个人信息 2.查看个人成绩\n");
printf("3.修改密码 4.查看详细成绩\n");
printf("0.退出系统\n");
while(1 != scanf("%d", &choice))
{
BUFF_CLEAR;
printf("输入非法!请重新选择\n");
}
switch(choice)
{
case 1:
show_mes(std_id);
break;
case 2:
show_score(std_id);
break;
case 3:
change_sec(std_id);
break;
case 4:
show_score1();
break;
case 0:
return;
default:
printf("无效指令,请重新输入\n");
sleep(2);
}
}
}
static int code(int std_id)
{
system("clear");
if(std_id == -1)
{
printf("没有此学生!\n");
sleep(3);
return 1;
}
int k = strcmp("123",G_student[std_id].secret);
if(k==0)
{
first_code(k,G_student[std_id].secret);
printf("修改成功!\n");
sleep(2);
}
if(G_student[std_id].flag == 3 )
{
printf("您的帐号已锁定,请联系老师解锁!\n");
sleep(3);
return 1;
}
system("clear");
if(0 == landing(G_student[std_id].secret,&G_student[std_id].flag))
{
printf("您的帐号已锁定,请联系老师解锁!\n");
sleep(3);
return 1;
}
return 0;
}