高级·语言程序设计第九次作业
这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/2024C/
这个作业要求在哪里: https://edu.cnblogs.com/campus/fzu/2024C/homework/13311
学号:092300125
姓名:张天荣
14.17
3.
struct Month {
char name[12];
char nmee2[4];
int day;
int id;
};
#include <stdio.h>
typedef struct Month {
char name[12];
char nmee2[4];
int day;
int id;
}Month;
int main(void) {
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},
{"August","aug",31,8},
{"September","sep",30,9},
{"October","oct",31,10},
{"Novermber","nov",30,11},
{"December","dec",31,12},
};
return 0;
}
#include <stdio.h>
typedef struct Month {
char name[12];
char nmee2[4];
int day;
int id;
}Month;
int SumDay(Month* arr,int n) {
int i = 0;
int sum = 0;
if (n < 1 && n>12)
return -1;
else {
for (i = 0; i < n; i++)
sum += arr[i].day;
}
return sum;
}
int main(void) {
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},
{"August","aug",31,8},
{"September","sep",30,9},
{"October","oct",31,10},
{"Novermber","nov",30,11},
{"December","dec",31,12},
};
int n = 2;
printf("%d",SumDay(arr,n));
return 0;
}
10
typedef struct gas {
float distance;
float gals;
float mpg;
}gas;
gas a(gas x) {
if (x.gals > 0)
x.mpg = x.distance / x.gals;
else
x.mpg = -1.0;
return x;
}
void b(gas* x) {
if (x->gals > 0)
x->mpg = x->distance / x->gals;
else
x->mpg = -1.0;
}
#include <stdio.h>
enum choices{no,yes,maybe};
int main(void) {
enum choices a;
a = no;
printf("%d ",a);
a = yes;
printf("%d ", a);
a = maybe;
printf("%d ", a);
return 0;
}
14.18
3.
#include <stdio.h>
#include <string.h>
#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);
void sort_title(struct book* pb[], int n);
void sort_value(struct book* pb[], int n);
int main(void)
{
struct book library[MAXBKS];
struct book* book[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) && 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);
book[count] = &library[count];
count++;
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");
for (index = 0; index < count; index++)
{
printf("%s by %s: $%.2f\n", library[index].title,
library[index].author, library[index].value);
}
sort_title(book, count);
printf("\nHere is the list of your books sorted by title letters:\n");
for (index = 0; index < count; index++)
{
printf("%s by %s: $%.2f\n", book[index]->title,
book[index]->author, book[index]->value);
}
sort_value(book, count);
printf("\nHere is the list of your books sorted by value(from low to high):\n");
for (index = 0; index < count; index++)
{
printf("%s by %s: $%.2f\n", book[index]->title,
book[index]->author, book[index]->value);
}
}
else
{
printf("No books? Too bad.\n");
}
return 0;
}
void sort_title(struct book* pb[], int n)
{
int i, j;
struct book* temp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(pb[j]->title, pb[i]->title) < 0)
{
temp = pb[j];
pb[j] = pb[i];
pb[i] = temp;
}
}
}
return;
}
void sort_value(struct book* pb[], int n)
{
int i, j;
struct book* temp;
for (i = 0; i < n - 1; i++)
{
for (j = i + 1; j < n; j++)
{
if (pb[j]->value < pb[i]->value)
{
temp = pb[j];
pb[j] = pb[i];
pb[i] = temp;
}
}
}
return;
}
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')
continue;
}
}
return ret_val;
}
4.
a.
#include <stdio.h>
#include <string.h>
#define N 15
#define LEN 30
struct names
{
char fname[N];
char mname[N];
char lname[N];
};
struct messages
{
char ins_num[LEN];
struct names name;
};
char *s_gets(char *st, int n);
void show(const struct messages pt[], int n);
int main(void)
{
int count = 0;
struct messages m[5];
printf("Please enter the insurance number:\n");
printf("Press [enter] at the start of a line to stop.\n");
while (count < 5 && s_gets(m[count].ins_num, LEN) && m[count].ins_num[0] != '\0')
{
printf("Now enter the former name.\n");
s_gets(m[count].name.fname, N);
printf("Now enter the middle name(Without, [enter] to the next).\n");
s_gets(m[count].name.mname, N);
printf("Now enter the last name.\n");
s_gets(m[count].name.lname, N);
if (count++ < 5)
{
printf("Enter the next insurance number:\n");
}
}
if (count > 0)
{
show(m, count);
}
else
{
printf("No data!\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')
continue;
}
}
return ret_val;
}
void show(const struct messages pt[], int n)
{
int i;
printf("All numbers messages:\n");
for (i = 0; i < n; i++)
{
if (pt[i].name.mname[0] == '\0')
{
printf("%s, %s", pt[i].name.fname, pt[i].name.lname);
printf(" -- %s\n", pt[i].ins_num);
}
else
{
printf("%s, %s %c.", pt[i].name.fname, pt[i].name.lname, pt[i].name.mname[0]);
printf(" -- %s\n", pt[i].ins_num);
}
}
return;
}
b.
#include <stdio.h>
#include <string.h>
#define N 15
#define LEN 30
struct names
{
char fname[N];
char mname[N];
char lname[N];
};
struct messages
{
char ins_num[LEN];
struct names name;
};
char *s_gets(char *st, int n);
void show_messages(const struct messages pt);
int main(void)
{
int i;
int count = 0;
struct messages m[5];
printf("Please enter the insurance number:\n");
printf("Press [enter] at the start of a line to stop.\n");
while (count < 5 && s_gets(m[count].ins_num, LEN) && m[count].ins_num[0] != '\0')
{
printf("Now enter the former name:\n");
s_gets(m[count].name.fname, N);
printf("Now enter the middle name(Without, [enter] to the next):\n");
s_gets(m[count].name.mname, N);
printf("Now enter the last name:\n");
s_gets(m[count].name.lname, N);
if (count++ < 5)
{
printf("Enter the next insurance number:\n");
}
}
if (count > 0)
{
printf("All numbers messages:\n");
for (i = 0; i < count; i++)
{
show_messages(m[i]);
}
}
else
{
printf("No data!\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')
continue;
}
}
return ret_val;
}
void show_messages(const struct messages pt)
{
if (pt.name.mname[0] == '\0')
{
printf("%s, %s", pt.name.fname, pt.name.lname);
printf(" -- %s\n", pt.ins_num);
}
else
{
printf("%s, %s %c.", pt.name.fname, pt.name.lname, pt.name.mname[0]);
printf(" -- %s\n", pt.ins_num);
}
return;
}
5.
#include <stdio.h>
#include <string.h>
#define LEN 15
#define CSIZE 4
#define SCORES 3
struct name
{
char fname[LEN];
char lname[LEN];
};
struct student
{
struct name mes;
float grade[SCORES];
float aver;
};
void set_students(struct student ar[], int n);
void find_averages(struct student ar[], int n);
void show_messages(const struct student ar[], int n);
void show_averages(const struct student ar[], int n);
int main(void)
{
struct student classes[CSIZE] = {
{"Flip", "Snide"},
{"Clare", "Voyans"},
{"Bingo", "Higgs"},
{"Fawn", "Hunter"},
};
set_students(classes, CSIZE);
find_averages(classes, CSIZE);
show_messages(classes, CSIZE);
show_averages(classes, CSIZE);
return 0;
}
void set_students(struct student ar[], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
printf("Please enter three scores for %s %s:\n", ar[i].mes.fname, ar[i].mes.lname);
for (j = 0; j < SCORES; j++)
{
while (scanf("%f", &ar[i].grade[j]) != 1)
{
while (getchar() != '\n')
continue;
printf("Please enter a number: ");
}
}
}
return;
}
void find_averages(struct student ar[], int n)
{
int i, j;
float sum;
for (i = 0; i < n; i++)
{
for (j = 0, sum = 0.0f; j < SCORES; j++)
{
sum += ar[i].grade[j];
}
ar[i].aver = sum / SCORES;
}
return;
}
void show_messages(const struct student ar[], int n)
{
int i, j;
for (i = 0; i < n; i++)
{
printf("%s %s's three scores: ", ar[i].mes.fname, ar[i].mes.lname);
for (j = 0; j < SCORES; j++)
{
printf("%g ", ar[i].grade[j]);
}
printf("\nAverage: %g\n", ar[i].aver);
}
return;
}
void show_averages(const struct student ar[], int n)
{
int i;
float total;
for (i = 0, total = 0.0f; i < n; i++)
{
total += ar[i].aver;
}
printf("Class average: %g\n", total / n);
return;
}