首页 > 其他分享 >实验六

实验六

时间:2023-12-17 23:23:24浏览次数:25  
标签:head int month 实验 printf Date day

task1.c

// P286例8.17
// 对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块
// 打印不及格学生信息和所有学生信息程分别调用 
 
#include <stdio.h>
#include<stdlib.h>
#include <string.h> 
#define N 3        // 运行程序输入测试时,可以把这个数组改小一些输入测试 

typedef struct student {
    int id;             // 学号 
    char name[20];         // 姓名 
    char subject[20];     // 考试科目
    double perf;         // 平时成绩 
    double mid;         // 期中成绩 
    double final;         // 期末成绩
    double total;         // 总评成绩 
    char level[10];     // 成绩等级
} STU;

void input(STU [], int);            // 录入学生信息
void output(STU [], int);            // 输出学生信息
void calc(STU [], int);                // 计算总评和等级 
int fail(STU [], STU [], int);        // 统计不及格学生信息
void sort(STU [], int);                // 排序 

int main() {
    STU st[N], fst[N];   // 数组st记录学生信息,fst记录不及格学生信息 
    int k;  // 用于记录不及格学生个数
    
    printf("录入学生成绩信息:\n");
    input(st, N);
    
    printf("\n成绩处理...\n");
    calc(st, N);
    
    k = fail(st, fst, N);
    sort(st, N);
    printf("\n学生成绩排名情况:\n");
    output(st, N);
    
    printf("\n不及格学生信息:\n");
    output(fst, k);
    
    return 0;
} 

void input(STU s[], int n) {
    int i;
    
    for(i = 0; i < n; i++)
        scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,
                                      &s[i].perf, &s[i].mid, &s[i].final);
}

void output(STU s[], int n) {
       int i;
   
      printf("-----------------\n");
      printf("学号   姓名     科目   平时   期中   期末   总评   等级\n");
       for(i = 0; i<n; i++)
          printf("%d   %-6s   %-4s   %-4.0f   %-4.0f   %-4.0f   %-4.1f   %s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level);
}


void calc(STU s[],int n) {
    int i;

    for(i = 0; i < n; i++) {    
        s[i].total = s[i].perf * 0.2 + 
                     s[i].mid * 0.2 +
                     s[i].final * 0.6;
        
        if(s[i].total >= 90)
          strcpy(s[i].level, "优");
        else if(s[i].total >= 80 && s[i].total < 90)
          strcpy(s[i].level, "良");
        else if(s[i].total >= 70 && s[i].total < 80)
          strcpy(s[i].level, "中"); 
        else if(s[i].total >= 60 && s[i].total < 70)
          strcpy(s[i].level, "及格");
        else
          strcpy(s[i].level, "不及格");         
    }
}

int fail(STU s[], STU t[], int n) {
      int i, cnt = 0;
      
      for(i = 0; i < n; i++)
          if(s[i].total < 60)
            t[cnt++] = s[i];
            
    return cnt;
}

void sort(STU s[], int n) {
    int i, j;
    STU t;
    
    for(i = 0; i < n-1; i++)
      for(j = 0; j < n-1-i; j++)
        if(s[j].total < s[j+1].total) {
            t = s[j];
            s[j] = s[j+1];
            s[j+1] = t;
            system("pause");
        }
}

task2.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include<stdlib.h> 
 4 #define N 10
 5 #define M 80
 6 
 7 typedef struct {
 8     char name[M];       // 书名
 9     char author[M];     // 作者
10 } Book;
11 
12 int main() {
13     Book x[N] = { {"《一九八四》", "乔治.奥威尔"},
14                   {"《美丽新世界》", "赫胥黎"},
15                   {"《昨日的世界》", "斯蒂芬.茨威格"}, 
16                   {"《万历十五年》", "黄仁宇"},
17                   {"《一只特立独行的猪》", "王小波"},
18                   {"《百年孤独》", "马尔克斯"},
19                   {"《查令十字街84号》", "海莲.汉芙"},
20                   {"《只是孩子》", "帕蒂.史密斯"}, 
21                   {"《刀锋》", "毛姆"},
22                   {"《沉默的大多数》", "王小波"} };
23     Book *ptr;
24     int i;
25     char author[M];
26 
27     // 使用指针遍历结构体数组
28     printf("所有图书信息: \n");
29     for(ptr = x; ptr < x + N; ++ptr)
30         printf("%-30s%-30s\n", ptr->name, ptr->author);
31 
32     // 查找指定作者的图书
33     printf("\n输入作者名: ");
34     gets(author);
35     for(ptr = x; ptr < x + N; ++ptr)
36         if(strcmp(ptr->author, author) == 0) {
37             printf("%-30s%-30s\n", ptr->name, ptr->author);
38         }
39 system("pause");
40     return 0;
41 }
View Code

task3_1.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 typedef struct FilmInfo {
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct FilmInfo *next;
11 } Film;
12 
13 
14 void output(Film *head);   // 遍历输出链表信息
15 Film *insert(Film *head, int n);   // 向链表中插入n个结点,返回头指针
16 
17 
18 int main() {
19     int n;          // 结点数
20     Film *head;     // 头指针变量,存放链表中第一个节点的地址
21 
22     head = NULL;
23     printf("输入影片数目: ");
24     scanf("%d", &n);
25 
26     // 向链表中插入n部影片信息
27     head = insert(head, n);
28 
29     // 遍历输出链表中所有影片信息
30     printf("\n所有影片信息如下: \n");
31     output(head);
32 
33     return 0;
34 }
35 
36 // 向链表中插入n个结点,从表头插入,返回头指针变量
37 Film *insert(Film *head, int n) {
38     int i;
39     Film *p;
40 
41     for(i = 1; i <= n; ++i) {
42         p = (Film *)malloc(sizeof(Film));
43         printf("请输入第%d部影片信息: ", i);
44         scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
45         
46         // 把结点从表头插入到链表中
47         p->next = head;
48         head = p;   // 更新头指针变量
49     }
50 
51     return head;
52 }
53 
54 // 遍历输出链表信息
55 void output(Film *head) {
56     Film *p;
57 
58     p = head;
59     while(p != NULL) {
60         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
61         p = p -> next;
62     }
63 }
View Code

task3_2.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 typedef struct FilmInfo {
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct FilmInfo *next;
11 } Film;
12 
13 
14 void output(Film *head);   // 遍历输出链表信息
15 Film *insert(Film *head, int n);   // 向链表中插入n个结点,返回头指针
16 
17 
18 int main() {
19     int n;          // 结点数
20     Film *head;     // 头指针变量,存放链表中第一个节点的地址
21     Film *p;        // 存放新申请的Film节点内存空间地址
22 
23     // 创建头结点
24     p = (Film *)malloc(sizeof(Film));
25     p->next = NULL;
26     head = p;       // 头指针变量存放头节点的地址
27 
28     printf("输入影片数目: ");
29     scanf("%d", &n);
30 
31     // 向链表中插入n部影片信息
32     head = insert(head, n);
33 
34     // 遍历输出链表中所有影片信息
35     printf("\n所有影片信息如下: \n");
36     output(head);
37 system("pause");
38     return 0;
39 }
40 
41 // 向链表中插入n个结点,从表头插入,返回头指针变量
42 Film *insert(Film *head, int n) {
43     int i;
44     Film *p;
45 
46     for(i = 1; i <= n; ++i) {
47         p = (Film *)malloc(sizeof(Film));
48         printf("请输入第%d部影片信息: ", i);
49         scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
50         
51         // 把结点从表头插入到链表中
52         p->next = head->next;
53         head->next = p;
54     }
55 
56     return head;
57 }
58 
59 // 遍历输出链表信息
60 void output(Film *head) {
61     Film *p;
62 
63     p = head->next;
64     while(p != NULL) {
65         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
66         p = p -> next;
67     }
68 }
View Code

task4.c

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

 

task5.c

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

 

 

 

 

task6.c

 

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

 

 

标签:head,int,month,实验,printf,Date,day
From: https://www.cnblogs.com/zyqJ/p/17894966.html

相关文章

  • 实验六
    实验一源代码#include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//平时成绩dou......
  • 实验6 模板类、文件IO和异常处理
    任务41#include<iostream>2#include"Vector.hpp"34voidtest(){5usingnamespacestd;67intn;8cin>>n;910Vector<double>x1(n);11for(autoi=0;i<n;++i)12x1.at(......
  • 实验6 C语言结构体、枚举应用编程
    1、实验1运行结果2、实验2源代码 1#include<stdio.h>2#include<string.h>3#defineN104#defineM8056typedefstruct{7charname[M];//书名8charauthor[M];//作者9}Book;1011intmain(){12Bookx[N]=......
  • 实验6 模板类、文件IO和异常处理
    实验任务4#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{public:Vector(intn);Vector(intn,Tvalue);Vector(constVector<T>&vi);~Vector();......
  • 实验六、模板类,文件I/O流,异常处理
    实验四:Vector.hpp://#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector(intsz=0,constT&value=T());V......
  • 4.1-华三-irf中的bfd mad实验配置
    1.BFDMad概述用途:核心层的irf,最好做MAD检测,来确保网络的稳定性。BFD:BidirectionalForwardingDetection(双向转发检测)。1.是一种网络协议,用于快速检测和报告两个网络节点之间的连接状态。主要目标是提供低延迟、高可靠性的链路故障检测,以便网络设备可以快速做出响应并进行故障恢复......
  • 实验6
    实验41#include<stdio.h>2#include<string.h>3#defineN104typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;//销售册数10}......
  • 实验六 模板类,文件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......