記事のディレクトリ
1.最初のC ++プログラム
C ++でHelloWorldを作成する
例
#include <iostream> //固定语句
using namespace std; //固定语句
//1、单行注释
/* 2、多行注释
main是一个程序的入口
每个程序只能有一个main函数
*/
int main()
{
//在屏幕中输出hello world
cout << "hello world" << endl;
system("pause"); //固定语句
return 0; //固定语句
}
2.プログラムに関する注記
1.単一行コメント: //コメント内容
2.複数行コメント: / *コメント内容* /
3つ、変数
1.変数の存在の重要性:メモリ空間を管理するのに便利です
2.変数作成の構文:データ型変数名=変数の初期値(int a = 10;)
例
#include <iostream>
using namespace std;
int main()
{
//定义一个变量
int a = 10;
cout << "a = "<< a << endl;
system("pause");
return 0;
}
第四に、定数
**1。定数の意味:**プログラムに変更できないデータを記録するために使用されます
2. C ++で定数を定義する2つの方法
(1)#defineマクロ定数#define定数名定数値
==通常はファイルの先頭で定義されます==これは定数を意味します
**(2)const変更変数** constデータ型定数名=定数値
==通常、キーワードconst ==は変数定義の前に追加され、変数を定数として変更します。変更することはできません。
例
#include<iostream>
using namespace std;
//常量的定义
//1.#define 宏常量
#define Day 7
int main()
{
//Day = 10; 此处为错误,前面用define 定义后,不可再次更改
cout << "一周总共有" << Day <<"天"<<endl;
const int month = 12;
//const = 13 ;此处错误,用const定义后,其值不可再次修改
cout << "一年有" << month << "月" << endl;
system("pause");
return 0;
}
5、キーワード
変数や定数に名前を付けるためにキーワードを使用しないでください
6、識別子の命名規則
**関数:** C ++には、識別子(変数、定数)に名前を付けるときに独自のルールセットがあります
-
識別子をキーワードにすることはできません
-
識別子は、文字、数字、およびアンダースコアのみで構成できます
-
最初の文字は文字またはアンダースコアである必要があります
-
識別子の文字は大文字と小文字が区別されます
7、データ型
C ++では、変数または定数を作成するときに、対応するデータ型を指定する必要があると規定されています。指定しないと、変数を割り当てることができません。メモリを割り当てる
7.1整数
1.短整数(short)は2バイトを占めます
2.整数(int)は4バイトを占めます
3.長整数(long)ウィンドウは4バイトを占有します
4. longlongは8バイトを占有します
7.2sizeofキーワード
sizeofキーワードを使用できます==統計データ型が占めるメモリサイズ==
構文:sizeof(データ型/変数)
【例】
#include<iostream>
using namespace std;
int main()
{
//整型 :short (2) int (4) long (4) long long (8)
short num1 = 10;
cout << "short占用的内存空间为" << sizeof(short) << endl;
cout << "short占用的内存空间为" << sizeof(num1) << endl;
int num2 = 10;
cout << "int占用的内存空间为" << sizeof(int) << endl;
cout << "int占用的内存空间为" << sizeof(num2) << endl;
long num3 = 10;
cout << "long占用的内存空间为" << sizeof(long) << endl;
cout << "long占用的内存空间为" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用的内存空间为" << sizeof(long long) << endl;
cout << "long long占用的内存空间为" << sizeof(num4) << endl;
system("pause");
return 0;
}
結果
7.3実数型
小数を表します
1.単精度:floatは4バイトを占め、カウントできます7位実効数
2.倍精度:倍精度は8バイトを占め、カウントできます15〜16有効数字
例
#include<iostream>
using namespace std;
//两种实型输出
int main()
{
float f1 = 3.14529265f;
cout << "f1 = " << f1 << endl;
double d1 = 3.14159265;
cout << "d1 = " << d1 << endl;
//科学计数法
float f2 = 3e2; //3*10^2
float f3 = 2e-2; //2*0.1^2
cout << "f2 = " << f2 << endl;
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
出力結果
【注意】 C ++では、小数点以下を出力する場合、デフォルトで有効数字6桁が出力されます。
7.4文字タイプ
1文字を表示==
構文:char ch = 'a';(アポストロフィしかありえない1文字==)
例
#include<iostream>
using namespace std;
int main()
{
//1、字符型变量创建方式
char ch = 'a';
cout << ch << endl;
//2、字符型变量所占用内存大小
cout <<"char型占用内存:"<< sizeof(char) << endl;
//3、字符型变量对应ASCII编码
cout << "a对应的ASCII编码:"<< (int)ch << endl;
system("pause");
return 0;
}
出力結果
7.5エスケープ文字
\ n改行、現在の位置を次の行の先頭に移動します
C ++では、endlは改行も意味します
\ t水平タブ(次のTAB位置にスキップ)、\ tは8文字です
\円記号 '\'を表します
例
#include<iostream>
using namespace std;
int main()
{
//换行
cout << "hello world\n";
//制表
cout << "a\tdivition" << endl;
cout << "aa\tdivition" << endl;
cout << "aaa\tdivition" << endl;
cout << "aaaa\tdivition" << endl;
//单斜杠
cout << "\\" << endl;
system("pause");
return 0;
}
出力結果
7.6文字列型
1. Cスタイルの文字列:char変数名[] = "文字列値"
2. C ++スタイルの文字列:文字列変数名= "文字列値"
例
#include<iostream>
#include<string> //若用C++风格定义字符串,则需添加此头文件
using namespace std;
int main()
{
//1、C风格
char str1[] = "hello world" ;
cout << str1 << endl;
//2、C++风格
string str2 = "hello world";
cout << str2 << endl;
system("pause");
return 0;
}
7.7ブール型bool
ブールデータ型は真または偽の値を表します
-
true(基本的に1)
-
false(基本的に0)
例
#include<iostream>
using namespace std;
int main()
{
//1、创建bool类型
bool flag = false;
cout << "结果为假时输出:" << flag << endl;
flag = true;
cout << "结果为真时输出:" << flag << endl;
//2、查看bool类型所占内存空间
cout << "bool类型所占内存空间为:" << sizeof(bool) << endl;
system("pause");
return 0;
}
出力結果
7.8データ入力
キーボードからデータを取得するために使用されます
キーワード:cin
構文:cin >>変数
例
#include<iostream>
using namespace std;
int main()
{
//1、整型
int a = 0;
cout << "请给整型变量a赋值:" << endl;
cin >> a;
cout<<"整型变量a = "<<a<<endl;
//2、浮点型
//3、字符型
//4、字符串型
//5、布尔类型
system("pause");
return 0;
}
出力結果
[注]学習コースは-ダークホースプログラムC ++チュートリアル