首页 > 其他分享 >实验6

实验6

时间:2023-12-17 21:59:09浏览次数:23  
标签:return int 31 30 实验 printf day

实验4

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

 实验5

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

 实验6

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

 

标签:return,int,31,30,实验,printf,day
From: https://www.cnblogs.com/huangyi-bit/p/17897650.html

相关文章

  • 实验六 模板类,文件io和异常处理
    实验任务4#pragmaonce#include<iostream>#include<stdexcept>usingstd::cout;usingstd::endl;template<typenameT>classVector{public://构造函数,默认大小为0Vector(intn=0):size(n),data(newT[n]){if(n<0)......
  • 实验6
    1.实验任务41#include<iostream>2#include<stdexcept>34template<typenameT>5classVector{6private:7T*data;8size_tsize;9public:10Vector(size_tn):size(n){11data=newT[size];12......
  • 实验6 模板类、文件I/O和异常处理
    实验任务4Vector.hpp#pragmaonce#include<iostream>#include<stdexcept>#include<string>usingnamespacestd;template<typenameT>classVector{public:Vector(intx=0,constT&value=T());Vector(constVecto......
  • 实验6 模板类、文件I/O和异常处理
    Task4: vector.hpp:#include<iostream>#include<string>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector():data(nullptr),size(0){}Vector......
  • 实验7
    实验任务4:\#include<stdio.h>intmain(){inti=0;chars;FILE*fp;fp=fopen("data4.txt","r");while(1){s=fgetc(fp);if(s==EOF){break;}elseif(s=='�......
  • 实验六
    实验代码实验任务4.hpp#include<iostream>#include<stdexcept>#include<cassert>usingnamespacestd;template<typenameT>classVector{private:T*ptr;intsize;public:Vector(intsize){if(size<0)throwstd:......
  • 实验六
    任务四1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 实验六
    task4.hpp#include<iostream>#include<stdexcept>#include<cassert>usingnamespacestd;template<typenameT>classVector{private:T*ptr;intsize;public:Vector(intsize){if(size<0)......
  • 实验六
    4.hpp#include<iostream>#include<stdexcept>template<typenameT>classVector{private:intsize;T*p;public:Vector<T>(ints);Vector<T>(ints,Tvalue);Vector<T>(Vector<T>&x);......
  • 渗透测试实验报告一
    1. 实验目的和要求实验目的:理解网络扫描、网络侦察的作用;通过搭建网络渗透测试平台,了解并熟悉常用搜索引擎、扫描工具的应用,通过信息收集为下一步渗透工作打下基础。系统环境:KaliLinux2、Windows网络环境:交换网络结构2.实验步骤 1:利用Google语法搜索site:mit.eduinti......