首页 > 其他分享 >实验6

实验6

时间:2023-12-11 20:14:18浏览次数:27  
标签:int void else 实验 printf month day

 

task4

代码

 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-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    %-30s    %-20s    %.1lf    %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-i-1;j++){
50             if(x[j+1].sales_count>x[j].sales_count){
51                 t=x[j];
52                 x[j]=x[j+1];
53                 x[j+1]=t;
54             }
55         }
56     } 
57     
58 }
59 
60 double sales_amount(Book x[], int n){
61     double count=0;
62     int i;
63     for(i=0;i<n;i++){
64         count+=x[i].sales_count*x[i].sales_price;
65     }
66     return count;
67 }

结果

 

task5

代码

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

结果

 

task6

代码

 

 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 void output(Account x[], int n);    
14 
15 int main() {
16     Account x[] = {{"A1001", "123456", student},
17                     {"A1002", "123abcdef", student},
18                     {"A1009", "xyz12121", student}, 
19                     {"X1009", "9213071x", admin},
20                     {"C11553", "129dfg32k", teacher},
21                     {"X3005", "921kfmg917", student}};
22     int n;
23     n = sizeof(x)/sizeof(Account);
24     output(x, n);
25 
26     return 0;
27 }
28 
29 
30 void output(Account x[], int n) {
31     int i,j;
32     
33     for(i=0;i<n;i++){
34         printf("%-20s",x[i].username);
35         int count=0;
36         count=strlen(x[i].password);
37         while(count!=0){
38             printf("*");
39             count--;
40         }
41         
42         j=20;
43         while(j>strlen(x[i].password)){
44             printf(" ");
45             j--;
46         }
47         
48         switch(x[i].type){
49             case admin:printf("admin\n");break;
50             case student:printf("student\n");break;
51             case teacher:printf("teacher\n");break;
52         }
53         
54     }
55 }

 

结果

 

task7

代码

 

  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 void set_vip_contact(Contact x[], int n, char name[]);  
 12 void output(Contact x[], int n);    
 13 void display(Contact x[], int n);   
 14 
 15 
 16 #define N 10
 17 int main() {
 18     Contact list[N] = {{"刘一", "15510846604", 0},
 19                        {"陈二", "18038747351", 0},
 20                        {"张三", "18853253914", 0},
 21                        {"李四", "13230584477", 0},
 22                        {"王五", "15547571923", 0},
 23                        {"赵六", "18856659351", 0},
 24                        {"周七", "17705843215", 0},
 25                        {"孙八", "15552933732", 0},
 26                        {"吴九", "18077702405", 0},
 27                        {"郑十", "18820725036", 0}};
 28     int vip_cnt, i;
 29     char name[20];
 30 
 31     printf("显示原始通讯录信息: \n"); 
 32     output(list, N);
 33 
 34     printf("\n输入要设置的紧急联系人个数: ");
 35     scanf("%d", &vip_cnt);
 36     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
 37     for(i = 0; i < vip_cnt; ++i) {
 38         scanf("%s", name);
 39         set_vip_contact(list, N, name);
 40     }
 41 
 42     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
 43     display(list, N);
 44 
 45     return 0;
 46 }
 47 
 48 
 49 void set_vip_contact(Contact x[], int n, char name[]) {
 50     int i;
 51     for(i=0;i<n;i++){
 52         if(strcmp(x[i].name,name)==0){
 53             x[i].vip=1;
 54         }
 55     }
 56     
 57 }
 58 
 59 
 60 void display(Contact x[], int n) {
 61     Contact t;
 62     int i,j;
 63     for(i=0;i<n-1;i++){
 64         for(j=0;j<n-i-1;j++){
 65             if(x[j].vip==0&&x[j+1].vip==1){
 66                 t=x[j];
 67                 x[j]=x[j+1];
 68                 x[j+1]=t;
 69             } 
 70         }
 71     }
 72     
 73     int count=0;
 74     for(i=0;i<n;i++){
 75         if(x[i].vip==1){
 76             count++;
 77         }
 78     }
 79     for(i=0;i<count-1;i++){
 80         for(j=0;j<count-i-1;j++){
 81             if(strcmp(x[j].name,x[j+1].name)>0){
 82                 t=x[j];
 83                 x[j]=x[j+1];
 84                 x[j+1]=t;
 85             }
 86         }
 87     }
 88     for(i=count;i<n-1;i++){
 89         for(j=count;j<n-i-1;j++){
 90             if(strcmp(x[j].name,x[j+1].name)>0){
 91                 t=x[j];
 92                 x[j]=x[j+1];
 93                 x[j+1]=t;
 94             }
 95         }
 96     }
 97     
 98     for(i = 0; i < n; ++i) {
 99         printf("%-10s%-15s", x[i].name, x[i].phone);
100         if(x[i].vip)
101             printf("%5s", "*");
102         printf("\n");
103     }
104 }
105 
106 void output(Contact x[], int n) {
107     int i;
108 
109     for(i = 0; i < n; ++i) {
110         printf("%-10s%-15s", x[i].name, x[i].phone);
111         if(x[i].vip)
112             printf("%5s", "*");
113         printf("\n");
114     }
115 }

 

结果

 

 

标签:int,void,else,实验,printf,month,day
From: https://www.cnblogs.com/cy-t520/p/17894971.html

相关文章

  • 实验六 C语言结构体、枚举应用编程
    实验四源代码#include<stdio.h>#include<string.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;......
  • 实验6
     #include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidoutp......
  • 实验6
    任务41#include<stdio.h>2#include<stdlib.h>3#defineN1045typedefstruct{6charisbn[20];//isbn号7charname[80];//书名8charauthor[80];//作者9doublesales_price;//售价10......
  • 实验6
    task4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;......
  • 实验六
    task4code1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;//销售册数10}Book;1112v......
  • 实验6
     #include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验三:JFinal极速开发框架实验
    实验三:JFinal极速开发框架实验 (2023.12.13日完成)    根据参考资料,学习JFinal极速开发框架的使用并如下任务:    任务一:了解Maven及其使用方法,总结其功能作用(占20%)    任务二:学习JFinal框架,基于Maven建立JFinal工程,并对JFinal框架功能进行总结介绍(占30%)    任......
  • 第六次实验
    task1//P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>#include<string.h>#defineN3 //运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstu......
  • 实验六 C语言结构体、枚举应用
    4.任务41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];6charname[80];7charauthor[80];8doublesales_price;9intsales_count;10}Book;1112void......
  • 实验6
    #include<stdio.h>#include<string.h>#defineN3typedefstructstudent{intid;charname[20];charsubject[20];doubleperf;doublemid;doublefinal;doubletotal;charlevel[10];}STU;voidinput(STU[],......