高级语言程序设计课程第九次个人作业

2024高级语言程序设计:https://edu.cnblogs.com/campus/fzu/2024C
高级语言程序设计课程第九次个人作业:https://edu.cnblogs.com/campus/fzu/2024C/homework/13311
学号:102400226
姓名:石华波

//14.17.3-5
#include<stdio.h>

void GetDays(void);

typedef struct{
    char name[10];
    char three[4];
    int days;
    int number;
} Month;

Month month[]={
    {"January","Jan",31,1},
    {"February","Feb",28,2},
    {"March","Mar",31,3},
    {"April","Apr",30,4},
    {"May","May",31,5},
    {"June","Jun",30,6},
    {"July","Jul",31,7},
    {"August","Aug",31,8},
    {"September","Sep",30,9},
    {"October","Oct",31,10},
    {"November","Nov",30,11},
    {"December","Dec",31,12}
};

int main(){
    GetDays();
    return 0;
} 

void GetDays(void){
    int input,sum=0;
    scanf("%d",&input);
    for(int i=0;i<input;i++) sum+=month[i].days;
    printf("%d",sum);
}

//14.17.10
#include<stdio.h>

typedef struct gas{
    float distance;
    float gals;
    float mpg;
} Gas;

Gas GetMpg1(Gas G);
void GetMpg2(Gas *G);

int main(){
    Gas G1={430,14},G2={525,13};
    G1=GetMpg1(G1);
    GetMpg2(&G2);
    printf("%f %f",G1.mpg,G2.mpg);
    return 0;
}

Gas GetMpg1(Gas G){
    G.mpg=G.distance/G.gals;
    return G;
}

void GetMpg2(Gas *G){
    G->mpg=G->distance/G->gals;
}

//14.17.11
#include<stdio.h>

enum choices{
    no,
    yes,
    maybe
};

int main(){
    enum choices Cho=yes;
    printf("%d",Cho);
    return 0;
}

//14.18.3
#include<stdio.h>
#include<string.h>

#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100

typedef struct{
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
} Book;

char *s_gets(char *st,int n);
void SortByTitle(Book library[],int n);
void SortByValue(Book library[],int n);

int main(){
    Book library[MAXBKS];
    int count=0;
    int index;
    
    printf("Please enter the book title.\n");
    printf("Press [enter] at the start of a line to stop.\n");
    while(count<MAXBKS&&s_gets(library[count].title,MAXTITL)!=NULL&&library[count].title[0]!='\0'){
        printf("Now enter the author.\n");
        s_gets(library[count].author,MAXAUTL);
        printf("Now enter the value.\n");
        scanf("%f",&library[count++].value);
        while(getchar()!='\n');
        if(count<MAXBKS) printf("Enter the next title.\n");
    }
    
    if(count>0){
        printf("Here is the list of your books:\n");
        for(index=0;index<count;index++){
            printf("%s by %s: $%.2f\n",library[index].title,library[index].author,library[index].value);
        }
        printf("\nThe list sorted by title:\n");
        SortByTitle(library,count);
        for(index=0;index<count;index++){
            printf("%s by %s: $%.2f\n",library[index].title,library[index].author,library[index].value);
        }
        printf("\nThe list sorted by value:\n");
        SortByValue(library,count);
        for(index=0;index<count;index++){
            printf("%s by %s: $%.2f\n",library[index].title,library[index].author,library[index].value);
        }
    }
    else printf("No books?Too bad.\n");
    return 0;
}

char *s_gets(char *st,int n){
    char *ret_val;
    char *find;
    
    ret_val=fgets(st,n,stdin);
    if(ret_val){
        find=strchr(st,'\n');
        if(find) *find='\0';
        else while(getchar()!='\n');
    }
    return ret_val;
}

void SortByTitle(Book library[],int n){
    Book temp;
    for(int i=0;i<n-1;i++){
        int IsSwapped=0;
        for(int j=0;j<n-1;j++){
            if(strcmp(library[j].title,library[j+1].title)>0){
                temp=library[j];
                library[j]=library[j+1];
                library[j+1]=temp;
                IsSwapped=1;
            }
        }
        if(IsSwapped==0) break;
    }
}

void SortByValue(Book library[],int n){
    Book temp;
    for(int i=0;i<n-1;i++){
        int IsSwapped=0;
        for(int j=0;j<n-1;j++){
            if(library[j].value>library[j+1].value){
                temp=library[j];
                library[j]=library[j+1];
                library[j+1]=temp;
                IsSwapped=1;
            }
        }
        if(IsSwapped==0) break;
    }
}

//14.18.4-a
#include<stdio.h>

struct Name{
    char First[20];
    char Middle[20];
    char Last[20];
};

struct Info {
    char SocialSecurityNumber[10];
    struct Name Names;
};

void PrintInfo(struct Info Info);

int main(){
    struct Info Infos[5] = {
        {"123456789", {"Liam", "James", "Smith"}},
        {"987654321", {"Sophia", "", "Johnson"}},
        {"555666777", {"Noah", "Charles", "Williams"}},
        {"444555666", {"Emma", "", "Brown"}},
        {"222333444", {"Ethan", "Thomas", "Jones"}}
    };
    for(int i=0;i<5;i++) PrintInfo(Infos[i]);
    return 0;
}

void PrintInfo(struct Info Info){
    printf("%s, %s",Info.Names.First,Info.Names.Last);
    if(Info.Names.Middle[0]!='\0') printf(" %c.",Info.Names.Middle[0]);
    printf(" -- %s\n",Info.SocialSecurityNumber);
}
//14.18.4-b
#include<stdio.h>

struct Name{
    char First[20];
    char Middle[20];
    char Last[20];
};

struct Info {
    char SocialSecurityNumber[10];
    struct Name Names;
};

void PrintInfo(struct Info Info);

int main(){
    struct Info Infos[5] = {
        {"123456789", {"Liam", "James", "Smith"}},
        {"987654321", {"Sophia", "", "Johnson"}},
        {"555666777", {"Noah", "Charles", "Williams"}},
        {"444555666", {"Emma", "", "Brown"}},
        {"222333444", {"Ethan", "Thomas", "Jones"}}
    };
    for(int i=0;i<5;i++) PrintInfo(Infos[i]);
    return 0;
}

void PrintInfo(struct Info Info){
    printf("%s, %s",Info.Names.First,Info.Names.Last);
    if(Info.Names.Middle[0]!='\0') printf(" %c.",Info.Names.Middle[0]);
    printf(" -- %s\n",Info.SocialSecurityNumber);
}

//14.18.5
#include<stdio.h>
#include<string.h>
#define CSIZE 4

struct Name{
    char SurName[20];
    char GivenName[20];
};

struct Student{
    struct Name Name;
    float Grade[3];
    float Average;
};

int main(){
    struct Student Students[CSIZE]={
        {"石", "华波"},
        {"邱", "青鹏"},
        {"倪", "群策"},
        {"卢", "宗园"},
    };
    int SucInput=0;
    float Average=0;
    while(SucInput<4){
        char InName[20];
        printf("请输入学生姓名: ");
        scanf("%s",InName);
        int Find=0;
        for(int i=0;i<4;i++){
            char FindName[40];
            strcpy(FindName,Students[i].Name.SurName);
            strcat(FindName,Students[i].Name.GivenName);
            if(strcmp(FindName,InName)==0){
                Find=1;
                SucInput++;
                printf("输入 %s 的三次成绩: ",InName);
                scanf(" %f %f %f",&Students[i].Grade[0],&Students[i].Grade[1],&Students[i].Grade[2]);
                Students[i].Average=(Students[i].Grade[0]+Students[i].Grade[1]+Students[i].Grade[2])/3;
                Average+=Students[i].Average;
                break;
            }
        }
        if(Find==0) printf("没有该学生。\n");
    }
    Average/=4;
    for(int i=0;i<4;i++){
        printf("学生: %s%s\n",Students[i].Name.SurName,Students[i].Name.GivenName);
        printf("三次成绩: %.2f %.2f %.2f\n",Students[i].Grade[0],Students[i].Grade[1],Students[i].Grade[2]);
        printf("平均: %.2f\n",Students[i].Average);
        putchar('\n');
    }
    printf("总平均分: %.2f\n",Average);
}

posted @ 2024-12-01 19:21  102400226石华波  阅读(26)  评论(0)    收藏  举报