首页 > 其他分享 >实验

实验

时间:2024-12-22 19:42:40浏览次数:3  
标签:int void printf month 实验 year day

任务4

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];          
 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-5327-6082-4", "门将之死", "罗纳德.伦", 42, 51},
18                   {"978-7-308-17047-5", "自由与爱之地:入以色列记", "云也退", 49 , 30},
19                   {"978-7-5404-9344-8", "伦敦人", "克莱格泰勒", 68, 27},
20                   {"978-7-5447-5246-6", "软件体的生命周期", "特德姜", 35, 90}, 
21                   {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
22                   {"978-7-5133-5750-0", "主机战争", "布莱克.J.哈里斯", 128, 42},
23                   {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44},
24                   {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
25                   {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
26                   {"978-7-229-14156-1", "源泉", "安.兰德", 84, 59}};
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("%10s  %-25s %-20s %-10.0lf %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
42     } 
43 }
44 
45 void sort(Book x[], int n){
46     int i, j;
47     Book t;
48     for(i = 0; i < n - 1; ++i) {
49         for(j = 0; j < n - 1 - i; ++j) {
50             if(x[j].sales_count < x[j + 1].sales_count) {
51                 t = x[j];
52                 x[j] = x[j + 1];
53                 x[j + 1] = t;
54             }
55         }
56     }
57 }
58 
59 double sales_amount(Book x[], int n){
60     int i;
61     double a=0; 
62     for(i=0;i<n;++i){
63         a+=x[i].sales_price*x[i].sales_count; 
64     }
65 return a;
66 }

任务5

 1 #include <stdio.h>
 2 
 3 typedef struct {
 4     int year;
 5     int month;
 6     int day;
 7 } Date;
 8 
 9 void input(Date *pd);                   
10 int day_of_year(Date d);                
11 int compare_dates(Date d1, Date d2);    
12 
13 
14 void test1() {
15     Date d;
16     int i;
17 
18     printf("输入日期:(以形如2024-12-16这样的形式输入)\n");
19     for(i = 0; i < 3; ++i) {
20         input(&d);
21         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
22     }
23 }
24 
25 void test2() {
26     Date Alice_birth, Bob_birth;
27     int i;
28     int ans;
29 
30     printf("输入Alice和Bob出生日期:(以形如2024-12-16这样的形式输入)\n");
31     for(i = 0; i < 3; ++i) {
32         input(&Alice_birth);
33         input(&Bob_birth);
34         ans = compare_dates(Alice_birth, Bob_birth);
35         
36         if(ans == 0)
37             printf("Alice和Bob一样大\n\n");
38         else if(ans == -1)
39             printf("Alice比Bob大\n\n");
40         else
41             printf("Alice比Bob小\n\n");
42     }
43 }
44 
45 int main() {
46     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
47     test1();
48 
49     printf("\n测试2: 两个人年龄大小关系\n");
50     test2();
51 }
52 
53 
54 void input(Date *pd) {
55    scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
56 }
57 
58 
59 int day_of_year(Date d) {
60    int days_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
61     int day_count = 0;
62 int i;
63 
64     if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0)) {
65         days_month[1] = 29; 
66     }
67 
68     for (i = 0; i < d.month - 1; i++) {
69         day_count += days_month[i];
70     }
71 
72 
73     day_count += d.day;
74 
75     return day_count;
76 }
77 
78 int compare_dates(Date d1, Date d2) {
79     if (d1.year != d2.year) {
80         return d1.year < d2.year ? -1 : 1;
81     }
82 
83     if (d1.month != d2.month) {
84         return d1.month < d2.month ? -1 : 1;
85     }
86 
87     if (d1.day != d2.day) {
88         return d1.day < d2.day ? -1 : 1;
89     }
90 
91     return 0; 
92 }

任务6

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 100 
 4 
 5 
 6 enum Role {admin, student, teacher};
 7 
 8 typedef struct {
 9     char username[20];  
10     char password[20];  
11     enum Role type;     
12 } Account;
13 
14 
15 
16 void output(Account x[], int n);    
17 
18 int main() {
19     Account x[] = {{"A1001", "123456", student},
20                     {"A1002", "123abcdef", student},
21                     {"A1009", "xyz12121", student}, 
22                     {"X1009", "9213071x", admin},
23                     {"C11553", "129dfg32k", teacher},
24                     {"X3005", "921kfmg917", student}};
25     int n;
26     n = sizeof(x)/sizeof(Account);
27     output(x, n);
28 
29     return 0;
30 }
31 
32 
33 void output(Account x[], int n) {
34 int i,j;
35 int a;
36 char b[N][20]; 
37 for(i=0;i<n;++i){ 
38   printf("%-10s",x[i].username);
39 a=strlen(x[i].password);
40 for(j=0;j<a;j++){
41 
42     b[i][j]='*';
43 
44 }
45  b[i][a] = '\0'; 
46 printf("%-10s",b[i]);
47 
48 
49       switch (x[i].type) {
50             case admin:
51                 printf("%30s","admin");
52                 break;
53             case student:
54                 printf("%30s","student");
55                 break;
56             case teacher:
57                 printf("%30s","teacher");
58                 break;
59             default:
60                 printf("%30s","unknown");
61         }
62     
63     printf("\n");
64   
65     
66 }
67 }

任务7

  1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 typedef struct {
  5     char name[20];      
  6     char phone[12];    
  7     int  vip;           
  8 } Contact; 
  9 
 10 
 11 
 12 void set_vip_contact(Contact x[], int n, char name[]);  
 13 void output(Contact x[], int n);    
 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     
 38     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
 39     for(i = 0; i < vip_cnt; ++i) {
 40         scanf("%s", name);
 41         set_vip_contact(list, N, name);
 42     }
 43 
 44     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
 45     display(list, N);
 46 
 47     return 0;
 48 }
 49 
 50 
 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 }
 59 
 60 
 61 void display(Contact x[], int n) {
 62     int i,j,k;
 63     Contact a,b;
 64          
 65          for (i = 0; i < n - 1; i++) {
 66         for (j = 0; j < n - 1 - i; j++) {
 67         
 68          if (strcmp(x[j].name, x[j + 1].name) > 0){
 69                 a = x[j];
 70                 x[j] = x[j + 1];
 71                 x[j + 1] = a;
 72             }
 73    
 74         }
 75     }
 76     
 77     
 78        for (i = 0; i < n - 1; i++) {
 79         for (j = 0; j < n - 1 - i; j++) {
 80                  if (x[j + 1].vip == 1 && x[j].vip == 0) {
 81                 a = x[j];
 82                 x[j] = x[j + 1];
 83                 x[j + 1] = a;
 84             }
 85         
 86     }
 87 }    
 88 
 89       
 90     
 91     for(k=0;k<n;++k){
 92     printf("%-20s %-20s ",x[k].name,x[k].phone);
 93            if(x[k].vip){
 94             printf("%5s", "*");
 95         printf("\n");
 96     }
 97 else
 98 printf("\n");
 99 }
100     
101 }
102 
103 void output(Contact x[], int n) {
104     int i;
105 
106     for(i = 0; i < n; ++i) {
107         printf("%-10s%-15s", x[i].name, x[i].phone);
108         if(x[i].vip)
109             printf("%5s", "*");
110         printf("\n");
111     }
112 }

 

标签:int,void,printf,month,实验,year,day
From: https://www.cnblogs.com/qjj1004/p/18622430

相关文章

  • 实验6
    任务4源代码1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • ACL 和 NAT(配eNSP实验模拟)
    目录一:ACL(访问控制列表)1.什么是ACL(访问控制列表)2.ACL概述和应用3.ACL工作原理:4.ACL种类5.ACL组成和规则​6.ACL的匹配位置。7.acl实例模拟二.NAT(网络地址转换)1.NAT工作原理如下:​2.NAT技术转换流程如下:​3.静态NAT案例分析1.动态NAT:4.Easy-IP5.NATPT(端口映......
  • 实验 6
    任务11#include<stdio.h>2#include<string.h>3#defineN345typedefstructstudent{6intid;7charname[20];8charsubject[20];9doubleperf;10double......
  • 实验6 模板类、文件I\O和异常处理
    实验任务4#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intn,intvalue=0);Vector(Vector<T>&v);~Vector();intget_size()......
  • 实验六
    task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验6 C语言结构体、枚举应用编程
    4.实验任务4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidoutput(Bookx[],intn);voidsort(Bookx[],intn);......
  • 实验6
    task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验6
     任务4:源代码:1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count......
  • 实验6 C语言结构体、枚举应用编程
    实验一://P286例8.17//对教材示例代码作了微调,把输出学生信息单独编写成一个函数模块//打印不及格学生信息、打印所有学生信息均调用该模块实现#include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把N改小一些输入测试typedefstr......
  • 实验六
    Task4Vector.hpp:1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<exception>67template<typenameT>8classVector{9private:10T*elements;11size_tsize_;1213pu......