实验六:
任务4
#include <stdio.h> #define N 10 typedef struct { char isbn[20]; // isbn号 char name[80]; // 书名 char author[80]; // 作者 double sales_price; // 售价 int sales_count; // 销售册数 } Book ; void output(Book x[], int n) { int i; printf("ISBN号\t\t 书名\t\t\t 作者\t\t 售价\t\t 销售册数\t\n"); for (i = 0; i < n; i++) { printf("%-20s%-35s%-20s%-20.1f%-10d\n", x[i].isbn, x[i].name, x[i].author, x[i].sales_price, x[i].sales_count); } }; void sort(Book x[], int n) { int i, j; Book temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - i - 1; j++) { if (x[j+1].sales_count > x[j].sales_count) { temp = x[j+1]; x[j+1] = x[j]; x[j] = temp; } } } } double sales_amount(Book x[], int n) { double all=0; int i; for (i = 0; i < n; i++) { all += x[i].sales_count * x[i].sales_price; } return all; } int main() { Book x[N] = { {"978-7-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51}, {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30}, {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27}, {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49}, {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42}, {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}, {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42}, {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55}, {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59} }; printf("图书销量排名(按销售册数): \n"); sort(x, N); output(x, N); printf("\n图书销售总额: %.2f\n", sales_amount(x, N)); return 0; }
任务5:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> typedef struct { int year; int month; int day; } Date; // 函数声明 void input(Date* pd) { scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day); } int day_of_year(Date d) { int i,day=0; int x[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; if (((d.year % 4 == 0) && (d.year % 100 != 0))||d.year%400==0) { x[1] = 29; } for (i = 0; i < d.month - 1; i++) { day += x[i]; } day += d.day; return day; } // 返回日期d是这一年的第多少天 int compare_dates(Date d1, Date d2) { if (d1.year > d2.year) { return 1; } if (d1.year < d2.year) { return -1; } if (d1.year == d2.year) { if (d1.month > d2.month) { return 1; } if (d1.month < d2.month) { return -1; } if (d1.month == d2.month) { if (d1.day > d2.day) { return 1; } if (d1.day < d2.day) { return -1; } if (d1.day == d2.day) { return 0; } } } } void test1() { Date d; int i; printf("输入日期:(以形如2024-12-16这样的形式输入)\n"); for (i = 0; i < 3; ++i) { input(&d); printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d)); } } void test2() { Date Alice_birth, Bob_birth; int i; int ans; printf("输入Alice和Bob出生日期:(以形如2024-12-16这样的形式输入)\n"); for (i = 0; i < 3; ++i) { input(&Alice_birth); input(&Bob_birth); ans = compare_dates(Alice_birth, Bob_birth); if (ans == 0) printf("Alice和Bob一样大\n\n"); else if (ans == -1) printf("Alice比Bob大\n\n"); else printf("Alice比Bob小\n\n"); } } int main() { printf("测试1: 输入日期, 打印输出这是一年中第多少天\n"); test1(); printf("\n测试2: 两个人年龄大小关系\n"); test2(); }
任务6:
#include <stdio.h> #include <string.h> enum Role { admin, student, teacher}; char* name[3] = { "admin","student","teacher" }; typedef struct { char username[20]; // 用户名 char password[20]; // 密码 enum Role type; // 账户类型 } Account; void output(Account x[], int n) { Account* p; int i,j; for (p = x; p < x + n; p++) { for (j = 0; *((p->password) + j) != '\0'; j++) { *((p->password) + j) = '*'; } } for (i = 0; i < 6; i++) { printf("%-10s%-15s%s\n", x[i].username, x[i].password, name[x[i].type]); } } // 输出账户数组x中n个账户信息,其中,密码用*替代显示 int main() { Account x[] = { {"A1001", "123456", student}, {"A1002", "123abcdef", student}, {"A1009", "xyz12121", student}, {"X1009", "9213071x", admin}, {"C11553", "129dfg32k", teacher}, {"X3005", "921kfmg917", student} }; int n; n = sizeof(x) / sizeof(Account); output(x, n); return 0; }
任务7:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> typedef struct { char name[20]; // 姓名 char phone[12]; // 手机号 int vip; // 是否为紧急联系人,是取1;否则取0 } Contact; // 函数声明 void set_vip_contact(Contact x[], int n, char name[]); // 设置紧急联系人 void output(Contact x[], int n); // 输出x中联系人信息 void display(Contact x[], int n); // 按联系人姓名字典序升序显示信息,紧急联系人最先显示 #define N 10 int main() { Contact list[N] = { {"刘一", "15510846604", 0}, {"陈二", "18038747351", 0}, {"张三", "18853253914", 0}, {"李四", "13230584477", 0}, {"王五", "15547571923", 0}, {"赵六", "18856659351", 0}, {"周七", "17705843215", 0}, {"孙八", "15552933732", 0}, {"吴九", "18077702405", 0}, {"郑十", "18820725036", 0} }; int vip_cnt, i; char name[20]; printf("显示原始通讯录信息: \n"); output(list, N); printf("\n输入要设置的紧急联系人个数: "); scanf("%d", &vip_cnt); printf("输入%d个紧急联系人姓名:\n", vip_cnt); for (i = 0; i < vip_cnt; ++i) { scanf("%s", name); set_vip_contact(list, N, name); } printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n"); display(list, N); return 0; } // 补足函数set_vip_contact实现 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1) void set_vip_contact(Contact x[], int n, char name[]) { int i,j; for (j = 0; j < N; j++) { if (strcmp(name, x[j].name) == 0) { x[j].vip = 1; } } } // 补足函数display实现 // 功能: 显示联系人数组x中的联系人信息 // 按姓名字典序升序显示, 紧急联系人显示在最前面 void display(Contact x[], int n) { int i, j, cnt; Contact temp, * p; cnt = 0; for (i = 0, j = 0; i < n; i++) { if (x[i].vip == 1) { temp = x[i]; x[i] = x[j]; x[j] = temp; j++; cnt++; } } for (i = 0; i < cnt - 1; i++) { for (j = 0; j < cnt - 1; j++) { if (strcmp(x[j].name, x[j + 1].name) > 0) { temp = x[j]; x[j] = x[j + 1]; x[j + 1] = temp; } } } for (i = cnt; i < N - cnt - 1; i++) { for (j = cnt; j < N - cnt + 1; j++) { if (strcmp(x[j].name, x[j + 1].name) > 0) { temp = x[j]; x[j] = x[j + 1]; x[j + 1] = temp; } } } for (i = 0; i < n; i++) { printf("%-10s%-15s", x[i].name, x[i].phone); if (x[i].vip) printf("%5s", "*"); printf("\n"); } } void output(Contact x[], int n) { int i; for (i = 0; i < n; ++i) { printf("%-10s%-15s", x[i].name, x[i].phone); if (x[i].vip) printf("%5s", "*"); printf("\n"); } }
标签:name,int,++,vip,实验,printf,day From: https://www.cnblogs.com/keobo/p/18622530