首页 > 其他分享 >实验六

实验六

时间:2024-06-10 11:21:53浏览次数:10  
标签:int year 实验 printf d2 day d1

task4

#include <stdio.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){
    int i;
    
    printf("ISBN号     书名      作者       售价        销售册数\n");
    for(i=0;i<n;i++)
        printf("%d  %-20s  %-10s   %-2f   %d\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,t;
    for(i = 0; i <=n; i++)
       if(x[i].sales_count<x[i+1].sales_count){
           t=x[i].sales_count;
           x[i].sales_count=x[i+1].sales_count;
           x[i+1].sales_count=t;
       }
}

// 待补足:函数sales_count()实现
double sales_amount(Book x[],int n){
    double total_sales = 0.0;
    int i;
    for(i=0;i<n;i++){
        total_sales+=x[i].sales_price*x[i].sales_count;
    }
    return total_sales;
}

 

 

 

task5

#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();   // 测试函数1: 测试某个日期时这一年第多少天                   
void test2();   // 测试函数2: 测试两个日期先后顺序


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

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

// 测试函数实现
void test1() {
    Date d;
    int i;

    printf("输入日期:(以形如2024-06-01这样的形式输入)\n");
    for(i = 0; i < 3; ++i) {
        input(&d);
        printf("%04d-%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出生日期:(以形如2005-08-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");
    }
}

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

// 补足函数day_of_year实现
// 功能:返回日期d是这一年的第多少天
int day_of_year(Date d) {
    int day = d.day;
    int days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if ((d.year % 4 == 0 && d.year % 100 != 0) || (d.year % 400 == 0)) {
        if (d.month > 2) {
            day++;
        }
    }

     int i;
    for (i = 0; i < d.month - 1; ++i) {
        day += days_in_month[i];
    }

    return day;
}

// 补足函数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)
        return 1;

    if (d1.month < d2.month)
        return -1;
    else if (d1.month > d2.month)
        return 1;

    if (d1.day < d2.day)
        return -1;
    else if (d1.day > d2.day)
        return 1;

    return 0;
}

 

 

 

task6

#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) {
    printf("Username\tPassword\tRole\n");
    printf("----------------------------------------\n");
    int i;
    for (i = 0; i < n; i++) {
        printf("%s\t", x[i].username);
        
    int password_length = strlen(x[i].password);
         int j;
        for (j = 0; j < password_length; j++) {
            printf("*");
        }
        printf("\t");
        
         switch (x[i].type) { 
            case admin:
                printf("Admin\n");
                break;
            case student:
                printf("Student\n");
                break;
            case teacher:
                printf("Teacher\n");
                break;
            default:
                printf("Unknown\n");
        }
    }
}

 

标签:int,year,实验,printf,d2,day,d1
From: https://www.cnblogs.com/lmbjyx/p/18240509

相关文章

  • 实验Ⅵ
    task4#include<stdio.h>#include<stdlib.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;......
  • 实验6
    task1#include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目dou......
  • 实验六
    task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidoutput(Bookx[],intn);voidsort(Bookx[],intn);doubl......
  • 实验6
    #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;//销售册数}Bo......
  • 实验六
    task4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voido......
  • 实验6
    task1#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试#include<stdlib.h>typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//......
  • 实验室扫描电镜为什么需要主动减震台?
    实验室扫描电镜(ScanningElectronMicroscope,SEM)是一种高分辨率的显微镜,用于观察样本的表面形貌。在SEM的应用过程中,对环境振动非常敏感,因此使用主动减震台是为了减小或消除外部振动对SEM成像质量的影响。以下是为什么实验室扫描电镜需要主动减震台的一些原因:高分辨率要求......
  • 提升实验室精密仪器性能的秘密:主动减震台的作用和优势
    导言:在现代科学实验室中,精密仪器的性能对研究的准确性和可靠性起着关键作用。为了确保实验设备的高效运行,科学家们不仅仅依赖于先进的技术,还采用了一些创新的解决方案。本文将介绍一种关键的实验室设备——主动减震台,探讨它的作用和优势,以及如何提升实验室仪器的性能。1.......
  • 实验6 C语言结构体、枚举应用编程
    #defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试#include<stdlib.h>typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doubleperf;//平时成绩......