第2章 C++的簡單程式設計(一)
阿新 • • 發佈:2018-12-06
第2章 C++的簡單程式設計(一)
C++語言概述
名稱空間
避免命名衝突:
std是C++標準庫的名稱空間( namespace)名
using namespace std表示開啟std名稱空間
#include <iostream> //編譯預處理命令
using namespace std; //避免命名衝突
int main(){
cout<< "Hello!" << endl;
return 0;
}
基本資料型別、常量、變數
//初始化
int a=0;
int a(0);
int a={0};
int a{0};
//常量
const float PI=3.1415926;
程式舉例
讀入並顯示資料
int main()
{
//讀入並顯示整數
const double pi(3.14); //符號常量
int radius(0); //變數初始化
cout << "The initial value of radius is " << radius << "\n";
cout << "Please enter the radius!\n";
cin >> radius;
cout << "The radius is " << radius << "\n";
cout << "Pi is " << pi << "\n";
cout << "Please enter a different radius!\n";
cin >> radius;
cout << "Now the radius is " << radius;
return 0;
}
運算與表示式
sizeof, 位運算
sizeof運算
語法形式: sizeof (型別名) 或 sizeof 表示式
結果值:
“型別名”所指定的型別,或“表示式”的結果型別所佔的位元組數。
例:
sizeof(short)
sizeof x
位運算
按位與:某些位置0
按位或:某些位置1
按位異或:相同為0,不同為1。某些位翻轉
資料的輸入和輸出
cout<<表示式1<<表示式2
int a,b;
cin>>a>>b;
選擇結構
if 語句
if (x>y) cout<<x;
if (x>y) cout <<x;
else cout<<y;
if (表示式1) 語句1;
else if (表示式2) 語句2;
else if (表示式3) 語句3;
...
else 語句n;
//輸入一個年份,判斷是否是閏年
#include <iostream>
using namespace std;
int main()
{
int year;
bool isLeapYear;
cout<<"enter the year:";
cin>>year;
isLeapYear= ((year%4==0 && year%100!=0)||(year%400=0));
if (isLeapYear)
cout<<year<<"is a leap year"<<endl;
else
cout<<year<<"is not a leap year"<<endl;
return 0;
}
//輸入兩個整數,比較兩個整數的大小
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout << "enter x and y:";
cin >> x >> y;
if (x > y)
cout << "x>y" << endl;
else if (x == y)
cout << "x=y" << endl;
else
cout << "x<y";
return 0;
}
switch語句
//輸入一個0-6的整數,並且轉換為星期輸出
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number (0-6):";
cin >> number;
switch(number){
case 0: cout << "Sunday" << endl; break;
case 1: cout << "Monday" << endl; break;
case 2:cout << "Tuesday" << endl; break;
case 3:cout << "Wednesday" << endl; break;
case 4: cout << "Thursday" << endl; break;
case 5:cout << "Friday" << endl; break;
case 6:cout << "Saturday" << endl; break;
default: cout << "Day out of range." << endl; break;
}
return 0;
}
迴圈結構
while迴圈
//求自然數1-10的和
#include <iostream>
#using namespace std;
int main()
{
int i(1),sum(0);
while (i <= 10)
{
sum += i;
i += 1;
}
cout << sum << endl;
return 0;
}
do while
//輸入一個數,將各位數字翻轉後輸出
#include <iostream>
using namespace std;
int main()
{
cout << "enter a number:" << endl;
int number;
cin >> number;
do
{
cout << number % 10;
number /= 10;
} while (number != 0);
return 0;
}
//輸出0-10的和
#include <iostream>
using namespace std;
int main()
{
int i = 1, sum = 0;
do {
sum += i;
i += 1;
} while (i <= 10);
cout << sum << endl;
return 0;
}
for 語句
//輸入一個整數,求出他所有的因子
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "enter a postive number:";
cin >> number;
cout << "number " << number << " factors:";
for (int i = 1; i <= number; i++) {
if (number%i == 0) {
cout << i<<" ";
}
}
return 0;
}
巢狀的控制結構、其他控制語句
//輸入一系列整數,統計出正整數個數i和負整數個數j,讀入0則結束
using namespace std;
int main()
{
int pos = 0, neg = 0, n;
cout << "enter some integers (enter 0 to quit):";
cin >> n;
while (n != 0) {
if (n > 0) pos += 1;
else if (n < 0) neg += 1;
cin >> n;
}
cout << pos << " postive integers" << endl;
cout << neg << " negative integers" << endl;
return 0;
}
其他控制語句
自定義型別
類型別名:為已有型別另外命名
列舉型別
//設某次體育比賽的結果有4種可能:勝(WIN),負(LOSE),平局(TIE),比賽取消(CANCEL),編寫程式順序輸出這四種情況
using namespace std;
enum GameResult {WIN,LOSE,TIE,CANCEL};
int main()
{
GameResult result;
enum GameResult omit = CANCEL;
for (int count = WIN; count <= CANCEL; count++) {
result = GameResult(count);
if (result == omit) cout << "The game was cancelled." << endl;
else
{
cout << "The game was played" ;
if (result == WIN) cout << " and we won!";
if (result == LOSE) cout << " and we lost!";
cout << endl;
}
}
return 0;
}
auto型別與decltype型別
實驗
面積
#include <iostream>
using namespace std;
const float PI = 3.1416;
int main()
{
cout << "please select a type (1-circle, 2-rectangle 3-square):";
int type;
cin >> type;
switch (type) {
case 1:
cout << "please enter r:";
float r;
cin >> r;
cout << "the area is " << PI * r*r << endl;
break;
case 2:
cout << "please enter the width and height:";
float wid, hei;
cin >> wid >> hei;
cout << "the area is " << wid * hei << endl;
break;
case 3:
cout << "please enter the length:";
float len;
cin >> len;
cout << "the area is " << len * len << endl;
break;
defalt:
cout << "sorry. please enter a correct type." << endl;
}
return 0;
}
時間
宣告一個表示時間的結構體,可以精確表示年、月、日、小時、分、秒,提示使用者輸入年、月、日、小時、分、秒,然後完整地顯示出來。
#include <iostream>
using namespace std;
const float PI = 3.1416;
struct myTimeStruct
{
unsigned int year;
unsigned int month;
unsigned int day;
unsigned int hour;
unsigned int min;
unsigned int sec;
};
int main()
{
myTimeStruct myTime;
cout << "enter the year:";
cin >> myTime.year;
cout << "enter the month:";
cin >> myTime.month;
cout << "enter the day:";
cin >> myTime.day;
cout << "enter the hour:";
cin >> myTime.hour;
cout << "enter the min:";
cin >> myTime.min;
cout << "enter the sec:";
cin >> myTime.sec;
cout << "the time is set to "
<< myTime.year << "/"
<< myTime.month << "/"
<< myTime.day << " "
<< myTime.hour << ":"
<< myTime.min << ":"
<< myTime.sec << endl;
return 0;
}
作業題
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int max, min;
int num,sum=0;
for (int i = 1; i <= n; i++) {
cin >> num;
sum += num;
if (i == 1) {
max = num;
min = num;
}
else {
if (num > max) max = num;
if (num < min) min = num;
}
}
cout << sum << " " << min << " " << max << endl;
}
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int space, symbol;
for (int i = 1; i <= n; i++) {
symbol = 2 * i - 1; //number of symbols
space = (2 * n - 1 - symbol) / 2; //number of symbols
for (int j = 1; j <= space; j++)
cout << " ";
for (int j = 1; j <= symbol; j++)
cout << "*";
for (int j = 1; j <= space; j++)
cout << " ";
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
symbol = 2 * i - 1; //number of symbols
space = (2 * n - 1 - symbol) / 2; //number of symbols
for (int j = 1; j <= space; j++)
cout << " ";
for (int j = 1; j <= symbol; j++)
cout << "*";
for (int j = 1; j <= space; j++)
cout << " ";
cout << endl;
}
return 0;
}