高级语言程序设计课程第九次个人作业
高级语言程序设计课程第九次个人作业
- 这个作业属于哪个课程:2024高级语言程序设计
- 这个作业要求在哪里:高级语言程序设计课程第九次个人作业
- 学号:032201218
- 姓名:陈彦哲
一、编写并运行书本第14章14.17复习题中的第3~5,10,11题。
14.3
题目:
设计一个结构模板储存一个月份名、该月份名的3个字母缩写、该月 的天数以及月份号。
#include <stdio.h>
#include <string.h>
struct Month {
char fullName[20];
char shortName[4];
int days;
int monthNumber;
};
int main() {
struct Month month;
strcpy(month.fullName, "January");
strcpy(month.shortName, "Jan");
month.days = 31;
month.monthNumber = 1;
printf("月份名: %s\n", month.fullName);
printf("缩写: %s\n", month.shortName);
printf("天数: %d\n", month.days);
printf("月份号: %d\n", month.monthNumber);
return 0;
}
思路:
根据题意理解即可。
问题:无
解决:无
14.4
题目:
定义一个数组,内含12个结构(第3题的结构类型)并初始化为一个 年份(非闰年)
#include <stdio.h>
#include <string.h>
struct Month {
char fullName[20];
char shortName[4];
int days;
int monthNumber;
};
int main() {
struct Month year[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},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
for (int i = 0; i < 12; i++) {
printf("月份: %s, 缩写: %s, 天数: %d, 月份号: %d\n", year[i].fullName, year[i].shortName, year[i].days, year[i].monthNumber);
}
return 0;
}
思路:按题意理解即可
问题:无
解决:无
14.5
题目:编写一个函数,用户提供月份号,该函数就返回一年中到该月为止 (包括该月)的总天数。假设在所有函数的外部声明了第3题的结构模版和 一个该类型结构的数组。
#include <stdio.h>
#include <string.h>
struct Month {
char fullName[20];
char shortName[4];
int days;
int monthNumber;
};
int calculateTotalDays(struct Month year[], int monthNumber) {
int totalDays = 0;
for (int i = 0; i < monthNumber; i++) {
totalDays += year[i].days;
}
return totalDays;
}
int main() {
struct Month year[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},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12}
};
int monthNumber;
scanf("%d", &monthNumber);
if (monthNumber < 1 || monthNumber > 12) {
printf("无效的月份号\n");
return 0;
}
int totalDays = calculateTotalDays(year, monthNumber);
printf("到%d月为止的总天数: %d\n", monthNumber, totalDays);
return 0;
}
思路:按题意理解即可
问题:无
解决:无
14.10
题目:假设有如下结构:
struct gas { float distance; float gals; float mpg; };
a.设计一个函数,接受struct gas类型的参数。假设传入的结构包含 distance和gals信息。该函数为mpg成员计算正确的值,并把值返回该结构。
b.设计一个函数,接受struct gas类型的参数。假设传入的结构包含 distance和gals信息。该函数为mpg成员计算正确的值,并把该值赋给合适的成员
解答:
#include <stdio.h>
struct gas {
float distance;
float gals;
float mpg;
};
struct gas calculateMpg(struct gas g) {
if (g.gals != 0) {
g.mpg = g.distance / g.gals;
} else {
g.mpg = 0;
}
return g;
}
void setMpg(struct gas *g) {
if (g->gals != 0) {
g->mpg = g->distance / g->gals;
} else {
g->mpg = 0;
}
}
int main() {
struct gas myGas = {650.0, 65.0, 0.0};
myGas = calculateMpg(myGas);
printf("通过calculateMpg函数计算的MPG: %.2f\n", myGas.mpg);
myGas.distance = 400.0;
myGas.gals = 25.0;
setMpg(&myGas);
printf("通过setMpg函数计算的MPG: %.2f\n", myGas.mpg);
return 0;
}
思路:按题意理解即可
问题:无
解决:无
14.11
题目:声明一个标记为choices的枚举,把枚举常量no、yes和maybe分别设置为0、1、2。
#include <stdio.h>
#include <string.h>
enum choices { no = 0, yes = 1, maybe = 2 };
int main() {
enum choices myChoice;
char input[10];
printf("请输入选项 (no, yes, maybe): ");
scanf("%s", input);
if (strcmp(input, "no") == 0) {
myChoice = no;
} else if (strcmp(input, "yes") == 0) {
myChoice = yes;
} else if (strcmp(input, "maybe") == 0) {
myChoice = maybe;
} else {
printf("输入无效,请输入no, yes或maybe。\n");
return 0;
}
printf("当前选择: %d\n", myChoice);
return 0;
}
思路:按题意理解即可
问题:无
解决:无
二、编写并运行书本第14章14.18编程练习题目中的第3,4,5题。
14.3
题目:
修改程序清单 14.2 中的图书目录程序,使其按照输入图书的顺序输出 图书的信息,然后按照标题字母的声明输出图书的信息,最后按照价格的升 序输出图书的信息。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *s_gets(char *st, int n);
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
struct book {
char title[MAXTITL];
char author[MAXAUTL];
float value;
};
// 类似C++的compare的写法
int compare_by_title(const void *a, const void *b) {
return strcmp(((struct book *)a)->title, ((struct book *)b)->title);
}
int compare_by_price(const void *a, const void *b) {
float diff = ((struct book *)a)->value - ((struct book *)b)->value;
if (diff < 0) return -1;
if (diff > 0) return 1;
return 0;
}
int main(void) {
struct 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'){
continue;
} //清理输入的行
if (count < MAXBKS) printf("Enter the next title.\n");
}
if (count > 0) {
printf("Here is the list of your books:\n");
// 1. 按输入顺序输出
printf("\nBooks in the order entered:\n");
for (index = 0; index < count; index++) {
printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
}
// 2. 按标题字母顺序输出
qsort(library, count, sizeof(struct book), compare_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);
}
// 3. 按价格升序输出
qsort(library, count, sizeof(struct book), compare_by_price);
printf("\nBooks sorted by price:\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 *val;
char *find;
val = fgets(st, n, stdin);
if (val) {
find = strchr(st, '\n'); // 查找换行符
if (find)
*find = '\0';
else
while (getchar() != '\n') continue; // 处理输入行剩余的字符
}
return val;
}
思路:按题意理解即可
问题:一开始在排序的相关代码上遇到了问题
解决:会议了一下C++里给vector写的利用仿函数写的排序函数,按照类似的方法处理了一下就解决了
14.4
题目:
编写一个程序,创建一个有两个成员的结构模板:
a.第1个成员是社会保险号,第2个成员是一个有3个成员的结构,第1个 成员代表名,第2个成员代表中间名,第3个成员表示姓。创建并初始化一个 内含5个该类型结构的数组。该程序以下面的格式打印数据:
Dribble, Flossie M.–– 302039823
如果有中间名,只打印它的第1个字母,后面加一个点(.);如果没有 中间名,则不用打印点。编写一个程序进行打印,把结构数组传递给这个函 数。
b.修改a部分,传递结构的值而不是结构的地址。
// a.c
#include <stdio.h>
#include <string.h>
#define MAX_NAME 30
#define NUM_PEOPLE 5
struct fullName {
char first[MAX_NAME];
char middle[MAX_NAME];
char last[MAX_NAME];
};
struct person {
int ssn;
struct fullName name;
};
void printPersons(const struct person *people, int size) {
for (int i = 0; i < size; i++) {
printf("%s, %s", people[i].name.last, people[i].name.first);
if (strlen(people[i].name.middle) > 0) {
printf(" %c.", people[i].name.middle[0]);
}
printf("-–%d\n", people[i].ssn);
}
}
int main() {
struct person people[NUM_PEOPLE] = {
{302039823, {"Flossie", "Mickey", "Dribble"}},
{908739200, {"Charles", "", "Smith"}},
{402983029, {"Alice", "Richard", "Johnson"}},
{292018396, {"Bob", "", "Brown"}},
{999302194, {"Jane", "Kevin", "Wood"}}
};
printPersons(people, NUM_PEOPLE);
return 0;
}
// b.c
#include <stdio.h>
#include <string.h>
#define MAX_NAME 30
#define NUM_PEOPLE 5
struct fullName {
char first[MAX_NAME];
char middle[MAX_NAME];
char last[MAX_NAME];
};
struct person {
int ssn;
struct fullName name;
};
void printPerson(struct person individual) {
printf("%s, %s", individual.name.last, individual.name.first);
if (strlen(individual.name.middle) > 0) {
printf(" %c.", individual.name.middle[0]);
}
printf("-- %d\n", individual.ssn);
}
int main() {
struct person people[NUM_PEOPLE] = {
{302039823, {"Flossie", "Mickey", "Dribble"}},
{908739200, {"Charles", "", "Smith"}},
{402983029, {"Alice", "Richard", "Johnson"}},
{292018396, {"Bob", "", "Brown"}},
{999302194, {"Jane", "Kevin", "Wood"}}
};
for (int i = 0; i < NUM_PEOPLE; i++) {
printPerson(people[i]);
}
return 0;
}
思路:按题意理解即可
问题:无
解决:无
14.5
题目:
编写一个程序满足下面的要求。
a.外部定义一个有两个成员的结构模板name:一个字符串储存名,一个 字符串储存姓。
b.外部定义一个有3个成员的结构模板student:一个name类型的结构, 一个grade数组储存3个浮点型分数,一个变量储存3个分数平均数。
c.在main()函数中声明一个内含CSIZE(CSIZE = 4)个student类型结构的 数组,并初始化这些结构的名字部分。用函数执行g、e、f和g中描述的任 务。
d.以交互的方式获取每个学生的成绩,提示用户输入学生的姓名和分 数。把分数储存到grade数组相应的结构中。可以在main()函数或其他函数中 用循环来完成。
e.计算每个结构的平均分,并把计算后的值赋给合适的成员。
f.打印每个结构的信息。
g.打印班级的平均分,即所有结构的数值成员的平均值
#include <stdio.h>
#include <string.h>
#define NAMELEN 30
#define CSIZE 4
#define GRADES 3
struct name {
char first[NAMELEN];
char last[NAMELEN];
};
struct student {
struct name studentName;
float grades[GRADES];
float average;
};
void inputGrades(struct student *students, int size);
void calculateAverages(struct student *students, int size);
void printStudentInfo(const struct student *students, int size);
void printClassAverage(const struct student *students, int size);
int main() {
struct student students[CSIZE] = {
{{"Alice", "AA"}, {0}, 0},
{{"Bob", "BB"}, {0}, 0},
{{"Charles", "CC"}, {0}, 0},
{{"David", "DD"}, {0}, 0}
};
inputGrades(students, CSIZE);
calculateAverages(students, CSIZE);
printStudentInfo(students, CSIZE);
printClassAverage(students, CSIZE);
return 0;
}
void inputGrades(struct student *students, int size) {
for (int i = 0; i < size; i++) {
printf("Enter grades for %s %s:\n", students[i].studentName.first, students[i].studentName.last);
for (int j = 0; j < GRADES; j++) {
printf("Grade %d: ", j + 1);
scanf("%f", &students[i].grades[j]);
}
}
}
void calculateAverages(struct student *students, int size) {
for (int i = 0; i < size; i++) {
float total = 0;
for (int j = 0; j < GRADES; j++) {
total += students[i].grades[j];
}
students[i].average = total / GRADES;
}
}
void printStudentInfo(const struct student *students, int size) {
printf("\nStudent Information:\n");
for (int i = 0; i < size; i++) {
printf("%s %s:\n", students[i].studentName.first, students[i].studentName.last);
printf("Grades: ");
for (int j = 0; j < GRADES; j++) {
printf("%.2f ", students[i].grades[j]);
}
printf("\nAverage: %.2f\n", students[i].average);
}
}
void printClassAverage(const struct student *students, int size) {
float totalAverage = 0;
for (int i = 0; i < size; i++) {
totalAverage += students[i].average;
}
printf("\nClass Average: %.2f\n", totalAverage / size);
}
思路:按题意理解即可
问题:无
解决:无
三、总结思考:
- 复习了C语言的结构体的用法
- 复习了C语言自定义排序函数,让我意识到了C和C++尽管语法上有着一些区别,但是所有的算法的思想本质上都是一致的
- 复习了C语言的枚举
- 复习了C语言的结构体数组,对未来的有关C语言的大型项目的创建打下了坚实的基础