--- title: Chapter2 - 程式架構的認識與實作 tags: 程式設計實習C++ --- # 建置開發環境 [下載 CodeBlock](https://www.codeblock.org/downloads/binaries/) ## 新增方案 1. 新增: Console application ![](https://i.imgur.com/uq294M0.png) 2. 選擇C++ ![] (https://i.imgur.com/KcEooZI.png) 3. 儲存專案 ![] (http://i.imgur.com/KcEooZI.png) ```cpp=1 #include <iostream> using namespace std; int main() { // 宣告2個整數變數x, y int x, y; // 輸入 2個整數分別存入 x, y cin>>x>>y; //計算 x+y 後輸出 cout <<x+y<< endl; //計算 x-y 後輸出 cout <<x-y<<"\n"; //計算 x*y 後輸出 cout <<x*y<<"\n"; return 0; } ``` # 認識演算法 :::success 演算法是一組用來解決特定問題的有限指令或步驟 ::: ## 演算法特性 :::warning 1. 輸入:演算法中經常需要輸入資料 2. 輸出:演算法至少要有一個以上的輸出資料 3. 有限性:演算法應該要在有限的處理步驟內得到結果 4. 明確性:每個步驟都必須明確,不能有模稜兩可的情況 ::: # 家家 { cout << "sizeof()的使用\nC++ 基本型態的使用\n"; cout << "char 字元型態所占記憶體空間 - "<< sizeof(char)<< "bytes\n"; cout << "int 整數型態所占記憶體空間 - "<< sizeof(int) << "bytes\n"; cout << "float 浮點數型態所占記憶體空間 - "<< sizeof(float)<< "bytes\n"; cout << "doublc 倍精型態所占記憶體空間 - " << sizeof(double)<< "bytes\n"; cout << "int 正整數型態所占記憶體空間 - "<< sizeof(unsigned int)<< "bytes\n"; return 0; } ## ouo #include <iostream> using namespace std; int main() { float f = 0.123456789; double d = 0.123456789; // 探索精確度 cout << "f="<<"tsizeof(float)="<<sizeof(float)<<"\n"; cout<<"d="<<d<<"tsizeof(double)="<<sizeof(double)<<"\n"; // 設定輸出小數點後9位 cout.precision(9); cout << "f="<<"tsizeof(float)="<<sizeof(float)<<"\n"; cout<<"d="<<d<<"tsizeof(double)="<<sizeof(double)<<"\n"; //讓使用者輸入3個分數(整數),輸出平均 int x,y,z; // 宣告 整數變數來儲存x,y,z cin>>x>>y>>z; // 輸入資料儲存到x,y,z // 輸出 (int+int+int)/int得到int cout<<"Average is " << (x+y+z)/3<<"\n"; // 輸出 (int+int+int)/int得到int cout<<"Average is " << (x+y+z)/3.<<"\n"; return 0; } # cnc // 預設以數字輸出bool值 bool b1 = true, b2 = false; cout<<"b1="<<b1<<"\tb2="<<b2<<"\n"; b1= -100; b2=1000; cout<<"b1="<<b1<<"\tb2="<<b2<<"\n\n"; //以文字方式輸出 bool值 cout<<boolalpha; b1=-1; b2=0; cout<<"b1="<<b1<<"\tb2="<<b2<<"\n";