2024.1.22

2024.1.22_第1张图片

#include
#include
#include
typedef struct goods
{
	char name[20];
	float price;
	int number;
	char information[50];
}goods;
//创建
goods * creat( int n)
{
  goods *a=(goods*)malloc(sizeof(goods)*n);
 return a;
};
	//输入
goods * input1(int n,goods *q)
{
	for(int i=0;iname);
		scanf("%f",&(q+i)->price);
		scanf("%d",&(q+i)->number);
		scanf("%s",(q+i)->information);
	}
	return q;
}
//最大价格
int max_price(int n,goods*m)
{
	for(int i=0;iprice<(m+j+1)->price)
			{
				float t=(m+j)->price;
				(m+j)->price=(m+j+1)->price;
				(m+j+1)->price=t;
			}
		}
	}
	return (m+0)->price;
}
	//金钱总和

float money(int n,goods *l)
{
	float sum=0;
	for(int i=0;iprice*(l+i)->number;
	}
	return sum;
}

	//输出
void output(float max2,float money,int n,goods *h)
{
	char temp[20];
	for(int i=0;iprice<(h+j+1)->price)
			{
				float t=(h+j)->price;
				(h+j)->price=(h+j+1)->price;
				(h+j+1)->price=t;	
				strcpy(temp,(h+j)->name);
				strcpy((h+j)->name,(h+j+1)->name);
				strcpy((h+j+1)->name,temp);
				float  jiage=(h+j)->number;
				(h+j)->number=(h+j+1)->number;
				(h+j+1)->number=jiage;
				strcpy(temp,(h+j)->information);
				strcpy((h+j)->information,(h+j+1)->information);
				strcpy((h+j+1)->information,temp);
					}
		}
	}
	for(int i=0;iname,(h+i)->price,(h+i)->number,(h+i)->information);
	}
	printf("max_price=%.1f\n",max2);
	printf("money=%.1f\n",money);
}
//释放
goods *free1(goods * b)
{
	free(b);
	b=NULL;
	return b;
}

int main(int argc, const char *argv[])
{
	int n;
	printf("please input n:\n");
    scanf("%d",&n);
	goods *o=creat(n);
	o=input1(n,o);
   float max=max_price(n,o);
  float	sum=money(n,o);
  output(max,sum,n,o);
	o=free1(o);
	return 0;
}

2024.1.22_第2张图片

2024.1.22_第3张图片

你可能感兴趣的:(算法)