首页 > 其他分享 >实验6

实验6

时间:2024-06-09 10:44:14浏览次数:20  
标签:int void printf 978 实验 year Date

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号\t\t\t书名\t\t\t\t作者\t\t\t售价\t\t销售册数\n");
    for(i = 0; i<n; i++)
    printf("%-24s%-32s%-24s%-2.0f\t\t%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, j;
    Book t;
    
    for(i = 0; i < n-1; i++)
      for(j = 0; j < n-1-i; j++)
        if(x[j].sales_count < x[j+1].sales_count) {
            t = x[j];
            x[j] = x[j+1];
            x[j+1] = t;
        }
}

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

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) {
    scanf("%d-%d-%d",&(pd->year),&(pd->month),&(pd->day));
}

// 补足函数day_of_year实现
// 功能:返回日期d是这一年的第多少天
int day_of_year(Date d) {
    int i;
    int sum=0;
    if(d.month==1){return d.day;}
    else
        for(i=1;i<d.month;i++){
            if(i==2){
                if(((d.year)%400==0)||((d.year%4==0&&(d.year)%100!=0)))
                    sum=sum+29;
                else
                    sum=sum+28;
            }
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                sum =sum+31;
            if(i==4||i==6||i==9||i==11)
                sum=sum+30;
        }
        sum=sum+d.day;
        return sum;
}






// 补足函数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;
    if(d1.year>d2.year)
        return 1;
    if(d1.year==d2.year){
        if(d1.month<d2.month)
            return -1;
        if(d1.month>d2.month)
            return 1;
        if(d1.month==d2.month){
            if(d1.day<d2.day)
                return -1;
            if(d1.day>d2.day)
                return 1;
            else
                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) {
    int i,j;
    for(i=0;i<n;i++){
        printf("%s\t",x[i].username);
        for(j=0;j<strlen(x[i].password);j++){
            printf("*");
        }
        if(strlen(x[i].password)<8)
            printf("\t\t");
        else
            printf("\t");
        switch(x[i].type){
        case(admin):printf("admin\n");break;
        case(student):printf("student\n");break;
        case(teacher):printf("teacher\n");break;
            printf("\n");
        }
    }
}
    

 

标签:int,void,printf,978,实验,year,Date
From: https://www.cnblogs.com/ichd/p/18237526

相关文章

  • 实验6 C语言结构体、枚举应用编程
    task1.c1//P286例8.172//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块3//打印不及格学生信息和所有学生信息程分别调用45#include<stdio.h>6#include<string.h>7#defineN3//运行程序输入测试时,可以把这个数组......
  • 实验6
    task4点击查看代码#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}B......
  • 实验六
    task41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • DOM型xss靶场实验
    DOM型xss可以使用js去控制标签中的内容。我使用的是一个在线的dom型xss平台,靶场链接:Challenges第一关MaSpaghet!:MaSpaghet!关卡<h2id="spaghet"></h2><script>spaghet.innerHTML=(newURL(location).searchParams.get('somebody')||"Somebody&qu......
  • C语言实验六
    #include<stdio.h>#include<string.h>#defineN3//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid;//学号charname[20];//姓名charsubject[20];//考试科目doublepe......
  • kvm链接克隆虚拟机迁移到openstack机器的实验
     总结如果是完整克隆的那种虚拟机,是可以直接在openstack使用的,如果镜像格式没问题的话。 因为kvm虚拟机大部分都是链接克隆出来的镜像,不可用直接复制使用,所以需要创建新的镜像文件 创建空盘:qemu-imgcreate-fqcow2mcwlink1-new.qcow250G将链接克隆镜像数据导入到空......
  • 实验6
    `#include<stdio.h>include<stdlib.h>include<string.h>defineN3 //运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{intid; //学号charname[20]; //姓名charsubject[20]; //考试科目doubleperf; //平时成绩doublemid;......
  • 设备树下的 LED 驱动实验
    设备树下的LED驱动实验本章实验重点内容如下:①、在imx6ull-alientek-emmc.dts文件中创建相应的设备节点。②、编写驱动程序(在第四十二章实验基础上完成),获取设备树中的相关属性值。③、使用获取到的有关属性值来初始化LED所使用的GPIO。设备树文件添加设备节点:在根......
  • Java 实验8 集合类
    (一)实验目的1、掌握JAVA集合类中的Collection的特点及其应用情形;3、掌握Collection、熟悉集合的特点及应用。(二)实验内容和步骤1、仿照课堂练习的MyStack示例,使用LinkedList集合类实现一个先进先出的队列数据结构,可以往该结构中压入数据push()以及弹出数据pop(),并遵循先进入......
  • 实验 6
    task1点击查看代码#include<stdio.h>#include<string.h>#defineN3 //运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{ intid; //学号 charname[20]; //姓名 charsubject[20]; //考试科目 doubleperf; //平时成......