main.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5. const char Unknow[] = {"Unknown letter grade in input"};
  6. int match(char ch) {
  7. switch (ch) {
  8. case 'A':
  9. return 4;
  10. case 'B':
  11. return 3;
  12. case 'C':
  13. return 2;
  14. case 'D':
  15. return 1;
  16. case 'F':
  17. return 0;
  18. }
  19. return -1;
  20. }
  21. int main() {
  22. string input;
  23. while (getline(cin, input)) {
  24. int length = input.length();
  25. char *charInput = (char *) input.data();
  26. int score = 0, total = 0, num = 0;
  27. bool flag = true;
  28. for (int i = 0; i < length; i++) {
  29. if (charInput[i] < 'A' || charInput[i] > 'Z') {
  30. continue;
  31. }
  32. score = match(charInput[i]);
  33. if (score < 0) {
  34. flag = false;
  35. cout << Unknow << endl;
  36. break;
  37. }
  38. total += score;
  39. num++;
  40. }
  41. if (flag) {
  42. cout << fixed << setprecision(2) << float(total) / float(num) << endl;
  43. }
  44. }
  45. return 0;
  46. }