首页 > 其他分享 >实验6

实验6

时间:2024-06-09 16:57:01浏览次数:27  
标签:int void printf 实验 year Date 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     for(i=0;i<N;i++)
42         printf("%-20s %-30s %-20s %.1f %10d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
43 }
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-1-i;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.0;
66     for(i=0;i<N;i++){
67         sum=sum+1.0*x[i].sales_price*x[i].sales_count;
68     }
69     return sum;
70 }

任务五

  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();   // 测试函数1: 测试某个日期时这一年第多少天                   
 18 void test2();   // 测试函数2: 测试两个日期先后顺序
 19 
 20 
 21 int main() {
 22     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
 23     test1();
 24 
 25     printf("\n测试2: 两个人年龄大小关系\n");
 26     test2();
 27 }
 28 
 29 // 测试函数实现
 30 void test1() {
 31     Date d;
 32     int i;
 33 
 34     printf("输入日期:(以形如2024-06-01这样的形式输入)\n");
 35     for(i = 0; i < 3; ++i) {
 36         input(&d);
 37         printf("%04d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
 38     }
 39 }
 40 
 41 void test2() {
 42     Date Alice_birth, Bob_birth;
 43     int i;
 44     int ans;
 45 
 46     printf("输入Alice和Bob出生日期:(以形如2005-08-11这样的形式输入)\n");
 47     for(i = 0; i < 3; ++i) {
 48         input(&Alice_birth);
 49         input(&Bob_birth);
 50         ans = compare_dates(Alice_birth, Bob_birth);
 51         
 52         if(ans == 0)
 53             printf("Alice和Bob一样大\n\n");
 54         else if(ans == -1)
 55             printf("Alice比Bob大\n\n");
 56         else
 57             printf("Alice比Bob小\n\n");
 58     }
 59 }
 60 
 61 // 补足函数input实现
 62 // 功能: 输入日期给pd指向的Date变量
 63 void input(Date *pd) {
 64     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 65 }
 66 
 67 // 补足函数day_of_year实现
 68 // 功能:返回日期d是这一年的第多少天
 69 int day_of_year(Date d) {
 70     int x[12]={31,0,31,30,31,30,31,31,30,31,30,31};
 71     int i,h=0;
 72     int sum=0;
 73     if(d.year%4==0&&d.year%100!=0||d.year%400==0)
 74         x[1]=29;
 75     else
 76         x[1]=28;
 77     for(i=0;i<d.month-1;i++){
 78         h=h+x[i];
 79     }
 80     sum=h+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{
 95         if(day_of_year(d1)>day_of_year(d2))
 96             return 1;
 97         else if(day_of_year(d1)<day_of_year(d2))
 98             return -1;
 99         else
100             return 0;
101     }
102 }

任务六

#include <stdio.h>
#include <string.h>

enum Role {admin, student, teacher};

typedef struct {
    char username[20];  // 用户名
    char password[20];  // 密码
    enum Role type;     // 账户类型
} Account;


// 函数声明
void output(Account x[], int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示

int main() {
    Account x[] = {{"A1001", "123456", student},
                    {"A1002", "123abcdef", student},
                    {"A1009", "xyz12121", student}, 
                    {"X1009", "9213071x", admin},
                    {"C11553", "129dfg32k", teacher},
                    {"X3005", "921kfmg917", student}};
    int n;
    n = sizeof(x)/sizeof(Account);
    output(x, n);

    return 0;
}

// 待补足的函数output()实现
// 功能:遍历输出账户数组x中n个账户信息
//      显示时,密码字段以与原密码相同字段长度的*替代显示
void output(Account x[], int n) {
    int i,j;
    char a[n][20];
    for(i=0;i<n;i++){
        strcpy(a[i],x[i].password);
        for(j=0;a[i][j];j++)
            a[i][j]='*';}
    for(i=0;i<n;i++){
        printf("%-10s%-15s",x[i].username,a[i]);
        switch(x[i].type){
            case 0:printf("admin");break;
            case 1:printf("student");break;
            case 2:printf("teacher");break;
        }
        printf("\n");
    }


}

 

标签:int,void,printf,实验,year,Date,31
From: https://www.cnblogs.com/800225shi/p/18239732

相关文章

  • 实验6
    点击查看代码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;......
  • 实验六
    task4#include<stdio.h>#include<stdlib.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;......
  • 实验6 C语言结构体、枚举应用编程
    //task4.c#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价int......
  • 实验6 C语言结构体、枚举应用编程
    task.1#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试#include<stdlib.h>typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//......
  • 实验6
    task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验6 C语言结构体、枚举应用编程
    task1.c1//P286例8.172//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块3//打印不及格学生信息和所有学生信息程分别调用45#include<stdio.h>6#include<string.h>7#defineN3//运行程序输入测试时,可以把这个数组......
  • 实验6
    task4点击查看代码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}B......
  • 实验六
    task41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • DOM型xss靶场实验
    DOM型xss可以使用js去控制标签中的内容。我使用的是一个在线的dom型xss平台,靶场链接:Challenges第一关MaSpaghet!:MaSpaghet!关卡<h2id="spaghet"></h2><script>spaghet.innerHTML=(newURL(location).searchParams.get('somebody')||"Somebody&qu......
  • C语言实验六
    #include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doublepe......