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

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

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

学号:<102400227>

姓名:<谭培>

14.17.3:没问题

#include <stdio.h>

struct month{
	char month[10];
	char mon[3];
	int days;
	int math;
}months[12];

14.17.4:没问题

int main(){
	struct month months[12]={
		("January","Jan",31,1),
		("Febtuary","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),
		("Octorber","Oct",31,10),
		("November","Nov",30,11),
		("Decmber","Dec",31,12)	
	};
	return 0;
}

14.17.5:一来char mon[]里打3导致数据丢失了,应该是3+1

#include <stdio.h>

struct month{
	char month[10];
	char mon[4];
	int days;
	int math;
};
struct month months[12]={
		{"January","Jan",31,1},
		{"Febtuary","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},
		{"Octorber","Oct",31,10},
		{"November","Nov",30,11},
		{"Decmber","Dec",31,12}	
};	
	
int cnt_days(int month){
	int total=0;
	for(int i=0; i<month; i++){
		total+=months[i].days;
	}
	return total;
}

int main(){
	
	int n=0;
	if(scanf("%d",&n)==1&&n>0&&n<13){
		int result=cnt_days(n);
		printf("%d",result);
		}
		
	else return -1;
	
	return 0;
}

14.17.10a

#include <stdlib.h>
#include <stdio.h>
struct gas{
	float distance;
	float gals;
	float mpg;//mpg=distance/gals
};

int mpg(int distance,int gals){
	int mpg=distance/gals;
	return mpg;
}

int main(){
	
}

14.17.10.b

void get_mpg(struct gas *ptrip){
	if(ptrip->gals>0){
		ptrip->mpg=ptrip->distance/ptrip->gals;
	}else{
		ptrip->mpg=-1.0;
	}
}

14.17.11:没问题

#include <stdio.h>

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

14.18.3:有点难,还查了strchr函数,运行也不顺利

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
		/*strchr 函数用于在一个字符串中查找指定字符第一次出现的位置,返回指向该字符的指针。
		如果未找到,返回 NULL。其原型为char *strchr(const char *s, int c);。
		例如:strchr("hello world", 'o')会返回指向字符串中第一个'o'的指针。*/
#define MAXTITL 40
/* 书名的最大长度 + 1 */
#define MAXAUTL 30
/* 作者姓名的最大长度 + 1 */

// 添加s_gets函数原型声明
char *s_gets(char *st, int n);

struct book {
    /* 结构模版: 标记是book */
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
};

// 比较函数,用于qsort按书名排序
int compare_titles(const void *a, const void *b) {
    struct book *book_a = (struct book *)a;
    struct book *book_b = (struct book *)b;
    return strcmp(book_a->title, book_b->title);
}

// 比较函数,用于qsort按价格排序
int compare_values(const void *a, const void *b) {
    struct book *book_a = (struct book *)a;
    struct book *book_b = (struct book *)b;
    if (book_a->value < book_b->value)
        return -1;
    else if (book_a->value > book_b->value)
        return 1;
    return 0;
}

void input_book(struct book *library) {
    printf("Please enter the book title.\n");
    s_gets(library->title, MAXTITL);

    printf("Now enter the author.\n");
    s_gets(library->author, MAXAUTL);

    printf("Now enter the value.\n");
    scanf("%f", &library->value);
}

void print_book(const struct book *library) {
    printf("%s by %s: $%.2f\n", library->title,
           library->author, library->value);
}

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;
}

int main() {
    int num_books;
    printf("Enter the number of books: ");
    scanf("%d", &num_books);

    // 动态分配数组来存储图书信息
    struct book *books = (struct book *)malloc(num_books * sizeof(struct book));
    if (books == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // 输入图书信息
    for (int i = 0; i < num_books; i++) {
        printf("Enter information for book %d:\n", i + 1);
        input_book(&books[i]);
    }

    // 按照输入图书顺序输出图书信息
    printf("\nBooks in input order:\n");
    for (int i = 0; i < num_books; i++) {
        print_book(&books[i]);
    }

    // 按照书名字母顺序排序并输出图书信息
	qsort(books, num_books, sizeof(struct book), compare_titles);
	printf("\nBooks in alphabetical order by title:\n");
	for (int i = 0; i < num_books; i++) {
    print_book(&books[i]);
	}

    // 按照图书价格升序排序并输出图书信息
    qsort(books, num_books, sizeof(struct book), compare_values);
    printf("\nBooks in ascending order by value:\n");
    for (int i = 0; i < num_books; i++) {
        print_book(&books[i]);
    }

    // 释放动态分配的内存
    free(books);

    printf("Done.\n");

    return 0;
}

14.18.4a

#include <stdio.h>

struct Name {	
    char first[50];
    char middle[50];
    char last[50];
};

struct Person {
    char social_insurance_num[50];
    struct Name name;
};

struct Person people[5] = {
    {"123456789", {"John", "M", "Doe"}},
    {"987654321", {"Jane", "", "Smith"}},
    {"456789123", {"Bob", "A", "Johnson"}},
    {"321456987", {"Alice", "L", "Brown"}},
    {"789456123", {"David", "", "Lee"}}
};

void printPeople(struct Person *p, int size) {
    for (int i = 0; i < size; i++) {
        printf("%s, ", p[i].name.first);
        if (p[i].name.middle[0]!= '\0') {
            printf("%c. ", p[i].name.middle[0]);
        }
        printf("%s -- %s\n", p[i].name.last, p[i].social_insurance_num);
    }
}

int main() {
    printPeople(people, 5);
    return 0;
}

14.18.4b

#include <stdio.h>

struct Name {	
    char first[50];
    char middle[50];
    char last[50];
};

struct Person {
    char social_insurance_num[50];
    struct Name name;
};

struct Person people[5] = {
    {"123456789", {"John", "M", "Doe"}},
    {"987654321", {"Jane", "", "Smith"}},
    {"456789123", {"Bob", "A", "Johnson"}},
    {"321456987", {"Alice", "L", "Brown"}},
    {"789456123", {"David", "", "Lee"}}
};

void printPeople(struct Person p, int size) {
    for (int i = 0; i < size; i++) {
        printf("%s, ", p.name.first);
        if (p.name.middle[0]!= '\0') {
            printf("%c. ", p.name.middle[0]);
        }
        printf("%s -- %s\n", p.name.last, p.social_insurance_num);
    }
}

int main() {
    for (int i = 0; i < 5; i++) {
        printPeople(people[i], 1);
    }
    return 0;
}

14.18.5:用Dev-C++确实费时间

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

typedef struct {
	char first[11];
	char last[11];
}full_name;

typedef struct {
	full_name name;
	double grade[3];
	double average;
}student;

void set_grade(student list[]);
void set_average(student list[]);
void print(student list[]);
void set_all_average(student list[]);

int main() {
	student list[CSIZE]={};
	set_grade(list);
	set_average(list);
	print(list);
	set_all_average(list);
	return 0;
}

void set_grade(student list[]){
	char c;
	for(int i=0; i<CSIZE; i++){
		printf("Please enter the students' name:\n");
		if(scanf("%10s %10s",list[i].name.first,list[i].name.last)==2){
			printf("Now,enter the 3 scode of this student:\n");
			if(scanf("%lf%lf%lf",&list[i].grade[0],&list[i].grade[1],&list[i].grade[2])==3)
				printf("Well,continue.\n");
		}				
	}	
}
void set_average(student list[]){
	for(int i=0; i<CSIZE; i++){
		double sum=0;
		for(int j=0; j<3; j++){
			sum+=list[i].grade[j];
		}
		list[i].average=sum/3;
	}	
}
void print(student list[]){
	printf("%-10s %-10s%12s%20s\n","first name","last name","grades","average");
	for(int i=0; i<CSIZE; i++){
		printf("%-10s%-10s %.2f %.2f %.2f %10.2f\n",list[i].name.first,list[i].name.last,
		list[i].grade[0],list[i].grade[1],list[i].grade[2],list[i].average);
	}
}
void set_all_average(student list[]){
	double sum=0;
	for(int i=0; i<CSIZE; i++){
		sum+=list[i].average;
	} 
	double all_average=sum/4;
	printf("\nall average is %.2f",all_average);
}

posted @ 2024-12-01 10:40  皓月冰轮  阅读(13)  评论(0编辑  收藏  举报