首页 > 其他分享 >实验6

实验6

时间:2023-12-12 09:22:22浏览次数:30  
标签:int void 978 实验 printf Date day

task4.c

 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 void output(Book x[], int n){
38     int i;
39     printf("ISBN号           书名               作者              售价        销售册数\n"); 
40     for(i=0;i<n;i++){
41     printf("%-20s %-20s %-20s %-20.1lf %-20d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
42 }
43 }
44 void sort(Book x[], int n){
45     int i,j;
46     for(j=0;j<n;j++){
47     for(i=0;i<n-1;i++){
48         if(x[i].sales_count<x[i+1].sales_count){
49             x[n]=x[i];
50             x[i]=x[i+1];
51             x[i+1]=x[n];
52         }
53         
54     }
55 }
56     
57 }
58 double sales_amount(Book x[], int n){
59     int i;
60     double cnt;
61     double sum=0;
62     for(i=0;i<n;i++){
63         cnt=x[i].sales_count*x[i].sales_price;
64         sum+=cnt;
65     }
66     return sum;
67 }

 

task5.c

  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 day=0,i;
 66    for(i=1;i<d.month;i++){
 67        switch (i){
 68            case 1: day+=31;break;
 69            case 2:
 70            if(d.year%4==0&&d.year%100!=0||d.year%400==0){
 71            day+=29;break;
 72            }else{
 73                day+=28;break;
 74            }
 75            case 3:day+=31;break;
 76            case 4:day+=30;break;
 77            case 5:day+=31;break;
 78            case 6:day+=30;break;
 79            case 7:day+=31;break;
 80            case 8:day+=31;break;
 81            case 9:day+= 30;break;
 82            case 10:day+=31;break;
 83            case 11:day+=30;break;
 84            case 12:day+=31;break;
 85    }
 86    }
 87    day+=d.day;
 88    return day;
 89 }
 90 
 91 // 补足函数compare_dates实现
 92 // 功能:比较两个日期: 
 93 // 如果d1在d2之前,返回-1;
 94 // 如果d1在d2之后,返回1
 95 // 如果d1和d2相同,返回0
 96 int compare_dates(Date d1, Date d2) {
 97     d1.day=day_of_year(d1);
 98     d2.day=day_of_year(d2);
 99     if(d1.day>d2.day) return 1;
100     if(d1.day<d2.day) return -1;
101     if(d1.day=d2.day) return 0;
102 }

 

task6.c

 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 cnt[20];
35    int i,j;
36    for(i=0;i<n;i++){
37   cnt[i]=strlen(x[i].password);
38     printf("%-20s",x[i].username);
39     for(j=0;j<cnt[i];j++){
40     printf("*");
41 }
42  switch(x[i].type){
43             case 1:printf("        student");break;
44             case 0:printf("        admin");break;
45             case 2:printf("        teacher");break;
46         }-
47         printf("\n");
48     }
49 }

 


task4.c运行截图

 

task5.c运行截图

 

task6.c运行截图

 

标签:int,void,978,实验,printf,Date,day
From: https://www.cnblogs.com/homeh/p/17896043.html

相关文章

  • 实验6
    #include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//平时成绩doublemid;//期中成绩doublefinal......
  • 实验6
    实验任务1 实验任务2 实验任务3_1  实验任务3_2 实验任务4 实验任务5 实验任务6 ......
  • 实验6 C语言结构体,枚举应用编程(附实验5 C语言指针应用编程)
    实验6一,实验目的二,实验准备三,实验内容1,实验任务1task1.c1#include<stdio.h>2#include<string.h>3#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试45typedefstructstudent{6intid;//学号7......
  • 实验6
    task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验六
    任务一代码1//P286例8.172//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块3//打印不及格学生信息和所有学生信息程分别调用45#include<stdio.h>6#include<string.h>7#defineN3//运行程序输入测试时,可以把这个......
  • 实验6
    test4代码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;vo......
  • 实验六
    task.4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voido......
  • 实验6 C语言结构体、枚举应用编程
    四、实验结论4.实验任务4task4.c1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9......
  • 实验6
     task4代码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];6charname[80];7charauthor[80];8doublesales_price;9intsales_count;10}Book;1112voidoutput(Book......
  • 实验六 C语言结构体、枚举应用编程
    实验四源代码#include<stdio.h>#include<string.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;......