Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <list>
  7. #include <iterator>
  8. #include <cstdlib>
  9. #include <math.h>
  10. #include <set>
  11.  
  12. using namespace std;
  13.  
  14. void view()
  15. {
  16. ifstream in("C:\\newfile.txt");
  17. list<string> lines;
  18. while(!in.eof())
  19. {
  20. string temp;
  21. getline(in, temp, '\n');
  22. if(temp != "")
  23. lines.push_back(temp);
  24. }
  25. for (string x : lines)
  26. cout << x << endl;
  27. in.close();
  28. }
  29.  
  30. string sum(string temp)
  31. {
  32. vector<string> tokens;
  33. istringstream iss(temp);
  34. copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));
  35. double sum = 0;
  36. for (int i = tokens.size()-1 ; i >= 4; i--)
  37. {
  38. string a = tokens[i];
  39. sum = sum + atoi(a.c_str());
  40. }
  41. sum /= 4;
  42. temp = temp + " " + to_string(sum);
  43. return temp;
  44. };
  45.  
  46. void add()
  47. {
  48. ifstream in("C:\\newfile.txt");
  49. list<string> lines;
  50. while(!in.eof())
  51. {
  52. string temp;
  53. getline(in, temp, '\n');
  54. if(temp != "")
  55. lines.push_back(temp);
  56. }
  57. string temp;
  58. cout << "Vvedite studenta: " << endl;
  59. cin.get();
  60. getline(cin, temp, '\n');
  61. temp = sum(temp);
  62. lines.push_back(temp);
  63. ofstream out("C:\\newfile.txt");
  64. copy(lines.begin(), lines.end(), ostream_iterator<string> (out, "\n"));
  65. in.close();
  66. out.close();
  67. cout << "dobavlen" << endl;
  68. }
  69.  
  70. void edit()
  71. {
  72. ifstream in("C:\\newfile.txt");
  73. vector<string> lines;
  74. while(!in.eof())
  75. {
  76. string temp;
  77. getline(in, temp, '\n');
  78. if(temp != "")
  79. lines.push_back(temp);
  80. }
  81. cout << "VVedite nomer studenta kotorogo xotite izmenit': ";
  82. int num;
  83. cin >> num;
  84. for (int i = 0; i < lines.size(); i++)
  85. {
  86. if(i == num-1)
  87. {
  88. string temp;
  89. cin.get();
  90. getline(cin, temp);
  91. temp = sum(temp);
  92. lines[i] = temp;
  93. }
  94. }
  95. ofstream out("C:\\newfile.txt");
  96. copy(lines.begin(), lines.end(), ostream_iterator<string> (out, "\n"));
  97. in.close();
  98. out.close();
  99. }
  100.  
  101. void find()
  102. {
  103. ifstream in("C:\\newfile.txt");
  104. vector<string> lines;
  105. while(!in.eof())
  106. {
  107. string temp;
  108. getline(in, temp, '\n');
  109. if(temp != "")
  110. lines.push_back(temp);
  111. }
  112. cout << "VVedite nomer groupi: ";
  113. int group;
  114. cin >> group;
  115. set<string> findResult;
  116. for (int i = 0; i < lines.size(); i++)
  117. {
  118. string temp;
  119. temp = lines[i];
  120. vector<string> tokens;
  121. istringstream iss(temp);
  122. copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));
  123. for (int i = 0; i < tokens.size(); i++)
  124. {
  125. if( tokens[2] == to_string(group) && (tokens[i] == "0"|| tokens[i] == "1"|| tokens[i] == "2"||tokens[i] == "3" ))
  126. {
  127. findResult.insert(temp);
  128. }
  129. }
  130. }
  131. for (string x : findResult)
  132. cout << x << endl;
  133. ofstream out("C:\\out.txt");
  134. copy(findResult.begin(), findResult.end(), ostream_iterator<string> (out, "\n"));
  135. in.close();
  136. out.close();
  137. }
  138.  
  139.  
  140. int main()
  141. {
  142. int a;
  143. cout << "1 - VIEW, 2 - ADD, 3 - EDIT, 4 - FIND" << endl;
  144. cout << "choise: ";
  145.  
  146. while(true)
  147. {
  148. cin >> a;
  149. switch (a)
  150. {
  151. case 1:
  152. view();
  153. break;
  154. case 2:
  155. add();
  156. break;
  157. case 3:
  158. edit();
  159. break;
  160. case 4:
  161. find();
  162. break;
  163. }
  164. }
  165.  
  166. system("pause");
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement