8.1作业

#include
//定义从堆区申请空间的函数,num表示申请个数
int *men(int num)
{
    int *p=(int *)malloc(sizeof(int)*num);
}
//定义输入函数
void input(int *p,int n)
{
	if(NULL!=p)
	{
		int i=0;
		for(i=0;i<n;i++)
		{
			scanf("%d", p+i);
		}
	}
}
//定义输出函数
void output(int *p, int n)
{
	int i = 0;
	for(i=0;i<n;i++)
	{
		printf("%-4d", *(p+i));
	}
}
//定义冒泡排序
void sort_(int *p, int n)
{
	int i,j,temp;
	for(i=0;i<n-1;i++)
	{
		for(j=0;j<n-1-i;j++)
		{
			if(*(p+j)>*(p+j+1))
			{
				temp = *(p+j);
				*(p+j) = *(p+j+1);
				*(p+j+1) = temp;
			}
		}
	}
}
void FREE(int *p)
{
	if(NULL != 0)
	{
		free(p);
		p = NULL;
	}
}
int main()
{
	int *p=(mem);
	input(p,6);
	sort_(p,6);
	output(p,6);
	printf("\n");
	FREE(p);
	p = NULL;
	return 0;
}
#include
#include
//将无符号短整型重命名为uint
typedef unsigned short int uint16;
//将int*类型重命名为Ptr_i
typedef int * Ptr_i;
//将char[10]重命名为String
typedef char String[10];

int main()
{
	//等价于unsigned short int num =520;
	uint16 num=520;
	printf("sizeof num=%d\n",sizeof(num)); //2
	printf("num=%d\n",num); //520
	Ptr_i p;  //此时p是指向指针变量 int *p;
	printf("sizeof p=%ld\n", sizeof(p));  //8
	String s;
	strcpy(s,"hello");
	printf("s=%s\n", s);
	printf("sizeof s=%ld\n",sizeof(s));//10
	printf("strlen s=%ld\n",strlen(s));//5
	return 0;
}
#include
//声明一个英雄结构体类型
struct Hero
{
	char name[20];
	int Hp;
	double speed;
	char KDA;
}h3={"盖伦", 3500, 500, 5/3/1};
struct Goods
{
	char name[30];
	char position[30];
	float price;
	float weight;
}/*g1={"三鹿奶粉", "China", 350, 1000}*/;

int main()
{
	//使用英雄类型定义一个英雄变量
	struct Hero h1={"亚索", 594, 350, 0/8/0};
	//定义英雄变量指定某个成员进行赋值
	struct Hero h2={.Hp=2000, .speed=1000};
	//输出英雄变量h1中的所有内容
	printf("h1.name=%s\n", h1.name);
	printf("h1.Hp=%d\n", h1.Hp);
	printf("h1.speed=%lf\n", h1.speed);
	printf("h1.KDA=%s\n", h1.KDA);
	
	//给商品变量中的内容赋值
	struct Goods g2;
	printf("请输入商品名称:");
	gets(g2.name);
	printf("请输入商品产地:");
	gets(g2,position);
	printf("请输入商品价格:");
	scanf("%f", &g2.price);
	printf("请输入商品重量:");
	scanf("%f", &g2.weight);
	//输出商品变量中的内容
	printf("商品名称:=%s\n",g2.name);
	printf("商品产地:=%s\n",g2.position);
	printf("商品价格:=%f\n",g2.price);
	printf("商品重量:=%f\n",g2.weight);
	struct Hero *ptr = (struct Hero*)malloc(sizeof(struct Hero));
	//给英雄名字赋值
	strcpy(ptr->name, "亚瑟");
	ptr->Hp = 3000;
	ptr->speed = 350;
	ptr->KDA = 3/7/0;

	printf("英雄信息为:%s %d %.2f %s\n", ptr->name, ptr->Hp, ptr=->speed, ptr->KDA);
	return 0;
}

你可能感兴趣的:(数据结构)