首页 > 其他分享 >实验6

实验6

时间:2023-12-17 15:46:01浏览次数:24  
标签:int void ans day 实验 printf 31

 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("%20s %20s %5s %.1f   %d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
44 } 
45 
46 // 待补足:函数sort()实现
47 // ×××
48 void sort(Book x[],int n){
49     int i, j;
50     Book t;
51     
52     for(i = 0; i < n-1; i++)
53       for(j = 0; j < n-1-i; j++)
54         if(x[j].sales_count < x[j+1].sales_count) {
55             t = x[j];
56             x[j] = x[j+1];
57             x[j+1] = t;
58         }
59 }
60 
61 // 待补足:函数sales_count()实现
62 // ×××
63 double sales_amount(Book x[],int n){
64     int i,sum=0,s;
65     for(i=0;i<n;++i){
66         s=(x[i].sales_price)*(x[i].sales_count);
67         sum+=s;
68     }
69     return sum;
70 }

实验4

实验5

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

实验6

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

实验7

  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(name,x[i].name)==0){
 55             x[i].vip=1;
 56         }
 57     }
 58 }
 59 
 60 // 补足函数display实现
 61 // 功能: 显示联系人数组x中的联系人信息
 62 //      按姓名字典序升序显示, 紧急联系人显示在最前面
 63 void display(Contact x[], int n) {
 64     int i,j,cnt=0;
 65     Contact y;
 66     for(i=0;i<n;++i){
 67         if(x[i].vip==1){
 68             y=x[i];
 69             x[i]=x[cnt];
 70             x[cnt]=y;
 71             ++cnt;
 72         }
 73     }
 74     for(i=cnt;i<n-1;++i){
 75         for(j=i+1;j<n;++j){
 76             if(strcmp(x[i].name,x[j].name)==1){
 77                 y=x[i];
 78                 x[i]=x[j];
 79                 x[j]=y;
 80             }
 81         }
 82     }
 83     for(i = 0; i < n; ++i) {
 84         printf("%-10s%-15s", x[i].name, x[i].phone);
 85         if(x[i].vip)
 86             printf("%5s", "*");
 87         printf("\n");
 88     }
 89 }
 90 
 91 void output(Contact x[], int n) {
 92     int i;
 93 
 94     for(i = 0; i < n; ++i) {
 95         printf("%-10s%-15s", x[i].name, x[i].phone);
 96         if(x[i].vip)
 97             printf("%5s", "*");
 98         printf("\n");
 99     }
100 }

 

标签:int,void,ans,day,实验,printf,31
From: https://www.cnblogs.com/almost2223/p/17909132.html

相关文章

  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献1简述你完成的工作作为组长,分配任务到各个小组组员上,组织统筹组员完成各项工作。参与组内博客和文档的编写工作。参与后端设计的代码编写和加密通信过程的实现2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为5535......
  • 实验6
    任务4#include<iostream>#include"Vector.hpp"usingnamespacestd;template<typenameT>voidoutput(constVector<T>&v){for(autoi=0;i<v.get_size();i++){cout<<v.at(i)<<",";......
  • 实验6 C语言结构体、枚举应用编程
    实验任务1源代码:1#include<stdio.h>2#include<string.h>3#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试45typedefstructstudent{6intid;//学号7charname[20];//姓名8......
  • 实验六
    #pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:intsize;T*ptr;public:Vector(intn):size{n}{if(n<0)throwstd::leng......
  • 实验7
    4.实验任务4task4.c源码:#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intcnt=0;fp=fopen("data4.txt","r");if(fp==NULL){printf(&qu......
  • 实验6
    4.实验任务4task4.c源码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册......
  • 鸿蒙小车之多任务调度实验
    说到鸿蒙我们都会想到华为mate60:遥遥领先!我们一直领先!我们这个小车也是采用的是鸿蒙操作系统,学习鸿蒙小车,让你遥遥领先于你的同学。@TOC前言本专栏将依次介绍鸿蒙小车的内核实验,硬件实验,wifi实验。一、什么是任务?为什么要有任务任务是操作系统(RTOS)中的基本组成单元,它们为嵌入式......
  • 实验七
    task4.c#include<stdio.h>intmain(){ charch; intcnt=0;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){ printf("failtoopen\n"); return1; }while(1){ch=fgetc(fp);......
  • 实验6-模板类、文件I/O和异常处理
    Vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>67template<classT>8classVector{9private:10T*arr;11intsize;1213public:14Vector():arr(nu......
  • 实验六
    task4.cpp:点击查看代码#include<iostream>#include"vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);for(autoi=0;i<n;++i)x1.at(i)=i*0.7;output......