Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.08 KB | None | 0 0
  1. /*
  2. COPYRIGHT (C) 2019 Savannah Rimmele (smr165) All rights reserved.
  3. CSI Assignment 5
  4. Author. Savannah Rimmele
  5. smr165@zips.uakron.edu
  6. Version. 1.01 07.09.2017
  7. Purpose: This program is to express the use of basic input, output, flow of control, calculation, iteration, and uses functions.
  8. Using these it will calculate a grade for a specified amount of exercises within a range of points, any inputs will be validated.
  9. */
  10. /*
  11. Functional Decomposition:
  12. Level 1:
  13. 1. Determine the subject
  14. 2. Calculate the amount of exercises
  15. 3. Calculate the score on an exercise
  16. 4. Calculate the amount of possible points on an exercise
  17. 5. Calculate the actual total points scored
  18. 6. Calculate the actual amount of total points possible
  19. 7. Calculate the grade percent
  20. 8. Determine the letter grade
  21. 9. Get various entries
  22. 10. Display
  23.  
  24. Level 2:
  25. 1. Determine the subject
  26. 1.1 Get the subject entry
  27. 1.2 Validate subject entries
  28. 2. Calculate the amount of exercises
  29. 2.1 Get the exercise entries
  30. 2.2 Validate the amount of exercises
  31. 3. Calculate the score on an exercise
  32. 3.1 Get the score entries
  33. 3.2 Validate these entries
  34. 4. Calculate the amount of possible points on an exercise
  35. 4.1 Get the entries for possible points
  36. 4.2 Validate these entries
  37. 5. Calculate the actual total points scored
  38. 5.1 Set total points scored to the addition of the amount of points scored for each exercise
  39. 6. Calculate the actual amount of total points possible
  40. 6.1 Set total points possible to the addition of the amount of points possible for each exercise
  41. 7. Calculate the grade percent
  42. 7.1 Set grade percent to the total score received divided by the total points possible
  43. multiplied by 100.
  44. 8. Determine the letter grade
  45. 8.1 Set A within the range of 93 and 100
  46. 8.2 Set A- within the range of 90 and 92.99
  47. 8.3 Set B+ within the range of 97 and 89.99
  48. 8.4 Set B within the range of 83 and 86.99
  49. 8.5 Set B- within the range of 80 to 82.99
  50. 8.6 Set C+ within the range of 77 to 79.99
  51. 8.7 Set C within the range of 73 to 76.99
  52. 8.8 Set C- within the range of 70 to 72.99
  53. 8.9 Set D+ within the range of 67 to 69.99
  54. 8.10 Set D within the range of 63 to 66.99
  55. 8.11 Set D- within the range of 60 to 62.99
  56. 8.12 Set F to anything below 60.
  57. 9. Get various entries
  58. 9.1 Continue adding entries
  59. 9.2 Each entry will calculate a different percent grade
  60. 10. Display
  61. 10.1 Print the subject choices menu
  62. 10.2 Print the amount of exercises
  63. 10.3 Print the amount of scored points on an exercise
  64. 10.4 Print the amount of possible points for an exercise
  65. 10.5 Print the score received out of the total possible points
  66. 10.6 Print the percent grade
  67. 10.7 Print the letter grade
  68. */
  69.  
  70. #include <iostream>
  71. #include <iomanip>
  72. #include <limits>
  73. #include <cstdlib> //for rand and srand
  74. #include <ctime>
  75. #include <fstream>
  76. #include <sstream>
  77. #include <cstring>
  78. using std::cout;
  79. using std::cin;
  80. using std::endl;
  81. using std::string;
  82. using std::ofstream;
  83. using std::ifstream;
  84. using std::stringstream;
  85.  
  86. bool validateInput(int, int, int, string & ); //Control for validation
  87. int getSubject();
  88. int getExercise();
  89. int getExerciseReceived(int, int);
  90. int getExerciseTotal(int);
  91. void printPercentGrade(double &, double &);
  92. void getGrades(int, double &, double &, double &, double &, double &, double &, double &, double &, double & , double &, double &, double &);
  93. void printSubjectGrades( double &, double &, double &, double &, double &, double &, double &, double &, double &, double &);
  94. string subjectToString(int);
  95. int getArraySize(int []);
  96. void bubbleSort(int [], int);
  97. double mean(int [], int);
  98. int mode(int [], int);
  99. int median(int [], int);
  100. void getArr();
  101. void password();
  102. int getArrSize(string &);
  103.  
  104. int main()
  105. {
  106. password();
  107. ofstream oStream;
  108. oStream.open( "Subject.txt", std::ios::app);
  109. double earnedBiology = 0, totalBiology =0, earnedSociology = 0, totalSociology = 0,
  110. earnedGeography = 0, totalGeography = 0, earnedCalculus = 0, totalCalculus = 0,
  111. earnedPhysics = 0, totalPhysics = 0;
  112. while(true)
  113. {
  114. int subject = getSubject(); //Get the user's choice of subject
  115. if (subject == 6)
  116. {
  117. printSubjectGrades(earnedBiology, totalBiology, earnedSociology, totalSociology, earnedCalculus, totalCalculus, earnedGeography, totalGeography, earnedPhysics, totalPhysics);
  118. oStream.close(); //file closes when quit is chosen
  119. getArr();
  120. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  121. password();
  122. return 0;
  123. }
  124.  
  125. oStream << subjectToString(subject); //displays subject string inside of file
  126. int numberExercises = getExercise(); //between 1 and 10
  127. int countup = 0;
  128. double scoreReceived = 0;
  129. double totalPointsPossible = 0;
  130. while (countup < numberExercises)
  131. {
  132. int exerciseTotal = getExerciseTotal(countup +1);
  133. int exerciseReceived = getExerciseReceived(countup +1, exerciseTotal);
  134. oStream << exerciseReceived << " " << exerciseTotal << " ";
  135. cout << "\n\n";
  136. scoreReceived = exerciseReceived + scoreReceived;
  137. totalPointsPossible = totalPointsPossible + exerciseTotal;
  138. countup ++;
  139.  
  140. }
  141. oStream << "\n";
  142. getGrades(subject, scoreReceived, totalPointsPossible, earnedBiology, totalBiology, earnedSociology, totalSociology, earnedCalculus, totalCalculus, earnedGeography, totalGeography, earnedPhysics, totalPhysics);
  143. printPercentGrade(scoreReceived, totalPointsPossible);
  144. }
  145. }
  146. //precondition: validates the input
  147. // postcondition: returns true or displays the invalid message.
  148. bool validateInput(int subject, int num1, int num2, string & message)
  149. {
  150. if (subject >= num1 && subject <= num2)
  151. {
  152. return true;
  153. }
  154. else
  155. {
  156. cout << message << endl;
  157. return false;
  158. }
  159. }
  160.  
  161. int getSubject()
  162. // Menu to choose which subject the exercises were for
  163. //precondition: call when subject menu is to be displayed
  164. // postcondition: return an int for the user's choice
  165. {
  166. int subject;
  167. bool controlFlag = true;
  168. do
  169. {
  170. string msg("The valid choices are 1 through 6."); //Displays when an invalid integer is given
  171. cout << "Please choose a subject from the menu: \n"
  172. << "1. Biology \n"
  173. << "2. Sociology \n"
  174. << "3. Geography \n"
  175. << "4. Calculus \n"
  176. << "5. Physics \n"
  177. << "6. Quit \n\n";
  178. cout << "Your choice: \n";
  179. cin >> subject;
  180. controlFlag = !validateInput(subject, 1, 6, msg);
  181. }
  182. while (controlFlag);
  183. return subject;
  184. }
  185.  
  186. int getExercise()
  187. //precondition: Use to generate a random integer
  188. // postcondition: Displays a random integer between 1 and 10.
  189. {
  190. int numberExercises;
  191. srand(time(0));
  192. numberExercises = 1 + rand() % 10;
  193. cout << "How many exercises to input? " << numberExercises;
  194. cout << "\n\n";
  195. return numberExercises;
  196. }
  197.  
  198. int getExerciseReceived(int exerciseNum, int exerciseTotal)
  199. // precondition: determines the score received for each exercise.
  200. // postcondition: Displays the input given by the user.
  201. // Validates the user's input between a specified range
  202. {
  203. bool controlFlag = true;
  204. int exerciseReceived;
  205. do
  206. {
  207. string msg("The valid choices are 1 through 100 and must be within the total points possible.");
  208. cout << "Score Received for exercise " << exerciseNum << ": " ;
  209. cin >> exerciseReceived;
  210. if (cin.fail() || exerciseReceived < 1 || exerciseReceived > exerciseTotal)
  211. {
  212. cout << msg << endl;
  213. cin.clear();
  214. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  215. }
  216. else
  217. {
  218. controlFlag = false;
  219. }
  220. }
  221. while (controlFlag);
  222. return exerciseReceived;
  223. }
  224.  
  225. int getExerciseTotal(int exerciseNum)
  226. // precondition: Determines the amount amount of total points possible for each exercise
  227. // postcondition: Displays the input given by user
  228. // Validates the input between the specified range
  229. {
  230. bool controlFlag = true;
  231. int exerciseTotal;
  232. do
  233. {
  234. string msg("The valid choices are 1 through 100.");
  235. cout << "Total points possible for exercise " << exerciseNum << ": " ;
  236. cin >> exerciseTotal;
  237. if(cin.fail() || exerciseTotal < 1 || exerciseTotal > 100)
  238. {
  239. cout << msg << endl;
  240. cin.clear();
  241. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  242. }
  243. else
  244. {
  245. controlFlag = false;
  246. }
  247. }
  248. while (controlFlag);
  249. return exerciseTotal;
  250. }
  251.  
  252. void printPercentGrade(double & scoreReceived, double & totalPointsPossible)
  253. //precondition: Calculates the percent grade and determines the matching letter grade
  254. // postcondition: Displays the grade percentage
  255. // Uses percentage to determine letter grade
  256. // Displays the Letter Grade
  257. {
  258. double percentGrade = 0;
  259. percentGrade = totalPointsPossible ? ((scoreReceived / totalPointsPossible) * 100) : 100;
  260. cout.precision(4);
  261. cout << "Your total is " << scoreReceived << " out of " << totalPointsPossible << ", or " << percentGrade << "%.\n";
  262. if (percentGrade >= 93)
  263. {
  264. cout << "Your letter grade is A.";
  265. }
  266. else if (percentGrade >= 90 && percentGrade <= 92.99)
  267. {
  268. cout << "Your letter grade is A-.";
  269. }
  270. else if (percentGrade >= 87 && percentGrade <= 89.99)
  271. {
  272. cout << "Your letter grade is B+.";
  273. }
  274. else if (percentGrade >= 83 && percentGrade <= 86.99)
  275. {
  276. cout << "Your letter grade is B.";
  277. }
  278. else if (percentGrade >= 80 && percentGrade <= 82.99)
  279. {
  280. cout << "Your letter grade is B-.";
  281. }
  282. else if (percentGrade >= 77 && percentGrade <= 79.99)
  283. {
  284. cout << "Your letter grade is C+.";
  285. }
  286. else if (percentGrade >= 73 && percentGrade <= 76.99)
  287. {
  288. cout << "Your letter grade is C.";
  289. }
  290. else if (percentGrade >= 70 && percentGrade <= 72.99)
  291. {
  292. cout << "Your letter grade is C-.";
  293. }
  294. else if (percentGrade >= 67 && percentGrade <= 69.99)
  295. {
  296. cout << "Your letter grade is D+.";
  297. }
  298. else if (percentGrade >= 63 && percentGrade <= 66.99)
  299. {
  300. cout << "Your letter grade is D.";
  301. }
  302. else if (percentGrade >= 60 && percentGrade <= 62.99)
  303. {
  304. cout << "Your letter grade is D-.";
  305. }
  306. else
  307. {
  308. cout << "Your letter grade is F.";
  309. }
  310. cout << endl << endl;
  311. }
  312.  
  313. void getGrades(int subject, double & scoreReceived, double & totalPointsPossible, double & earnedBiology, double & totalBiology, double & earnedSociology, double & totalSociology, double & earnedCalculus, double & totalCalculus, double & earnedGeography, double & totalGeography, double & earnedPhysics, double & totalPhysics )
  314. // precondition: Calculates the total sum for each subject
  315. // postcondition: Displays the sum for each subject
  316.  
  317. {
  318. switch(subject)
  319. {
  320. case 1:
  321. earnedBiology += scoreReceived;
  322. totalBiology += totalPointsPossible;
  323. break;
  324. case 2:
  325. earnedSociology += scoreReceived;
  326. totalSociology += totalPointsPossible;
  327. break;
  328. case 3:
  329. earnedGeography += scoreReceived;
  330. totalGeography += totalPointsPossible;
  331. break;
  332. case 4:
  333. earnedCalculus += scoreReceived;
  334. totalCalculus += totalPointsPossible;
  335. break;
  336. case 5:
  337. earnedPhysics += scoreReceived;
  338. totalPhysics += totalPointsPossible;
  339. break;
  340. default:
  341. cout << "Something broke.";
  342. }
  343. }
  344.  
  345. void printSubjectGrades( double & earnedBiology, double & totalBiology, double & earnedSociology, double & totalSociology, double & earnedCalculus, double & totalCalculus, double & earnedGeography, double & totalGeography, double & earnedPhysics, double & totalPhysics)
  346. // precondition: Calculates the percent grade for each subject and displays the matching letter grade
  347. // postcondition: Displays each subject
  348. // Displays the percent grade
  349. // Displays the letter grade
  350. {
  351.  
  352. cout << "For Biology: \n";
  353. printPercentGrade(earnedBiology, totalBiology);
  354.  
  355. cout << "For Sociology: \n";
  356. printPercentGrade(earnedSociology, totalSociology);
  357.  
  358. cout << "For Geography: \n";
  359. printPercentGrade(earnedGeography, totalGeography);
  360.  
  361. cout << "For Calculus: \n";
  362. printPercentGrade(earnedCalculus, totalCalculus);
  363.  
  364. cout << "For Physics: \n";
  365. printPercentGrade(earnedPhysics, totalPhysics);
  366. }
  367.  
  368. string subjectToString(int subject)
  369. //precondition: Changes the numerical values of the subjects to the specific string
  370. //postcondition: Displays the subjects as a string rather than a value
  371. {
  372. switch(subject)
  373. {
  374. case 1: return "Biology ";
  375. case 2: return "Sociology ";
  376. case 3: return "Geography ";
  377. case 4: return "Calculus ";
  378. case 5: return "Physics ";
  379. }
  380. return "";
  381. }
  382.  
  383. int getArraySize(int arr[])
  384. //precondition: Calculates the size of the array when it is not zero.
  385. //postcondition: displays the array with determined size, without displaying zeros where points were not entered.
  386. {
  387. int size = 0;
  388. while(arr[size] != 0)
  389. {
  390. ++size;
  391. }
  392. return size;
  393. }
  394.  
  395. void bubbleSort(int arr[], int size)
  396. // precondition: Sorts the array numerically by comparing adjacent integers
  397. // postcondition: Displays the array in order, numerically.
  398. {
  399. bool swap;
  400. int temp;
  401. do
  402. {
  403. swap = false;
  404. for(int count = 0; count < (size - 2); count += 2)
  405. {
  406. if(arr[count] > arr[count + 2])
  407. {
  408. temp = arr[count];
  409. arr[count] = arr[count + 2];
  410. arr[count + 2] = temp;
  411. temp = arr[count + 1];
  412. arr[count + 1] = arr[count + 3];
  413. arr[count + 3] = temp;
  414. swap = true;
  415. }
  416. }
  417. }
  418. while (swap);
  419. }
  420.  
  421. double mean(int arr[], int size)
  422. // precondition: Calculates the average for a specific subject
  423. // Adds up all of the integers and divides by the size of the array
  424. // postcondition: Displays the mean of each subjects
  425. {
  426.  
  427. if(size <= 1)
  428. {
  429. return 0.0;
  430. }
  431.  
  432. int total = 0;
  433. for(int i = 0; i < size; i += 2)
  434. {
  435. total += arr[i];
  436. }
  437. return (double)total/(size / 2);
  438. }
  439.  
  440. int median(int arr[], int size)
  441. //precondition: Uses the size of the sorted array to determine the center of the array, then takes the floor of that.
  442. //postcondition: Displays the average of each subject
  443. {
  444. if(size <= 1)
  445. {
  446. return 0;
  447. }
  448.  
  449. return arr[(size / 2) - 1];
  450. }
  451.  
  452. int mode(int arr[], int size)
  453. //precondition: Calculates which number is present the most in the array
  454. //postcondition: Displays the mode of each subject
  455. {
  456. if(size <= 1)
  457. {
  458. return 0;
  459. }
  460.  
  461. int mode = arr[0], modeCount = 1, curCount = 1;
  462. for(int i = 2; i < size; i += 2)
  463. {
  464. if(arr[i] == arr[i - 2])
  465. {
  466. ++curCount;
  467. if(curCount > modeCount)
  468. {
  469. modeCount = curCount;
  470. mode = arr[i];
  471. }
  472. }
  473. else
  474. {
  475. curCount = 1;
  476. }
  477. }
  478. return mode;
  479. }
  480.  
  481. void getArr()
  482. //precondition: opened file numbers are put into an array
  483. //postcondition: Array is sorted in ascending order
  484. // The mean, median, and mode is displayed
  485. {
  486. ifstream iStream;
  487. int *subjectGrades[5];
  488.  
  489. for (int i = 1; i < 6; i++)
  490. {
  491. iStream.open("Subject.txt");
  492. string subFile, scores;
  493. string subjectStr = subjectToString(i);
  494. int arrSize = getArrSize(subjectStr);
  495. subjectGrades[i - 1] = new int [arrSize];
  496. int count = 0;
  497.  
  498. while(!getline(iStream, subFile, ' ').eof())
  499. {
  500. getline(iStream, scores, '\n');
  501. subFile += ' ';
  502. if(subFile == subjectStr)
  503. {
  504. stringstream scoreStream;
  505. scoreStream << scores;
  506. while(!scoreStream.eof() && count < 25)
  507. {
  508. scoreStream >> subjectGrades[i - 1][count++];
  509. }
  510. }
  511. else
  512. {
  513. iStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  514. }
  515. }
  516. iStream.clear();
  517. iStream.close();
  518. bubbleSort(subjectGrades[i - 1], arrSize);
  519.  
  520. cout << subjectStr << " mean is " << mean(subjectGrades[i - 1], arrSize) << ", the mode is "
  521. << mode(subjectGrades[i - 1], arrSize) << ", the median is " << median(subjectGrades[i -1], arrSize) << "\n\n";
  522.  
  523. }
  524. }
  525.  
  526. void password()
  527. //precondition: Prompts user for a password
  528. //postcondition: Password is encrypted
  529. // Password at the end must be the same as the beginning
  530. {
  531. const int KEY = 256;
  532. static char pass[KEY] = {'\0'};
  533. char inputPass[KEY];
  534. cout << "Enter the password. \n";
  535.  
  536. cin.getline(inputPass, KEY - 1, '\n');
  537. for(int i = 0; i < (KEY - 1); ++i)
  538. {
  539. if(inputPass[i] + KEY > 256)
  540. {
  541. inputPass[i] = 32 + ((inputPass[i] + KEY) - 127);
  542. }
  543. else
  544. {
  545. inputPass[i] = (inputPass[i] + KEY);
  546. }
  547. }
  548. if(pass[0] == '\0')
  549. {
  550. strcpy(pass, inputPass);
  551. }
  552. else
  553. {
  554. if(!strcmp(inputPass, pass))
  555. cout << "Password correct. \n";
  556. else
  557. cout << "Incorrect Password. \n";
  558.  
  559. }
  560. }
  561.  
  562. int getArrSize(string & subject)
  563. {
  564. ifstream is("Subject.txt");
  565. string currentLine;
  566. int size = 0;
  567. while(!getline(is, currentLine, '\n').eof() && size < 25)
  568. {
  569. if(subject == currentLine.substr(0, currentLine.find(' ') + 1))
  570. {
  571. int spaceIndex = currentLine.find(' ');
  572. string grades = currentLine.substr(spaceIndex);
  573. stringstream currentLineStream(currentLine);
  574. string dummy;
  575. while(!getline(currentLineStream, dummy, ' ').eof() && size < 25)
  576. {
  577. ++size;
  578. }
  579. --size;
  580. }
  581. is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  582. }
  583. return size;
  584. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement