C語言進階
(一)前言:
在學習c語言基礎語法後我們需要通過項目的訓練進行進階提升,一個小的項目能夠使初學者對編程語言掌握的更牢固、進一步熟悉編程的邏輯,所以在這裏例出了項目的題目要求、算法分析以及完整的程序代碼供初學者學習。
(二)題目要求:學生成績統計系統實現
設計結構體數組,結構中包含學生數據爲:學號、姓名、物理分數、數學分數、外語分數、計算機分數。
設計各個函數,分別實現以下功能:
(1)錄入:輸入學生數據
(2)顯示:所有學生信息
(3)統計:統計每科的最高分、最低分;輸出不及格人數、不及格學生數據。
設計菜單,通過選擇菜單調用以上各函數。
(三)算法設計:
設計結構體數組,結構中包含學生數據爲:學號、姓名、物理分數、數學分數、外語分數、計算機分數。
設計各個函數,分別實現以下功能:
(1)錄入:輸入學生數據
(2)顯示:所有學生信息
(3)統計:統計每科的最高分、最低分;輸出不及格人數、不及格學生數據。
設計菜單,通過選擇菜單調用以上各函數
<1>介紹每個函數功能:
int main():運用switch選擇,調用所需要實現相應功能的函數。
int choose_carte():顯示選擇菜單,讓使用者對系統的功能有更好的瞭解和操作。
int Intput(struct Student stu[],int n):實現對學生成員的數據輸入。
int Output(struct Student stu[],int n):實現對已輸入學生成員的輸出。
void Data(struct Student stu[],int n):實現判斷對已輸入學生成員的各科最高分和最低分,還有不及格學生的顯示。
(四)完整代碼實現
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
# include<ctype.h>
struct Student #自定義結構體
{
int number;
char name[10];
int phy;
int math;
int eng;
int com;
}stu[40];
int choose_carte() #菜單顯示
{ int c;
do{ system("cls");
printf("\t\t*********請選擇該學生成績統計系統所實現功能*********\n");
printf("\t\t*1.錄入:輸入學生數據 *\n");
printf("\t\t*2.顯示:顯示學生數據 *\n");
printf("\t\t*3.統計:統計最高分,最低分 *\n");
printf("\t\t*0.退出 *\n");
printf("\t\t****************************************************\n");
printf("\t\t\tGive you Choice(0-3):");
scanf("%d",&c);
}while(c<0||c>3);
return c;
}
int Intput(struct Student stu[],int n) #輸入學生信息
{
int i=0;
char c;
do
{
printf("\t\t\t學號:");
scanf("\t\t\t%d",&stu[n+i].number);
printf("\t\t\t姓名:");
scanf("\t\t\t%s",&stu[n+i].name);
printf("\t\t\t物理分數:");
scanf("\t\t\t%d",&stu[n+i].phy);
printf("\t\t\t數學分數:");
scanf("\t\t\t%d",&stu[n+i].math);
printf("\t\t\t外語分數:");
scanf("\t\t\t%d",&stu[n+i].eng);
printf("\t\t\t計算機分數:");
scanf("\t\t\t%d",&stu[n+i].com);
i++;
printf("\t\t\t您還要繼續錄入成績嗎?\n");
printf("\t\t\t請輸入‘y’繼續,或'n'停止");
scanf("\t\t\t%c",&c);
}while(c=='y');
return (i+n);
}
int Output(struct Student stu[],int n) #輸出學生信息
{
int i;
printf("\t\t----------------------------------------------------\n");
printf("\t\t| Number | Name | Phy | Math | Eng | Com | \n");
printf("\t\t----------------------------------------------------\n");
for( i=1;i<n+1;i++)
{
printf("\t\t%4d%10s%9d%9d%9d%9d\n",stu[i-1].number,stu[i-1].name,stu[i-1].phy,stu[i-1].math,stu[i-1].eng,stu[i-1].com);
printf("\t\t----------------------------------------------------\n");
}
system("pause");
}
void Data(struct Student stu[],int n) #統計
{
int i;
int sum=0;
int phy_hight=0;
int math_hight=0;
int eng_hight=0;
int com_hight=0;
int phy_low=0;
int math_low=0;
int eng_low=0;
int com_low=0;
for(i=0;i<n;i++)
{
if(stu[phy_hight].phy<stu[i].phy)
phy_hight=i;
if(stu[phy_low].phy>stu[i].phy)
phy_low=i;
if(stu[i].phy<60)
printf("\t\t物理不及格的%4d%10s%9d\n",stu[i].number,stu[i].name,stu[i].phy);
if(stu[math_hight].math<stu[i].math)
math_hight=i;
if(stu[math_low].math>stu[i].math)
math_low=i;
if(stu[i].math<60)
printf("\t\t數學不及格的%4d%10s%9d\n",stu[i].number,stu[i].name,stu[i].math);
if(stu[eng_hight].eng<stu[i].eng)
eng_hight=i;
if(stu[eng_low].eng>stu[i].eng)
eng_low=i;
if(stu[i].eng<60)
printf("\t\t英語不及格的%4d%10s%9d\n",stu[i].number,stu[i].name,stu[i].eng);
if(stu[com_hight].com<stu[i].com)
com_hight=i;
if(stu[com_low].com>stu[i].com)
com_low=i;
if(stu[i].phy<60)
printf("\t\t計算機不及格的%4d%10s%9d\n",stu[i].number,stu[i].name,stu[i].com);
++sum;
}
printf("\t\t該數據中不及格的人數爲%d\n",sum);
system("pause");
printf("\t\t物理最高分爲:\n");
printf("\t\t%4d%10s%9d\n",stu[phy_hight].number,stu[phy_hight].name,stu[phy_hight].phy);
printf("\t\t數學最高分爲:\n");
printf("\t\t%4d%10s%9d\n",stu[math_hight].number,stu[math_hight].name,stu[math_hight].math);
printf("\t\t外語最高分爲:\n");
printf("\t\t%4d%10s%9d\n",stu[eng_hight].number,stu[eng_hight].name,stu[eng_hight].eng);
printf("\t\t計算機最高分爲:\n");
printf("\t\t%4d%10s%9d",stu[com_hight].number,stu[com_hight].name,stu[com_hight].com);
printf("\n\n\n");
printf("\t\t物理最低分爲:\n");
printf("\t\t%4d%10s%9d\n",stu[phy_low].number,stu[phy_low].name,stu[phy_low].phy);
printf("\t\t數學最低分低爲:\n");
printf("\t\t%4d%10s%9d\n",stu[math_low].number,stu[math_low].name,stu[math_low].math);
printf("\t\t外語最低分爲:\n");
printf("\t\t%4d%10s%9d\n",stu[eng_low].number,stu[eng_low].name,stu[eng_low].eng);
printf("\t\t計算機最低分爲:\n");
printf("\t\t%4d%10s%9d\n",stu[com_low].number,stu[com_low].name,stu[com_low].com);
system("pause");
}
void main()
{
int n=0;
do{
switch(choose_carte())
{
case 0:
break;
case 1:
printf("\t\t\t請輸入學生的個人信息\n");
n=Intput(stu,n);
break;
case 2:
printf("\t\t\t輸出所有學生的個人信息\n");
Output(stu,n);
break;
case 3:
Data(stu,n);
break;
}
}while(choose_carte()!=0);
}
(五)結果展示
(六)項目小結
要熟練掌握循環用來控制顯示面板和所要調用的函數,結構清晰有便於後期系統的調試和運行,要使用暫停函數system("pause");讓數據停留,綜合性非常高,也需要對每個數據非常嚴謹的編寫。