首页 > 其他分享 >实验六

实验六

时间:2023-12-17 23:22:38浏览次数:24  
标签:head int void 实验 printf output Film

实验一源代码

#include <stdio.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;
            }
}

实验一运行结果

实验二源代码

#include<stdio.h>
#include<string.h>
#define N 10
#define M 80
typedef struct {
    char name[M]; // 书名
    char author[M]; // 作者
} Book;
int main() {
    Book x[N] = { {"《一九八四》", "乔治.奥威尔"},
    {"《美丽新世界》", "赫胥黎"},
    {"《昨日的世界》", "斯蒂芬.茨威格"},
    {"《万历十五年》", "黄仁宇"},
    {"《一只特立独行的猪》", "王小波"},
    {"《百年孤独》", "马尔克斯"},
    {"《查令十字街84号》", "海莲.汉芙"},
    {"《只是孩子》", "帕蒂.史密斯"},
    {"《刀锋》", "毛姆"},
    {"《沉默的大多数》", "王小波"} };
    Book* ptr;
    int i;
    char author[M];
    // 使用指针遍历结构体数组
    printf("所有图书信息: \n");
    for (ptr = x; ptr < x + N; ++ptr)
        printf("%-30s%-30s\n", ptr->name, ptr->author);
    // 查找指定作者的图书
    printf("\n输入作者名: ");
    gets(author);
    for (ptr = x; ptr < x + N; ++ptr)
        if (strcmp(ptr->author, author) == 0) {
            printf("%-30s%-30s\n", ptr->name, ptr->author);
        }
    return 0;
}

实验二运行结果

实验三

3.1源代码

#include <stdio.h>
#include <stdlib.h>
#define N 80
typedef struct FilmInfo {
    char name[N];
    char director[N];
    char region[N];
    int year;
    struct FilmInfo* next;
} Film;
void output(Film* head); // 遍历输出链表信息
Film* insert(Film* head, int n); // 向链表中插入n个结点,返回头指针
int main() {
    int n; // 结点数
    Film* head; // 头指针变量,存放链表中第一个节点的地址
    head = NULL;
    printf("输入影片数目: ");
    scanf("%d", &n);
    // 向链表中插入n部影片信息
    head = insert(head, n);
    // 遍历输出链表中所有影片信息
    printf("\n所有影片信息如下: \n");
    output(head);
    return 0;
}
// 向链表中插入n个结点,从表头插入,返回头指针变量
Film* insert(Film* head, int n) {
    int i;
    Film* p;
    for (i = 1; i <= n; ++i) {
        p = (Film*)malloc(sizeof(Film));
        printf("请输入第%d部影片信息: ", i);
        scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
        // 把结点从表头插入到链表中
        p->next = head;
        head = p; // 更新头指针变量
    }
    return head;
}
// 遍历输出链表信息
void output(Film* head) {
    Film* p;
    p = head;
    while (p != NULL) {
        printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p -> year);
        p = p->next;
    }
}

实验3.1运行结果

实验3.2源代码

#include <stdio.h>
#include <stdlib.h>
#define N 80
typedef struct FilmInfo {
    char name[N];
    char director[N];
    char region[N];
    int year;
    struct FilmInfo* next;
} Film;
void output(Film* head); // 遍历输出链表信息
Film* insert(Film* head, int n); // 向链表中插入n个结点,返回头指针
int main() {
    int n; // 结点数
    Film* head; // 头指针变量,存放链表中第一个节点的地址
    Film* p; // 存放新申请的Film节点内存空间地址
    // 创建头结点
    p = (Film*)malloc(sizeof(Film));
    p->next = NULL;
    head = p; // 头指针变量存放头节点的地址
    printf("输入影片数目: ");
    scanf("%d", &n);
    // 向链表中插入n部影片信息
    head = insert(head, n);
    // 遍历输出链表中所有影片信息
    printf("\n所有影片信息如下: \n");
    output(head);
    return 0;
}
// 向链表中插入n个结点,从表头插入,返回头指针变量
Film* insert(Film* head, int n) {
    int i;
    Film* p;
    for (i = 1; i <= n; ++i) {
        p = (Film*)malloc(sizeof(Film));
        printf("请输入第%d部影片信息: ", i);
        scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
        // 把结点从表头插入到链表中
        p->next = head->next;
        head->next = p;
    }
    return head;
}
// 遍历输出链表信息
void output(Film* head) {
    Film* p;
    p = head->next;
    while (p != NULL) {
        printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p -> year);
        p = p->next;
    }
}

实验3.2运行结果

实验四源代码

 

#include <stdio.h>
#include<string.h>
#define N 10

typedef struct {
    char isbn[20];          // isbn号
    char name[80];          // 书名
    char author[80];        // 作者
    double sales_price;     // 售价
    int  sales_count;       // 销售册数
} Book;

void output(Book x[], int n);
void sort(Book x[], int n);
double sales_amount(Book x[], int n);

int main() {
    Book x[N] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59},
                 {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16},
                 {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27},
                 {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
                 {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42},
                 {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39},
                 {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
                 {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31},
                 {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
                 {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}};

    printf("图书销量排名: \n");
    sort(x, N);
    output(x, N);

    printf("\n图书销售总额: %.2f\n", sales_amount(x, N));

    return 0;
}

// 待补足:函数output()实现
// ×××
void output(Book x[],int n){
    printf("ISBN号                   书名                          作者                     售价           销售册数\n");
    int i;
    for(i=0;i<n;i++){
        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);
    }
}

// 待补足:函数sort()实现
// ×××
void sort(Book x[],int n){

    int i,j,t;
    for(i=0;i<n-1;++i){
        for(j=0;j<n-i-1;++j){
            if(x[j].sales_count<x[j+1].sales_count){
                t=x[j].sales_count;
                x[j].sales_count=x[j+1].sales_count;
                x[j+1].sales_count=t;
            }

        }
    }



}

// 待补足:函数sales_count()实现
// ×××
double sales_amount(Book x[],int n){
    int i;
    double s=0;
    for(i=0;i<n;i++){

        s=s+x[i].sales_count*x[i].sales_price;

    }
    return s;
}

实验四运行结果

实验五源代码

#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 s=0;
    int i;
    for(i=1;i<d.month;i++){
        switch(i){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:s+=31;continue;
            case 2:
                if((d.year%4==0&&d.year%100!=0)||d.year%400==0){
                    s+=29;
                }else{
                    s+=28;
                }continue;

            default :s+=30;continue;
    }
}
s+=d.day;

return s;
}

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

实验五运行结果

实验六源代码

#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,k;
    int s;
    for(i=0;i<n;i++){

    printf("%-15s",x[i].username);
    s=strlen(x[i].password);
    for(j=0;j<s;j++){
        printf("*");
    }
    for(k=0;k<10;k++){
        printf(" ");
    }
    switch(x[i].type){
        case admin:printf("admin");break;
        case student:printf("student");break;
        case teacher:printf("teacher");break;

    }
    printf("\n");



    }

}

实验六运行结果

 

标签:head,int,void,实验,printf,output,Film
From: https://www.cnblogs.com/wyf827/p/17894877.html

相关文章

  • 实验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......
  • 实验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......