首页 > 其他分享 >实验六

实验六

时间:2023-12-15 13:55:49浏览次数:22  
标签:int 31 28 实验 printf sum d1

task4

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];          // isbn号
 6     char name[80];          // 书名
 7     char author[80];        // 作者
 8     double sales_price;     // 售价
 9     int  sales_count;       // 销售册数
10 } Book;
11 
12 void output(Book x[], int n);
13 void sort(Book x[], int n);
14 double sales_amount(Book x[], int n);
15 
16 int main() {
17     Book x[N] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59},
18                  {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16},
19                  {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27},
20                  {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
21                  {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42},
22                  {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39},
23                  {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
24                  {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31},
25                  {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
26                  {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}};
27     
28     printf("图书销量排名: \n");
29     sort(x, N);
30     output(x, N);
31 
32     printf("\n图书销售总额: %.2f\n", sales_amount(x, N));
33     
34     return 0;
35 }
36 
37 // 待补足:函数output()实现
38 // ×××
39 void output(Book x[], int n){
40     int i;
41     printf("ISBN号               书名                         作者                      售价       销售册数\n");
42     for(i=0;i<n;++i)
43         printf("%s    %-25s    %-20s      %-7.1f    %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
44 } 
45 // 待补足:函数sort()实现
46 // ×××
47 void sort(Book x[], int n){
48     int i,j;
49     Book t;
50     for(i=0;i<n-1;++i){
51         for(j=0;j<n-i-1;++j){
52             if(x[j].sales_count<x[j+1].sales_count){
53                 t=x[j];
54                 x[j]=x[j+1];
55                 x[j+1]=t;
56             }
57         }
58     }
59 }
60 
61 // 待补足:函数sales_count()实现
62 // ×××
63 double sales_amount(Book x[], int n){
64     int i;
65     double sum=0;
66     for(i=0;i<n;++i)
67         sum=sum+x[i].sales_price*x[i].sales_count;
68     return sum;    
69 }
View Code

task5

  1 #include <stdio.h>
  2 
  3 typedef struct {
  4     int year;
  5     int month;
  6     int day;
  7 } Date;
  8 
  9 // 函数声明
 10 void input(Date *pd);                   // 输入日期给pd指向的Date变量
 11 int day_of_year(Date d);                // 返回日期d是这一年的第多少天
 12 int compare_dates(Date d1, Date d2);    // 比较两个日期: 
 13                                         // 如果d1在d2之前,返回-1;
 14                                         // 如果d1在d2之后,返回1
 15                                         // 如果d1和d2相同,返回0
 16 
 17 void test1() {
 18     Date d;
 19     int i;
 20 
 21     printf("输入日期:(以形如2023-12-11这样的形式输入)\n");
 22     for(i = 0; i < 3; ++i) {
 23         input(&d);
 24         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
 25     }
 26 }
 27 
 28 void test2() {
 29     Date Alice_birth, Bob_birth;
 30     int i;
 31     int ans;
 32 
 33     printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n");
 34     for(i = 0; i < 3; ++i) {
 35         input(&Alice_birth);
 36         input(&Bob_birth);
 37         ans = compare_dates(Alice_birth, Bob_birth);
 38         
 39         if(ans == 0)
 40             printf("Alice和Bob一样大\n\n");
 41         else if(ans == -1)
 42             printf("Alice比Bob大\n\n");
 43         else
 44             printf("Alice比Bob小\n\n");
 45     }
 46 }
 47 
 48 int main() {
 49     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
 50     test1();
 51 
 52     printf("\n测试2: 两个人年龄大小关系\n");
 53     test2();
 54 }
 55 
 56 // 补足函数input实现
 57 // 功能: 输入日期给pd指向的Date变量
 58 void input(Date *pd) {
 59     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 60 }
 61 
 62 // 补足函数day_of_year实现
 63 // 功能:返回日期d是这一年的第多少天
 64 int day_of_year(Date d) {
 65     int sum;
 66      switch(d.month){
 67          case 1: sum=0;break;
 68          case 2: sum=31;break;
 69          case 3: sum=31+28;break;
 70          case 4: sum=31*2+28;break;
 71          case 5: sum=31*2+28+30;break;
 72          case 6: sum=31*3+28+30;break;
 73          case 7: sum=31*3+28+30*2;break;
 74          case 8: sum=31*4+28+30*2;break;
 75          case 9: sum=31*5+28+30*2;break;
 76          case 10: sum=31*5+28+30*3;break;
 77          case 11: sum=31*6+28+30*3;break;
 78          case 12: sum=31*6+28+30*4;break;
 79      }
 80      sum+=d.day;
 81      return sum;
 82 }
 83 
 84 // 补足函数compare_dates实现
 85 // 功能:比较两个日期: 
 86 // 如果d1在d2之前,返回-1;
 87 // 如果d1在d2之后,返回1
 88 // 如果d1和d2相同,返回0
 89 int compare_dates(Date d1, Date d2) {
 90     if(d1.year<d2.year)
 91         return -1;
 92     else if(d1.year>d2.year)
 93         return 1;
 94     else if(d1.year=d2.year){
 95         if(d1.month<d2.month)
 96             return -1;
 97         else if(d1.month>d2.month)
 98             return 1;
 99         else if(d1.month=d2.month){
100             if(d1.day<d2.day)
101                 return -1;
102             else if(d1.day>d2.day)
103                 return 1;
104             else if(d1.day=d2.day)
105                 return 0;
106         }
107     }
108 }
View Code

task6

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 enum Role {admin, student, teacher};
 5 
 6 typedef struct {
 7     char username[20];  // 用户名
 8     char password[20];  // 密码
 9     enum Role type;     // 账户类型
10 } Account;
11 
12 
13 // 函数声明
14 void output(Account x[], int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示
15 
16 int main() {
17     Account x[] = {{"A1001", "123456", student},
18                     {"A1002", "123abcdef", student},
19                     {"A1009", "xyz12121", student}, 
20                     {"X1009", "9213071x", admin},
21                     {"C11553", "129dfg32k", teacher},
22                     {"X3005", "921kfmg917", student}};
23     int n;
24     n = sizeof(x)/sizeof(Account);
25     output(x, n);
26 
27     return 0;
28 }
29 
30 // 待补足的函数output()实现
31 // 功能:遍历输出账户数组x中n个账户信息
32 //      显示时,密码字段以与原密码相同字段长度的*替代显示
33 void output(Account x[], int n) {
34     int i,j;
35     char role[3][10]={"admin","student","teacher"};
36     for(i=0;i<n;i++){
37         printf("%s\t",x[i].username);
38         for(j=0;j<strlen(x[i].password);j++)
39             printf("*");
40         printf("  \t%s\n",role[x[i].type]);
41     } 
42 }
View Code

task7

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 typedef struct {
 5     char name[20];      // 姓名
 6     char phone[12];     // 手机号
 7     int  vip;           // 是否为紧急联系人,是取1;否则取0
 8 } Contact; 
 9 
10 
11 // 函数声明
12 void set_vip_contact(Contact x[], int n, char name[]);  // 设置紧急联系人
13 void output(Contact x[], int n);    // 输出x中联系人信息
14 void display(Contact x[], int n);   // 按联系人姓名字典序升序显示信息,紧急联系人最先显示
15 
16 
17 #define N 10
18 int main() {
19     Contact list[N] = {{"刘一", "15510846604", 0},
20                        {"陈二", "18038747351", 0},
21                        {"张三", "18853253914", 0},
22                        {"李四", "13230584477", 0},
23                        {"王五", "15547571923", 0},
24                        {"赵六", "18856659351", 0},
25                        {"周七", "17705843215", 0},
26                        {"孙八", "15552933732", 0},
27                        {"吴九", "18077702405", 0},
28                        {"郑十", "18820725036", 0}};
29     int vip_cnt, i;
30     char name[20];
31 
32     printf("显示原始通讯录信息: \n"); 
33     output(list, N);
34 
35     printf("\n输入要设置的紧急联系人个数: ");
36     scanf("%d", &vip_cnt);
37     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
38     for(i = 0; i < vip_cnt; ++i) {
39         scanf("%s", name);
40         set_vip_contact(list, N, name);
41     }
42 
43     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
44     display(list, N);
45 
46     return 0;
47 }
48 
49 // 补足函数set_vip_contact实现
50 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1)
51 void set_vip_contact(Contact x[], int n, char name[]) {
52     int i;
53     for(i=0;i<n;i++)
54         if(strcmp(x[i].name,name)==0)
55             x[i].vip=1;
56 }
57 
58 // 补足函数display实现
59 // 功能: 显示联系人数组x中的联系人信息
60 //      按姓名字典序升序显示, 紧急联系人显示在最前面
61 void display(Contact x[], int n) {
62     Contact t;
63     int i,j;
64     for(i=0;i<n-1;++i){
65         for(j=0;j<n-i-1;++j){
66             if(strcmp(x[j].name,x[j+1].name)>0){
67                 t=x[j];
68                 x[j]=x[j+1];
69                 x[j+1]=t;
70             }
71         }
72     }
73     for(i=0;i<n-1;++i){
74         for(j=0;j<n-i-1;++j){
75             if(x[j].vip<x[j+1].vip){
76                 t=x[j];
77                 x[j]=x[j+1];
78                 x[j+1]=t;
79             }
80         }
81     }
82     output(x,n);
83 }
84 
85 void output(Contact x[], int n) {
86     int i;
87 
88     for(i = 0; i < n; ++i) {
89         printf("%-10s%-15s", x[i].name, x[i].phone);
90         if(x[i].vip)
91             printf("%5s", "*");
92         printf("\n");
93     }
94 }
View Code

 

标签:int,31,28,实验,printf,sum,d1
From: https://www.cnblogs.com/zjl-love-wsx/p/17903233.html

相关文章

  • 实验六
    实验四 源代码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count......
  • 什么是LIS系统?实验室信息管理系统是什么?
    在医疗信息化改革的这个大背景下,很多医院都在不断完善信息化建设,有些人还不知道医院实验室信息管理系统。本文章仅以个人的理解整理出来,本文从LIS系统定义、LIS系统实现了哪些功能、LIS系统解决了实验室实际工作中的哪些问题,进行分享;希望能帮助更多基础医疗单位了解【实验室信息管......
  • 实验6
     #include<stdio.h>#include<string.h>#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//平时成绩doublemid;//期中成绩doublef......
  • 实验六
    task_4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voido......
  • 实验6
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#defineN10typedefstruct{charisbn[20];charname[80];charauthor[80];doublesales_price;intsales_count;}Book;voidoutput(Bookx[],intn);voi......
  • 实验七
    实验一//将图书信息写入文本文件data1.txt#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》&q......
  • 实验7
    四、实验结论1.实验任务1思考*:把line34改成n=i;,重新编译,再次运行程序,观察结果,对比运行结果是否有不同。如有不同,尝试分析原因。若改成n=1,则有以下运行截图:尝试分析原因:feof函数实际上是根据读取到最后一个位置的内容来判断的。所以要多走一字节。 2.实验任务2 3......
  • 实验7
    实验4 实验5 实验6  ......
  • 实验6_C语言结构体、枚举应用编程
    4.task_41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];6charname[80];7charauthor[80];8doublesales_price;9intsales_count;10}Book;1112voidoutput(Bookx[],intn);13voids......
  • java实验
    零.前置芝士(可以不了解)信息量定义信号量(semaphore)是操作系统中用来解决并发中的互斥和同步问题的一种方法。是可以用来保证两个或多个关键代码段不被并发调用。目的类似计数器,常用在多线程同步任务上,信号量可以在当前线程某个任务完成后,通知别的线程,再进行别的任务。同步:处......