顯示具有 C語言系列 標籤的文章。 顯示所有文章
顯示具有 C語言系列 標籤的文章。 顯示所有文章

c語言常用IO參數

%d %i 十進位整數
%u unsigned 十進位整數
%x unsigned 16進位整數,小寫表示(a-f)
%X unsigned 16進位整數,大寫表示(A-F)
%o unsigned 8進位整數
以上加-和數字表示左對齊最少幾位,如%-9d表示左切齊最少印9位
以上加+和數字表示右對齊最少幾位,如%+9d表示右切齊最少印9位,不足處補空白
以上加+和0開頭的數字表示右對齊最少幾位,不足補0,如%+09d表示右切齊最少印9位,不足處補0
%ld 以十進位印出long
%lld 以十進位印出long long
%f浮點數float
%e %E科學表示法浮點數double,%e小寫 %E大寫
%g %G依照double的數值自動選擇以%f或%e格式印出
以上加-和具有小數點的數字表示左對齊最少幾位,如%-9.2f表示左切齊最少印9位,其中小數點以下2位
以上加+和具有小數點的數字表示右對齊最少幾位,如%=9.2f表示右切齊最少印9位,其中小數點以下2位
%c unsigned char
%s string(array of char terminated by 0)
%p pointer to void
%% 印出%

用C來寫複數

GNU C99以前通常是用結構(struct)來表示,簡單的例子如下
------------------------------------------------------ 
#include < stdio.h >

struct my_complex {
double real;
double imag;
};
int main(void){
struct my_complex z = {42.0, 42.0};

printf("z = %f + %fI\n",x.real, x.imag);

return 0;
}
-----------------------------------------------------
C99提供了一個方便的表示方法,在complex.h裡面
#include < stdio.h >
#include < complex.h >
int main (void){
complex double z = 42.0 + 42.0*I;

printf("z = %f + %fI\n", creal(z), cimag(z));

return 0;
}
----------------------------------------------------- 
— Function: double creal (complex double z) 
— Function: float crealf (complex float z) 
— Function: long double creall (complex long double z)These functions return the real part of the complex number z.
— Function: double cimag (complex double z) 
— Function: float cimagf (complex float z) 
— Function: long double cimagl (complex long double z)These functions return the imaginary part of the complex number z.
 
詳細的內容可以參考:
http://www.gnu.org/s/libc/manual/html_node/Complex-Numbers.html#Complex-Numbers 

fflush( )

一般電腦在執行IO的時候都會先將資料存在buffer裡面

等到system有空的時候才會讓資料寫入硬碟裡。因此我們要確保資料已經寫入

這時候就可以用fflush這個function。

以下是在論壇找到別人回覆的資料:

flush(stdin)刷新標準輸入緩衝區,把輸入緩衝區裡的東西丟棄  
fflush(stdout)刷新標準輸出緩衝區,把輸出緩衝區裡的東西打印到標準輸出設備上。

 

Example:

取自:http://www.cplusplus.com/reference/clibrary/cstdio/fflush/

/* fflush example */
#include 
char mybuffer[80];
int main()
{
   FILE * pFile;
   pFile = fopen ("example.txt","r+");
   if (pFile == NULL) perror ("Error opening file");
   else {
     fputs ("test",pFile);
     fflush (pFile);    // flushing or repositioning required
     fgets (mybuffer,80,pFile);
     puts (mybuffer);
     fclose (pFile);
     return 0;
  }
}
 
在linux上可以下sync這個指令。讓他將"記憶體"的內容寫入"硬碟"。
並不是IO的buffer..........。

 

0.0f ????????

0.0跟0.0f有什麼不一樣?
在C語言上印出來的結果都是一樣的?差在那邊?

0.00 is a double (64 bits)
0.0f is a float (32 bits)

嗯…就這麼簡單

C語言- clock() 運用

Example1:開始計時與結束
#include
#include
double ms_time();
int main(void)
{
  float a[100000],b[100000],c[100000];
  int i,j;

  //宣告clock function
  clock_t start, end;
  //開始計時
  start = clock();
  //以下亂寫,單純讓電腦跑久一點
  for(j=0;j<1000;j++){
    for(i=0;i<100000;i++){
      a[i] = drand48()*5;
      b[i] = drand48()*5;
      c[i] = a[i]*b[i];
    }
  }
  //計時結束
  end = clock();
  printf("The time was: %f\n", (double)(end - start));

  return 0;
}
備註:這樣發現跑出來的時間是以小時為單位,如下改成是以毫秒為單位。

Example2:以毫秒為單位

#include
#include
double ms_time();
int main(void)
{
  float a[100000],b[100000],c[100000];
  int i,j;
  double start, end;
  start = ms_time();

  for(j=0;j<1000;j++){
    for(i=0;i<100000;i++){
      a[i] = drand48()*5;
      b[i] = drand48()*5;
      c[i] = a[i]*b[i];
    }
  }
  end = ms_time();
  printf("The time was: %f\n", (double)(end - start));

  return 0;
}
double ms_time(){
  return (double)clock()/CLOCKS_PER_SEC*1000;
}

備註:clock()/CLOCKS_PER_SEC 在這邊就是以為單位