8.23作业

1.间接定义结构体数组,进行4种方式的定义和初始化
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
        char name[20];
        int age;
        char sex;
};

int main(int argc, const char *argv[])
{ 

        struct student stu1[2]={"bbaa",10,'W',"bbaa1",13,'W'};
    struct student stu2[2]={[1]={.sex='W',.name="李四",.age=20},
                            [0]={"张三",.sex='M',.age=18}};


     struct student stu3[2];
     strcpy(stu3[0].name,"张三");
     stu3[0].sex='M';
     stu3[0].age=16;
     strcpy(stu3[1].name,"李四");
     stu3[1].sex='M';
     stu3[1].age=18;

   struct student stu4[2];
    for(int i=0;i<2;i++)
    {
         scanf("%s",stu4[i].name);  
         scanf(" %c",&stu4[i].sex);
         scanf("%d",&stu4[i].age);
    }
        return 0;
}

2.定义结构体存储10辆车(车的信息:品牌、单价、颜色)

        1,定义函数,实现循环输入

        2,定义函数,实现排序

        3,定义函数,计算红色车的个数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct{
        char brand[10];
        int price;
        char colour[10];
}car_t;

void input_car(car_t car[10]);      //输入函数
void sort_car(car_t car[10]);       //排序函数
int red_car(car_t car[10]);         //查找函数
void print_car(car_t car[10]);      //打印函数
void auto_input_car(car_t car[10]); //自动赋值

int main(int argc, const char *argv[])
{  
        car_t car[10];
        int red_num=0;
        //      input_car(car);
        auto_input_car(car);

        printf("\n\n排序前:\n");
        print_car(car);

        sort_car(car);
        printf("\n\n排序后:\n");
        print_car(car);

        red_num=red_car(car);
        printf("red色的车:%d辆\n",red_num);
        return 0;
}

void input_car(car_t car[10]){

        for(int i=0; i<10; i++){
                printf("--------------------第%d辆车--------------------\n",i+1);
                printf("brand:");
                scanf("%s",car[i].brand);
                printf("price:");
                scanf("%d",&car[i].price);
                printf("colour:");
                scanf("%s",car[i].colour);
        }
}
void sort_car(car_t car[10]){
        for(int i=0; i<10-1; i++){
                for(int j=0; j<10-1-i; j++){
                        if(car[j].price>car[j+1].price){
                                car_t t=car[j];
                                car[j]=car[j+1];
                                car[j+1]=t;
                        }
                }
        }
}
int red_car(car_t car[10]){
        int count=0;
        for(int i=0; i<10; i++){
                if(strcmp(car[i].colour,"red")==0){
                        count++;
                }
        }
        return count;
}

void print_car(car_t car[10]){
        for(int i=0; i<10; i++){

                printf("--------------------第%d辆车--------------------\n",i+1);
                printf("brand=%s\tprice=%d\tcolour=%s\n",car[i].brand,car[i].price,car[i].colour);
        }
        puts("------------------------------------------------");
}
void auto_input_car(car_t car[10]){
        srand((unsigned int) time(0));

        int i,j;
        for(i=0; i<10; i++){
                car[i].price=rand()%100*10;

                for(j=0; j<3; j++){
                        car[i].brand[j]=65+rand()%26;

                }
                        car[i].brand[j]='\0';
                        car[i].colour[0]=114+rand()%3;
                        car[i].colour[1]='e';
                        car[i].colour[2]='d';
                        car[i].colour[3]='\0';

        }


}
y@DESKTOP-1DH5HNK:~/23071/数据结构/day1$ ./a.out 


排序前:
--------------------第1辆车--------------------
brand=ICD       price=720       colour=red
--------------------第2辆车--------------------
brand=GQA       price=240       colour=ted
--------------------第3辆车--------------------
brand=OCQ       price=90        colour=ted
--------------------第4辆车--------------------
brand=NZO       price=570       colour=ted
--------------------第5辆车--------------------
brand=ALI       price=390       colour=sed
--------------------第6辆车--------------------
brand=VEU       price=900       colour=ted
--------------------第7辆车--------------------
brand=YBV       price=650       colour=ted
--------------------第8辆车--------------------
brand=OII       price=260       colour=red
--------------------第9辆车--------------------
brand=BEY       price=160       colour=ted
--------------------第10辆车--------------------
brand=GHO       price=200       colour=sed
------------------------------------------------


排序后:
--------------------第1辆车--------------------
brand=OCQ       price=90        colour=ted
--------------------第2辆车--------------------
brand=BEY       price=160       colour=ted
--------------------第3辆车--------------------
brand=GHO       price=200       colour=sed
--------------------第4辆车--------------------
brand=GQA       price=240       colour=ted
--------------------第5辆车--------------------
brand=OII       price=260       colour=red
--------------------第6辆车--------------------
brand=ALI       price=390       colour=sed
--------------------第7辆车--------------------
brand=NZO       price=570       colour=ted
--------------------第8辆车--------------------
brand=YBV       price=650       colour=ted
--------------------第9辆车--------------------
brand=ICD       price=720       colour=red
--------------------第10辆车--------------------
brand=VEU       price=900       colour=ted
------------------------------------------------
red色的车:2辆

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值