使用C语言中的文件实现排序,而不使用数组。 [英] Implement sorting by using file in C without using array.

查看:94
本文介绍了使用C语言中的文件实现排序,而不使用数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a)我的目标是创建整数个大小的文件num1.txt,num2.txt,num3.txt,num4.txt,num5.txt



1000,10000,20000,30000,50000分别没有数组的帮助。



b)创建它们之后我只需要在上面的文件中写一次,随机整数使用



rand()函数用于循环和fprintf并保持原样。



之后我需要使用上述文件的数据来实现heapsort。



所以为了实现它,我采取了以下步骤:



1)根据用户输入选择文件(分别在上面的步骤a和b中创建并写入的文件)



of数据大小(使用if else)。



2)选择适当的文件后,读取数据(在本例中读取整数)表格



文件直接(没有任何数组的帮助)。



3)然后使用heapsort算法对其进行排序并找到程序运行时间。我已经使用了difftime()库函数



做这个工作。



4)排序之后data将结果写入另一个整数文件(result.txt)。



5)直接读取并打印已排序的文件(result.txt)任何阵列的帮助。





我的问题:



来自(上面的a和b)我能够创建和写入名为num1.txt,num1.txt,num2.txt,num3.txt,



num4.txt的文件整数数量分别为1000,10000,20000,30000 - 但只能借助于



整数数组y []。



我不想使用整数数组y []。我希望我可以直接写入文件没有数组是一个



中介。我不想使用数组的原因是因为当我使用整数数组写入上述命名文件时,C编译器不接受超过30,000个数组整数。我想b / b
猜这是内存在程序中可以预留的最大空间限制



执行时间

所以我最多停留了30,000个整数。



步骤2,步骤4,步骤5的相同问题。



总而言之,我不想使用数组作为写入和读取文件的中介。



请告知如何在不使用数组的情况下实现这一点。





以下是代码:

a) My aim is to create files num1.txt, num2.txt, num3.txt, num4.txt, num5.txt of integer numbers of sizes

1000, 10000, 20000, 30000, 50000 respectively without the help of array.

b) After creating them I need to write only once into the above files , the random integer numbers using

rand() function for loop and fprintf and leave them as it is.

After that I need to implement heapsort using the data of the above files.

So in order to implement it I have taken the below steps:

1) Select the file (files cretaed and written in steps a and b respectively above) according to the user input

of data size (using if else).

2) After the appropriate file has been selected, read the data (in this case read the integer numbers) form

the file directly (without help of any array).

3) Then use heapsort algo to sort it and find the program run time. I have used difftime() library function to

do this job.

4) After sorting the data write the result into some another integer number file (result.txt).

5) Read and print the sorted file (result.txt) from the directly without the help of any array.


My issue :

From (a and b above) I am able to create and write files named num1.txt, num1.txt, num2.txt, num3.txt,

num4.txt of integer numbers of sizes 1000, 10000, 20000, 30000 respectively - "but only with the help of

an integer array y[]".

I dont want to use an integer array y[]. I wish I could directly write into the file "without" the array being an

intermediary. The reason I donot want to use the array is because when I use an integer array to write

into the above named files, the C compiler doesnot accept more than 30,000 array integer numbers. I

guess that is the maximum limit of the amout of space that the memory can reserve during program

execution time
So I am stuck with a maximum of 30,000 integer numbers.

Same issue with steps 2, step 4, step 5.

To summarize, I donot want to use an array as an intermediary for writing into and reading from the file.

Please advise how could this be implemented without using an array.


Below is the code :

//filee.c

// Creating files and writing data into them


#include<stdio.h>
#include<conio.h>
#define SIZE 30000
void main()
{

FILE *fp;  //fp is the file pointer 

int i , y[SIZE];  // y[SIZE] is the array that I am having issues with (discussed above)
clrscr();

//creating file num1.txt and writing into it

fp=fopen ("num1.txt", "w");
for (i=0;i<1000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);

//creating file num2.txt and writing into it

fp=fopen ("num2.txt", "w");
for (i=0;i<10000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);


//creating file num3.txt and writing into it


fp=fopen ("num3.txt", "w");
for (i=0;i<20000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);

//creating file num4.txt and writing into it


fp=fopen ("num4.txt", "w");
for (i=0;i<30000;i++)
{
  y[i]=rand()/100;
  fprintf (fp,"%d\n",&y[i]);
}
close(fp);


getch();
}



// Implemting heapsort   


#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>

void createheap (int x[],int n);
void heapsrt (int x[],int n);

void main()
{
int y[30000];
int i,size;
FILE *fp; // file pointer

time_t t1,t2;
clrscr();

//User input to select the file according to the size

printf ("\nEnter the size of the data : ");
scanf ("%d", &size);

if (size > 0 && size <=1000)
{
fp=fopen ("num1.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}

else if (size > 1000 && size <=10000)
{
fp=fopen ("num2.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}

else if (size > 10000 && size <=20000)
{
fp=fopen ("num3.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d ", y[i]);
}
}


else if (size > 20000 && size <=30000)
{
fp=fopen ("num4.txt", "r");
for (i=0;i<size;i++)>
{
fscanf (fp,"%d",&y[i]);
printf ("%d  ", y[i]);
}
}


else
{
printf ("\nInvalid entry!!");
exit (1);
}

  t1 = time(NULL);  /* Gets system time */

  createheap(y,size); // heap sort functions
  heapsrt(y,size); //heapsort functions

  t2 = time(NULL); /* Gets system time
			   again */

fp = fopen ("result.txt","w");

for (i=0;i<size;i++)>
{
fprintf (fp, "%d\n", y[i]);
}

fclose (fp);

printf ("\nHeap sort : \n");

fp = fopen("result.txt","r");

for (i=0;i<size;>{
fscanf (fp,"%d",&y[i]);
printf ("%d  ",y[i]);
}
printf("\n The sorting done in : %f seconds\n",difftime(t2,t1));

getch();
}


/*function definitions*/

void createheap (int arr[], int num)
{
	int i,numval,s,f;

	for (i=0;i<num;i++)>
	{
	numval=arr[i];
	s=i;
	f=(s-1)/2;

	while (s>0&&arr[f]<numval)>
	{
	arr[s] = arr[f];
	s=f;
	f=(s-1)/2;
	}

	arr[s]=numval;
	}
}

void heapsrt (int arr[], int num)
{
int i,s,f,val;
for (i=num-1;i>0;i--)
{
val = arr[i];
arr[i]=arr[0];
f=0;
if (i==1)
s=-1;
else
s=1;

if (i>2 && arr[2]>arr[1])
s=2;

while (s>0 && val < arr[s])
{
arr[f]=arr[s];
f=s;
s=2*f+1;

if (s+1 <= i-1 && arr[s] < arr[s+1])
s++;
if (s > i-1)
s=-1;
}

arr[f]=val;
}
}

推荐答案

我们不做你的作业:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于使用C语言中的文件实现排序,而不使用数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆