前言
一位地大的朋友面臨一個複雜的課設找我尋求幫助,而我呢,面臨期末,壓力巨大,一邊複習一邊帶着做,想着做完發篇博客。終於,昨天考完了最後一門,熬夜把項目給寫了出來,但是還需要很大的優化,因爲時間倉促,基本功能已經可以體現。
題目
已知文本文件f1.txt中存放了某校若干名大一學生的相關信息(不超過5000人),具體數據及存放格式爲:每行存放一個學生的數據,數據之間用空格分隔,共有11項,依次爲:學號、姓名、專業編號、通識教育課一、通識教育課二、通識教育課三、公共基礎課一、公共基礎課二、公共基礎課三、專業主幹課一、專業主幹課二等8門課程的成績。其中,學號爲5位數字字符;姓名爲長度不超過10個的字符;專業編號爲二位數字字符,編號從01-50;所有課程成績均爲0-100的整數。(具體功能見附件)
相關資料放入附件裏。
代碼
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 5000 //總學生數
#define M 50 //專業
struct student
{
char num[6];
char name[20];
char major[3];
int score1;
int score2;
int score3;
int score4;
int score5;
int score6;
int score7;
int score8;
int total;
bool estu1; //是否是校級優秀學生
bool estu2; //是否是專業優秀學生
};
struct student st[N];
char majorinfo[M][3] = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50"};
int nummajor[M] = {0};
void mysort() //按總評成績冒泡排序
{
bool change;
int i,j;
change=true;
for(i=0;i<N&&change;++i)
{
change=false;
for(j=0;j<N;++j)
{
if(st[i].total > st[j].total)
{
struct student top = st[i];
st[i] = st[j];
st[j] = top;
change=true;
}
}
}
}
//校級優秀學生
void excellent1(){
int i=0,j=0;
FILE *fp2;
if((fp2=fopen("f2.txt","w"))==NULL)
{
printf("數據有誤!");
}else{
while(j<N*0.05){
if(st[i].score1>=60&&st[i].score2>=60&&st[i].score3>=60&&st[i].score4>=70&&st[i].score5>=70&&st[i].score6>=70&&st[i].score7>=80&&st[i].score8>=80){
st[i].estu1 = true;
fprintf(fp2,"%s %s %s %d %d %d %d %d %d %d %d %d\n",st[i].num,st[i].name,st[i].major,st[i].score1,st[i].score2,st[i].score3,st[i].score4,st[i].score5,st[i].score6,st[i].score7,st[i].score8,st[i].total);
j++;
}
i++;
}
}
printf("校級優秀學生信息已放入f2.txt中!\n");
fclose(fp2);
}
int main(){
printf("請喝杯茶等候片刻~~");
FILE *fp;
int i=0,j,max=0,k,l=0;
int arr[M]={0}; //存放各專業獲得校級優秀學生的人數
int brr[M]={0}; //專業優秀的人數
int crr[M]={0}; //專業校級優秀學生人數
int drr[M]={0}; //專業不具有資格的
int err[8]={0}; //分析
if((fp=fopen("f1.txt","r"))==NULL){
printf("查詢不到數據!");
}else{
while(!feof(fp)){
fscanf(fp,"%s%s%s%d%d%d%d%d%d%d%d",st[i].num,st[i].name,st[i].major,&st[i].score1,&st[i].score2,&st[i].score3,&st[i].score4,&st[i].score5,&st[i].score6,&st[i].score7,&st[i].score8);
st[i].total = (int)(0.8*(st[i].score4+st[i].score5+st[i].score6)+1.2*(st[i].score7+st[i].score8));
for(j=0;j<M;j++){
if(strcmp(majorinfo[j],st[i].major)==0){
nummajor[j]++;
}
}
i++;
}
}
mysort();
system("cls");
FILE *fp1; //獲取s1
if((fp1=fopen("s1.txt","w"))==NULL)
{
printf("數據有誤!");
}else{
for(i=0;strlen(st[i].name)!=0;i++) //沒有考分的部分要標註防止錄不了數據
{
fprintf(fp1,"%s %s %s %d %d %d %d %d %d %d %d %d\n",st[i].num,st[i].name,st[i].major,st[i].score1,st[i].score2,st[i].score3,st[i].score4,st[i].score5,st[i].score6,st[i].score7,st[i].score8,st[i].total);
}
}
printf("排序後的總成績表已放入s1.txt中!\n");
struct student maj[200]; //專業符合排名的學生
for(i=0;i<M;i++){
k=0;
for(j=0;j<N;j++){
if(strcmp(majorinfo[i],st[j].major)==0){
if(st[j].score1>=60&&st[j].score2>=60&&st[j].score3>=60&&st[j].score4>=70&&st[j].score5>=70&&st[j].score6>=70&&st[j].score7>=80&&st[j].score8>=80){
maj[k]=st[j];
k++;
}
}
}
brr[i]=k;
max=0;
for(l=0;l<N;l++){
if(max<(int)((double)k*0.1+0.5)){
if(strcmp(majorinfo[i],st[l].major)==0){
if(st[l].score1>=60&&st[l].score2>=60&&st[l].score3>=60&&st[l].score4>=70&&st[l].score5>=70&&st[l].score6>=70&&st[l].score7>=80&&st[l].score8>=80){
st[l].estu2 = true;
max++;
}
}
}
}
l=0;
FILE *fp3;
if((fp3=fopen("f3.txt","a"))==NULL)
{
printf("數據有誤!");
}else{
while(l<(int)((double)k*0.1+0.5)){ //沒有考分的部分要標註防止錄不了數據
fprintf(fp3,"%s %s %s %d %d %d %d %d %d %d %d %d\n",maj[l].num,maj[l].name,maj[l].major,maj[l].score1,maj[l].score2,maj[l].score3,maj[l].score4,maj[l].score5,maj[l].score6,maj[l].score7,maj[l].score8,maj[l].total);
l++;
}
}
fclose(fp3);
}
printf("專業優秀學生信息已放入f3.txt中!\n");
excellent1(); //校級優秀學生
fclose(fp);
fclose(fp1);
int n;
bool truth = true;
//功能部分
while(truth){
printf("請選擇您的操作:\n");
printf("(1) 生成校級優秀學生的評優成績。\n");
printf("(2) 生成各個專業的專業優秀學生的評優成績。\n");
printf("(3) 統計各個專業校級優秀學生的人數。\n");
printf("(4) 統計沒有校級優秀學生的專業信息。\n");
printf("(5) 統計校級優秀學生人數最多的專業信息。\n");
printf("(6) 統計各專業不具備評優資格的人數。\n");
printf("(7) 統計不具備評優資格人數最多的專業信息。\n");
printf("(8) 統計各個專業優秀學生的人數。\n");
printf("(9) 統計各個專業優秀學生的信息。\n");
printf("(10) 根據學生輸入的學號及姓名,查詢本人的評優結果。\n(如果輸入的學號與姓名不匹配,提示錯誤信息。如果只輸入一項信息,禁止查詢。)\n");
printf("(11) 分析出不具備校級優秀評選資格的各因素所點的比重以及最主要原因。\n");
printf("(12) 退出程序。\n\n\n");
printf("輸入數字序號:");
scanf("%d",&n);
if(n<=12&&n>=1){
switch(n){
case 1:
i=0,j=0;
FILE *fp2;
if((fp2=fopen("option1.txt","w"))==NULL)
{
printf("數據有誤!");
}else{
while(j<N*0.05){
if(st[i].score1>=60&&st[i].score2>=60&&st[i].score3>=60&&st[i].score4>=70&&st[i].score5>=70&&st[i].score6>=70&&st[i].score7>=80&&st[i].score8>=80){
fprintf(fp2,"學號:%s 姓名:%s 專業:%s 評優成績:%d\n",st[i].num,st[i].name,st[i].major,st[i].total);
j++;
}
i++;
}
}
fclose(fp2);
printf("數據已放入option1.txt中!\n");
printf("---------------------------\n\n\n\n");
break;
case 2:
FILE *fp5;
if((fp5=fopen("option2.txt","w"))==NULL)
{
printf("數據有誤!");
}else{
for(i=0;i<M;i++){
for(j=0;j<N;j++){
if(strcmp(majorinfo[i],st[j].major)==0){
if(st[j].estu2==true){
fprintf(fp5,"學號:%s 姓名:%s 專業:%s 評優成績:%d\n",st[j].num,st[j].name,st[j].major,st[j].total);
}
}
}
}
}
fclose(fp5);
printf("專業優秀學生的評優成績已放入option2.txt中!\n");
printf("---------------------------\n\n\n\n");
break;
case 3:
i=0,j=0;
while(j<N*0.05){
if(st[i].score1>=60&&st[i].score2>=60&&st[i].score3>=60&&st[i].score4>=70&&st[i].score5>=70&&st[i].score6>=70&&st[i].score7>=80&&st[i].score8>=80){
for(k=0;k<M;k++){
if(strcmp(st[i].major,majorinfo[k])==0){
arr[k]++;
}
}
j++;
}
i++;
}
printf("各個專業校級優秀學生的人數分別爲:\n");
for(i=0;i<M;i++){
printf("專業%d的人數爲:%d\n",i+1,arr[i]);
}
printf("---------------------------\n\n\n\n");
break;
case 4:
i=0;
FILE *fp4;
if((fp4=fopen("option4.txt","a"))==NULL)
{
printf("數據有誤!");
}else{
for(i=0;strlen(st[i].name)!=0;i++){
if(st[i].estu1==false){
fprintf(fp4,"學號:%s 姓名:%s 專業:%s 評優成績:%d\n",st[i].num,st[i].name,st[i].major,st[i].total);
}
}
}
fclose(fp4);
printf("統計成功!數據已放入option4.txt!\n");
printf("---------------------------\n\n\n\n");
break;
case 5:
for(i=0;i<M;i++){
for(j=0;j<N;j++){
if(strcmp(majorinfo[i],st[j].major)==0){
if(st[j].estu1==true){
crr[i]++;
}
}
}
}
for(i=0;i<M;i++){
j= j+crr[i];
printf("專業%d的校級優秀學生數爲:%d\n",i+1,crr[i]);
}
printf("---------------------------\n\n\n\n");
break;
case 6:
for(j=0;j<M;j++){
for(i=0;i<N;i++){
if(strcmp(majorinfo[j],st[i].major)==0){
if(!(st[i].score1>=60&&st[i].score2>=60&&st[i].score3>=60&&st[i].score4>=70&&st[i].score5>=70&&st[i].score6>=70&&st[i].score7>=80&&st[i].score8>=80)){
drr[j]++;
}
}
}
}
for(i=0;i<M;i++){
printf("%d專業有%d人沒有評選資格;\n",i+1,drr[i]);
}
printf("---------------------------\n\n\n\n");
break;
case 7:
for(j=0;j<M;j++){
for(i=0;i<N;i++){
if(strcmp(majorinfo[j],st[i].major)==0){
if(!(st[i].score1>=60&&st[i].score2>=60&&st[i].score3>=60&&st[i].score4>=70&&st[i].score5>=70&&st[i].score6>=70&&st[i].score7>=80&&st[i].score8>=80)){
drr[j]++;
}
}
}
}
j=0,l=0;
for(i=0;i<M;i++){
if(j<drr[i]){
j=drr[i];
l=i;
}
}
printf("不具備評優資格人數最多的專業爲:%d,有%d人。\n",l+1,drr[l]);
printf("---------------------------\n\n\n\n");
printf("---------------------------\n\n\n\n");
break;
case 8:
for(i=0;i<M;i++){
printf("%d專業有%d個專業優秀學生;\n",i+1,brr[i]);
}
printf("---------------------------\n\n\n\n");
break;
case 9:
FILE *fp6;
if((fp6=fopen("option9.txt","a"))==NULL)
{
printf("數據有誤!");
}else{
for(j=0;j<M;j++){
for(i=0;i<N;i++){
if(strcmp(majorinfo[j],st[i].major)==0){
if(st[i].estu2==true){
fprintf(fp6,"學號:%s 姓名:%s 專業:%s 評優成績:%d\n",st[i].num,st[i].name,st[i].major,st[i].total);
}
}
}
}
}
fclose(fp6);
printf("各個專業優秀學生的信息已放入option9.txt中!\n");
printf("---------------------------\n\n\n\n");
break;
case 10:
char name0[20];
char num0[6];
printf("請輸入要查詢的姓名:");
scanf("%s",&name0);
printf("請輸入要查詢的學號:");
scanf("%s",&num0);
bool sured;
for(i=0;i<N;i++){
if(!strcmp(st[i].num,num0)&&!strcmp(st[i].name,name0)){
if(st[i].estu1==true&&st[i].estu2==false){
printf("%s同學是校級優秀學生!\n",st[i].name);
sured = true;
}
else if(st[i].estu1==false&&st[i].estu2==true){
printf("%s同學是專業優秀學生!\n",st[i].name);
sured = true;
}
else if(st[i].estu1==true&&st[i].estu2==true){
printf("%s同學既是校級優秀學生,又是專業優秀學生!\n",st[i].name);
sured = true;
}
}
}
if(sured==false){
printf("輸入信息錯誤!\n");
}
printf("---------------------------\n\n\n\n");
break;
case 11:
for(i=0;strlen(st[i].name)!=0;i++){
if(st[i].score1<60){
err[0]++;
}
if(st[i].score2<60){
err[1]++;
}
if(st[i].score3<60)
{
err[2]++;
}
if(st[i].score4<70)
{
err[3]++;
}
if(st[i].score5<70)
{
err[4]++;
}
if(st[i].score6<70)
{
err[5]++;
}
if(st[i].score7<80)
{
err[6]++;
}
if(st[i].score8<80)
{
err[7]++;
}
}
int m1,m2,m3,h; //三種課的平均值
m1=(err[0]+err[1]+err[2])/3;
m2=(err[3]+err[4]+err[5])/3;
m3=(err[6]+err[7])/2;
if(m1>m2){
if(m1>m3){
printf("通識教育課程掛科率太高,需要加強通識教育。\n");
}else if(m1<m3){
printf("專業主幹課程掛科率太高,需要加強專業課教育。\n");
}
}else if(m1<m2){
if(m2>m3){
printf("公共基礎課掛科率太高,需要加強公共教育。\n");
}else if(m2<m3){
printf("專業主幹課程掛科率太高,需要加強專業課教育。\n");
}
}
printf("輸入1查看各課程掛科數,退出請輸入0!\n");
scanf("%d",&h);
if(h==1){
for(i=0;i<8;i++){
printf("第%d門課的掛科數:%d\n",i+1,err[i]);
}
}else if(h==0){
break;
}
printf("---------------------------\n\n\n\n");
break;
case 12:
truth = false;
printf("已退出程序,請關閉窗口!");
break;
}
}else{
printf("請輸入正確的數字!");
}
}
return 0;
}
效果演示