Guest User

hw11_p1.cpp

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