習作プログラム のバックアップソース(No.2)

C言語プログラミングの練習用に書いたプログラムです.

#contents


* カレンダーを表示 [#g00ed97b]

 /* ===================================================================== 
    引数で指定した 月 年 のカレンダーを表示する May 26 2003
    ===================================================================== */
 
 #include <stdio.h>
 #include <stdlib.h>  /* for atoi() */
 
 
 
 /* ---------------------------------------------------------------------
    曜日 -- ツェラー(Zeller)の公式による計算
    --------------------------------------------------------------------- */
 
 int dayOfWeek(int year, int month, int day){
   if (month==1 || month==2) {
     year--;
     month += 12;
   }    
 
   /* 0:日, 1:月, 2:火, 3:水, 4:木, 5:金, 6:土 */
   return (year + year/4 - year/100 + year/400 + (13*month+8)/5 + day)%7;
 }
 
 
 
 /* ---------------------------------------------------------------------
    閏年の判定 -- 閏年なら1を返す
    --------------------------------------------------------------------- */
 
 int leapYear(int year){
   return (year%4 == 0) && (year%100 != 0 || year%400 == 0);
 }
 
 
 
 /* ---------------------------------------------------------------------
    月の日数計算
    --------------------------------------------------------------------- */
 
 int daysOfMonth(int year, int month){
   static int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
   /* 1--12以外では1月とみなす */
   if (month<1 || month>12)
     month = 1;
 
   /* うるう年のとき */
   if (month == 2)
     return days[1] + leapYear(year);
   else
     return days[month-1];
 }
 
 
 
 /* ---------------------------------------------------------------------
    カレンダーを表示
    --------------------------------------------------------------------- */
 
 void dispCalender(int month, int year){
   int i, k, days;
 
 /*  for (month=1; month<=12; month++) { */
 
   printf("      %d月 %d\n", month, year);
   printf("Su Mo Te We Th Fr Sa\n");
   
   k = dayOfWeek(year, month, 1);
   days = daysOfMonth(year, month);
   
   /* 1日目まで空白で埋める */
   for (i=0; i<k; i++)
     printf("   ");
   
   /* 月の最終日まで表示 */
   for (i=1; i<=days; i++) {
     printf("%2d ", i);
     /* 日曜日の直前で改行 */
     if (++k%7 == 0)
       printf("\n");
   }
   printf("\n\n");
 
 /* } */
 
 }
 
 
 
 /* ---------------------------------------------------------------------
    main
    --------------------------------------------------------------------- */
 
 int main(int argc, char* argv[]){
   int year, month;
 
   if (argc<=2) {
     printf("usage:calender [month] [year]\n");
     return 1;
   }
 
   month = atoi(argv[1]);
   year = atoi(argv[2]);
 
   dispCalender(month, year);
 
   return 0;
 }


* 現在時刻を表示 [#u84e461f]

 /* =====================================================================
    time 関数をつかって現在時刻を表示 Oct 3 2002
    ===================================================================== */
 
 #include <stdio.h>
 #include <time.h>  /* fot time() */
 
 
 
 int main(void){
   time_t t1;
 
   time(&t1);
   printf("%s", ctime(&t1));
 
   return 0;
 }


* 現在のディレクトリを表示 [#l22c44c1]

 #include <stdio.h>
 
 int main(void){
   char dirName[64];
 
   /* カレントディレクトリ名を取得 */
   getcwd(dirName, 64);
 
   printf("%s\n", dirName);
 
   return 0;
 }

* ファイル中の文字を数える [#b03d51a7]


* バブルソート [#d05bc646]


* ユークリッドの互助法 [#affd0d26]


* 自然数を素因数分解 [#n743d08d]
Valid XHTML 1.1! home >
トップ 一覧 検索 最終更新 バックアップ   ヘルプ   最終更新のRSS
Modified by 物理のかぎプロジェクト PukiWiki 1.4.5_1 Copyright © 2001-2005 PukiWiki Developers Team. License is GPL.
Based on "PukiWiki" 1.3 by yu-jiPowered by PHP 5.3.29HTML convert time to 0.003 sec.