2024.1.22


 1.使用多文件编辑,
定义商品信息:商品名称,商品单价,商品购买个数,商品描述,循环输入购买的商品,按单价排序,输出商品信息,计算最贵的商品以及一共花了多少钱? 
在create函数,请实现在堆区申请内存5个连续的内存 
在input函数,请实现循环输入购买的商品 在输入函数,请实现循环输入购买的商品
在bubble函数,请实现按单价排序
在Max函数,计算最贵的商品名称

在Money函数,计算共花了多少钱在output函数,请实现输出 
在free_space函数。实现释放堆区内存 在自由空间函数.实现释放堆区内存

//头文件:
#include
#include
#include
struct goods
{
	char name[20];
	float price;
	int amout;
	char detail[40];
};
struct goods* create(struct goods *p);
void input(struct goods *p);
void bubble(struct goods *p);
void max(struct goods *p);
void money(struct goods *p);
void output(struct goods *p);
struct goods *free_space(struct goods *p);

//主函数:
#include "head.h"
int main(int argc, const char *argv[])
{
	struct goods *p=create(p);
	input(p);
	bubble(p);
	max(p);
	money(p);
	output(p);
	free_space(p);
	p=NULL;
	return 0;
}
//自定义函数:
#include "head.h"
struct goods* create(struct goods *p)
{
	 p=(struct goods *)malloc(sizeof(struct goods*)*5);
	if(p==NULL)
		return NULL;
	return p;
}
void input(struct goods *p)
{
	for(int i=0;i<5;i++)
	{
		scanf("%s",(p+i)->name);
		scanf("%f",&(p+i)->price);
		scanf("%d",&(p+i)->amout);
		scanf("%s",(p+i)->detail);
	}
}
void bubble(struct goods *p)
{
	for(int i=1;i<5;i++)
	{
		for(int j=0;j<5-i;j++)
		{
			if((*(p+j)).price<=(*(p+j+1)).price)
			{
				float t=(*(p+j)).price;
				(*(p+j)).price=(*(p+j+1)).price;
				(*(p+j+1)).price=t;
			}
		}
	}
}
void max(struct goods *p)
{
	float max=(*p).price;
	for(int i=1;i<5;i++)
	{
		if(max<=(*(p+i)).price)
		{
			max=(*(p+i)).price;
		}
	}
	for(int i=0;i<5;i++)
	{
		if(max==(*(p+i)).price)
		{
			printf("The most expensive goods is:%s\n",(*(p+i)).name);
		}
	}
}
void money(struct goods *p)
{
	float sum=0;
	for(int i=0;i<5;i++)
	{
		sum+=((*(p+i)).price)*((*(p+i)).amout);
	}
	printf("The total money is %.2f\n",sum);
		
}
void output(struct goods *p)
{
	for(int i=0;i<5;i++)
	{
		printf("Name\tPrice\tAmout\tDetail\n");
		printf("%s\t%.2f\t%d\t%s\n",(*(p+i)).name,(*(p+i)).price,(*(p+i)).amout,(*(p+i)).detail);
	}
	
}
struct goods *free_space(struct goods *p)
{
	if(p==NULL)
		return NULL;
	free(p);
	return p;
}

2024.1.22_第1张图片

思维导图

2024.1.22_第2张图片

你可能感兴趣的:(c语言,图论,算法)