看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《hansonJu (eric)》之銘言: : C語言 : # include <stdio.h> : # include <stdlib.h> : int main(void) : { : double sum=0; : int grade,highest=0,lowest=100,ID,passed=0,failed=0,id; : FILE * fp = fopen("grade1.txt","rt"); : if(fp==NULL) : printf("File not found\n"); : else : { : while(EOF != fscanf(fp,"%d %d",&ID,&grade)) : { : sum+=grade; : if(grade>=60); : else ; : } : passed++; : failed++; : printf("The average of class:%lg\n",sum/49); : printf("The passed number:%d\n",passed); : printf("The failed number:%d\n",failed); : } : system("pause"); : return(0); : } : 我這樣打 他並不會將成績及格與不及格分出來 我錯在哪? 先想好演算法再下去 code 好嗎? 1. repeat until there are no valid inputs 2. increase the summation of grade by the read grade 3. if the read grade is less than 60 then 4. increase failed count by 1 5. else 6. increase passed count by 1 7. output the average of total grades 8. output passed count 9. output failed count 辨認名詞, 然後定義變數: float summation_of_grade = 0.0f; float grade_be_read; unsigned passed_count = 0u, failed_count = 0u; 接著一步步把虛擬碼變成註解並且在下面補上 code // repeat until there are no valid inputs while( scanf( "%f", &grade_be_read ) == 1 ) { // increase the summation of grade by the read grade // if the read grade is less than 60 then // increase failed count by 1 // else // increase passed count by 1 } // output the average of total grades // output passed count // output failed count 簡單的程式碼先補上: // repeat until there are no valid inputs while( scanf( "%f", &grade_be_read ) == 1 ) { // increase the summation of grade by the read grade summation_of_grade += grade_be_read; // if the read grade is less than 60 then // increase failed count by 1 // else // increase passed count by 1 } // output the average of total grades printf( "%f\n", summation_of_grade /(passed_count + failed_count) ); // output passed count printf( "%u\n", passed_count ); // output failed count printf( "%u\n", failed_count ); 再來是條件判斷的部份: // repeat until there are no valid inputs while( scanf( "%f", &grade_be_read ) == 1 ) { // increase the summation of grade by the read grade summation_of_grade += grade_be_read; // if the read grade is less than 60 then if( grade_be_read < 60.0f ) // increase failed count by 1 failed_count += 1u; // else else // increase passed count by 1 passed_count += 1u; } // output the average of total grades printf( "%f\n", summation_of_grade/(passed_count + failed_count) ); // output passed count printf( "%u\n", passed_count ); // output failed count printf( "%u\n", failed_count ); 這裡會有輸入格式的問題, 需要多讀一個id進來, 接著再看你程式 的敘述性, 如果已經足夠, 那麼就可以把註解給砍掉, 最後的程式 碼: float summation_of_grade = 0.0f; float grade_be_read; unsigned passed_count = 0u, failed_count = 0u; int unused_identifier; // repeat until there are no valid inputs while( scanf( "%d %f", &unused_identifier, &grade_be_read ) == 2 ) { summation_of_grade += grade_be_read; if( grade_be_read < 60.0f ) failed_count += 1u; else passed_count += 1u; } printf( "%f\n", summation_of_grade/(passed_count + failed_count) ); printf( "%u\n", passed_count ); printf( "%u\n", failed_count ); 要多 fashion的輸出就看你的設計了, 重要的是功能先搞出來再說 ! 用標準輸入測試無誤之後, 要改成讀檔也很簡單, 只有兩步驟: 1. 所有scanf改成fscanf, 並且傳入stdin作為第一個參數 2. 開你想要的檔案, 並且把step1的stdin都改成代表該檔 的檔案指標 完成. 太早跳下去寫程式將會很難發現問題所在, 甚至是對語法不 熟悉的新手來說更是如此, 用上述方法「轉譯」程式有下列好處: 1. 清楚的把想法記錄下來, 得以驗證正確性 2. 若分解得當, 寫程式都變得很簡單, 要是有錯誤大部分都是 出在 step 1 運用此法要儘量抽象化但又不可太虛幻導致不好轉譯, 避免導入特 定程式語言才有的敘述, 這樣你的虛擬碼才可以被重複套用在別的 程式上. 更多參考 :《Code Complete》9.3 Write the pseudocode - * C++ and Beyond: Meyers, Sutter, & Alexandrescu, Aug 7-10 in Banff (http://cppandbeyond.com/) - -- ▂▂ ▄▂ T.T.L Listen 2 http://ppt.cc/jIUk ˇ ˇˇ ˇ 說什麼結束 ▃▃ http://ppt.cc/zQtB ψ髮箍 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.121.197.115 ※ 編輯: loveme00835 來自: 140.121.197.115 (05/02 15:14)
tropical72:看 L 大寫碼都很像 看文說故事/看圖說故事 05/02 15:21
xatier:推L大與 <code complete> 05/02 15:28
bill42362:推!! 05/02 15:38