高级语言程序设计作业 11/25
- 2024高级语言程序设计:https://edu.cnblogs.com/campus/fzu/2024C
- 高级语言程序设计课程第五次作业:https://edu.cnblogs.com/campus/fzu/2024C/homework/13311
- 学号:102400215
- 姓名:胡加乘
14.17 复习题
3
struct month
{
const char* name; // 月份名
const char abbr[4]; // 月份缩写
int days; // 天数
int no; // 月份号
};
4
struct month arr[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", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 },
};
5
struct month arr[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", "Sep", 30, 9 },
{ "October", "Oct", 31, 10 },
{ "November", "Nov", 30, 11 },
{ "December", "Dec", 31, 12 },
};
int totalDays(int month);
int main()
{
cout << "到七月为止的总天数(包括七月):" << totalDays(7);
return 0;
}
int totalDays(int month)
{
int sum = 0;
for (int i = 0; i < month; i++)
{
sum += arr[i].days;
}
return sum;
}
10
struct gas
{
float distance; // 英里数
float gals; // 加仑
float mpg; // 每加仑行驶的英里数
};
void cal_mpg(struct gas* g);
int main()
{
struct gas g = { 100, 20, -1 };
cal_mpg(&g);
cout << g.mpg;
return 0;
}
void cal_mpg(struct gas* g)
{
g->mpg = g->distance / g->gals;
}
11
enum choices
{
no = 0,
yes = 1,
maybe = 2,
};
14.18 编程练习
3
#include <iostream>
using namespace std;
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; // 价格
};
void sort_by_title(struct book* bks, int count);
void sort_by_value(struct book* bks, int count);
int main()
{
struct book library[MAXBKS];
struct book* pbk[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_s("%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");
// 按标题排序
sort_by_title(library, count);
// 按价格排序
// sort_by_value(library, count);
for (index = 0; index < count; index++)
{
printf("%s is by %s: $%.2f\n",
library[index].title,
library[index].author,
library[index].value);
}
}
else
{
printf("No books? Too bad.\n");
}
return 0;
}
void sort_by_predicate(struct book* bks, int count,
bool (*predicate)(struct book bk1, struct book bk2))
{
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - i - 1; j++)
{
if (predicate(bks[j], bks[j + 1]))
{
struct book tmp = bks[j];
bks[j] = bks[j + 1];
bks[j + 1] = tmp;
}
}
}
}
bool title_predicate(struct book bk1, struct book bk2)
{
return bk1.title[0] > bk2.title[0];
}
void sort_by_title(struct book* bks, int count)
{
sort_by_predicate(bks, count, title_predicate);
}
bool value_predicate(struct book bk1, struct book bk2)
{
return bk1.value > bk2.value;
}
void sort_by_value(struct book* bks, int count)
{
sort_by_predicate(bks, count, value_predicate);
}
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'); // look for newline
if (find) // if the address is not NULL,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}
4
#include <iostream>
using namespace std;
typedef struct
{
int insurance;
struct
{
const char* first_name;
const char* middle_name;
const char* last_name;
} name;
} person;
void printPerson(person p);
int main()
{
person contacts[5] =
{
{ 302039823, { "Michael", "James", "Johnson" } },
{ 485729163, { "Olivia", "Rose", "Smith" } },
{ 793462815, { "William", "Henry", "Anderson" } },
{ 628413957, { "Benjamin", NULL, "Harris" } },
{ 172948506, { "Charlotte", "Anne", "Lee" } },
};
for (int i = 0; i < 5; i++)
{
printPerson(contacts[i]);
}
return 0;
}
void printPerson(person p)
{
cout << p.name.first_name << ", " << p.name.last_name;
if (p.name.middle_name)
cout << " " << p.name.middle_name[0] << ".";
cout << " -- " << p.insurance << endl;
}
5
#include <iostream>
using namespace std;
#define LEN 21
#define SCORES 3
#define CSIZE 4
struct name
{
char first_name[LEN];
char last_name[LEN];
};
struct student
{
struct name person;
float grade[SCORES];
float avg;
};
void input_scores(struct student arr[], int n);
void calc_avg(struct student arr[], int n);
void show_students(const struct student arr[], int n);
int main()
{
struct student students[CSIZE] =
{
{ "Emily", "Carter" },
{ "Michael", "Johnson" },
{ "William", "Anderson" },
{ "Charlotte", "Lee" }
};
// d.输入学生成绩
input_scores(students, CSIZE);
// e.计算平均值
calc_avg(students, CSIZE);
// f. 打印每个学生的信息、平均分
show_students(students, CSIZE);
return 0;
}
void input_scores(struct student arr[], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
// 提示用户输入学生成绩
printf("Please enter %d scores for %s %s:\n",
SCORES, arr[i].person.first_name, arr[i].person.last_name);
for (j = 0; j < SCORES; j++)
{
while (scanf_s("%f", &arr[i].grade[j]) != 1)
{
scanf_s("%*s");
puts("Please use numeric input.");
}
}
}
}
void calc_avg(struct student arr[], int n)
{
int i, j;
float sum;
for (i = 0; i < n; i++)
{
sum = 0;
for (j = 0; j < SCORES; j++)
sum += arr[i].grade[j];
// 计算平均值
arr[i].avg = sum / SCORES;
}
}
void show_students(const struct student arr[], int n)
{
int i, j;
// 全名
char full_name[2 * LEN];
printf("\nHere is student's score list:\n");
for (i = 0; i < n; i++)
{
strcpy(full_name, arr[i].person.first_name);
strcat(full_name, " ");
strcat(full_name, arr[i].person.last_name);
// 打印全名
printf("%15s: ", full_name);
for (j = 0; j < SCORES; j++)
printf("%6.1f ", arr[i].grade[j]);
// 打印平均分
printf(" Average = %5.2f\n", arr[i].avg);
}
}