Facebook
From Nikolay Stoitsov, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 140
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. vector<vector<string>> weatherConditions;
  10.  
  11. const int numberOfPorts = 5;
  12.  
  13. struct SpacePort {
  14.     string name;
  15.     int perfectDay = 0;
  16.     string csvFileName;
  17.     int lengthEquator = 0;
  18.     int lowestTemps = 1;
  19.     int maxTemps = 32;
  20.     int maxWindSpeed = 11;
  21.     int maxHumidity = 55;
  22.     int maxPrecipitation = 0;
  23.     string lightning = "No";
  24.     string cloudsFirst = "Cumulus";
  25.     string cloudsSecond = "Nimbus";
  26. };
  27.  
  28. void CreateFile&#40;vector<SpacePort>& ports&#41; {
  29.     ofstream outputFile&#40;"LaunchAnalysisReport.csv"&#41;;
  30.     if (!outputFile) {
  31.         cout << "Error creating file" << endl;
  32.         return;
  33.     }
  34.     outputFile << "Port name,Date" << endl;
  35.  
  36.     for (int i = 0; i < numberOfPorts; i++) {
  37.         outputFile << ports[i].name << ",";
  38.         if (ports[i].perfectDay == 0) {
  39.             outputFile << "No viable shuffle day" << endl;
  40.         }
  41.         else {
  42.             outputFile << ports[i].perfectDay << endl;
  43.         }
  44.     }
  45.     outputFile.close();
  46. }
  47.  
  48. void GetWeatherConditions(const string fileName) {
  49.     ifstream File&#40;fileName&#41;;
  50.     if (!File.is_open()) {
  51.         cout << "Unable to open file" << endl;
  52.         return;
  53.     }
  54.     string line;
  55.     while (getline(File, line)) {
  56.         stringstream ss(line);
  57.         string word;
  58.         vector<string> row;
  59.         getline(ss, word, ',');
  60.         while (getline(ss, word, ',')) {
  61.             row.push_back(word);
  62.         }
  63.         weatherConditions.push_back(row);
  64.     }
  65.     File.close();
  66. }
  67.  
  68. int GetAppropriateDays(SpacePort port) {
  69.     const size_t numberOfDays = weatherConditions[0].size();
  70.  
  71.     for (size_t i = 0; i < numberOfDays; i++) {
  72.         if (stoi(weatherConditions[1][i]) > port.lowestTemps && stoi(weatherConditions[1][i]) < port.maxTemps &&
  73.             stoi(weatherConditions[2][i]) < port.maxWindSpeed && stoi(weatherConditions[3][i]) < port.maxHumidity &&
  74.             stoi(weatherConditions[4][i]) == port.maxPrecipitation && weatherConditions[5][i] == port.lightning &&
  75.             weatherConditions[6][i] != port.cloudsFirst && weatherConditions[6][i] != port.cloudsSecond) {
  76.             return i + 1;
  77.         }
  78.     }
  79.     return 0;
  80. }
  81.  
  82. void GetBestDay(vector<SpacePort>& ports) {
  83.     int day = 0;
  84.     for (int i = 0; i < numberOfPorts;i++) {
  85.         GetWeatherConditions(ports[i].csvFileName);
  86.         day = GetAppropriateDays(ports[i]);
  87.         ports[i].perfectDay = day;
  88.         weatherConditions.clear();
  89.     }
  90. }
  91.  
  92. void GetPerfectDayAndLocation(vector<SpacePort>& ports) {
  93.     int port = -1;
  94.     int currentLength = 999999;
  95.     for (int i = 0; i < numberOfPorts; i++) {
  96.         if (ports[i].perfectDay != 0 && ports[i].lengthEquator <= currentLength) {
  97.             port = i;
  98.             currentLength = ports[i].lengthEquator;
  99.         }
  100.     }
  101.     if (port != -1) {
  102.         cout << "Perfect location and day:" << endl;
  103.         cout << ports[port].name << "  " << ports[port].perfectDay;
  104.     }
  105.     else {
  106.         cout << "There are no appropriate days";
  107.     }
  108. }
  109.  
  110.  
  111.  
  112. int main() {
  113.     string portNames[numberOfPorts] = { "Kourou", "Tenegashima", "Cape Canaveral", "Mahia", "Kodiak" };
  114.     int arr[numberOfPorts] = { 333, 1934, 1807, 2500, 4112 };
  115.     vector<SpacePort> spacePorts(numberOfPorts);
  116.  
  117.     cout << "Enter file path" << endl;
  118.     string fileName;
  119.     getline(cin, fileName);
  120.  
  121.     string choice;
  122.     cout << "Do you want to use default weather criteria? (Yes/No): ";
  123.     cin >> choice;
  124.  
  125.     SpacePort port;
  126.     for (int i = 0; i < numberOfPorts; i++) {
  127.         port.name = portNames[i];
  128.         port.csvFileName = fileName + "\\" + portNames[i] + ".csv";
  129.         port.lengthEquator = arr[i];
  130.         spacePorts[i] = port;
  131.     }
  132.  
  133.     if (choice == "No") {
  134.         int numberOfPortsToChange;
  135.         cout << "How many port's criteria do you want to change?" << endl;
  136.         cin >> numberOfPortsToChange;
  137.  
  138.         if (numberOfPortsToChange > numberOfPorts) {
  139.             cout << "Invalid input" << endl;
  140.         }
  141.         else {
  142.             string portName;
  143.             bool isValid = false;
  144.             for (int j = 0; j < numberOfPortsToChange; j++) {
  145.                 cout << "Enter the name of the port to change criteria: ";
  146.                 cin >> portName;
  147.  
  148.                 for (int i = 0; i < numberOfPorts; i++) {
  149.                     if (portName == portNames[i]) {
  150.                         cout << "Enter lowest temperature: ";
  151.                         cin >> spacePorts[i].lowestTemps;
  152.  
  153.                         cout << "Enter maximum temperature: ";
  154.                         cin >> spacePorts[i].maxTemps;
  155.  
  156.                         cout << "Enter maximum wind speed: ";
  157.                         cin >> spacePorts[i].maxWindSpeed;
  158.  
  159.                         cout << "Enter maximum humidity: ";
  160.                         cin >> spacePorts[i].maxHumidity;
  161.  
  162.                         cout << "Enter maximum precipitation: ";
  163.                         cin >> spacePorts[i].maxPrecipitation;
  164.  
  165.                         cout << "Enter lightning (Yes/No): ";
  166.                         cin >> spacePorts[i].lightning;
  167.  
  168.                         cout << "Enter first cloud condition: ";
  169.                         cin >> spacePorts[i].cloudsFirst;
  170.  
  171.                         cout << "Enter second cloud condition: ";
  172.                         cin >> spacePorts[i].cloudsSecond;
  173.  
  174.                         isValid = true;
  175.                         break;
  176.                     }
  177.                 }
  178.                 if (!isValid) {
  179.                     cout << "Invalid data" << endl;
  180.                 }
  181.             }
  182.         }
  183.     }
  184.  
  185.     GetBestDay(spacePorts);
  186.     CreateFile&#40;spacePorts&#41;;
  187.     GetPerfectDayAndLocation(spacePorts);
  188.  
  189.     return 0;
  190. }
  191.