# ch2 課本51~52頁
**程式語言:** C++
**題目:** say hello
* 程式由 main() 開始 { }。
* #include < ...>來引入編譯器廠商內建的標頭檔
* 關鍵字必須是小寫。
* 陳述的結尾為分號(;)。
* 字串由兩個雙引號" "含括表示。
* 在C++程式中大小寫視為不同的變數名稱。
* \n 或 endl的意思為游標會在下一行的開始處(跳行)
* std::cout<< 將字串顯示在螢幕上
* 註解(comment) // 或 /* */
* using namespace std
---
> - [x] C++
``` c++=
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
// 課本51頁 為求解直角三角形、梯形、圓形面積。
cout << "\n 課本51頁 為求解直角三角形、梯形、圓形面積。\n " ;
const float pi=3.14159f;
int s,length,width,height,top,bottom,radius;
bool loop=true;
while(loop){
cout << " pleas input select data 1,2,3";
cin >> s;
if (s==1){
cout << "\n 計算三角形面積 please input botton & height data: ";
cin>>bottom>>height;
cout<<" 三角形面積為 :"<<(bottom*height/2)<<endl;
}
else if (s==2){
cout<<" 請依序輸入梯形的上底、下底、高 :";
cin>>bottom>>height;
cout<<" 梯形面積為 :"<<(top+bottom*height/2)<<endl;
}
else if (s==3){
cout<<" 請依序輸入圓形的半徑 :";
cin>>radius;
cout<<" 圓形面積為 :"<<(pi*radius*radius)<<endl;
}
else
loop=!loop;
cout << "Exit" ;
}
// 課本52頁 為追蹤變數的數值。
cout << "\n\n 課本52頁 為追蹤變數的數值。\n " ;
int i,sum,v1,v2,v3,v4,v5;
v1=3; sum=0;
v2=v1++;
cout << "\n v1= " << v1; //結果v1為__4__
cout << "\n v2= " << v2; //結果v2為__3__
v3=++v2;
cout << "\n v2= " << v2; //結果v2為_4___
cout << "\n v3= " << v3; //結果v3為__4__
v4=v3--;
cout << "\n v3= " << v3; //結果v3為___3_
cout << "\n v4= " << v4; //結果v4為___4_
v5=--v4;
cout << "\n v4= " << v4; //結果v4為__3__
cout << "\n v5= " << v5; //結果v5為___3_
for(i=0;i<10;i++)
sum=sum+i;
cout << "\n sum= " << sum; //結果sum為___45_
return 0;
}
```
> 輸出結果

###### tags: `教學`