天天看點

C++基礎之資料類型,運算符,流程結構

資料類型

字元型

# include <iostream>
using namespace std;

int main(){
	
	char ch = 'a';
	cout << ch << endl;
	
	//char ch2 = "a";
	//char ch1 = 'abcde';
	
	cout << "字元變量所占的記憶體空間是:"<<sizeof(ch)<<endl;
	
	cout << (int)ch<<endl;
	
	char ch3 = 'A';
	cout << (int)ch3 << endl;
	
	return 0;
} 
/*
a
字元變量所占的記憶體空間是:1
97
65


*/
           

轉義字元

# include <iostream>
using namespace std;

int main(void){
	
	//轉義字元
	//1.換行符\n
	cout<<"hello world"<<endl;
	cout <<"hello world\n";
	
	//2.反斜杠
	cout<<"\\hello world\n";
	
	//3.橫向制表符\t(占8個空格,兩個間隔的空格數是:8減去前面變量所占的空格數) 
	cout << "abcde\t12345\n";
	cout << "abc\t3456\n" ;
	cout << "a\t12344567\n";
	cout <<"abcedfgh\t123\n";
	
	return 0;
}
/*
hello world
hello world
\hello world
abcde   12345
abc     3456
a       12344567
abcedfgh        123
*/
           

字元串

#include <iostream>
using namespace std;
#include <string>//string頭檔案 

int main(){
	
	//1.C風格的字元串
	char str[] = "不積跬步無以至千裡";
	cout << str << endl;
	
	//2.C++風格的字元串
	string str1 = "一口吃不成一個胖子";//string 小寫 
	cout << str1 << endl; 
	
	return 0;
}
/*
不積跬步無以至千裡
一口吃不成一個胖子

*/
           

布爾類型

# include <iostream>
using namespace std;
int main(){
	
	bool flag = true;
	cout << flag << endl;
	flag = false;
	cout << flag << endl;
	
	//bool占一個位元組 
	cout << "bool 所占的記憶體空間是:"<< sizeof(bool) << endl;
	
	return 0;
}
/*
1
0
bool 所占的記憶體空間是:1
*/
           

資料輸入

# include <iostream>
using namespace std;
# include <string>

int main(){
	
	//1.鍵盤輸入int類型
	int i = 10;
	cout << "請輸入int類型的變量i的值" << endl;
	cin >> i ;
	cout << "int 類型的值a = " << i << endl;

	return 0;
} 
           
# include <iostream>
using namespace std;
# include <string>

int main(){
	
	//2.鍵盤輸入float類型
	float f = 23.4f;
	cout << "請輸入float類型變量的值:"<< endl;
	cin >> f;
	cout << "float 類型變量的值是:"<< f << endl; 

	return 0;
} 
           
# include <iostream>
using namespace std;
# include <string>

int main(){
	
//3.鍵盤輸入char類型
	char ch = 'H';
	cout << "請輸入char 類型變量的值"<< endl;
	cin >> ch;
	cout << "ch = "<< ch <<endl;
	return 0;
} 
/*
請輸入char 類型變量的值
a
ch = a

*/
           
# include <iostream>
using namespace std;
# include <string>

int main(){
	
	//4.鍵盤輸入string類型
	string str = "日積月累";
	cout << "請輸入字元串的值"<< endl;
	cin >> str;
	cout << "str = " << str << endl;

	return 0;
} 
/*
請輸入字元串的值
你好
str = 你好

*/
           
# include <iostream>
using namespace std;
# include <string>

int main(){
	
	//5.鍵盤輸入bool類型 
	bool flag = false;
	cout << "請輸入布爾類型的變量flag的值" << endl;
	cin >> flag;
	//flag的值隻有0,1,0表示假,1表示真 
	cout << "flag = "<< flag << endl; 

	return 0;
} 
/*
請輸入布爾類型的變量flag的值
23
flag = 1

           

運算符

算術運算符

# include <iostream>
using namespace std;
int main(){
	
	int a = 10;
	int b = 30;
	cout << "a+b=" << a+b << endl;
	cout << "a-b=" << a-b << endl;
	cout << "a*b=" << a*b << endl;
	cout << "a/b=" << a/b << endl;//兩個整數相除得到整數
	cout << "2.4/2=" << 2.4/2 << endl;
	//cout << "a/0=" << a/0;//0不能作為除數
	cout << "a%b=" << a%b << endl;
	//cout << "2.4 % 1.2=" << 2.4 % 1.2 << endl;//隻能對整數取餘 (模)
	
	//前自增和前自減
	int x = 10;
	++x; 
	int y = 20;
	--y; 
	cout << "x = "<< x << endl;
	cout << "y =" << y << endl;
	//後自增和後自減
	int m = 11;
	m++;
	int n = 12;
	n-- ;
	cout << "m = " << m << endl;
	cout << "n = " << n << endl;
	//前自增(減)和後自增(減)的差別 
	 int k = 10;
	 int p = ++k * 10;
	 cout <<"k = " << k << ",l = " << p << endl;
	 //前自增(減):先自增(減),再給表達式指派 
	 int s = 11;
	 int h = s--%3;
	 cout << "s = "<< s << ",h = " << h << endl;
	 
	return 0;
} 
/*
a+b=40
a-b=-20
a*b=300
a/b=0
2.4/2=1.2
a%b=10
x = 11
y =19
m = 12
n = 11
k = 11,l = 110
s = 10,h = 2
*/
           

指派運算符

# include <iostream>
using namespace std;
int main(){
	
	//指派運算符
	//  =
	int a = 10;
	a = 100;
	cout << a << endl;
	
	//  +=
	a = 100;
	a += 100;
	cout << a << endl;
	
	//  -=
	a = 100;
	a -= 50;
	cout << a << endl;
	
	//  *=
	a = 100;
	a *= 2;
	cout << a << endl;
	
	//   /=  
	a = 100;
	a /= 10;
	cout << a << endl;
	
	//  %=
	a = 100;
	a %= 30;
	cout << a << endl;
}
/*
100
200
50
200
10
10

*/
           

比較運算符

# include <iostream>
using namespace std;
int main(void)
{
	//比較運算符
	int a = 10;
	int b = 30;
	// ==
	cout << (a == b) << endl;
	// !=
	cout << (a!= b) << endl;
	// >
	cout << (a > b) << endl;
	// <
	cout << (a < b) << endl;
	// >=
	cout << (a >= b) << endl;
	// <= 
	cout << (a <= b) << endl; 
	
	return 0;
}
/*
0
1
0
1
0
1

*/ 
           

邏輯運算符

# include <iostream>
using namespace std;
int main(){
	
	//邏輯運算符
	// !
	int a = 10;
	int b = 0;
	cout << (!a) << endl;
	cout << (!b) << endl;
	
	// &&
	cout << (10 && 10) << endl;
	cout << (0 && 20) << endl;
	cout << (0 && 0) << endl;
	
	// ||
	cout << (10 || 0) << endl;
	cout << (10 || 10) << endl;
	cout << (0 || 0) << endl;
	
	return 0;
}
/*
0
1
1
0
0
1
1
0
*/ 
           

程式流程結構

選擇結構-if語句

# include <iostream>
using namespace std;

int main(){
	
	
	//if 單行判斷
	int age = 0;
	cout << "請輸入你的年齡:"<< endl;
	cin >> age;
	cout << "您輸入的年齡是:" << age << endl;
	//如果年齡大于20,已經成年
	if (age > 18)
	{
		cout << "您已經成年" << endl; 
	} 
	return 0;
} 
/*
  if單行判斷 
請輸入你的年齡:
18
您輸入的年齡是:18

請輸入你的年齡:
20
您輸入的年齡是:20
您已經成年

*/
           
# include <iostream>
using namespace std;

int main(){
	
	
	//if判斷
	int age = 0;
	cout << "請輸入你的年齡:"<< endl;
	cin >> age;
	cout << "您輸入的年齡是:" << age << endl;
	//如果年齡大于20,已經成年
	if (age > 18)
	{
		cout << "您已經成年" << endl; 
	} 
	else
	{
		cout << "您還沒有成年" << endl; 
	}
	return 0;
} 
/*
 請輸入你的年齡:
16
您輸入的年齡是:16
您還沒有成年


*/
           
# include <iostream>
using namespace std;

int main(){
	
	
	//if判斷
	int age = 0;
	cout << "請輸入你的年齡:"<< endl;
	cin >> age;
	cout << "您輸入的年齡是:" << age << endl;
	//如果年齡大于20,已經成年
	if (age > 18)
	{
		cout << "您已經成年" << endl; 
	} 
	else if (age > 15)
	{
		cout << "正在上國中" << endl; 
	}
	else if (age > 7)
	{
		cout << "正在上國小" << endl; 
	} 
	else if (age > 5)
	{
		cout << "在上幼稚園" << endl; 
	}
	else
	{
		cout << "還沒有上學" << endl; 
	}
	return 0;
} 
/*
 請輸入你的年齡:
16
您輸入的年齡是:16
正在上國中

請輸入你的年齡:
10
您輸入的年齡是:10
正在上國小


*/
           

選擇結構-嵌套if

# include <iostream>
using namespace std;

int main()
{
	//找出三個人中身高最高的人;
	int a = 0;
	int b = 0;
	int c = 0;
	cout << "請輸入第一個人的身高:"<< endl;
	cin >> a;
	cout << "請輸入第二個人的身高:"<< endl;
	cin >> b;
	cout << "請輸入第三個人的身高:" << endl;
	cin >> c;
	cout << "你輸入的三個人的身高分别是:"<< a << "," << b << "," << c << endl;
	
	if ( a > b)
	{
		if (a > c)
		{
			cout << "第一個人最高" << a << endl;
		}
		else
		{
			cout << "第三個人最高 " << c << endl; 
		}
	}
	else
	{
		if (b > c)
		{
			cout << "第二個人最高" << b << endl; 
		}
		else
		{
			cout << "第三個人最高" << c << endl; 
		}
	} 
	
	cout << "三目運算符" << endl;
	int temp = 0;
	int max = 0;
	temp = (a > b)? a : b;
	max = (temp > c)? temp : c;
	cout << "max = " << max << endl;
	return 0;
}
/*
請輸入第一個人的身高:
189
請輸入第二個人的身高:
190
請輸入第三個人的身高:
188
你輸入的三個人的身高分别是:189,190,188
第二個人最高190
三目運算符
max = 190
*/
           

選擇結構-三目運算符

# include <iostream>
using namespace std;
int main()
{
	
	//三目運算符傳回的是變量
	int a = 10;
	int b = 3;
	int c = 0;
	c = (a > b? a : b); //把b 的值賦給a
	cout << "c = " << c << endl;//10
	//(a > b? a:b)傳回的是a 
	( a > b? a:b) = 100;//把100的值賦給a;
	cout << "a = " << a << ",b = " << b << endl;//a = 100,b = 3
	(a < b?b:a) = 30;
	cout << "a = " << a << ",b = " << b << endl;//a = 30,b = 3
} 
           

選擇結構-switch

# include <iostream>
using namespace std;
int main()
{
	int month;
	cout << "請輸入目前的月份"<< endl;
	cin >> month;
	cout <<"您輸入的月份是" << month << endl;
	
	switch (month)
	{
		case 12:
			cout << "冬天" << endl;
			break;
		case 1:
			cout << "冬天" << endl;
			break;
		case 2:
			cout << "冬天" << endl;
			break;
		case 3:
			cout << "春天" << endl;
			break;
		case 4:
			cout << "春天" << endl;
			break;
		case 5:
			cout << "春天" << endl;
			break;
		case 6:
			cout << "夏天" << endl;
			break;
		case 7:
			cout << "夏天" << endl;
			break;
		case 8: 
			cout << "夏天" << endl;
			break;
		case 9:
			cout << "秋天" << endl;
			break;
		case 10:
			cout << "秋天" << endl;
			break;
		case 11:
			cout << "秋天" << endl;
			break;
		default:
			cout << "您的輸入有誤"<<endl;
			break; 
	}
	return 0;
}
/*
請輸入目前的月份
12
您輸入的月份是12
冬天


*/
           
c++

繼續閱讀