伊莉討論區

標題: 誰可以幫我看看哪裡有錯阿 [打印本頁]

作者: a976378    時間: 2011-12-26 11:17 PM     標題: 誰可以幫我看看哪裡有錯阿

誰可以幫我看看哪裡有錯誤   我用的是C++2005 Express版 我找不到哪裡有錯

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream.h>
  4. #include <time.h>

  5. using namespace std;

  6. int gcd(int a,int b);
  7. int lcm(int a,int b);

  8. int main(int argc, char *argv[])
  9. {
  10.     int a=0;
  11.     int b=0;
  12.     int c=0;
  13.    
  14.     FILE *fp_in,*fp_out;        //宣告檔案指標,fp_in為輸入用,fp_out為輸出用
  15.    
  16.     if((fp_in=fo pen("c:/Input.txt","r"))==NULL) //開啟檔案,"r"代表讀取模式
  17.     {                                               //fo pen()傳為NULL時代表開啟失敗
  18.         cout<<"未找到Input.txt,程式中止.....\n";   //失敗時的處理
  19.         system("PAUSE");
  20.         return EXIT_SUCCESS;
  21.     }
  22.    
  23.     fs canf(fp_in,"%d %d %d",&a,&b,&c); //依整數格式讀入a,b,c,用法跟scanf()很像,前面多檔案指標說明要從哪個檔案讀入
  24.     fc lose(fp_in);                      //關掉檔案
  25.                                                          
  26.     //顯示訊息到營幕上,保心安用           
  27.     cout<<"從Input.txt讀入:\n"<<a<<","<<b<<","<<c<<"\n";  
  28.     cout<<"\n寫出到Output.txt:\n";
  29.     cout<<a<<","<<b<<","<<c<<"之最大公因數為"<<gcd(gcd(a,b),c)<<"\n";
  30.     cout<<a<<","<<b<<","<<c<<"之最小公倍數為"<<lcm(lcm(a,b),c)<<"\n\n";

  31.     if((fp_out=fo pen("c:/Output.txt","w"))==NULL)   //開啟檔案,"w"代表寫入模式 ,如有同名檔案,則清除同名檔案,重新寫入
  32.     {                                                   //開啟失敗時的處理  
  33.         cout<<"寫入檔案Output.txt失敗,程式中止.....\n";
  34.         system("PAUSE");
  35.         return EXIT_SUCCESS;
  36.     }
  37.     fp rintf(fp_out,"%d %d %d 之最大公因數為%d\n",a,b,c,gcd(gcd(a,b),c));//寫出到檔案,用法跟p rintf很像,前面多一個檔案指標說明要輸出到哪個檔案
  38.     fp rintf(fp_out,"%d %d %d 之最小公倍數為%d\n",a,b,c,lcm(lcm(a,b),c));
  39.     fc lose(fp_out);                             //關掉檔案
  40.     system("PAUSE");

  41.     return EXIT_SUCCESS;
  42. }

  43. int gcd(int a,int b) //函數gcd():求最大公因數
  44. {
  45.     int temp;
  46.     while(a%b!=0)
  47.     {
  48.         temp=a%b;
  49.         a=b;

  50.         b=temp;
  51.     }

  52.     return b;
  53. }

  54. int lcm(int a,int b) //函數lcm():求最小公倍數
  55. {
  56.     return (a*b/gcd(a,b)); //a*b=gcd*lcm
  57. }
複製代碼

作者: 刃丑    時間: 2011-12-27 12:26 AM

提示: 作者被禁止或刪除 內容自動屏蔽




歡迎光臨 伊莉討論區 (http://a401.file-static.com/) Powered by Discuz!