1. 程式人生 > 其它 >C++資料型別/資料輸入

C++資料型別/資料輸入

技術標籤:C++字串c++程式語言

1.整型

#include <iostream>
using namespace std;
/*
語法:資料型別 變數=變數初始值;
int a=10;
資料型別存在意義:給變數分配合適的記憶體空間
*/
int main()
{
	//整型
	//1.短整型(-2^15 ~ 2^15-1 即是 -32768 ~ 32767 -1) 佔用2位元組
	short num1 = 32768;
	//2.整型(-2^31 ~ 2^31-1) 最常用!!!佔用4位元組
	int num2 = 10; 
	//3.長整型(-2^31 ~ 2^31-1) 佔用4位元組
	long
num3 = 10; //4.長長整型(-2^63 ~ 2^63-1) 佔用8位元組 long long num4 = 10; cout << "短整型num1=" << num1 << endl; system("pause"); //固定語句 return 0;//固定語句 }

11
2.sizeof關鍵字

#include <iostream>
using namespace std;
/*
	sizeof統計資料型別所佔記憶體大小
*/
int main()
{
	short num1 = 10;
	int num2 =
10; //第一種表示 cout << "short佔用的記憶體空間:" << sizeof(num1) << endl; cout << "int佔用的記憶體空間:" << sizeof(num2) << endl; //第二種表示 cout << "short佔用的記憶體空間:" << sizeof(short) << endl; cout << "int佔用的記憶體空間:" <<
sizeof(int) << endl; system("pause"); //固定語句 return 0;//固定語句 }

在這裡插入圖片描述
3.實型

#include <iostream>
using namespace std;

int main()
{
	//1.單精度 float  2.雙精度 double
	//預設情況下,輸出一個小數,顯示6位有效數字
	float f1 = 3.14f;
	cout << "f1=" << f1 << endl;

	double d1 = 3.14;  //預設型別
	cout << "d1=" << d1 << endl;

	//統計記憶體空間
	cout << "float佔用的記憶體空間:" << sizeof(float) << endl; //佔4個位元組空間
	cout << "double佔用的記憶體空間:" << sizeof(double) << endl; //佔8個位元組空間

	//科學計數法
	float f2 = 3e2;//3*10^2
	cout << "f2=" << f2 << endl;
	float f3 = 3e-2;//3*0.1^2
	cout << "f3=" << f3 << endl;
	system("pause"); //固定語句
	return 0;//固定語句
}

在這裡插入圖片描述
4.字元型

#include <iostream>
using namespace std;

int main()
{
	//字元型變數建立方式
	char ch = 'a';
	cout << ch << endl;

	//字元型變數所佔記憶體大小
	cout << "char字元型變數所佔記憶體:"<<sizeof(char) << endl;

	//字元型常見錯誤
	//char ch2 = "b";需要單引號
	//char ch2 = 'abcdefg';建立字元變數時候,單引號內只能有一個字元;

	//字元型變數對應的ASCII編碼
	//a - 97; A - 65 按照此可以推算其他字母的ASCII編碼
	cout << "字元型變數a對應的ASCII編碼:" << (int)ch << endl;
	char ch1 = 'A';
	cout << "字元型變數A對應的ASCII編碼:" << (int)ch1 << endl;

	system("pause"); //固定語句
	return 0;//固定語句
}

在這裡插入圖片描述
5.轉義字元

#include <iostream>
using namespace std;

int main()
{
	//轉義字元

	//換行符\n
	cout << "hello world\n";

	//反斜槓\\輸出一個\

	cout << "\\"<<endl;

	//水平製表符\t 使得資料整齊輸出
	cout << "aaaa\thelloworld" << endl;
	cout << "aa\thelloworld" << endl;
	cout << "aaaaaa\thelloworld" << endl;

	system("pause"); //固定語句
	return 0;//固定語句
}

在這裡插入圖片描述
6.字串型

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

int main()
{
	//1.C語言風格字串
	//注意事項 char 字串名需要加[] 
	//注意事項2 等號後面 要用雙引號
	char str[] = "hello world";
	cout << str << endl;

	//2.C++風格字串
	//注意事項 需要在新增標頭檔案#include <string>
	string str2 = "hello world";
	cout << str2 << endl;

	system("pause"); //固定語句
	return 0;//固定語句
}

7.布林型別

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

int main()
{
	//建立bool資料型別
	bool flag = true; 
	cout << flag << endl;//輸出為1代表真

	flag = false; 
	cout << flag << endl;//輸出為0代表假

	//檢視bool型別所佔記憶體空間
	cout << "bool型別所佔的記憶體空間:" << sizeof(bool) << endl;

	system("pause"); //固定語句
	return 0;//固定語句
}

在這裡插入圖片描述
8.資料的輸入
作用:用於從鍵盤獲取資料
關鍵字:cin
語法:cin>>變數

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

int main()
{
	//cin:從鍵盤輸入資料
	//1.整型
	int a = 0;
	cout << "請給整型變數a賦值:" << endl;
	cin >> a;
	cout << "整型變數:" << a << endl;

	//2.浮點型
	float f = 3.14f;
	cout << "請給浮點型變數f賦值:" << endl;
	cin >> f;
	cout << "浮點型變數:" << f << endl;

	//3.字元型
	char ch = 'a';
	cout << "請給字元型變數ch賦值:" << endl;
	cin >> ch;
	cout << "字元變數:" << ch << endl;

	//4.字串型
	string str = "aanfndg";
	cout << "請給字串型變數str賦值:" << endl;
	cin >> str;
	cout << "字串變數:" << str << endl;

	//5.bool型別 非0值均代表真
	bool flag = true;
	cout << "請給boll型變數flag賦值:" << endl;
	cin >> flag;
	cout << "bool變數:" << flag << endl;

	system("pause"); //固定語句
	return 0;//固定語句
}

本筆記為學習記錄

學習資源:https://www.bilibili.com/video/BV1et411b73Z?p=16