哈工大C語言程序設計精髓第十三週

由於這些代碼也是我初學時寫的代碼,故其中的規範程度及簡潔程度並不很好(此處我後來寫的有可以參考一下->C語言代碼規範),但是能很好的接近出初學者的水平,也更有參考價值!排版不易,喜歡就點個贊吧!如有問題,請勿吐槽,歡迎留言互相學習。

練兵區——編程題

  1. 學生成績管理系統V4.0
    題目內容

    某班有最多不超過30人(具體人數由鍵盤輸入)參加期末考試,最多不超過6門(具體門數由鍵盤輸入)。參考學生成績管理系統V3.0,用二維數組作函數參數編程實現如下菜單驅動的學生成績管理系統:
    (1)錄入每個學生的學號、姓名和各科考試成績;
    (2)計算每門課程的總分和平均分;
    (3)計算每個學生的總分和平均分;
    (4)按每個學生的總分由高到低排名名次表;
    (5)按每個學生的總分由低到高排出名次表;
    (6)按學號由小到大排出成績表;
    (7)按姓名的字典順序排出成績表;
    (8)按學號查詢學生排名及其各科考試成績;
    (9)按姓名查詢學生排名及其各科考試成績;
    (10)按優秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5個類別,對每門課程分別統計每個類別的人數以及所佔的百分比;
    (11)輸出每個學生的學號、姓名、各科考試成績、總分、平均分,以及每門課程的總分和平均分。
    要求程序運行後先顯示如下菜單,並提示用戶輸入選項:
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    然後,根據用戶輸入的選項執行相應的操作。
    請按照下面的定義及函數原型編程
#define   MAX_LEN  10                		/* 字符串最大長度 */
#define   STU_NUM 30                       /* 最多的學生人數 */
#define   COURSE_NUM 6                     /* 最多的考試科目數 */
int   Menu(void);
void  ReadScore(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], int n, int m);
void AverSumofEveryStudent(float score[][COURSE_NUM], int n, int m,
float  sum[STU_NUM], float aver[STU_NUM]);
void AverSumofEveryCourse(float score[][COURSE_NUM], int n, int m);
void  SortbyScore(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float  sum[], float aver[],
int n, int m, int (*compare)(float a, float b));
int   Ascending(float a, float b);
int   Descending(float a, float b);
void  SwapFloat(float *x, float *y);
void  SwapLong(long *x, long *y);
void  SwapChar(char x[], char y[]);
void  AsSortbyNum(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float  sum[], float aver[],
int n, int m);
void  SortbyName(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float  sum[], float aver[],
int n, int m);
void  SearchbyNum(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float  sum[], float aver[],
int n, int m);
void  SearchbyName(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float  sum[], float aver[],
int n, int m);
void  StatisticAnalysis(float score[][COURSE_NUM], int n, int m);
void  PrintScore(long num[], char name[][MAX_LEN],
float score[][COURSE_NUM], float  sum[], float aver[],int n, int m) ;
  1. 下面是程序運行示例:
    Input student number(n<30):
    6↙
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    1↙
    Input course number(m<=6):
    3↙
    Input student’s ID, name and score:
    11003001↙
    lisi↙
    87↙
    82↙
    89↙
    11003005↙
    heli↙
    98↙
    92↙
    90↙
    11003003↙
    ludi↙
    75↙
    78↙
    80↙
    11003002↙
    dumo↙
    48↙
    50↙
    67↙
    11003004↙
    zuma↙
    65↙
    69↙
    72↙
    11003006↙
    suyu↙
    100↙
    95↙
    94↙

    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    2↙
    course 1:sum=473,aver=79
    course 2:sum=466,aver=78
    course 3:sum=492,aver=82
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    3↙
    student 1:sum=258,aver=86
    student 2:sum=280,aver=93
    student 3:sum=233,aver=78
    student 4:sum=165,aver=55
    student 5:sum=206,aver=69
    student 6:sum=289,aver=96
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    4↙
    Sort in descending order by score:
    11003006 suyu 100 95 94 289 96
    11003005 heli 98 92 90 280 93
    11003001 lisi 87 82 89 258 86
    11003003 ludi 75 78 80 233 78
    11003004 zuma 65 69 72 206 69
    11003002 dumo 48 50 67 165 55
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    5↙
    Sort in ascending order by score:
    11003002 dumo 48 50 67 165 55
    11003004 zuma 65 69 72 206 69
    11003003 ludi 75 78 80 233 78
    11003001 lisi 87 82 89 258 86
    11003005 heli 98 92 90 280 93
    11003006 suyu 100 95 94 289 96
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    6↙
    Sort in ascending order by number:
    11003001 lisi 87 82 89 258 86
    11003002 dumo 48 50 67 165 55
    11003003 ludi 75 78 80 233 78
    11003004 zuma 65 69 72 206 69
    11003005 heli 98 92 90 280 93
    11003006 suyu 100 95 94 289 96
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    7↙
    Sort in dictionary order by name:
    11003002 dumo 48 50 67 165 55
    11003005 heli 98 92 90 280 93
    11003001 lisi 87 82 89 258 86
    11003003 ludi 75 78 80 233 78
    11003006 suyu 100 95 94 289 96
    11003004 zuma 65 69 72 206 69
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    8↙
    Input the number you want to search:
    11003007↙
    Not found!
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    8↙
    Input the number you want to search:
    11003004↙
    11003004 zuma 65 69 72 206 69
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    9↙
    Input the name you want to search:
    lili↙
    Not found!
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    9↙
    Input the name you want to search:
    lisi↙
    11003001 lisi 87 82 89 258 86
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    10↙
    For course 1:
    <60 1 16.67%
    60-69 1 16.67%
    70-79 1 16.67%
    80-89 1 16.67%
    90-99 1 16.67%
    100 1 16.67%
    For course 2:
    <60 1 16.67%
    60-69 1 16.67%
    70-79 1 16.67%
    80-89 1 16.67%
    90-99 2 33.33%
    100 0 0.00%
    For course 3:
    <60 0 0.00%
    60-69 1 16.67%
    70-79 1 16.67%
    80-89 2 33.33%
    90-99 2 33.33%
    100 0 0.00%
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    11↙
    11003002 dumo 48 50 67 165 55
    11003005 heli 98 92 90 280 93
    11003001 lisi 87 82 89 258 86
    11003003 ludi 75 78 80 233 78
    11003006 suyu 100 95 94 289 96
    11003004 zuma 65 69 72 206 69
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    12↙
    Input error!
    Management for Students’ scores
    1.Input record
    2.Caculate total and average score of every course
    3.Caculate total and average score of every student
    4.Sort in descending order by score
    5.Sort in ascending order by score
    6.Sort in ascending order by number
    7.Sort in dictionary order by name
    8.Search by number
    9.Search by name
    10.Statistic analysis
    11.List record
    0.Exit
    Please Input your choice:
    0↙
    End of program!
    輸入格式:
    ( 1 ) 錄入學生的人數:
    要求輸入數據格式爲:"%d"
    提示信息爲:“Input student number(n<30):\n”
    ( 2 )錄入課程數:
    要求輸入數據格式爲:"%d"
    提示信息爲:“Input course number(m<=%d):\n”
    ( 3 )錄入每個學生的學號、姓名和考試成績:
    要求學號、姓名的輸入數據格式爲:"%ld%s"
    要求考試成績的輸入數據格式爲:"%f"
    提示信息爲:“Input student’s ID, name and score:\n”
    輸出格式:
    計算每門課程的總分和平均分:
    要求輸出總分與平均分格式爲:“course %d:sum=%.0f,aver=%.0f\n”
    計算每個學生的總分和平均分:
    要求輸出總分與平均分格式爲:“student %d:sum=%.0f,aver=%.0f\n”
    按成績由高到低排出名次表:
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    提示信息爲:“Sort in descending order by score:\n”
    按成績由低到高排出名次表:
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    提示信息爲:“Sort in ascending order by score:\n”
    按學號由小到大排出成績表:
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    提示信息爲:“Sort in ascending order by number:\n”
    按姓名的字典順序排出成績表
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    提示信息爲:“Sort in dictionary order by name:\n”
    按學號查詢學生排名及其考試成績:
    如果未查到此學號的學生,提示信息爲:“Not found!\n”;
    如果查詢到該學生
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    提示信息爲:“Input the number you want to search:\n”
    按姓名查詢學生排名及其考試成績;
    如果未查到此學號的學生,提示信息爲:“Not found!\n”
    如果查詢到該學生
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    提示信息爲:“Input the name you want to search:\n”
    按優秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5個類別,統計每個類別的人數以及所佔的百分比:
    成績<60輸出提示格式爲:"<60\t%d\t%.2f%%\n";
    成績=100輸出格式爲:"%d\t%d\t%.2f%%\n";
    其他要求輸出百分比格式爲:"%d-%d\t%d\t%.2f%%\n"
    提示信息爲: “For course %d:\n”
    輸出每個學生的學號、姓名、考試成績,以及課程總分和平均分
    要求學號、姓名的輸出格式爲:"%ld\t%s\t"
    要求成績的輸出格式爲:"%.0f\t"
    要求總分及平均分的輸出格式爲:"%.0f\t%.0f\n"
    選擇退出(菜單項0)
    提示信息:“End of program!”
    菜單項選擇錯誤(不在0-11之間)
    提示信息:“Input error!\n”

代碼實現

#include <stdio.h>
#include <string.h>
#define   MAX_LEN  10                		/* 字符串最大長度 */
#define   STU_NUM 30                       /* 最多的學生人數 */
#define   COURSE_NUM 6                     /* 最多的考試科目數 */
int   Menu(void);
void  ReadScore(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], int n, int m);
void AverSumofEveryStudent(float score[][COURSE_NUM], int n, int m,float  sum[STU_NUM], float aver[STU_NUM]);
void AverSumofEveryCourse(float score[][COURSE_NUM], int n, int m);
void  SortbyScore(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m, int (*compare)(float a, float b));
int   Ascending(float a, float b);
int   Descending(float a, float b);
void  SwapFloat(float *x, float *y);
void  SwapLong(long *x, long *y);
void  SwapChar(char x[], char y[]);
void  AsSortbyNum(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m);
void  SortbyName(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m);
void  SearchbyNum(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m);
void  SearchbyName(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m);
void  StatisticAnalysis(float score[][COURSE_NUM], int n, int m);
void  PrintScore(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m) ;
int main()
{
    long num[STU_NUM];
    char name[STU_NUM][MAX_LEN];
    float score[STU_NUM][COURSE_NUM],sum[STU_NUM],aver[STU_NUM];
    int n,m,co;
    printf("Input student number(n<30):\n");
    scanf("%d",&n);
    while(1)
    {
        co=Menu();
        switch(co)
        {
        case 1:
            printf("Input course number(m<=%d):\n",COURSE_NUM);
            scanf("%d",&m);
            ReadScore(num,name,score,n,m);
            break;
        case 2:
            AverSumofEveryCourse(score,n,m);
            break;
        case 3:
            AverSumofEveryStudent(score,n,m,sum,aver);
            break;
        case 4:
            SortbyScore(num,name,score,sum,aver,n,m,Descending);
            printf("Sort in descending order by score:\n");
            PrintScore(num,name,score,sum,aver,n,m);
            break;
        case 5:
            SortbyScore(num,name,score,sum,aver,n,m,Ascending);
            printf("Sort in ascending order by score:\n");
            PrintScore(num,name,score,sum,aver,n,m);
            break;
        case 6:
            AsSortbyNum(num,name,score,sum,aver,n,m);
            printf("Sort in ascending order by number:\n");
            PrintScore(num,name,score,sum,aver,n,m);
            break;
        case 7:
            SortbyName(num,name,score,sum,aver,n,m);
            printf("Sort in dictionary order by name:\n");
            PrintScore(num,name,score,sum,aver,n,m);
            break;
        case 8:
            SearchbyNum(num,name,score,sum,aver,n,m);
            break;
        case 9:
            SearchbyName(num,name,score,sum,aver,n,m);
            break;
        case 10:
            StatisticAnalysis(score,n,m);
            break;
        case 11:
            PrintScore(num,name,score,sum,aver,n,m);
            break;
        case 0:
            printf("End of program!");
            exit(0);
        default :
            printf("Input error!\n");
        }
    }
}
int   Menu(void)
{
    int co;
    printf("Management for Students' scores\n\
1.Input record\n\
2.Caculate total and average score of every course\n\
3.Caculate total and average score of every student\n\
4.Sort in descending order by score\n\
5.Sort in ascending order by score\n\
6.Sort in ascending order by number\n\
7.Sort in dictionary order by name\n\
8.Search by number\n\
9.Search by name\n\
10.Statistic analysis\n\
11.List record\n\
0.Exit\n\
Please Input your choice:\n");
    scanf("%d",&co);
    return co;
}
void  ReadScore(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], int n, int m)
{
    int i,j;
    printf("Input student's ID, name and score:\n");
    for(i=0;i<n;i++)
    {
        scanf("%ld%s",&num[i],name[i]);
        for(j=0;j<m;j++)
        {
            scanf("%f",&score[i][j]);
        }
    }
}
void AverSumofEveryStudent(float score[][COURSE_NUM], int n, int m, float  sum[STU_NUM], float aver[STU_NUM])
{
    int i,j;
    for(i=0;i<n;i++)
    {
        sum[i]=0;
        for(j=0;j<m;j++)
        {
            sum[i]+=score[i][j];
        }
        aver[i]=sum[i]/m;
        printf("student %d:sum=%.0f,aver=%.0f\n",i+1,sum[i],aver[i]);
    }
}
void AverSumofEveryCourse(float score[][COURSE_NUM], int n, int m)
{
    float sum1[COURSE_NUM],aver1[COURSE_NUM];
    int i,j;
    for(i=0;i<m;i++)
    {
        sum1[i]=0;
        for(j=0;j<n;j++)
        {
            sum1[i]+=score[j][i];
        }
        aver1[i]=sum1[i]/n;
        printf("course %d:sum=%.0f,aver=%.0f\n",i+1,sum1[i],aver1[i]);
    }
}
void  SortbyScore(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m, int (*compare)(float a, float b))
{
    int i,j,y,t;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if((*compare)(sum[i],sum[j]))
            {
                SwapChar(name[i],name[j]);
                SwapLong(&num[i],&num[j]);
                SwapFloat(&sum[i],&sum[j]);
                SwapFloat(&aver[i],&aver[j]);
                for(t=0;t<m;t++)
                {
                    SwapFloat(&score[i][t],&score[j][t]);
                }
            }
        }
    }
}
int   Ascending(float a, float b)
{
    return a>b;
}
int   Descending(float a, float b)
{
    return a<b;
}
void  SwapFloat(float *x, float *y)
{
    float temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void  SwapLong(long *x, long *y)
{
    long temp;
    temp=*x;
    *x=*y;
    *y=temp;
}
void  SwapChar(char x[], char y[])
{
    char temp[MAX_LEN];
    strcpy(temp,x);
    strcpy(x,y);
    strcpy(y,temp);
}
void  AsSortbyNum(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m)
{
    int i, j, t;
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(num[i]>num[j])
            {
                SwapChar(name[i],name[j]);
                SwapLong(&num[i],&num[j]);
                SwapFloat(&sum[i],&sum[j]);
                SwapFloat(&aver[i],&aver[j]);
                for(t=0;t<m;t++)
                {
                    SwapFloat(&score[i][t],&score[j][t]);
                }
            }
        }
    }
}
void  SortbyName(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m)
{
    int i, j, t;
    for(i=0; i<n-1; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if(strcmp(name[i],name[j])>0)
            {
                SwapChar(name[i],name[j]);
                SwapLong(&num[i],&num[j]);
                SwapFloat(&sum[i],&sum[j]);
                SwapFloat(&aver[i],&aver[j]);
                for(t=0;t<m;t++)
                {
                    SwapFloat(&score[i][t],&score[j][t]);
                }
            }
        }
    }
}
void  SearchbyNum(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m)
{
    long se;
    int i,j,t=0;
    printf("Input the number you want to search:\n");
    scanf("%ld",&se);
    for(i=0;i<n;i++)
    {
        if(num[i]==se)
        {
            printf("%ld\t%s\t",num[i],name[i]);
            for(j=0;j<m;j++)
            {
                printf("%.0f\t",score[i][j]);
            }
            printf("%.0f\t%.0f\n",sum[i],aver[i]);
            t=1;
        }
    }
    if(t==0)
    {
        printf("Not found!\n");
    }
}
void  SearchbyName(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m)
{
    char se[MAX_LEN];
    int i,j,t=0;
    printf("Input the name you want to search:\n");
    scanf("%s",se);
    for(i=0;i<n;i++)
    {
        if(strcmp(name[i],se)==0)
        {
            printf("%ld\t%s\t",num[i],name[i]);
            for(j=0;j<m;j++)
            {
                printf("%.0f\t",score[i][j]);
            }
            printf("%.0f\t%.0f\n",sum[i],aver[i]);
            t=1;
        }
    }
    if(t==0)
    {
        printf("Not found!\n");
    }
}
void  StatisticAnalysis(float score[][COURSE_NUM], int n, int m)
{
    int d1[COURSE_NUM], d2[COURSE_NUM], d3[COURSE_NUM], d4[COURSE_NUM], d5[COURSE_NUM], d6[COURSE_NUM], i, j;
    for(i=0;i<m;i++)
    {
        d1[i]=d2[i]=d3[i]=d4[i]=d5[i]=d6[i]=0;
    }
    for(j=0;j<m;j++)
    {

        for(i=0;i<n;i++)
        {
            if(score[i][j]<60){
                d1[j]++;}
            else if(score[i][j]<70){
                d2[j]++;}
            else if(score[i][j]<80){
                d3[j]++;}
            else if(score[i][j]<90){
                d4[j]++;}
            else if(score[i][j]<100){
                d5[j]++;}
            else{
                d6[j]++;}
        }
        printf("For course %d:\n",j+1);
        printf("<60\t%d\t%.2f%%\n", d1[j], 100*(float)d1[j]/n);
        printf("%d-%d\t%d\t%.2f%%\n", 60, 69, d2[j], 100*(float)d2[j]/n);
        printf("%d-%d\t%d\t%.2f%%\n", 70, 79, d3[j], 100*(float)d3[j]/n);
        printf("%d-%d\t%d\t%.2f%%\n", 80, 89, d4[j], 100*(float)d4[j]/n);
        printf("%d-%d\t%d\t%.2f%%\n", 90, 99, d5[j], 100*(float)d5[j]/n);
        printf("%d\t%d\t%.2f%%\n", 100, d6[j], 100*(float)d6[j]/n);
    }
}
void  PrintScore(long num[], char name[][MAX_LEN],float score[][COURSE_NUM], float  sum[], float aver[],int n, int m)
{
    int i,j,y;
    for(i=0;i<n;i++)
    {
        printf("%ld\t%s\t",num[i],name[i]);
        for(j=0;j<m;j++)
        {
            printf("%.0f\t",score[i][j]);
        }
        printf("%.0f\t%.0f\n",sum[i],aver[i]);
    }
}
  1. 尋找最高分成績的學生
    題目內容

    下面程序的功能是用動態數組編程輸入任意m個班學生(每班n個學生)的某門課的成績,計算最高分,並指出具有該最高分成績的學生是第幾個班的第幾個學生。其中,m和n的值由用戶從鍵盤任意輸入(不限定m和n的上限值)。程序的運行結果如下所示:
    Input array size m,n:
    3,4↙
    Input 3*4 array:
    80 82 63 74↙
    60 81 75 68↙
    87 91 78 92↙

    maxScore = 92, class = 3, number = 4
    按要求在空白處填寫適當的表達式或語句,使程序完整並符合題目要求。
#include  <stdio.h>
#include  <stdlib.h>
void InputScore(int *p, int m, int n);
int  FindMax(int *p, int m, int n, int *pRow, int *pCol);             
int main()
{ 
    int  *pScore, m, n, maxScore, row, col;
    printf("Input array size m,n:\n");
    scanf("%d,%d", &m, &n);  
    ___________________; /* 申請動態內存 */
     
    if (pScore == NULL) 
    {
        printf("No enough memory!\n");
        exit(0); 
    }
    InputScore(pScore, m, n);
    maxScore = FindMax(________________);
     
    printf("maxScore = %d, class = %d, number = %d\n", maxScore, row+1, col+1);                              
    free(pScore);                                      /* 釋放動態內存 */
    return 0;
}
 
/* 函數功能:輸入m行n列二維數組的值 */
void InputScore(_______, int m, int n) 
{
    int i, j;
    printf("Input %d*%d array:\n", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", _________); 
        }
    }
}
/*  函數功能:計算任意m行n列二維數組中元素的最大值,並指出其所在行列下標值 */
int  FindMax(int *p, int m, int n, int *pRow, int *pCol)    
{
    int  i, j, max = p[0];
    __________; 
     
    __________;                   
     
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            if (___________)        
            {
                max = p[i*n+j];
                *pRow = i;       /*記錄行下標*/
                *pCol = j;             /*記錄列下標*/
            } 
        }  
    }  
    return max;                
}
  1. 輸入格式:
    輸入數組大小格式:"%d,%d"
    輸入數組元素格式:"%d"
    輸出格式
    輸入數組大小的提示信息:“Input array size m,n:\n”
    輸入數組元素的提示信息:“Input %d*%d array:\n”
    輸出數據格式:“maxScore = %d, class = %d, number = %d\n”

代碼實現

#include  <stdio.h>
#include  <stdlib.h>
void InputScore(int *p, int m, int n);
int  FindMax(int *p, int m, int n, int *pRow, int *pCol);
int main()
{
    int  *pScore, m, n, maxScore, row, col;
    printf("Input array size m,n:\n");
    scanf("%d,%d", &m, &n);
    pScore=(int*)malloc(m*n*sizeof(int)); /* 申請動態內存 */
    if (pScore == NULL)
    {
        printf("No enough memory!\n");
        exit(0);
    }
    InputScore(pScore, m, n);
    maxScore = FindMax(pScore,m,n,&row,&col);

    printf("maxScore = %d, class = %d, number = %d\n", maxScore, row+1, col+1);
    free(pScore);                                      /* 釋放動態內存 */
    return 0;
}

/* 函數功能:輸入m行n列二維數組的值 */
void InputScore(int *p, int m, int n)
{
    int i, j;
    printf("Input %d*%d array:\n", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d",p+i*n+j);
        }
    }
}
/*  函數功能:計算任意m行n列二維數組中元素的最大值,並指出其所在行列下標值 */
int  FindMax(int *p, int m, int n, int *pRow, int *pCol)
{
    int  i, j, max = p[0];
    *pRow=0;

    *pCol=0;

    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            if (p[i*n+j]>max)
            {
                max = p[i*n+j];
                *pRow = i;       /*記錄行下標*/
                *pCol = j;             /*記錄列下標*/
            }
        }
    }
    return max;
}
  1. 程序改錯
    題目內容

    下面程序的功能是輸入m個學生(最多爲30人)n門課程(最多爲5門)的成績,然後計算並打印每個學生各門課的總分和平均分。其中,m和n的值由用戶從鍵盤輸入。希望的運行結果爲:
    程序運行結果如下:
    How many students?
    4↙
    How many courses?
    3↙
    Input scores:
    60 60 60↙
    70 70 70↙
    80 80 80↙
    90 90 90↙

    Result:
    60 60 60 180 60.0
    70 70 70 210 70.0
    80 80 80 240 80.0
    90 90 90 270 90.0
    目前程序存在錯誤,請找出錯誤所在並加以改正。
#include  <stdio.h>
#define STUD   30      /* 最多可能的學生人數 */
#define COURSE 5       /* 最多可能的考試科目數 */
void  Total(int *pScore, int sum[], float aver[], int m, int n);
void  Print(int *pScore, int sum[], float aver[], int m, int n);
int main()
{
    int     i, j, m, n, score[STUD][COURSE], sum[STUD];
    float   aver[STUD];
    printf("How many students?");
    scanf("%d", &m);
    printf("How many courses?");
    scanf("%d", &n);
    printf("Input scores:\n");
     
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", &score[i][j]);
        }
    }
     
    Total(*score, sum, aver, m, n);
    Print(*score, sum, aver, m, n);
     return 0;
}
void  Total(int *pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    for (i=0; i<m; i++)
    {
        sum[i] = 0;
        for (j=0; j<n; j++)
        {
            sum[i] = sum[i] + pScore[i*n+j];
        }
        aver[i] = (float) sum[i] / n;
    }
}
void  Print(int *pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    printf("Result:\n");
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            printf("%4d\t", pScore[i*n+j]);
        }
    printf("%5d\t%6.1f\n", sum[i], aver[i]);
    }
}
  1. 輸入格式:
    學生人數、課程數、成績的輸入格式都是: “%d”
    輸出格式
    輸入學生人數提示信息:“How many students?\n”
    輸入課程數提示信息:“How many courses?\n”
    輸入成績的提示信息:“Input scores:\n”
    輸出結果的提示信息: “Result:\n”
    每個學生每門課成績的輸出格式: “%4d”
    總分和平均分的輸出格式: “%5d%6.1f\n”

代碼實現

#include  <stdio.h>
#define STUD   30      /* 最多可能的學生人數 */
#define COURSE 5       /* 最多可能的考試科目數 */
void  Total(int *pScore, int sum[], float aver[], int m, int n);
void  Print(int *pScore, int sum[], float aver[], int m, int n);
int main()
{
    int     i, j, m, n, score[STUD][COURSE], sum[STUD];
    float   aver[STUD];
    printf("How many students?\n");
    scanf("%d", &m);
    printf("How many courses?\n");
    scanf("%d", &n);
    printf("Input scores:\n");

    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", &score[i][j]);
        }
    }
    Total(*score, sum, aver, m, n);
    Print(*score, sum, aver, m, n);
     return 0;
}
void  Total(int *pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    for (i=0; i<m; i++)
    {
        sum[i] = 0;
        for (j=0; j<n; j++)
        {
            sum[i] = sum[i] + pScore[i*COURSE+j];
        }
        aver[i] = (float) sum[i] / n;
    }
}
void  Print(int *pScore, int sum[], float aver[], int m, int n)
{
    int  i, j;
    printf("Result:\n");
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            printf("%4d", pScore[i*COURSE+j]);
        }
    printf("%5d%6.1f\n", sum[i], aver[i]);
    }
}
  1. 矩陣轉置
    題目內容

    下面程序的功能是用二維數組的列指針作爲函數實參,計算並輸出m×n階矩陣的轉置矩陣。其中,m和n的值由用戶從鍵盤輸入。已知m和n的值都不超過10。程序的運行結果如下所示:
    Input m, n:
    4,5↙
    Input 4*5 matrix:
    45 89 90 26 65↙
    21 34 56 77 99↙
    31 25 62 50 46↙
    78 69 84 73 15↙

    The transposed matrix is:
    45 21 31 78
    89 34 25 69
    90 56 62 84
    26 77 50 73
    65 99 46 15
    按要求在空白處填寫適當的表達式或語句,使程序完整並符合題目要求。
#include <stdio.h>
#define M 10
#define N 10
void Transpose(int *a, int *at, int m, int n);
void InputMatrix(int *a, int m, int n);
void PrintMatrix(int *at, int n, int m); 
int main()
{
    int s[M][N], st[N][M], m, n;
    printf("Input m, n:");
    scanf("%d,%d", &m, &n);
    InputMatrix(____, m, n);
    Transpose(______________);
    printf("The transposed matrix is:\n");
    PrintMatrix(*st, n,  m); 
    return 0;
}
/* 函數功能:計算m*n矩陣a的轉置矩陣at */
void Transpose(int *a, int *at, int m, int n)   
{ 
    int i, j;
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            _____________;
        }
    }
}
/* 函數功能:輸入m*n矩陣a的值 */
void InputMatrix(int *a, int m, int n)   
{
    int i, j;
    printf("Input %d*%d matrix:\n", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d", ____________); 
        }
    }
}
/* 函數功能:輸出n*m矩陣at的值 */
void PrintMatrix(int *at, int n, int m)   
{
    int i, j;
    for (i=0; i<n; i++)
    {
        for (j=0; j<m; j++)
        {
            printf("%-5d", ____________);
        }
        printf("\n");
    }
}
  1. 輸入格式:
    矩陣大小的輸入格式:"%d,%d"
    矩陣元素的輸入格式:"%d"
    輸出格式:
    輸入矩陣大小的提示信息:“Input m, n:\n”
    輸入矩陣元素的提示信息:“Input %d*%d matrix:\n”
    輸出轉置矩陣的提示信息:“The transposed matrix is:\n”
    轉置後矩陣元素的輸出格式:"%-5d"

代碼實現

#include <stdio.h>
#define M 10
#define N 10
void Transpose(int *a, int *at, int m, int n);
void InputMatrix(int *a, int m, int n);
void PrintMatrix(int *at, int n, int m);
int main()
{
    int s[M][N], st[N][M], m, n;
    printf("Input m, n:\n");
    scanf("%d,%d", &m, &n);
    InputMatrix(*s, m, n);
    Transpose(*s, *st, m, n);
    printf("The transposed matrix is:\n");
    PrintMatrix(*st, n,  m);
    return 0;
}
/* 函數功能:計算m*n矩陣a的轉置矩陣at */
void Transpose(int *a, int *at, int m, int n)
{
    int i, j;
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            *(at+j*N+i)=*(a+i*M+j);
        }
    }
}
/* 函數功能:輸入m*n矩陣a的值 */
void InputMatrix(int *a, int m, int n)
{
    int i, j;
    printf("Input %d*%d matrix:\n", m, n);
    for (i=0; i<m; i++)
    {
        for (j=0; j<n; j++)
        {
            scanf("%d",a+i*M+j);
        }
    }
}
/* 函數功能:輸出n*m矩陣at的值 */
void PrintMatrix(int *at, int n, int m)
{
    int i, j;
    for (i=0; i<n; i++)
    {
        for (j=0; j<m; j++)
        {
            printf("%-5d",*(at+i*N+j));
        }
        printf("\n");
    }
}
  1. 在升序排序的數組中插入一個元素
    題目內容

    用函數編程實現在一個按升序排序的數組中查找x應插入的位置,將x插入數組中,使數組元素仍按升序排列。
    提示:插入(Insertion)是數組的基本操作之一。插入法排序算法的關鍵在於要找到正確的插入位置,然後依次移動插入位置及其後的所有元素,騰出這個位置放入待插入的元素。插入排序的原理如圖所示:
    程序運行結果示例:
    Input array size:
    5↙
    Input array:
    1 3 5 7 9↙
    Input x:
    4↙
    After insert 4:
    1 3 4 5 7 9
    輸入格式:
    插入前數組元素個數、數組元素、待插入的元素x的輸入格式都是:"%d"
    輸出格式:
    輸入插入前數組元素個數提示信息:“Input array size:\n”
    輸入插入前已按升序排序的數組元素提示信息:“Input array:\n”
    輸入待插入的元素x提示信息:“Input x:\n”
    輸出插入x後的數組元素提示信息:“After insert %d:\n”
    數組元素輸出格式:"%4d"

代碼實現

#include <stdio.h>
#include <string.h>
int FF(int a[], int x, int n);
int main()
{
    int i, n;
    printf("Input array size:\n");
    scanf("%d",&n);
    int a[n+1], x, t;
    printf("Input array:\n");
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    printf("Input x:\n");
    scanf("%d",&x);
    t = FF(a,x,n);
    for(i=n;i>t;i--)
    {
        a[i]=a[i-1];
    }
    a[t] = x;
    printf("After insert %d:\n");
    for(i=0;i<n+1;i++)
    {
        printf("%4d",a[i]);
    }
}
int FF(int a[], int x, int n)
{
    int i;
    for(i = 0; i < n-1; i++)
    {
        if(x < a[0])
            return 0;
        if(x > a[n-1])
            return n-1;
        for(i = 0; i < n-1; i++)
        {
            if(x<a[i+1] && x>a[i])
                return i+1;
        }
    }
}
  1. 計算平均數、中位數和衆數
    題目內容

    在調查數據分析(Survey data analysis)中經常需要計算平均數、中位數和衆數。用函數編程計算40個輸入數據(是取值1—10之間的任意整數)的平均數(Mean)、中位數(Median)和衆數(Mode)。中位數指的是排列在數組中間的數。衆數是數組中出現次數最多的那個數(不考慮兩個或兩個以上的輸入數據出現次數相同的情況)。
    提示:計算中位數時,首先要調用排序函數對數組按升序進行排序,然後取出排序後數組中間位置的元素answer[n/2] ,就得到了中位數。如果數組元素的個數是偶數,那麼中位數就等於數組中間那兩個元素的算術平均值。衆數就是40個輸入數據中出現次數最多的那個數。計算衆數時,首先要統計不同取值的輸入數據出現的次數,然後找出出現次數最多的那個數據,這個數據就是衆數(這裏沒有考慮兩個或者兩個以上的輸入數據出現次數相同的情況)。
    程序運行結果示例:
    Input the feedbacks of 40 students:
    10 9 10 8 7 6 5 10 9 8↙
    8 9 7 6 10 9 8 8 7 7↙
    6 6 8 8 9 9 10 8 7 7↙
    9 8 7 9 7 6 5 9 8 7↙

    Mean value=7
    Median value=8
    Mode value=8
    輸入格式: “%d”
    輸出格式
    輸入數據的提示信息:“Input the feedbacks of 40 students:\n”
    平均數輸出:“Mean value=%d\n”
    中位數輸出:“Median value=%d\n”
    衆數輸出: “Mode value=%d\n”

代碼實現

#include<stdio.h>
#include<stdlib.h>
int cmp(const void* a,const void* b)
{
    const int *x = (const int*)a;
    const int *y = (const int*)b;
    if(*x -*y == 0)
        return 0;
    else if(*x - *y > 0)
        return 1;
    else
        return -1;
}
int Mean(int a[], int n);
int Median(int a[], int n);
int Mode(int a[], int n);
int main()
{
    int stu[40] = {0};
    int mean,med,mode;
    printf("Input the feedbacks of 40 students:\n");
    for(int i = 0; i < 40; i++)
        scanf("%d",&stu[i]);
    mean = Mean(stu,40);
    med = Median(stu,40);
    mode = Mode(stu,40);
    printf("Mean value=%d\n",mean);
    printf("Median value=%d\n",med);
    printf("Mode value=%d\n",mode);
    return 0;
}

int Mean(int a[], int n)
{
    int i,sum = 0;
    for(i = 0; i < n; i++)
        sum += a[i];
    return sum / n;
}

int Median(int a[], int n)
{
    qsort(a,n,sizeof(int),cmp);
    return a[n/2];
}

int Mode(int a[], int n)
{
    int num[11] = {0};
    int max = 0;
    int pos = 1;
    for(int i = 0; i < n; i++)
        num[a[i]]++;
    for(int i = 1; i <= 10; i++)
    {
        if(num[i] > max)
        {
            max = num[i];
            pos = i;
        }

    }
    return pos;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章