Guest User

hw11_p1.cpp

a guest
Nov 21st, 2013
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. int main(){
  11.     int i, j, k, n = 0, n1, n2, n3, pos;
  12.     std::vector<int> array1, array2, array3;
  13.    
  14.     char *inname1 = "HW11F1.txt";
  15.     char *inname2 = "HW11F2.txt";
  16.     char *inname3 = "output.txt";
  17.    
  18.     cout << "Filenames are restricted to 12 characters" << endl;
  19.    
  20.     cout << "Enter the first of two input file names: ";
  21.     cin >> inname1;
  22.     cout << endl;
  23.    
  24.     cout << "Now a second input file name: ";
  25.     cin >> inname2;
  26.     cout << endl;
  27.    
  28.     cout << "Enter the output file name." << endl
  29.      << "WARNING: ANY EXISTING FILE WITH THIS NAME WILL BE ERASED.";
  30.     cin >> inname3; cout << endl;
  31.    
  32.     ifstream infile1(inname1);
  33.     ifstream infile2(inname2);
  34.    
  35.     if (!infile1 or !infile2) {
  36.         cout << "There was a problem opening a file.";
  37.         return 0;
  38.     }
  39.    
  40.     pos = 0; i = 0;
  41.     while (infile1 >> i) {
  42.         array1.resize(pos+1);
  43.         array1[pos] = i;
  44.         pos++;
  45.     }
  46.    
  47.     pos = 0; i = 0;
  48.     while (infile2 >> i) {
  49.         array2.resize(pos+1);
  50.         array2[pos] = i;
  51.         pos++;
  52.     }
  53.    
  54.     n1 = array1.size();
  55.     n2 = array2.size();
  56.     n3 = n1 + n2;
  57.     array3.resize(n3);
  58.    
  59.     i = 0; j = 0; k = 0;
  60.     while (i < n1 && j < n2) {
  61.         if (array1[i] <= array2[j]) {
  62.             array3[k++] = array1[i++];
  63.         } else {
  64.             array3[k++] = array2[j++];
  65.         }
  66.     }
  67.    
  68.     while (i < n1) array3[k++] = array1[i++];
  69.     while (j < n2) array3[k++] = array2[j++];
  70.  
  71.     ofstream outfile;
  72.     outfile.open(inname3);
  73.    
  74.     i = 0;
  75.     while(i<n3){
  76.         outfile << array3[i] << endl;
  77.         i++;
  78.     }
  79.    
  80.     cout << "Contents of file " << infile1 << " are: ";
  81.     for(i = 0; i < array1.size(); i++){
  82.         cout << array1[i] << endl;
  83.     }  
  84.     cout << "Contents of file " << infile2 << " are: ";
  85.     for(i = 0; i < array2.size(); i++){
  86.         cout << array2[i] << endl;
  87.     }  
  88.     cout << "Contents of merged file, " << outfile << " are: ";
  89.     for(i = 0; i < array3.size(); i++){
  90.         cout << array3[i] << endl;
  91.     }
  92.    
  93.     infile1.close();
  94.     infile2.close();
  95.     outfile.close();
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment