This page looks plain and unstyled because you're using a non-standard compliant browser. To see it in its best form, please visit upgrade to a browser that supports web standards. It's free and painless.

羅伯特學程式 會員登入 會員註冊

« 上一篇 | 下一篇 »

試宣告一個具有 5 個元素的結構陣列 struct data student {…}, 結構內容有學生姓名和成績.
並於程式碼裡設定初值給陣列元素, 並依下列敘述進行程式撰寫 :

(1)試撰寫一函數 struct data best(struct data student[]), 可接受結構陣列 student, 傳回值則為成績最高分的結構陣列元素.

(2)試撰寫一函數 void failed(struct data student[]), 可接受結構陣列 student, 並於函數裡列出所有成績不及格之學生姓名與分數(60分為及格).

(3)試撰寫一函數 double average(struct data student[]), 可接受結構陣列 student, 傳回值則為成績的平均值.

(4)試撰寫一函數 void sort(struct data student[]), 可接受結構陣列 student, 並於函數裡將陣列元素排列. 分數越高者排列越前面.


… … … 寫了好久, 才寫出來, 糟糕 … … … 014_擦汗


--------------------  程式碼  --------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 5
struct data
{
    char name[10];
    int math;
};

struct data best(struct data student[]);
void failed(struct data student[]);
double average(struct data student[]);
void sort(struct data student[]);

main(void)
{
    int i;
    struct data student[MAX]={{"John",49},{"Marry",50},{"Chichi",100},{"Eric",99},{"Willie",93}};
    struct data h;
    h=best(student);
    printf("成績最高分:  ");
    printf("%s   ",h.name);
    printf("%d\n",h.math);
    printf("\n成績不及格:\n");
    failed(student);
    printf("\n平均成績: ");
    printf("%.2f\n",average(student));
    printf("\n成績排序:\n");
    sort(student);

    system("pause");
    return 0;
}

struct data best(struct data student[MAX])
{
    struct data high;
    high=student[0];
    int i,tmp=0,idx=0;
    for(i=1;i<MAX;i++)
        if(student[i].math>high.math)
        {
            strcpy(high.name,student[i].name);
            high.math=student[i].math;
        }
    return(high);
}

void failed(struct data student[MAX])
{
    int i;
    for(i=0;i<MAX;i++)
        if(student[i].math<60)
        {
            printf("%-10s",student[i].name);
            printf("%d\n",student[i].math);
        }
}

double average(struct data student[MAX])
{
    int i;
    double sum=0.0;
    for(i=0;i<MAX;i++)
        sum+=student[i].math;
    return(sum/MAX);
}

void sort(struct data student[MAX])
{
    int i,j,temp_math;
    char temp_name[10];
    int flag=0;
    for(i=1;(i<MAX)&&(!flag);i++)
    {
        flag=1;
        for(j=0;j<(MAX-i);j++)
            if(student[j].math<student[j+1].math)
            {
                strcpy(temp_name,student[j].name);
                strcpy(student[j].name,student[j+1].name);
                strcpy(student[j+1].name,temp_name);
                temp_math=student[j].math;
                student[j].math=student[j+1].math;
                student[j+1].math=temp_math;
                flag=0;
            }
    }
    for(i=0;i<MAX;i++)
    {
        printf("%-10s",student[i].name);
        printf("%d\n",student[i].math);
    }               
}

發表回應
  1. Re: [C語言練習] 以結構為引數傳遞到函數 [回覆]

    不错喔........

    It is a beautiful as well as a long name for a handbag. Most of all, the unbelievable fashion style will absolutely makes itself on your Favorite List, and will attract the attention of the world if you bring it. This louis vuitton handbags is a feminine handbag with an elegant timeless design. It truly can be considered an worthful investment since you will certainly be able to use it for years. Right now, five colors are available.

    replica hermes 回應於 17 六月, 2010 14:35