2023.1.30.作业

在堆区申请5个连续的存储空间,实现车辆信息的输入 (品牌,颜色,价格)

1>调用函数在堆区申请空间

2>调用函数实现输入

3>调用函数对价格排序

4>调用函数输出

5>释放堆区空间

#include "head.h"
int main(int argc, const char *argv[])
{
    int n=4;
    //scanf("%d",&n);
    //在堆区申请空间
    char *s=Space(n);
    //循环输入
    Input(s,n);
    //排序
    //Strrev(s,n);
    //输出
    print(s,n);
    //释放
    free(s);
    s=NULL;
    return 0;
}
//编写函数体
/*void MyStrrev(char *s)
{
    //字符串倒置
}
char MyStrcpy(char *s1,const char *s2)
{
    //字符串拷贝
    while(*s2!='\0')
    {
        *s2=*s1;
        s1++;
        s2++;
    }
}
int Mystrcmp(const char *s1,const char *src)
{
    //字符串比较
    int t=0;
    while(*s1!='\0')
    {
        if(*s2=='\0')
        {
            t=-1;
        }
        s2++;
        s1++;
    }
    if(*s2!='\0')
    {

    }
    return t;
}
char *Mystrcat(char *dest,const char *src)
{
    //字符串链接
}
long Mystrlen(const char *dest)
{
    //字符串长度
}*/
#include "head.h"
#include <stdio.h>
void StructIn(struct student s[],int t)
{
    int i;
    for(i=0;i<t;i++)
    {
        printf("姓名\n");
        scanf("%s",s[i].name);
        printf("年龄\n");
        scanf("%d",&s[i].age);
    }
}
void StructOut(struct student s[],int t )
{
    int i;
        for(i=0;i<t;i++)
    {
        printf("姓名:%s \n",s[i].name);
        printf("年龄:%d \n",s[i].age);

    }
}
//申请空间
struct student *Space(int n)
{
    struct student *s=(struct student *)malloc(sizeof(struct student)*n);
    if(NULL==s)
    {
        return NULL;
    }
    return s;
}

//输入
void Input(struct student s[],int n)
{
    int i;
    for(i=0;i<4;i++)
    {
        printf("输入品牌:");
        scanf("%s",s[i].name);
        printf("输入价格:");
        scanf("%d",&s[i].age);
        printf("输入颜色:");
        scanf("%s",s[i].color);
        printf("1\n");
    }

}

//排序
void Strrev(struct student s[],int n)
{
    struct student temp;
    int i,j,k=NULL;
    for(j=1;j<6;j++)
    {

        for(i=0;i<(n-2);i++)
        {
            if(s[i].age<s[i+1].age)
            {
                temp=s[i];
                s[i]=s[i+1];
                s[i+1]=temp;
                k=1;
            }
        }
        if(k==NULL)
        {
            break;
        }
    }
}


//输出
void print(struct student s[],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("品牌:%s 价格:%d 颜色:%s \n",&s[i].name,s[i].age,&s[i].color);
        printf("1\n");
    }

}







#ifndef __head_h__
#define __head_h__
//借用函数体名字
/*void *MyStrrev(char *s);
char MyStrcpy(char *dest,const char *src);
int Mystrcmp(const char *s1,const char *src);
char *Mystrcat(char *dest,const char *src);
long Mystrlen(const char *dest);*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
        char name[10];
         int age;
        char color[10];
};
void StructOut(struct student s[],int t);
void StructIn(struct student s[],int t);
void Input(struct student s[],int n);
void Strrev(struct student s[],int n);
void print(struct student s[],int n);
struct student *Space(int n);
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值