【0501】有序列表合并

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100


void openfile(char *str,int list[])
{
	FILE *fp;
	int i=0,j=0;
	int temp;
	char ch;
	if((fp = fopen(str,"r"))==NULL)
	{

		printf("can't open %s\n",str);
		exit(0);
	}
	while((fscanf(fp,"%d",&temp))==1)
	{
		list[++i] = temp;
		fscanf(fp,",",&ch);

	}
	list[0] = i;
	for(i=1;i<=list[0];i++)
	{

		printf("%d ",list[i]);
	}
	printf("\n");
	if(fclose(fp)!=0)
		printf("Error in closing file %s/n",str);
}
void write(int list[],char *str)
{
	FILE *fp;
	int i=1;
	int temp;
	if((fp = fopen(str,"w"))==NULL)
	{

		printf("can't open %s\n",str);
		exit(0);
	}
	while(i<=list[0])
	{
		fprintf(fp,"%d ",list[i]);
		i++;
	}
	if(fclose(fp)!=0)
		printf("Error in closing file %s/n",str);

}

void sort(int list1[],int list2[],int list[])
{

	int i=1,j=1,k=1,temp;
	while(i<=list1[0]&&j<=list2[0])
	{

		if(list1[i]<=list2[j])
		{

			list[k] = list1[i];
			i++;
			k++;
		}
		else
		{
			list[k] = list2[j];
			j++;
			k++;
		}
	}
	while(i<=list1[0])
	{
		list[k++] = list1[i++];

	}
	while(j<=list2[0])
	{
		list[k++] = list2[j++];

	}
	list[0] = k-1;
	for(i=1;i<=list[0];i++)
	{

		printf("%d ",list[i]);
	}
}
int main()
{
	int list1[MAXLEN];
	int list2[MAXLEN];
	int list[MAXLEN];
	openfile("A.txt",list1);
	openfile("B.txt",list2);
	sort(list1,list2,list);
	write(list,"c.txt");
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值