1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int count=0;
int SortedArray( int A[], int Int_num, int lo, istream &infile)
{
int in = 0;
int num=0;
bool found= false;
infile>>A[in];
in++;
//while (in < lo && infile>>A[in])
while(in < Int_num && infile>>A[in])
{
if (A[in] > A[in-1] )
{
in++;
//A[in]=num;
count++;
}
else
{
return -1;
}
}
return in;
}
int store(int A[],int Int_num, istream &infile)
{
int num=0;
while(infile>>A[num]&& num <= Int_num)
{
num++;
}
return num;
}
int display(int A[], int B[], int c[], int lo,int hi, ostream& outfile)
{
int i;
int j;
int f;
int counter = 0;
int counter1 = 0;
int counter3 = 0;
outfile<<"The numbers are: "<<endl;
for ( i = lo; i<= hi; i++)
{
outfile<<A[i]<< endl;
counter++;
}
for (j= lo; j<= hi;j++)
{
outfile<<B[j]<< endl;
counter1++;
}
for (f= lo; f<= hi;f++)
{
outfile<<c[f]<< endl;
counter3++;
}
return counter3;
return counter1;
return counter;
}
int main()
{
ofstream outfile;
ifstream infile, infile1;
const int Int_num=20;
int int_num2=20;
int int_num3=40;
char infile_name[20], outfile_name[20], infile_name1[20];
int p,A[Int_num],B[int_num2],c[int_num3],N,M,J,item,lo=0,hi,C=0;
bool nope;
// first input
cout<<"please enter your file: "<<endl;
cin>> infile_name;
N = store(A, 20, infile);
infile.open(infile_name);
// second input
cout<<"please enter your file: "<<endl;
cin>> infile_name1;
p = store(B, 20, infile1);
infile1.open(infile_name1);
// output
cout<<"please enter outfile: "<<endl;
cin>> outfile_name;
outfile.open(outfile_name);
if(!infile)
{
cout<<"error from infile";
return 0;
}
if(!outfile)
{
cout<<"error from outfile";
return 0;
}
// shorting the Array and storing it into c[]
J=SortedArray(A,Int_num,lo,infile);
M=SortedArray(B,Int_num,lo,infile);
J=0;
M=0;
while(J< Int_num && M < int_num2)
{
if(A[J] < B[M])
{
c[C]=A[J];
J++;
C++;
}
else
{
c[C]=B[M];
M++;
C++;
}
}
while (J < Int_num)
{
c[C]=A[J];
J++;
C++;
}
while (M < int_num2)
{
c[C]=B[M];
M++;
C++;
}
//outfile<<" the numbers are"<<c<<endl;//display
display(A,B,c, 0,(p-1),outfile);
infile.close();
outfile.close();
return 0;
}
|