首页 > 其他分享 >实验6

实验6

时间:2023-12-17 19:22:09浏览次数:23  
标签:int void printf else 实验 year output

实验1

代码

  1 // P286例8.17
  2 // 对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块
  3 // 打印不及格学生信息和所有学生信息程分别调用 
  4  
  5 #include <stdio.h>
  6 #include <string.h> 
  7 #define N 3        // 运行程序输入测试时,可以把这个数组改小一些输入测试 
  8 
  9 typedef struct student {
 10     int id;             // 学号 
 11     char name[20];         // 姓名 
 12     char subject[20];     // 考试科目
 13     double perf;         // 平时成绩 
 14     double mid;         // 期中成绩 
 15     double final;         // 期末成绩
 16     double total;         // 总评成绩 
 17     char level[10];     // 成绩等级
 18 } STU;
 19 
 20 void input(STU [], int);            // 录入学生信息
 21 void output(STU [], int);            // 输出学生信息
 22 void calc(STU [], int);                // 计算总评和等级 
 23 int fail(STU [], STU [], int);        // 统计不及格学生信息
 24 void sort(STU [], int);                // 排序 
 25 
 26 int main() {
 27     STU st[N], fst[N];   // 数组st记录学生信息,fst记录不及格学生信息 
 28     int k;  // 用于记录不及格学生个数 
 29     
 30     printf("录入学生成绩信息:\n");
 31     input(st, N);
 32     
 33     printf("\n成绩处理...\n");
 34     calc(st, N);
 35     
 36     k = fail(st, fst, N);
 37     sort(st, N);
 38     printf("\n学生成绩排名情况:\n");
 39     output(st, N);
 40     
 41     printf("\n不及格学生信息:\n");
 42     output(fst, k);
 43     
 44     return 0;
 45 } 
 46 
 47 void input(STU s[], int n) {
 48     int i;
 49     
 50     for(i = 0; i < n; i++)
 51         scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,
 52                                       &s[i].perf, &s[i].mid, &s[i].final);
 53 }
 54 
 55 void output(STU s[], int n) {
 56        int i;
 57    
 58       printf("-----------------\n");
 59       printf("学号   姓名     科目   平时   期中   期末   总评   等级\n");
 60        for(i = 0; i<n; i++)
 61           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);
 62 }
 63 
 64 
 65 void calc(STU s[],int n) {
 66     int i;
 67 
 68     for(i = 0; i < n; i++) {    
 69         s[i].total = s[i].perf * 0.2 + 
 70                      s[i].mid * 0.2 +
 71                      s[i].final * 0.6;
 72         
 73         if(s[i].total >= 90)
 74           strcpy(s[i].level, "优");
 75         else if(s[i].total >= 80 && s[i].total < 90)
 76           strcpy(s[i].level, "良");
 77         else if(s[i].total >= 70 && s[i].total < 80)
 78           strcpy(s[i].level, "中"); 
 79         else if(s[i].total >= 60 && s[i].total < 70)
 80           strcpy(s[i].level, "及格");
 81         else
 82           strcpy(s[i].level, "不及格");         
 83     }
 84 }
 85 
 86 int fail(STU s[], STU t[], int n) {
 87       int i, cnt = 0;
 88       
 89       for(i = 0; i < n; i++)
 90           if(s[i].total < 60)
 91             t[cnt++] = s[i];
 92             
 93     return cnt;
 94 }
 95 
 96 void sort(STU s[], int n) {
 97     int i, j;
 98     STU t;
 99     
100     for(i = 0; i < n-1; i++)
101       for(j = 0; j < n-1-i; j++)
102         if(s[j].total < s[j+1].total) {
103             t = s[j];
104             s[j] = s[j+1];
105             s[j+1] = t;
106         }
107 }

 实验2

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

 实验3

 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 }

截图

 实验4

 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 {
41     printf("ISBN号                   书名                          作者                     售价           销售册数\n");
42     int i;
43     for(i=0;i<n;i++){
44         printf("%-25s%-30s%-25s%-15.1lf%-15d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
45     }
46 }
47 
48 
49 // 待补足:函数sort()实现
50 // ×××
51 void sort(Book x[],int n)
52 {
53     int i,j,t;
54     for(i=0;i<n-1;i++){
55         for(j=0;j<n-1;j++){
56             if(x[j].sales_count<x[j+1].sales_count){
57                 t=x[j].sales_count;
58                 x[j].sales_count=x[j+1].sales_count;
59                 x[j+1].sales_count=t;
60             }
61         }
62     }
63 }
64 
65 
66 // 待补足:函数sales_count()实现
67 // ×××
68 double sales_amount(Book x[],int n)
69 {
70     int i;
71     double s=0;
72     for(i=0;i<n;i++){
73         s=s+x[i].sales_count*x[i].sales_price;
74     }
75 
76     return s;
77 }

截图

 实验五

#include <stdio.h>

typedef struct {
    int year;
    int month;
    int day;
} Date;

// 函数声明
void input(Date *pd);                   // 输入日期给pd指向的Date变量
int day_of_year(Date d);                // 返回日期d是这一年的第多少天
int compare_dates(Date d1, Date d2);    // 比较两个日期:
                                        // 如果d1在d2之前,返回-1;
                                        // 如果d1在d2之后,返回1
                                        // 如果d1和d2相同,返回0

void test1() {
    Date d;
    int i;

    printf("输入日期:(以形如2023-12-11这样的形式输入)\n");
    for(i = 0; i < 3; ++i) {
        input(&d);
        printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
    }
}

void test2() {
    Date Alice_birth, Bob_birth;
    int i;
    int ans;

    printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n");
    for(i = 0; i < 3; ++i) {
        input(&Alice_birth);
        input(&Bob_birth);
        ans = compare_dates(Alice_birth, Bob_birth);

        if(ans == 0)
            printf("Alice和Bob一样大\n\n");
        else if(ans == -1)
            printf("Alice比Bob大\n\n");
        else
            printf("Alice比Bob小\n\n");
    }
}

int main() {
    printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
    test1();

    printf("\n测试2: 两个人年龄大小关系\n");
    test2();
}

// 补足函数input实现
// 功能: 输入日期给pd指向的Date变量
void input(Date *pd) {
    scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day);
}

// 补足函数day_of_year实现
// 功能:返回日期d是这一年的第多少天
int day_of_year(Date d) {
    int n, j, k, flag;
    int i = 0;
    int xiao[5] = {2, 4, 6, 9, 11};
   if(d.month > 2){
       if(d.year % 4 == 0 && d.year % 100 != 0 || d.year % 400 == 0){
           i += 31 + 29;
       }
    else{
        i += 31 + 28;
    }
    for(j = d.month - 1; j > 2; j--){
        flag = 0;
        for(k = 0; k < 5; k++){
            if(j == xiao[k]){
                flag = 1;
                break;
            }
        }
        if(flag == 1){
            i += 30;
        }
        else{
            i += 31;
        }
    }
    i += d.day;
   }
   else if(d.month == 2){
      i = 31 + d.day;
   }
   else{
       i = d.day;
   }
   return i;
}

// 补足函数compare_dates实现
// 功能:比较两个日期:
// 如果d1在d2之前,返回-1;
// 如果d1在d2之后,返回1
// 如果d1和d2相同,返回0
int compare_dates(Date d1, Date d2) {
    int ans;
    if(d1.year == d2.year){
        if(d1.month == d2.month){
            if(d1.day == d2.day){
                ans = 0;
            }
            else if(d1.day > d2.day){
                ans = 1;
            }
            else{
                ans = -1;
            }
        }
        else if(d1.month > d2.month){
            ans = 1;
        }
        else{
            ans = -1;
        }
    }
    else if(d1.year > d2.year){
        ans = 1;
    }
    else{
        ans = -1;
    }
    return ans;
}

截图

 实验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 {
35     int i,j,k;
36     int s;
37     for(i=0;i<n;i++){
38         printf("%-15s",x[i].username);
39         s=strlen(x[i].password);
40 
41         for(j=0;j<s;j++){
42             printf("*");
43         }
44 
45         for(k=0;k<10;k++){
46             printf(" ");
47         }
48 
49         switch(x[i].type){
50             case admin:printf("admin");break;
51             case student:printf("student");break;
52             case teacher:printf("teacher");break;
53         }
54 
55         printf("\n");
56     }
57 }

截图

 

标签:int,void,printf,else,实验,year,output
From: https://www.cnblogs.com/z00z/p/17909594.html

相关文章

  • 实验三-电子公文传输系统1-个人贡献
    个人贡献1、简述你完成的工作部分前端代码及文档撰写github项目管理协助设计系统前端布局2、你们小组总共的代码行数,你贡献的代码行数?相关代码链接?贡献的代码行数:985https://github.com/hexaosf/codegramhttps://github.com/hexaosf/codegram/blob/608d2056e40859360e0ea6......
  • 人工智能-A*算法-最优路径搜索实验
    上次学会了《A*算法-八数码问题》,初步了解了A*算法的原理,本次再用A*算法完成一个最优路径搜索实验。 一、实验内容1.设计自己的启发式函数。2.在网格地图中,设计部分障碍物。3.实现A*算法,搜索一条最优路径。 二、A*算法实现步骤1.初始化:设置起始节点和目标节点,并创建一......
  • 2023-2024-1 20211327 实验三-电子公文传输系统1-个人贡献
    简述工作在项目前期,我撰写了部分博客,作为组长分配任务设计项目整体框架结构,完成vue全局过滤器的声明和相关设置撰写了普通用户(非管理员)增删改查部分的代码,对jsp和html等前端代码进行修改和完善对数据库部分相关代码进行补充进行系统测试任务摘录全局过滤器我们的公文传......
  • 实验三-电子公文传输系统1-个人贡献
      1简述你完成的工作作为组员,和其他组员相互配合,听从组长的安排,合理高效完成任务。参与组内文档的撰写工作。参与后端设计的代码编写以及前端设计的代码编写2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为55352行,其中大部分是gitee上的代码,我们组......
  • 实验6 模板类、文件IO和异常处理
    四、实验结论1.实验任务4Vector.hpp#pragmaonce#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector(intsz=0,constT&value......
  • 实验三-电子公文传输系统1-个人贡献
    一、任务详情简述你完成的工作你们小组总共的代码行数,你贡献的代码行数?相关代码链接?你们小组总共的文档数?你贡献的文档数?相关链接?二、简述你完成的工作我主要负责前端的页面设计和与后端同学对接的工作。以及上传GitHub和每日总结绘制燃尽图。三、你们小组总共的代码行数,......
  • 实验三-电子公文系统1-个人贡献
    1简述你完成的工作作为组员,积极配合组长的工作,合理高效完成任务;参与组内文档的撰写工作;参与后端设计的代码编写以及数据库搭建的代码编写。2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?总共代码行数为55352行,其中大部分是gitee上的代码,我们组总共贡献了13139行......
  • 实验六
    1#include<iostream>2#include"Vector.hpp"3usingnamespacestd;45template<typenameT>6voidoutput(constVector<T>&v){7for(autoi=0;i<v.get_size();i++){8cout<<v.at(i)<<&......
  • 实验六
    task4源码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;vo......
  • 实验6 模板类、文件I/O和异常处理
    task4vector.hpp#include<iostream>#include<stdexcept>usingnamespacestd;template<typenameT>classVector{private:T*data;intsize;public:Vector():data(nullptr),size(0){}Vector(intn):size(n){......