16-6.2:汽车销量排行榜

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#define N 200

typedef struct
{
	char cartype[20];
	char menufacturer[20];
	int sales[12];
	int sum_sales;//到指定定月份的累计销量 
}car;

int main()
{
	void input_save();
	void rank_print(int month);
	
	int month=0;
	input_save();
	while(month<1||month>12)
	{
		printf("Input the month you want to query:\n");
		scanf("%d",&month);
	}
	rank_print(month);
	return 0;
}

void rank_print(int month)
{
	int i,j,m=month-1,sum=0;
	car c[N],temp;
	FILE *fp;
	if((fp=fopen("Sales2015.dat","rb"))==NULL)
	{
		printf("Read Open Error!\n");
	}
	for(i=0;i<N;i++)
	{
		sum=0;
		if(fread(&c[i],sizeof(car),1,fp)!=1)
		{
			printf("Read Error!\n");
		}
		for(j=0;j<=m;j++)
		{
			sum+=c[i].sales[j]; 
		}
		c[i].sum_sales=sum;
	}
	fclose(fp);
	for(i=0;i<N-1;i++)
	{
		for(j=i+1;j<N;j++)
		{
			if(c[i].sales[m]<c[j].sales[m])
			{
				temp=c[i];
				c[i]=c[j];
				c[j]=temp;
			}
		}
	}
	printf("2015年%d月汽车销量排行榜\n",month);
	printf("%s  %s  %s  %d%s  %s\n","排名","车型","生产厂商",month,"月销量","累积销量"); 
	for(i=0;i<N;i++)
	{
		printf("%d\t%s\t%s\t%d\t%d\n",i+1,c[i].cartype,c[i].menufacturer,c[i].sales[m],c[i].sum_sales);
	}
}

void input_save()
{
	car c[N];
	int i,j;
	FILE *fp;
	printf("Enter information including cartype , manufacturer and sales within 12 months, separated by enter :\n");
	if((fp=fopen("Sales2015.dat","wb"))==NULL)
	{
		printf("Write Open Error!\n");
	}
	for(i=0;i<N;i++)
	{
		printf("Input No.%d:\n",i+1);
		scanf("%s",c[i].cartype);
		scanf("%s",c[i].menufacturer);
		for(j=0;j<12;j++)
		{
			scanf("%d",&c[i].sales[j]);
		}
		c[i].sum_sales=0;
		
	}
	for(i=0;i<N;i++)
	{
		if(fwrite(&c[i],sizeof(car),1,fp)!=1)
		{
			printf("Write Error!\n");
		}
	}
	fclose(fp);	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值