在堆区申请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