include ctime include cstdlib include iostream using namespace
剪刀石頭布 • • #include <ctime> #include <cstdlib> #include <iostream> using namespace std; • • • • • int main () { int x; cout << "請輸入代碼n||0. 石頭||1. 剪刀||2. 布||3. 離開此遊戲!!||n"; do{ cout << "您輸入的代碼為:"; cin >> x; switch (x){ case 0: cout<<"石頭n"; break; case 1: cout<<"剪刀n"; break; case 2: cout<<"布n"; break; case 3: break; default: cout << "請勿輸入 0 1 2 3 以外的東西"; }}while (x!=3); system ("pause"); return 0; } break;
費式遞迴 • • • • • #include <ctime> #include <iostream> using namespace std; int f(int n) { if (n <= 1) //如果n<=1則將n的值回傳 return(n); else //不式的話則回傳(f(n-1)+f(n-2)) return(f(n-1)+f(n-2)); } int main(void) { int n; cout << "請輸入: "; cin >> n; cout << f(n); system("pause"); return(0); }
費式非遞迴 • • • • • #include <ctime> #include <iostream> using namespace std; int main(void) {int a, b, i, n, temp; a = 0; //先設定a=0 b = 1; //先設定b=0 cin>>n; //要使用者輸入n for( i = 2; i<=n; i++) //迴圈i從 2開始 i在n截止 i遞增 { temp = b; //將b丟給temp b = a + b; //將a+b丟給b a = temp; //再將temp丟給a } cout<<b; //顯示b system("pause"); return a; }
N階層for迴圈 • • • • • #include <ctime> #include <iostream> using namespace std; int main(void) { int no, i=1, sum = 1; do{ cout<<"請輸入一個正整數:"; cin>>no; if( no <= 0 ) cout<<"請勿輸入負整數或零"; }while ( no <= 0 ); for (i=1 ; i<=no ; i++) { sum *= i; } cout<<sum; system ("pause"); return (0); }
N階層while迴圈 • • • • #include <ctime> #include <iostream> using namespace std; int main(void) { int num, i=1, sum=1; cout << "請輸入一個整數:"; cin >> num; while(i<=num) sum*=i++; cout << sum; system("pause"); return (0); }
N階層Do-while迴圈 • • • • • • #include <ctime> #include <iostream> using namespace std; int main(void) { int no, i=1, sum = 1; do{ cout<<"請輸入一個正整數:"; cin >> no; if( no <= 0 ) cout<<"a請勿輸入負整數或零"; }while ( no <= 0 ); do { sum *= i++; }while(i<=no); cout<< sum; system ("pause"); return (0); }
//Date. h • // 年號版日期類別Date(介面部份) 年號日期 • • #if !defined(___Class_Date) #define • • • #include <string> #include <iostream> using namespace std; • • • • • • • • //===== 年號版日期類別Date =====// class Date { public: enum Gengou {Meiji, Taisho, Showa, Heisei}; private: int year; // 西元年 int month; // 月 int day; // 日 public: Date(); Date(int y, int m = 1, int d = 1) : year(y), month(m), day(d) { } Date(Gengou g, int y, int m = 1, int d = 1) { // 建構子(年號) switch (g) { case Meiji : y += 1867; break; // 明治 case Taisho : y += 1911; break; // 大正 case Showa : y += 1925; break; // 昭和 case Heisei : y += 1988; break; // 平成 } year = y; month = m; day = d; } // y年是閏年嗎? static bool Is. Leap(int y) { return y % 4 == 0 && y % 100 != 0 || y % 400 == 0; } int Year() const { return year; } int Month() const { return month; } // 傳回月 int Day() const { return day; } // 傳回日 int Day. Of. Week() const; bool Is. Leap() const { return Is. Leap(year); } string to_string() const; }; • ostream& operator<<(ostream& s, const Date& x); • #endif ___Class_Date // 插入子 // 年號 // 預設建構子 // 建構子(西元) // 傳回年 // 傳回星期 // 是閏年嗎? // 以字串傳回
年號日期 • • //Date. cpp // 年號版日期類別Date(實作部份) • • • #include <ctime> #include <sstream> #include <iostream> #include "Date. h" using namespace std; • • //--- 預設建構子 ---// Date: : Date() { time_t current = time(NULL); struct tm* local = localtime(¤t); year = local->tm_year + 1900; month = local->tm_mon + 1; 11 day = local->tm_mday; } //--- 傳回星期(星期日~星期六分別對應0~ 6)---// • • • // 取得現在的時間 // 轉換為各元素的時間 // 年:tm_year是年-1900 // 月:tm_mon是 0~
年號日期 • • • • • • • • • int Date: : Day. Of. Week() const { int y = year; int m = month; if (m == 1 || m == 2) { y--; m += 12; } return (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + day) % 7; } //--- 以字串傳回 ---// string Date: : to_string() const { unsigned long idate = year * 10000 UL + month * 100 UL + day; ostringstream s; if (idate < 18680908 UL) s << year; else if (idate < 19120730 UL) s << "明治" << (year - 1867); else if (idate < 19261225 UL) s << "大正" << (year - 1911); else if (idate < 19890108 UL) s << "昭和" << (year - 1925); else s << "平成" << (year - 1988); s << "年" << month << "月" << day << "日"; return s. str(); } //--- 插入到ostream ---// ostream& operator<<(ostream& s, const Date& x) { return s << x. to_string(); }
- Slides: 18