Re: [Dev-C++] merging two sorted lists using file streams...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@nt...> - 2002-06-08 03:22:32
|
Quick tip:
get 'a'
get 'b'
repeat
if 'a' is greater than 'b'
output 'b'
get 'b'
else
output 'a'
get 'a'
until one of the streams is empty
This is a simplistic algorithm for you to follow, but it demonstrates the way two sorted streams can
be "merged" (lookup that word in an algorithms book). There is still one more thing for you to add:
When one of the streams runs out then you need to continue feeding from the other and outputing it
until it is also empty. And don't forget that the streams may be empty to start with! (tip: use a
while loop)
It would be wise for you to figure the rest out yourself, but one more tip: If you are repeating the
same steps several times in different places in a program, then it's a good idea to make those steps
into a function (i.e. getFromStream(); outputToStream();). Meaningful variable names also help
(instream_a, instream_b, outstream)
Jason.
PS: Not as quick a tip as I had planned, but at least I didn't hand everything to you on a plate.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Saturday, June 08, 2002 1:48 AM
Subject: [Dev-C++] merging two sorted lists using file streams...
Hi, here is my program that is suppose to read one piece of data from 2
different sorted lists each,
like these two text files:
File A:
1
2
4
8
16
File B:
0
1
1
2
3
5
8
output should be
File C:
0 1 1 1 2 2 3 4 5 8 8 16
Would anyone give a sample program that performs this function, using only
the "basic" file stream operations as in my program below? Thank you very
much
#include "apstring.h"
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
int main()
{
ifstream a; ifstream b;
ofstream c;
a.open("A.dat");
b.open("B.dat");
int ark, bum, j, k, temp;
a >> ark;
a.ignore(80,'\n');
j = ark;
b >> bum;
b.ignore(80,'\n');
k = bum;
if( ark < bum )
{
c << ark << endl;
a >> ark;
a.ignore(80,'\n');
temp = ark;
}
else
{
c << bum << endl;
b >> bum;
b.ignore(80,'\n');
temp = bum;
}
if(j<k)
{
while( b>>bum && bum<temp )
{
b >> bum;
b.ignore(80,'\n');
c << bum << endl;
b >> bum;
b.ignore(80,'\n');
}
c << temp << endl;
temp = bum;
}
else
{
while( a>>ark && ark<temp )
{
a >> ark;
a.ignore(80,'\n');
c << ark << endl;
a >> ark;
a.ignore(80,'\n');
}
c << temp << endl;
temp = ark;
}
while( c << temp )
{
cout << temp << endl;
}
system("PAUSE");
a.close();
b.close();
c.close();
return 0;
}
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
_______________________________________________________________
Don't miss the 2002 Sprint PCS Application Developer's Conference
August 25-28 in Las Vegas - http://devcon.sprintpcs.com/adp/index.cfm?source=osdntextlink
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|