首页 > 其他分享 >实验6

实验6

时间:2023-12-17 13:55:05浏览次数:21  
标签:case int 实验 printf Date d2 d1

4. 实验任务4

task4.c源码

#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("%-18s%-27s%-18s%-10s%-9s","ISBN号","书名","作者","售价","销售册数\n");
    for(i = 0;i < n;i++)
    {
        printf("%-18s%-27s%-18s%-10.1lf%-9d\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;
    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)
            {
                x[10] = x[j];
                x[j] = x[j+1];
                x[j+1] = x[10];
            }
        }
    }
}


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

截图:



5. 实验任务5

task5.c源码

#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, mthday,judge;
    s = d.day;
    judge = (d.year%400 == 0 && d.year%100 == 0) || (d.year%4 == 0 && d.year%100 != 0) ? 1 : 0;
    d.month--;
    for(;d.month != 0;d.month--)
    {
        switch(d.month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12: mthday = 31;break;
            case 2:
                {
                    switch(judge)
                    {
                        case 0: mthday = 28;break;
                        case 1: mthday = 29;break;
                    }
                }break;
            default: mthday = 30;
        }
        s += mthday;
    }
    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)
        return -1;
    else
    {
        if(d1.month > d2.month)
            return 1;
        else if(d1.month < d2.month)
            return -1;
        else
        {
            if(d1.day > d2.day)
                return 1;
            else if(d1.day < d2.day)
                return -1;
            else
                return 0;
        }
    }
}

截图:



6. 实验任务6

task6.c源码:

#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) {
    char out[20];
    int i;
    for(i = 0;i < n;i++)
    {
        memset(out,'\0',20);
        memset(out,'*',strlen(x[i].password));
        printf("%-8s%-20s",x[i].username,out);
        switch(x[i].type)
        {
            case 0:printf("%-8s\n","admin");break;
            case 1:printf("%-8s\n","student");break;
            case 2:printf("%-8s\n","teacher");break;
        }

    }
}

截图:



标签:case,int,实验,printf,Date,d2,d1
From: https://www.cnblogs.com/PetroniusMo/p/17908997.html

相关文章

  • 鸿蒙小车之多任务调度实验
    说到鸿蒙我们都会想到华为mate60:遥遥领先!我们一直领先!我们这个小车也是采用的是鸿蒙操作系统,学习鸿蒙小车,让你遥遥领先于你的同学。@TOC前言本专栏将依次介绍鸿蒙小车的内核实验,硬件实验,wifi实验。一、什么是任务?为什么要有任务任务是操作系统(RTOS)中的基本组成单元,它们为嵌入式......
  • 实验七
    task4.c#include<stdio.h>intmain(){ charch; intcnt=0;FILE*fp;fp=fopen("data4.txt","r");if(fp==NULL){ printf("failtoopen\n"); return1; }while(1){ch=fgetc(fp);......
  • 实验6-模板类、文件I/O和异常处理
    Vector.hpp1#ifndefVECTOR_HPP2#defineVECTOR_HPP34#include<iostream>5#include<stdexcept>67template<classT>8classVector{9private:10T*arr;11intsize;1213public:14Vector():arr(nu......
  • 实验六
    task4.cpp:点击查看代码#include<iostream>#include"vector.hpp"voidtest(){usingnamespacestd;intn;cin>>n;Vector<double>x1(n);for(autoi=0;i<n;++i)x1.at(i)=i*0.7;output......
  • 实验7
    任务4#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>intmain(){   longi=0;   charch;   FILE*fp;   fp=fopen("c://data//data4.txt","r");   while(!feof(fp))   {       ch=fgetc(fp);       if(ch!=''&......
  • 实验七
    任务1:#include<stdio.h>#defineN80typedefstruct{charname[N];//书名charauthor[N];//作者}Book;intmain(){Bookx[]={{"《雕塑家》","斯科特.麦克劳德"},{"《灯塔》","克里斯多夫.夏布特&quo......
  • 实验7 文件应用编程
    1.实验任务1源代码1//将图书信息写入文本文件data1.txt23#include<stdio.h>45#defineN8067typedefstruct{8charname[N];//书名9charauthor[N];//作者10}Book;1112intmain(){13Bookx[]={{"《雕塑家》",......
  • 实验7
    task41#include<stdio.h>23intmain(){45inti=0;6chars;78FILE*fp;9fp=fopen("data4.txt","r");1011while(1){12s=fgetc(fp);13if(s==EOF){14......
  • 实验6
    任务41#include<iostream>2#include"Vector1.hpp"34voidtest(){5usingnamespacestd;67intn;8cin>>n;910Vector<double>x1(n);11for(autoi=0;i<n;++i)12x1......
  • 实验6 模板类、文件I/O和异常处理
    实验任务41#defineVECTOR_HPP2#include<iostream>3#include<stdexcept>4usingnamespacestd;5template<typenameT>6classVector{7private:8T*data;9size_tsize;10public:11Vector(size_ta):size(a......