MartisK

C++ uždavinys. Mokymosi laikas

Dec 3rd, 2020
796
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. int dataCount = 0;
  6. int Aval[20], Amin[20], Bval[20], Bmin[20];
  7.  
  8. int ReadFromDataFile()
  9. {
  10.     std::ifstream dataFile("data.txt");
  11.     dataFile >> dataCount;
  12.  
  13.     for (int i = 0; i < dataCount; i++)
  14.     {
  15.         dataFile >> Aval[i] >> Amin[i] >> Bval[i] >> Bmin[i];
  16.     }
  17.     dataFile.close();
  18. }
  19.  
  20. int FindTimeInMinutesFromInterval(int fromHour, int fromMinute, int toHour, int toMinute)
  21. {
  22.     return abs(toHour - fromHour) * 60 + abs(toMinute - fromMinute);
  23. }
  24.  
  25. std::string ConvertTimeToHours(int totalMinutes)
  26. {
  27.     int hours = totalMinutes / 60;
  28.     int minutes = totalMinutes % 60;
  29.  
  30.     return std::to_string(hours) + " val. " + std::to_string(minutes) + " min.";
  31. }
  32.  
  33. int WriteToResultFile(int timeTotal)
  34. {
  35.     std::ofstream resultFile("result.txt");
  36.     resultFile << ConvertTimeToHours(timeTotal);
  37.     resultFile.close();
  38. }
  39.  
  40. int main()
  41. {
  42.     ReadFromDataFile();
  43.  
  44.     int totalTimeInMinutes = 0;
  45.     for (int i = 0; i < dataCount; i++)
  46.     {
  47.         totalTimeInMinutes += FindTimeInMinutesFromInterval(Aval[i], Amin[i], Bval[i], Bmin[i]);
  48.     }
  49.  
  50.     WriteToResultFile(totalTimeInMinutes);
  51. }
  52.  
RAW Paste Data