1. 程式人生 > 實用技巧 >實驗6 結構體與共用體

實驗6 結構體與共用體

實驗一

#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#define N 3 

typedef struct student {
    int id;             /*學生學號 */ 
    char name[20];         /*學生姓名 */ 
    char subject[20];     /*考試科目 */ 
    float perf;         /*平時成績 */ 
    float mid;             /* 期中成績 */ 
    float
final; /* 期末成績 */ float total; /* 總評成績 */ char level[10]; /* 成績等級 */ } STU; void input(STU[],int ); /*輸入學生資訊 */ void calc(STU[],int); /*計算總評和等級 */ int fail(STU[],STU [],int); /*不及格學生統計 */ void sort(STU[],int); /*排序 */ void print(STU[], int
); /*輸出學生資訊*/ int main() { STU st[N],fst[N]; int k; printf("錄入學生成績資訊:\n"); input(st,N); printf("\n成績處理...\n"); calc(st,N); k = fail(st,fst,N); sort(st, N); printf("\n學生成績排名情況:\n"); print(st, N); printf("\n不及格學生資訊:\n"); print(fst, k);
return 0; } void input(STU s[],int n) { int i; for(i=0;i<n;i++) scanf("%d %s %s %f %f %f",&s[i].id,s[i].name,s[i].subject,&s[i].perf,&s[i].mid,&s[i].final); } void calc(STU s[],int n) { int i; for(i=0;i<n;i++) { s[i].total=s[i].perf*0.2+s[i].mid*0.2+s[i].final*0.6; if(s[i].total>=90) strcpy(s[i].level,""); else if(s[i].total>=80 && s[i].total<90) strcpy(s[i].level,""); else if(s[i].total>=70 && s[i].total<80) strcpy(s[i].level,""); else if(s[i].total>=60 && s[i].total<70) strcpy(s[i].level,"及格"); else strcpy(s[i].level,"不及格"); } } int fail(STU s[],STU t[],int n) { int i,k=0; for(i=0;i<n;i++) if(s[i].total<60) t[k++]=s[i]; return k; } void sort(STU s[],int n) { int i,j; STU temp; for(i=0;i<n-1;i++) for(j=0;j<n-1-i;j++) if(s[j].total<s[j+1].total) { temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } void print(STU s[], int n) { int i; printf("-----------------\n"); printf("學號 姓名 考試科目 平時成績 期中成績 期末成績 總評成績 成績等級\n"); for(i=0;i<n;i++) printf("%5d %10s%20s %5.1f %5.1f %5.1f %5.1f %10s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level); }

要注意結構體內部的傳輸,注意正確使用結構體的申明定義

實驗二

#include <stdio.h>
const int N=3;

typedef struct student {
    long no;
    char name[20];
    int score;     
}STU;

void input(STU s[], int n);
int findMinlist(STU s[], STU t[], int n);
void output(STU s[], int n);

int main() {
    STU stu[N], minlist[N];
    int count;
    
    printf("錄入%d個學生資訊\n", N);
    input(stu, N);
    
    printf("\n統計最低分人數和學生資訊...\n");
    count = findMinlist(stu, minlist, N);
    
    printf("\n一共有%d個最低分,資訊如下:\n", count);
    output(minlist, count);
     
    return 0;
}  
void input(STU s[], int n) {
    int i;
    for(i=0; i<n; i++) 
        scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score);
} 

void output(STU s[], int n) {
    int i;
    for(i=0; i<n; i++)
        printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); 
} 
 
int findMinlist(STU s[], STU t[], int n) {
    int m,min,count=0,k=0;
    min=s[0].score;
    for(m=1;m<n;m++)
    {
        if(min>s[m].score)
        {
            min=s[m].score;
        }
     }
    for(m=1;m<n;m++)
    {
        if(min==s[m].score)
        {
            t[count++]=s[m];
        }
    }
    return count;   
}
#include <stdio.h> 
#include <string.h>
const int N = 8;

// 定義結構體型別struct student,並定義其別名為STU 
typedef struct student {
    long int id;
    char name[20];
    float objective;    /*客觀題得分*/
    float subjective;    /*操作題得分*/
    float sum;
    char level[10];    
}STU; 

// 函式宣告
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);

int main() {
    STU stu[N];
    
    printf("錄入%d個考生資訊: 准考證號,姓名,客觀題得分(<=40),操作題得分(<=60)\n", N); 
    input(stu, N);
    
    printf("\n對考生資訊進行處理: 計算總分,確定等級\n");
    process(stu, N);
    
    printf("\n列印考生完整資訊: 准考證號,姓名,客觀題得分,操作題得分,總分,等級\n");
    output(stu, N); 
    
    return 0;
} 

// 錄入考生資訊:准考證號,姓名,客觀題得分,操作題得分
void input(STU s[], int n) {
    int i;
    for(i=0;i<n;i++)
    scanf("%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
}

//輸出考生完整資訊: 准考證號,姓名,客觀題得分,操作題得分,總分,等級
void output(STU s[], int n) {
    int i;
    printf("准考證號 姓名 客觀題得分 操作題得分 總分 等級\n");
    for(i=0;i<n;i++)
    printf("%ld %s %f %f" ,&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
}
// 對考生資訊進行處理:計算總分,排序,確定等級
void process(STU s[], int n) 
{
    int i,max,j;
    STU temp;
    for (i=0;i<n;i++)
        s[i].sum=s[i].objective+s[i].subjective;
    for (j=0;j<n-1;j++)
    {
        max=j; 
        for (i=j;i<n;i++)
        { 
            if (s[i].sum>s[max].sum)
                max=i;
        }    
        if (max!=j)
        {            
            temp=s[max];
            s[max]=s[j];
            s[j]=temp;
        }
    }
    for (i=0;i<n;i++)
    {
        if (i<=(n/10-1))
            strcpy(s[i].level,"優秀"); 
        else if (i>(n/10-1)&&i<=(n/2-1))
            strcpy(s[i].level,"合格"); 
        else
            strcpy(s[i].level,"不合格");      
    
    }
}

實驗三

#include <stdio.h> 
#include <string.h>
const int N = 8;

// 定義結構體型別struct student,並定義其別名為STU 
typedef struct student {
    long int id;
    char name[20];
    float objective;    /*客觀題得分*/
    float subjective;    /*操作題得分*/
    float sum;
    char level[10];    
}STU; 

// 函式宣告
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);

int main() {
    STU stu[N];
    
    printf("錄入%d個考生資訊: 准考證號,姓名,客觀題得分(<=40),操作題得分(<=60)\n", N); 
    input(stu, N);
    
    printf("\n對考生資訊進行處理: 計算總分,確定等級\n");
    process(stu, N);
    
    printf("\n列印考生完整資訊: 准考證號,姓名,客觀題得分,操作題得分,總分,等級\n");
    output(stu, N); 
    
    return 0;
} 

// 錄入考生資訊:准考證號,姓名,客觀題得分,操作題得分
void input(STU s[], int n) {
    int i;
    for(i=0;i<n;i++)
    scanf("%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
}

//輸出考生完整資訊: 准考證號,姓名,客觀題得分,操作題得分,總分,等級
void output(STU s[], int n) {
    int i;
    printf("准考證號 姓名 客觀題得分 操作題得分 總分 等級\n");
    for(i=0;i<n;i++)
    printf("%ld %s %f %f" ,&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);
}
// 對考生資訊進行處理:計算總分,排序,確定等級
void process(STU s[], int n) 
{
    int i,max,j;
    STU temp;
    for (i=0;i<n;i++)
        s[i].sum=s[i].objective+s[i].subjective;
    for (j=0;j<n-1;j++)
    {
        max=j; 
        for (i=j;i<n;i++)
        { 
            if (s[i].sum>s[max].sum)
                max=i;
        }    
        if (max!=j)
        {            
            temp=s[max];
            s[max]=s[j];
            s[j]=temp;
        }
    }
    for (i=0;i<n;i++)
    {
        if (i<=(n/10-1))
            strcpy(s[i].level,"優秀"); 
        else if (i>(n/10-1)&&i<=(n/2-1))
            strcpy(s[i].level,"合格"); 
        else
            strcpy(s[i].level,"不合格");      
    
    }
}

好像出了一些問題,阿巴阿巴。。。