Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. class Line;
  9. class Difference;
  10.  
  11. vector <Line> differentLines;
  12.  
  13. class Difference
  14. {
  15. public:
  16.     int startPos;
  17.     int endPos;
  18.     string firstFileVersion;
  19.     string secondFileVersion;
  20.  
  21.     Difference(int startPos, int endPos, const string & firstFileVersion, const string & secondFileVersion)
  22.     {
  23.         this->startPos = startPos;
  24.         this->endPos = endPos;
  25.         this->firstFileVersion = firstFileVersion;
  26.         this->secondFileVersion = secondFileVersion;
  27.     }
  28.  
  29. };
  30.  
  31. class Line
  32. {
  33.  
  34.     string line1;
  35.     string line2;
  36.     vector <Difference> differences;
  37.     int lineNumber;
  38.  
  39. public:
  40.  
  41.     Line(const string & _line1, const string & _line2, int lineNumber)
  42.     {
  43.         line1 = _line1;
  44.         line2 = _line2;
  45.         this->lineNumber = lineNumber;
  46.     }
  47.  
  48.     void findDifferences()
  49.     {
  50.         if(line1 == line2)
  51.             return;
  52.         else
  53.         {
  54.             string differenceString1 = "";
  55.             string differenceString2 = "";
  56.             int startPos = -1;
  57.             int endPos = -1;
  58.  
  59.             if(line1.length() == line2.length()) // if lines are of the same size
  60.             {
  61.  
  62.                 for(int i = 0; i < line1.length(); i++)
  63.                 {
  64.                     if(line1[i]!=line2[i] && startPos == -1) // if it's new difference
  65.                     {
  66.                         startPos = i;
  67.                         differenceString1 += line1[i];
  68.                         differenceString2 += line2[i];
  69.                     }
  70.                     else if(line1[i]!=line2[i] && startPos != -1) // still the same difference, so only updating difference strings
  71.                     {
  72.                         differenceString1 += line1[i];
  73.                         differenceString2 += line2[i];
  74.                     }
  75.                     else if (line1[i] == line2[i] && startPos != -1) // end of the difference
  76.                     {
  77.                         endPos = i-1;
  78.                         this->differences.emplace_back(startPos, endPos, differenceString1, differenceString2);
  79.                         startPos = -1;
  80.                         endPos = -1;
  81.                         differenceString1 = "";
  82.                         differenceString2 = "";
  83.                     }
  84.                 }
  85.                 if(endPos == -1 && startPos != -1) // if line ended and last difference is still not complete, then the end of the difference is the end of the line
  86.                 {
  87.                     endPos = (int)line1.length()-1;
  88.                     this->differences.emplace_back(startPos, endPos, differenceString1, differenceString2);
  89.                 }
  90.                 differentLines.push_back(*this);
  91.             }
  92.             else if(line1.length()!=line2.length()) // if lines are not the same size
  93.             {
  94.                 string longerLine;
  95.                 string shorterLine;
  96.                 bool firstLonger = 0;
  97.                 if(line1.length() > line2.length()) // looking for longer line
  98.                 {
  99.                     longerLine = line1;
  100.                     shorterLine = line2;
  101.                     firstLonger = 1;
  102.                 }
  103.                 else
  104.                 {
  105.                     longerLine = line2;
  106.                     shorterLine = line1;
  107.                 }
  108.  
  109.                 if(shorterLine == longerLine.substr(0,shorterLine.length())) // if the lines are the same only one of them has something added at the end
  110.                 {
  111.                     differenceString1 = longerLine.substr(shorterLine.length(),longerLine.length()-shorterLine.length()); // catch the added value to the longer line
  112.                     startPos = shorterLine.length();
  113.                     endPos = longerLine.length();
  114.                     if(firstLonger == 1)
  115.                         this->differences.emplace_back(startPos, endPos, differenceString1, "");
  116.                     else
  117.                         this->differences.emplace_back(startPos, endPos, "", differenceString1);
  118.                 }
  119.                 else
  120.                 {
  121.                     for(int i = 0; i < shorterLine.length(); i++)
  122.                     {
  123.                         if(line1[i]!=line2[i] && startPos == -1) // if it's new difference
  124.                         {
  125.                             startPos = i;
  126.                             differenceString1 += line1[i];
  127.                             differenceString2 += line2[i];
  128.                         }
  129.                         else if(line1[i]!=line2[i] && startPos != -1) // still the same difference, so only updating difference strings
  130.                         {
  131.                             differenceString1 += line1[i];
  132.                             differenceString2 += line2[i];
  133.                         }
  134.                         else if (line1[i] == line2[i] && startPos != -1) // end of the difference
  135.                         {
  136.                             endPos = i-1;
  137.                             this->differences.emplace_back(startPos, endPos, differenceString1, differenceString2);
  138.                             startPos = -1;
  139.                             endPos = -1;
  140.                             differenceString1 = "";
  141.                             differenceString2 = "";
  142.                         }
  143.                     }
  144.                     if(endPos == -1 && startPos != -1) // if line ended and last difference is still not complete, then the end of the longer word is the end of the line
  145.                     {
  146.                         endPos = (int)longerLine.length()-1;
  147.                         this->differences.emplace_back(startPos, endPos, differenceString1, differenceString2);
  148.                     }
  149.                     else
  150.                     {
  151.                         differenceString1 = longerLine.substr(shorterLine.length(),longerLine.length()-shorterLine.length()); // catch the added value to the longer line
  152.                         startPos = shorterLine.length()-1;
  153.                         endPos = longerLine.length()-1;
  154.                         if(firstLonger == 1)
  155.                             this->differences.emplace_back(startPos, endPos, differenceString1, "");
  156.                         else
  157.                             this->differences.emplace_back(startPos, endPos, "", differenceString1);
  158.                     }
  159.                 }
  160.  
  161.                 differentLines.push_back(*this);
  162.             }
  163.         }
  164.     }
  165.  
  166.     void printDifferences() const
  167.     {
  168.         for(auto const & value : differences)
  169.         {
  170.             cout << endl << "In line: " << this->lineNumber <<  "  In positions " << value.startPos << " - " << value.endPos << endl << "Version 1: " << value.firstFileVersion << endl << "Version 2: " << value.secondFileVersion << endl;
  171.         }
  172.     }
  173. };
  174.  
  175.  
  176. bool parameterChecker(int argc, char ** argv) // check if parameters were passed correctly
  177. {
  178.     if(argc != 3)
  179.         return false;
  180.     string str1(argv[1]);
  181.     string str2(argv[2]);
  182.     if(str1.length()>=5 && str1.length()>=5)
  183.     {
  184.         str1 = str1.substr(str1.length() - 4, 4);
  185.         str2 = str2.substr(str2.length() - 4, 4);
  186.  
  187.         if(str1 == ".txt" && str2 == ".txt")
  188.             return true;
  189.         return false;
  190.     }
  191. }
  192.  
  193. inline int openFiles(char ** argv, ifstream & File1, ifstream & File2)
  194. {
  195.     File1.open(argv[1]);
  196.     File2.open(argv[2]);
  197.     if(File1 && File2) // file have been sucessfully opened
  198.         return 1; // 1 - opened
  199.     return 0; // failed to open
  200. }
  201.  
  202. int main(int argc, char ** argv)
  203. {
  204.     if(parameterChecker(argc,argv))
  205.     {
  206.         ifstream firstFile, secondFile;
  207.         openFiles(argv, firstFile, secondFile);
  208.  
  209.         string firstFileLine;
  210.         string secondFileLine;
  211.         int curline = 0;
  212.         Line * lineToCheck;
  213.  
  214.         while (true)
  215.         {
  216.             getline(firstFile, firstFileLine);
  217.             getline(secondFile, secondFileLine);
  218.  
  219.             if (firstFile.eof() && secondFile.eof())
  220.                 break;
  221.  
  222.             curline++;
  223.             if(firstFile.eof())
  224.             {
  225.                 Line *lastLines = new Line("", secondFileLine, curline);
  226.                 lastLines->findDifferences();
  227.                 while(getline(secondFile,secondFileLine))
  228.                 {
  229.                     delete lastLines;
  230.                     curline++;
  231.                     lastLines = new Line("", secondFileLine, curline);
  232.                     lastLines->findDifferences();
  233.                 }
  234.                 delete lastLines;
  235.             }
  236.             else if(secondFile.eof())
  237.             {
  238.                 Line *lastLines = new Line(firstFileLine,"", curline);
  239.                 lastLines->findDifferences();
  240.                 while(getline(firstFile,firstFileLine))
  241.                 {
  242.                     delete lastLines;
  243.                     curline++;
  244.                     lastLines = new Line(firstFileLine,"", curline);
  245.                     lastLines->findDifferences();
  246.                 }
  247.                 delete lastLines;
  248.             }
  249.             lineToCheck = new Line(firstFileLine, secondFileLine, curline);
  250.             lineToCheck->findDifferences();
  251.             delete lineToCheck;
  252.  
  253.         }
  254.  
  255.     }
  256.  
  257.         for (auto const &value : differentLines)
  258.             value.printDifferences();
  259.  
  260.     return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement