[問題] 印不出需要的結果= =

看板C_and_CPP作者 (fireslayer)時間15年前 (2010/03/14 17:43), 編輯推噓3(3012)
留言15則, 5人參與, 最新討論串1/1
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 ) ( 未必需要依照此格式,文章條理清楚即可 ) 遇到的問題: (題意請描述清楚) 又是基本的質數問題,不過上個版本改進後就印不出來了 希望得到的正確結果: 程式跑出來的錯誤結果: 都是0= = 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Windows Visual Studio 2010 Release Canditate 有問題的code: (請善用置底文標色功能) // 自動生成質數表(2).cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "stdlib.h" #include "math.h" int _tmain(int argc, _TCHAR* argv[]) { long long int number = 2; long long int test = 2; double max; long long int integer = 0; long long int exam = 0; printf("請輸入一個大於1的數,我們將為你輸出此數以下的所有質數到layout.txt: "); scanf_s("%lf", &max); if(max <= 1 || fmod(max,1.00) != 0) printf("你輸的數字與條件不符\n"); else while(max >= number) { while(number % test != 0&&number >= test*test) { test = test + 1; if(number % test == 0) { exam++; break; } } if(exam == 0) { integer++; printf("第%u個質數是%u\n",integer, number); } test = 2; exam = 0; number++; } system("pause"); return 0; } 補充說明: 謝謝大家的指導 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.117.40.80

03/14 17:43, , 1F
原本設計是印出到檔案,為了方便大家起見
03/14 17:43, 1F

03/14 17:44, , 2F
先改成print版本,成功的話會再改成fpaint版本
03/14 17:44, 2F

03/14 17:44, , 3F
謝謝大家
03/14 17:44, 3F

03/14 18:35, , 4F
%I64u || %llu ?
03/14 18:35, 4F

03/14 18:36, , 5F
題外話(number % test != 0)這個條件多餘的吧
03/14 18:36, 5F

03/14 18:40, , 6F
test = test + 1; 應該寫在if block後面不是前面
03/14 18:40, 6F

03/14 18:41, , 7F
設中斷點然後按F10,一步一步看他數值變化哪裡錯了
03/14 18:41, 7F

03/14 19:02, , 8F
喔喔謝謝大家
03/14 19:02, 8F

03/14 20:12, , 9F
fprintf(), 你用stdout取代用fopen拿回來的FILE ptr就可
03/14 20:12, 9F

03/14 20:13, , 10F
以做到印到檔案與印到螢幕的切換了, 不用改code這麼累@@
03/14 20:13, 10F

03/14 20:48, , 11F
不太會用stdout= =
03/14 20:48, 11F

03/14 20:56, , 12F
把fprintf第一個放FILE ptr的地方直接換成 stdout 試試
03/14 20:56, 12F

03/14 20:56, , 13F
你就知道了:)
03/14 20:56, 13F

03/14 21:08, , 14F
fprintf、fscanf都超好用的說, 建議使用這兩個, 因為
03/14 21:08, 14F

03/14 21:09, , 15F
參數型式上跟 sscanf 等很像, 不用背太多
03/14 21:09, 15F
文章代碼(AID): #1BdA-bKN (C_and_CPP)