bogdanNiculeasa

Server Password Problem

Feb 4th, 2020
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7. int * extractNumberArrayFromStream(stringstream& stream, int len);
  8. void sortArray (int * numbers, int arrayLength);
  9.  
  10. int main() {
  11.     // This is used to store the content of the first line
  12.     string firstLine;
  13.  
  14.     //This is used to store the content of the second line
  15.     string secondLine;
  16.  
  17.     // This is used for writing into a file
  18.     ofstream fileToWrite;
  19.  
  20.     //This is used to read the file containing the numbers.
  21.     ifstream fileToRead ("acces.in"); //Here, as a an argument we have to give the path to the file that we want to read
  22.  
  23.     //Read the file content
  24.     if (fileToRead.is_open()) {
  25.         //Read the first line and store it into the firstLine variable
  26.         getline(fileToRead, firstLine);
  27.        
  28.         //Read the second line and store it into the secondLine variable
  29.         getline(fileToRead, secondLine);
  30.         //CLose the file. THIS IS VERY IMPORTANT!~
  31.         fileToRead.close();
  32.     }
  33.  
  34.     // By using this streams variable we are able to parse the string and
  35.     // split it by space and extract the numbers from it
  36.     stringstream  streamOfFirstLine(firstLine);
  37.     stringstream streamOfSecondLine(secondLine);
  38.    
  39.     //If the streamOfFirstLine contains "12 23"
  40.     string intermediate;
  41.     // Now we extract the 12 and store it as an integer into the numberOfDigits variable.
  42.     //We use the stoi to convert a string into an integer
  43.     getline(streamOfFirstLine, intermediate, ' ');
  44.     int numberOfDigits = stoi(intermediate);
  45.    
  46.     //Now we read the 23 and do the same as before
  47.     //Remember that the first line contains the n and m numbers
  48.     getline(streamOfFirstLine, intermediate, ' ');
  49.     int numberOfDigitsToBeRemoved = stoi(intermediate);
  50.  
  51.     // We use this function to do the same logic of extracting numbers from a string
  52.     // Only that now we are also storing them into an array as there could be more than 2 numbers as we had before
  53.     int * arr = extractNumberArrayFromStream(streamOfSecondLine, numberOfDigits);
  54.    
  55.     // We call this function to store the number
  56.     //The logic is that if we sort it in descending order, we will have the largest number that we could obtain
  57.     sortArray(arr,numberOfDigits);
  58.     fileToWrite.open("acces.out");
  59.    
  60.     //We write all the numbers, to the output file with the except of the last m numbers
  61.     int numberOfDigitsToBewritten = numberOfDigits - numberOfDigitsToBeRemoved;
  62.     for (int i = 0; i < numberOfDigitsToBewritten; i++) {
  63.         fileToWrite <<  arr[i] << endl;
  64.     }
  65.     fileToWrite.close();
  66.     return 0;
  67. }
  68.  
  69. //This function extracts a number array from a stream.
  70. // When you are seeing int *, it is the same as int[]
  71. // stringstream& is just a type, by using the & symbol, it means that we have to pass the address of the variable also
  72. int * extractNumberArrayFromStream(stringstream& str, int len) {
  73.     // C++ does not allow dynamic allocation of arrays like c
  74.     // In C we could have done directly int numbers[len] but it is not working in some versions of C++
  75.     int * numbers;
  76.     numbers = new (nothrow) int[len];
  77.  
  78.     //Here we save intermediate results read by the function getline.
  79.     string intermediate;
  80.     int i = 0;
  81.     while (getline(str, intermediate, ' ')) {
  82.         //stoi(intermediate) is just a function which knows to convert from "3" to 3, for example.
  83.         //stoi comes from string to integer
  84.         numbers[i++] = stoi(intermediate);
  85.     }
  86.     return numbers;
  87. }
  88.  
  89. // This function simply sorts an array using the bubble sort algorithm
  90. void sortArray (int * numbers, int arrayLength) {
  91.     for (int i = 0 ; i < arrayLength -1 ; i++) {
  92.         for (int j = 0; j < (arrayLength - i - 1) ; j++) {
  93.             int temp;
  94.             if ( numbers[j] <  numbers[j+1]) {
  95.                 temp = numbers[j];
  96.                 numbers[j] = numbers[j+1];
  97.                 numbers[j+1] = temp;
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment