Re: [Dev-C++] merging two sorted lists using file streams...
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Luigi S. <gi...@li...> - 2002-06-08 16:22:50
|
At 00.48 06/08/02 +0000, chromatonus 712 wrote:
>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>
You are including too many things, the sole you need is:
#include <fstream>
If you plan to use the Dos function "pause", you should also include
"<cstdlib>". Note that if you run console programs from a console (I
recommend you do) there is no need for the line: system("pause"); which,
however, makes your code Dos / Windows specific.
>int main()
>{
> ifstream a; ifstream b;
> ofstream c;
> a.open("A.dat");
> b.open("B.dat");
Files may be attached to streams using the constructor:
std::ifstream a("A.dat");
std::ifstream b("B.dat");
std::ofstream c("C.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;
> }
The above code, is probably clearer this way:
int n1; // buffer for first stream
int n2; // buffer for second stream
a >> n1; // load buffers
b >> n2;
while (a && b)
if (n1 < n2) // print element of first list
{
c << n1 << ' ';
a >> n1; // load next element of first list
}
else // print element of second list
{
c << n2 << ' ';
b >> n2;
}
> while( c << temp )
> {
> cout << temp << endl;
> }
I don't understand what the above lines do (why are you printing on "c" and
then on the screen?), however, to complete the task, you should copy what
is left of the first or second stream, not forgetting to flush the buffer.
Here is what I mean:
if (a) // while-loop ended because second file exhausted
{
c << n1 << ' '; // flush buffer
copy(a, c); // copy the rest of the first list
}
else
{
c << n2 << ' ';
copy(b, c);
}
The function "copy" could be:
inline void
copy(std::istream is, std::ostream os)
{ int n; while (is >> n) os << n << ' '; }
> system("PAUSE");
> a.close();
> b.close();
> c.close();
>
>
> return 0;
None of these lines is really required. I've already explained the reason
why not to use"system", as far as file streams are concerned, the
destructor will take care of closing them. "return 0" is implicit.
>}
Putting pieces together and testing the program is your fun :)
NOTE: from the way you write include directives, I would say that you are
working with pre-ANSI C++. I strongly suggest you switch to ANSI C++ as
soon as you can.
--
Luigi
gi...@li...
|