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

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C

这个作业要求在哪里:https://edu.cnblogs.com/campus/fzu/2024C/homework/13311

学号:102300108

姓名:陈茜蕾

14.17 复习题

第三题

struct Month
{
const char name[20]; // 月份名
const char abbr[20]; // 月份缩写
int day; // 天数
int number; // 月份号
};

第四题

struct Month m[12] =
{
{ "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 },
{ "Augest", "Aug", 31, 8 },
{ "Spetember", "Sept", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 },
};

第五题

include <stdio.h>

include<malloc.h>

include<string.h>

pragma warning (disable:4996)

struct Month
{
const char* name; // 月份名
const char abbr[4]; // 月份缩写
int days; // 天数
int no; // 月份号
};

struct Month m[12] =
{
{ "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 },
{ "Augest", "Aug", 31, 8 },
{ "Spetember", "Sept", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 },
};

int getDays(int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += m[i].days;
}
return sum;
}

int main() {
int n;
scanf("%d", &n);
printf("%d\n", getDays(n));
}

第十题

include <stdio.h>

include<malloc.h>

include<string.h>

pragma warning (disable:4996)

struct gas
{
float distance; // 英里数
float gals; // 加仑
float mpg; // 每加仑行驶的英里数
};
void getMpg(struct gas* g)
{
g->mpg = g->distance / g->gals;
}

struct gas getMpg1(struct gas* g) {
g->mpg = g->distance / g->gals;
return *g;
}
int main()
{
struct gas g = { 10, 5, 0 };
getMpg(&g);
printf("%f ", g.mpg);

return 0;

}

第十一题

enum choices
{
no = 0,
yes = 1,
maybe = 2,
};

14.18 编程练习

第三题

include <stdio.h>

include <string.h>

include <stdlib.h> // 用于排序时的 qsort 函数

pragma warning (disable:4996)

define MAXTITL 40

define MAXAUTL 40

define MAXBKS 100 /* 最大书籍数量 */

/* 定义书籍结构 */
struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};

/* 输入函数,用于读取用户输入 /
char
s_gets(char* st, int n);

/* 按书名字母顺序排序的比较函数 /
int output_by_title(const void
a, const void* b) {
return strcmp(((struct book)a)->title, ((struct book)b)->title);
}

/* 按书籍价格升序排序的比较函数 /
int output_by_price(const void
a, const void* b) {
if (((struct book)a)->value > ((struct book)b)->value) return 1;
if (((struct book)a)->value < ((struct book)b)->value) return -1;
return 0;
}

int main(void) {
struct book library[MAXBKS]; /* book 类型结构的数组 */
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') continue; /* 清理输入行 */

    if (count < MAXBKS)
        printf("Enter the next title.\n");
}

if (count > 0) {
    /* 按照输入顺序输出图书信息 */
    printf("\nBooks in the order they were entered:\n");
    for (index = 0; index < count; index++) {
        printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
    }

    /* 按照标题字母顺序排序并输出图书信息 */
    qsort(library, count, sizeof(struct book), output_by_title);
    printf("\nBooks sorted by title:\n");
    for (index = 0; index < count; index++) {
        printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
    }

    /* 按照价格升序排序并输出图书信息 */
    qsort(library, count, sizeof(struct book), output_by_price);
    printf("\nBooks sorted by price (ascending):\n");
    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) // 如果地址不是 NULL, 将换行符替换为结束符
        *find = '\0';
    else
        while (getchar() != '\n') continue; // 处理输入行中剩余的字符
}

return ret_val;

}

第四题

a

include <stdio.h>

include <string.h>

define SIZE 5

struct name {
char first[20];
char middle[20];
char last[20];
};

struct person {
char ssn[20];
struct name full_name;
};

void print(struct person* p);

int main() {
// 初始化5个person结构体数组
struct person people[SIZE] = {
{"302039823", {"Flossie", "M", "Dribble"}},
{"302039824", {"John", "", "Doe"}},
{"302039825", {"Jane", "A", "Smith"}},
{"302039826", {"Alice", "", "Johnson"}},
{"302039827", {"Robert", "B", "Williams"}}
};

// 打印所有人的信息
for (int i = 0; i < SIZE; i++) {
    print(&people[i]);
}

return 0;

}

// 打印函数,打印一个person的信息
void print(struct person* p) {
// 打印姓,名,中间名的第一个字母,如果有中间名的话
printf("%s, %s", p->full_name.last, p->full_name.first);
if (strlen(p->full_name.middle) > 0) {
printf(" %c.", p->full_name.middle[0]); // 打印中间名的首字母
}
printf("–– %s\n", p->ssn);
}

b

include <stdio.h>

include <string.h>

define SIZE 5

struct name {
char first[20];
char middle[20];
char last[20];
};

struct person {
char ssn[20];
struct name full_name;
};

void print(struct person p);

int main() {
// 初始化5个person结构体数组
struct person people[SIZE] = {
{"302039823", {"Flossie", "M", "Dribble"}},
{"302039824", {"John", "", "Doe"}},
{"302039825", {"Jane", "A", "Smith"}},
{"302039826", {"Alice", "", "Johnson"}},
{"302039827", {"Robert", "B", "Williams"}}
};

// 打印所有人的信息
for (int i = 0; i < SIZE; i++) {
    print(people[i]);
}

return 0;

}

// 打印函数,打印一个person的信息
void print(struct person p) {
// 打印姓,名,中间名的第一个字母,如果有中间名的话
printf("%s, %s", p.full_name.last, p.full_name.first);
if (strlen(p.full_name.middle) > 0) {
printf(" %c.", p.full_name.middle[0]); // 打印中间名的首字母
}
printf("-- %s\n", p.ssn);
}

第五题

include <stdio.h>

include <string.h>

define CSIZE 4

pragma warning (disable:4996)

// 定义name结构体
struct name {
char first[20];
char last[20];
};

// 定义student结构体
struct student {
struct name full_name; // name结构体
float grade[3]; // 存储3个浮动分数
float average; // 存储平均分
};

// 函数声明
void get_grades(struct student* stu);
void calculate_average(struct student* stu);
void print_student_info(struct student stu);
void print_class_average(struct student students[], int size);

int main() {
// 声明并初始化学生结构体数组
struct student students[CSIZE] = {
{{"John", "Doe"}, {0, 0, 0}, 0},
{{"Jane", "Smith"}, {0, 0, 0}, 0},
{{"Alice", "Johnson"}, {0, 0, 0}, 0},
{{"Bob", "Brown"}, {0, 0, 0}, 0}
};

// 获取每个学生的成绩并计算平均分
for (int i = 0; i < CSIZE; i++) {
    get_grades(&students[i]);
    calculate_average(&students[i]);
}

// 打印每个学生的信息
for (int i = 0; i < CSIZE; i++) {
    print_student_info(students[i]);
}

// 打印班级平均分
print_class_average(students, CSIZE);

return 0;

}

// 获取每个学生的成绩
void get_grades(struct student* stu) {
printf("Enter the grades for %s %s:\n", stu->full_name.first, stu->full_name.last);
for (int i = 0; i < 3; i++) {
printf("Grade %d: ", i + 1);
scanf("%f", &stu->grade[i]);
}
}

// 计算学生的平均成绩
void calculate_average(struct student* stu) {
float total = 0;
for (int i = 0; i < 3; i++) {
total += stu->grade[i];
}
stu->average = total / 3;
}

// 打印学生的详细信息
void print_student_info(struct student stu) {
printf("\nStudent: %s %s\n", stu.full_name.first, stu.full_name.last);
printf("Grades: %.2f, %.2f, %.2f\n", stu.grade[0], stu.grade[1], stu.grade[2]);
printf("Average Grade: %.2f\n", stu.average);
}

// 打印班级平均分
void print_class_average(struct student students[], int size) {
float total_avg = 0;
for (int i = 0; i < size; i++) {
total_avg += students[i].average;
}
printf("\nClass Average: %.2f\n", total_avg / size);
}

posted @ 2024-12-01 19:19  取个昵称真的好难  阅读(3)  评论(0编辑  收藏  举报