Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <random>
  6. #include <sstream>
  7. #include <random>
  8. #include <string>
  9. #include <fstream>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. struct studentas {
  16. string firstName;
  17. string secondName;
  18.  
  19. vector<double> nd;
  20.  
  21. double visiNd = 0;
  22. double egz = 0;
  23.  
  24.  
  25. void readFile (int &index, vector<studentas> &s);
  26.  
  27. //Storing information
  28.  
  29. void data() {
  30. cout << "Studento vardas: ";
  31. cin >> firstName;
  32. cout << "\n";
  33. cout << "Studento pavarde: ";
  34. cin >> secondName;
  35. cout << "\n";
  36.  
  37. cout << "Ar noresite sugeneruoti random pazymius ir egzamino balus? Random - spauskite 1. Ivesties budu - spauskite 0: \n";
  38. int ivestiesRadom;
  39. cin >> ivestiesRadom;
  40.  
  41. while (cin.fail()) {
  42. cout << "Blogas irasymas, bandykite dar karta: \n";
  43. cin.clear();
  44. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  45. cin >> ivestiesRadom;
  46. }
  47.  
  48. if (b == 1) {
  49. randomNumbers();
  50. }
  51.  
  52. else {
  53. numberInput();
  54. cout << "Iveskite studento egzamino rezultata: \n";
  55. cin >> egz;
  56. }
  57. }
  58.  
  59. //Generuoja random skaicius
  60.  
  61. void randomNumbers() {
  62. cout << "Iveskite kiek norite sugeneruoti skaiciu!: \n";
  63. cin >> visiNd;
  64.  
  65. while (cin.fail()){
  66. cout << "Blogas irasymas, bandykite dar karta: \n";
  67. cin.clear();
  68. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  69. cin >> visiNd;
  70.  
  71. }
  72.  
  73. nd.reserve(visiNd);
  74.  
  75. const unsigned int seed = time(0);
  76. mt19937_64 rng(seed);
  77. uniform_real_distribution<double> random(1, 10);
  78.  
  79. for (int i = 0; i<visiNd; i++) {
  80. nd.push_back(static_cast<int>(random(rng)));
  81.  
  82. }
  83.  
  84. egz = static_cast<int>(random(rng));
  85. }
  86.  
  87. //Rankiniu budu turi suvest nd ir egz
  88.  
  89. void numberInput() {
  90. cout << "Iveskit namu darbu pazymius, norint pabaigti, iveskite ne skaiciu! \n";
  91.  
  92. int skc;
  93.  
  94. while (cin >> skc) {
  95. if (!(skc >= 1 && skc <= 10)){
  96. cout << "Pazymys turi buti tarp 1 ir 10! \n";
  97. continue;
  98. }
  99. nd.push_back(skc);
  100. }
  101.  
  102. visiNd = nd.size();
  103.  
  104. cin.clear();
  105. cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
  106. }
  107.  
  108. //Vidutinio skaiciavimas
  109.  
  110. double vid () {
  111. double vidutinis = 0;
  112.  
  113. for (auto &viens : nd) {
  114. vidutinis += viens;
  115. }
  116.  
  117. vidutinis /= nd.size();
  118.  
  119. return vidutinis;
  120. }
  121.  
  122. //Medianos skaiciavimas
  123.  
  124. double median (vector<double>set) {
  125. sort(set.begin(), set.end());
  126. return (set.size()% 2 == 0 ? (set[set.size()/2] + set[(set.size() / 2 ) - 1]) / 2 : set[set.size() / 2]);
  127. }
  128.  
  129. double galutinioM() {
  130. return 0.4 * median(nd) + 0.6 * egz;
  131. }
  132.  
  133. double galutinioV() {
  134. return 0.4 * vid() + 0.6 * egz;
  135. }
  136. };
  137.  
  138. void display(vector<studentas> s, int index);
  139.  
  140. void readFile (int &index, vector<studentas> &s);
  141.  
  142. int main()
  143. {
  144. vector<studentas> s;
  145. cout << "Ar norite nuskaityti studentus is failo, ar ivesti pats? 1 - jei pats, 0 - jei is failo\n";
  146.  
  147. int pasirinkimas;
  148. cin >> pasirinkimas;
  149.  
  150. int index = 0;
  151.  
  152. if (pasirinkimas == 0) {
  153. readFile(index, s);
  154. } else if (pasirinkimas == 1) {
  155. cout << "Kiek noresite ivesti studentu ?" << endl;
  156. int stud;
  157. cin >> stud;
  158. s.resize(stud);
  159. while(cin.fail()) {
  160. cin.clear();
  161. cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
  162. cout << "Neteisingai ivedete, veskite dar karta! ";
  163. cin >> stud;
  164. }
  165.  
  166. index = stud;
  167.  
  168. cout << "\n";
  169.  
  170. for (int i = 0; i < stud; i++) {
  171.  
  172. s[i].data();
  173. }
  174. }
  175.  
  176. display(s, index);
  177.  
  178. return 0;
  179. }
  180.  
  181. //skaito is failo
  182.  
  183. void readFile (int &index, vector<studentas> &s) {
  184.  
  185. string line;
  186.  
  187. int ndVisu;
  188.  
  189. ifstream file_("studentai.txt");
  190.  
  191. getline(file_, line); //pirmas
  192.  
  193. while (getline(file_, line)) {
  194. istringstream pirmi(line);
  195. s.push_back(studentas());
  196. pirmi >> s[index].firstName;
  197. pirmi >> s[index].secondName;
  198.  
  199. while (pirmi) {
  200. pirmi >> ndVisu;
  201. s[index].nd.push_back(ndVisu);
  202. }
  203.  
  204. s[index].egz = s[index].nd.back();
  205. s[index].nd.pop_back();
  206. index++;
  207. }
  208.  
  209. }
  210.  
  211. //Sortina studentus
  212.  
  213. /*void srt(int index)
  214. {
  215. studentas s[60];
  216.  
  217.  
  218. sort(s, s + index);
  219. [](studentas a, studentas b)-> bool{return (a.firstName<b.firstName);}
  220. }
  221. */
  222.  
  223. void display(vector<studentas> s, int index)
  224. {
  225. //Displaying information
  226.  
  227. cout << "Jusu pateikta informacija: \n";
  228.  
  229. cout << "Pavarde Vardas Galutinis(Vid.) Galutinis(Med.) \n";
  230.  
  231. cout <<"-------------------------------------------------------------------------------------";
  232.  
  233.  
  234. for (int i = 0; i < index; ++i) {
  235. cout << s[i].firstName << " " << s[i].secondName << fixed<< setprecision(2) <<" " << s[i].galutinioV() <<" "<<s[i].galutinioM()<<"\n";
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement