首页 > 编程语言 >实验6_c语言结构体、枚举应用编程

实验6_c语言结构体、枚举应用编程

时间:2023-12-11 17:23:46浏览次数:29  
标签:printf return int 31 编程 枚举 实验 year day

task4

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define N 10
typedef struct {
    char isbn[20];
    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;
}
void output(Book x[], int n) {
    Book* p;
    p = x;
    printf("ISBN号                书名                               作者                          售价      销售册数\n");
    while (p<x+N) {
        printf("%s     %-30s     %-30s %-10.1lf    %-10d\n", p->isbn, p->name, p->author, p->sales_price, p->sales_count);
        p++;
    }
}
void sort(Book x[], int n) {
    Book temp;
    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)
            {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
}
double sales_amount(Book x[], int n) {
    double sum = 0;
    int i = 0;
    for (i = 0; i < n; i++)
    {
        sum += x[i].sales_price* x[i].sales_count;
    }
    return sum;
}

 

task5

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
typedef struct {
int year;
int month;
int day;
}Date;
const int common[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
const int leap[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
void input(Date* pd);
int day_of_year(Date d);
int compare_dates(Date d1, Date d2);
void test1() {
Date d;
int i;
printf("输入日期\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();
}
void input(Date* pd)
{
scanf("%d-%d-%d", &pd->year, &pd->month, &pd->day);
}
int day_of_year(Date d) {
int i = 0;
int day=0;
if ((0 == d.year % 4 && d.year % 100 != 0) || (0 == d.year % 400))
{
for (i = 0; i < d.month - 1; i++)
day += leap[i];
day += d.day;

return day;
}
else
{
for (i = 0; i < d.month - 1; i++)
day += common[i];/*在这儿,已修改*/
day += d.day;

return day;
}
}
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;
}
}
}
}

 /*第一行输出错误是误把common打成leap了*/

 

 

task6

#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

enum Role { admin, student, teacher };

typedef struct {
    char username[20];  // 用户名
    char password[20];  // 密码
    char type[20];     // 账户类型
} 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;
}
void output(Account x[], int n) {
    int i = 0, j = 0,count = 0;
    char xin[100] = { '*','*','*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*', '*','*','*' };
    for (j = 0; j < n; j++) {
        count = 0;
        for (i = 0; i < 20; i++)
        {
            if (x[j].password[i] >= '0' && x[j].password[i] <= '9')
                count++;
        }
        printf("%-30s", x[j].username);
        for (i = 0; i < count; i++)
            printf("%c", xin[i]);
        printf("     %s\n", x[j].type);
    }
}

 

标签:printf,return,int,31,编程,枚举,实验,year,day
From: https://www.cnblogs.com/yrx0415/p/17894916.html

相关文章

  • 实验六
    实验4#include<stdio.h>#defineN10typedefstruct{charisbn[20];//isbn号charname[80];//书名charauthor[80];//作者doublesales_price;//售价intsales_count;//销售册数}Book;voidou......
  • 实验六
    task41#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsales_count;......
  • 实验6 C语言结构体、枚举应用编程
    实验任务4代码task4.c1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];//作者8doublesales_price;//售价9intsale......
  • 实验六
    任务1//P286例8.17//对教材上的程序作了微调整,把输出学生信息单独编写成一个函数模块//打印不及格学生信息和所有学生信息程分别调用#include<stdio.h>#include<string.h>#defineN10//运行程序输入测试时,可以把这个数组改小一些输入测试typedefstructstudent{......
  • 实验6
    四、实验结论4.实验任务4此部分书写内容:正确补足后完整的task4.c源码,及,运行结果截图1#include<stdio.h>2#defineN1034typedefstruct{5charisbn[20];//isbn号6charname[80];//书名7charauthor[80];/......
  • 上机编程-简易DHCP服务器
    题目描述DHCP服务器的功能是为每一个MAC地址分配唯一的IP地址。现假设:分配的IP地址范围从192.168.0.0到192.168.0.255总共256个可用地址(以点分十进制表示)。请实现一个简易的DHCP服务器,功能如下:分配Request:根据输入的MAC地址分配IP地址池中的IP地址:如果对应的IP已分配并......
  • 强类型枚举
    文章参考爱编程的大丙(subingwen.cn)1.枚举1.1概述在C/C++中的枚举来自于C,处于设计上简单的目的,枚举往往对应到整型数值。://匿名枚举enum{Male,Female}; //0,1//有名枚举enumColor{Red,Yellow=2,Blue}; //0,2,3枚举成员默认从0开始,随后向下递增,也......
  • 实验6
    task41#include<stdio.h>2#include<string.h>3#defineN104typedefstruct{5charisbn[20];6charname[80];7charauthor[80];8doublesales_price;9intsales_count;10}Book;11voidoutput(Book......
  • 无涯教程-MFC - 网络编程
    Microsoft提供了许多用于对客户端和服务器应用程序进行编程的API,正在为互联网编写许多新应用程序,并且随着技术,浏览器功能和安全选项的变化,将编写新类型的应用程序。MFC提供了一个CSocket类,用于使用WindowsSockets编写网络通信程序。这是CSocket类中方法的列表。Sr.No.Name......
  • Java并发编程的高级探索
    随着多核处理器的普及,Java并发编程变得越来越重要。为了充分利用硬件资源,开发者需要掌握并发模式和算法、锁的优化技术、并发工具的高级应用,以及并发程序的性能分析方法。并发模式和算法在并发编程中,特定的模式和算法可以帮助我们解决一些复杂的问题:生产者-消费者模式:在多线程环境......