Advertisement
DasShelmer

9.s.1 arrays

Dec 5th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7. int find(string* arr, int lenght, string name) {
  8.     for (int i = 0; i < lenght; i++)
  9.         if (arr[i] == name)
  10.             return i; // &arr[i]
  11.     return -1;
  12. }
  13.  
  14. int main() {
  15.     ifstream in ("f1.txt");
  16.  
  17.     string* names = NULL;
  18.     int* points = NULL;
  19.     string temp;
  20.     int pCount = 0, operations = 0;
  21.  
  22.     getline(in, temp);
  23.     pCount = stoi(temp);
  24.     names = new string[pCount];
  25.     points = new int[pCount];
  26.     for (int i = 0; i < pCount; i++) {
  27.         getline(in, names[i]);
  28.         points[i] = 0;
  29.     }
  30.  
  31.     getline(in, temp);
  32.     operations = stoi(temp);
  33.     int from, to; int transfer;
  34.     char c;
  35.     for (int i = 0; i < operations; i++) {
  36.         in >> temp;
  37.         from = find(names, pCount, temp);
  38.         in >> temp;
  39.         to = find(names, pCount, temp);
  40.         in >> temp;
  41.         if (from == -1 || to == -1 || temp == "")
  42.             continue;
  43.         transfer = stoi(temp);
  44.  
  45.         points[from] -= transfer;
  46.         points[to] += transfer;
  47.     }
  48.  
  49.     in.close();
  50.     ofstream out("f2.txt");
  51.  
  52.     for (int i = 0; i < pCount; i++)
  53.         out << names[i] << " " << points[i] << endl;
  54.  
  55.     out.close();
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement