瀏覽代碼

section2-5 completed

tangs 7 年之前
父節點
當前提交
35f61650cc
共有 2 個文件被更改,包括 59 次插入0 次删除
  1. 7 0
      section2-5/CMakeLists.txt
  2. 52 0
      section2-5/main.cpp

+ 7 - 0
section2-5/CMakeLists.txt

@@ -0,0 +1,7 @@
+cmake_minimum_required(VERSION 3.6)
+project(section2_5)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+
+set(SOURCE_FILES main.cpp)
+add_executable(section2_5 ${SOURCE_FILES})

+ 52 - 0
section2-5/main.cpp

@@ -0,0 +1,52 @@
+#include <iostream>
+#include <string>
+#include <iomanip>
+
+using namespace std;
+
+const char Unknow[] = {"Unknown letter grade in input"};
+
+int match(char ch) {
+    switch (ch) {
+        case 'A':
+            return 4;
+        case 'B':
+            return 3;
+        case 'C':
+            return 2;
+        case 'D':
+            return 1;
+        case 'F':
+            return 0;
+    }
+    return -1;
+}
+
+int main() {
+    string input;
+    while (getline(cin, input)) {
+        int length = input.length();
+        char *charInput = (char *) input.data();
+        int score = 0, total = 0, num = 0;
+        bool flag = true;
+        for (int i = 0; i < length; i++) {
+            if (charInput[i] < 'A' || charInput[i] > 'Z') {
+                continue;
+            }
+
+            score = match(charInput[i]);
+            if (score < 0) {
+                flag = false;
+                cout << Unknow << endl;
+                break;
+            }
+
+            total += score;
+            num++;
+        }
+        if (flag) {
+            cout << fixed << setprecision(2) << float(total) / float(num) << endl;
+        }
+    }
+    return 0;
+}