看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《mayday24 (青春之後 認輸之前)》之銘言: : 開發平台(Platform): (Ex: VC++, GCC, Linux, ...) : C++ : 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) : 無 : 問題(Question): : https://uva.onlinejudge.org/external/102/10295.pdf : 餵入的資料(Input): : sample input : 預期的正確結果(Expected Output): : 和sample output相同 : 錯誤結果(Wrong Output): : uva wrong answer : 程式碼(Code):(請善用置底文網頁, 記得排版) : http://ideone.com/abeRBD : 補充說明(Supplement): 老實說, 直接貼一串完全沒有註解的程式, 很難看出哪段是要做什麼 好歹像下面的程式一樣, 寫標註一下那一段或是接下來是在做什麼事情吧 #include <iostream> #include <fstream> #include <string> int main(int argc, char* argv[]) { // 確認執行輸入檔的參數 if(argc != 2) { std::cerr << "Require one input file." << std::endl; return 1; } // 開啟輸入檔 std::ifstream input(argv[1]); if(!input.is_open()) { std::cerr << "Error: cannot oopen file: " << argv[1] << std::endl; return 1; } int dict_length = 0; // 規格裡的 m int description_count = 0; // 規格裡的 n input >> dict_length; // 讀入 m input >> description_count; // 讀入 n std::string dict[dict_length]; // 用來存 dictionary的words int value[dict_length]; // 用來存對應的value // 讀入word跟value for(size_t i=0; i < dict_length; i++) { input >> dict[i]; input >> value[i]; } // 用來存讀入的job descriptions std::string descriptions[description_count]; // 讀入並處理job descriptions for(size_t i=0; i < description_count; i++) { std::string tmp; while(getline(input, tmp)) { if(tmp[0] != '.') { descriptions[i].append(tmp.append(" ")); } else { break; } } } // 檔案沒有要再用了, 關閉它 input.close(); // 用來存salary int salary[description_count]; // 處理/判斷salary for(size_t i=0; i<description_count; i++) { salary[i] = 0; std::string process_desc = descriptions[i]; int word_pos = process_desc.find(" "); while(word_pos >= 0) { // 切開job descriptions裡的每個word std::string check_string = process_desc.substr(0, word_pos); process_desc = process_desc.substr(word_pos+1, process_desc.size()); // 比對有沒有在dictionary裡, 有就加上value for(size_t j=0; j < dict_length; j++) { if(check_string.compare(dict[j]) == 0) { salary[i] += value[j]; break; } } // 找下一個word word_pos = process_desc.find(" "); } } // output for(size_t i = 0; i < description_count; i++) { std::cout << salary[i] << std::endl; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.244.41.5 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1442153735.A.BF4.html ※ 編輯: yichen (60.244.41.5), 09/13/2015 22:40:32
adrianshum: imho, 有一半以上的註解是多餘的... 很多時候可以把 09/13 22:45
adrianshum: code 寫得self-descriptive,就可以避免大量無謂註解 09/13 22:46
fatrabitree: 英文註解 is better 然後同意樓上 09/13 22:51
yichen: 我完全同意, 但是要寫在版上問人, 最好還是註解一下某部份 09/13 22:52
yichen: 的意圖, 或是自己本身的想法, 這樣別人比較好幫忙debug啦 09/13 22:54
mayday24: 抱歉 有重新加上一些註解了 09/13 23:00