Technologieaustausch

C-Studie

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Kommentare

/*多行
。。。
。。。
注释*/
//单行注释
#include <iostream>
using namespace std;
int main() {
	cout << "hellow" << endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2. Variablendefinition

#include <iostream>
using namespace std;
int main()
{
	int a = 10;//变量定义
	cout << "a="<<a << endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3. Konstanten

Wird verwendet, um unveränderliche Daten im Programm aufzuzeichnen

#include <iostream>
using namespace std;

//两种定义方式
//define 宏常量,不可修改,一旦修改就会报错
//const 修饰的变量
#define Day 7
int main()
{
	Day = 14;
	cout << "Day="<<Day << endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
#include <iostream>
using namespace std;

//两种定义方式
//define 宏常量,不可修改,一旦修改就会报错
//const 修饰的变量也是常量,不可修改
#define Day 7
int main()
{
	//Day = 14;
	cout << "Day="<<Day << endl;
	const int mount = 12; 
	mount = 24
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4. Namensregeln

  • Der Bezeichner darf kein Schlüsselwort sein
  • Bezeichner bestehen aus Buchstaben, Zahlen und Unterstrichen
  • Der erste Buchstabe der Kennung darf nur ein Buchstabe oder ein Unterstrich sein
  • Bei Bezeichnern muss die Groß-/Kleinschreibung beachtet werden

5. Datentyp

1. Integer-Typ
Fügen Sie hier eine Bildbeschreibung ein

#include <iostream>
using namespace std;
int main()
{
	short num1 = 10; //(-32768,32767)
	int num2 = 10;
	long num3 = 10;
	long long num4 = 10;
	short num5 = 32769; //打印为-32767
	cout << "num1="<<num1 << endl;
	cout << "num2="<<num2 << endl;
	cout << "num3="<<num3 << endl;
	cout << "num4="<<num4 << endl;
	cout << "num5="<<num5 << endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Fügen Sie hier eine Bildbeschreibung ein
2. Zeichenfolge

#include <iostream>
using namespace std;
int main()
{
	char str[] = "hellow";
	cout<<str<<endl;
	//包含头文件 #include <string>
	string str2 = "nihao";
	cout<<str2<<endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

Fügen Sie hier eine Bildbeschreibung ein
3.Boolescher Typ
Wahr, wahr
falsch falsch

#include <iostream>
using namespace std;
int main()
{
	bool flag = true;
	cout<<flag<<endl;
	flag = false;
	cout<<flag<<endl;
	system("pause");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Fügen Sie hier eine Bildbeschreibung ein
4. Gleitkommatyp

#include <iostream>
using namespace std;
int main() 
	//float 单精度  4字节
	//double 双精度  8字节
{
	float f1 = 3.1415926f;
	cout<<f1<<endl;
	double f2 = 3.1415926;
	cout<<f2<<endl;
	system("pause");
	
	//科学计数法
	float f3 = 3e2;//3*10~2
	cout<<f3<<endl;
	float f4 = 3e-2;//3*0.1~2
	cout<<f4<<endl;
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Fügen Sie hier eine Bildbeschreibung ein
5. Tastatureingabewert

#include <iostream>
using namespace std;
int main() 
	//cin>>
{
	int a = 0;
	cout<<"请输入a值"<<endl;
	cin>>a;
	cout<<a<<endl;

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

6. Betreiber

1. Arithmetische Operatoren
Fügen Sie hier eine Bildbeschreibung ein
Hinweis: Zwei kleine Löwen können keine Restoperationen durchführen.

#include <iostream>
using namespace std;
int main() 
{
	//前置递增
	int a1 = 10;
	int b1 = ++a1*10;//先让变量加1,然后进行表达式计算
	cout<<a1<<endl;
	cout<<b1<<endl;

	//后置递增
	int a2 = 10;
	int b2 = a2++*10;//先进行表达式计算,后让变量加1
	cout<<a2<<endl;
	cout<<b2<<endl;
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

2. Zuweisungsoperator
Fügen Sie hier eine Bildbeschreibung ein
3. Vergleichsoperatoren
Fügen Sie hier eine Bildbeschreibung ein
4. Logische Operatoren
Fügen Sie hier eine Bildbeschreibung ein

7. Funktion

1.Formatieren

返回值类型 函数名 (参数列表)
{
	函数体语句
	return 表达式

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2. Funktionsimplementierung und -aufruf

#include <iostream>
using namespace std;
int add(int num1,int num2)//num1,num2为形参
{
	int num3 = num1+num2;
	return num3;
}
int main() 
{
	int a = 1;
	int b = 2;
	int c = add(a,b); //a,b为实参
	cout<<c<<endl;
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

3. Wertübertragung

#include <iostream>
using namespace std;
void change(int num1,int num2)//num1,num2为形参
{
	//交换前
	cout<<"交换前"<<endl;
	cout<<"num1:"<<num1<<endl;
	cout<<"num2:"<<num2<<endl;
	int num3 = num1;
	num1 = num2;
	num2 = num3;
	//交换后
	cout<<"交换后"<<endl;
	cout<<"num1:"<<num1<<endl;
	cout<<"num2:"<<num2<<endl;
	return ;
}
int main() 
{
	int a = 1;
	int b = 2;
	//当函数做值传递时,函数的形参会发生改变,并不会影响实参
	cout<<"前a:"<<a<<endl;
	cout<<"前b:"<<b<<endl;
	change(a,b); 
	cout<<"后a:"<<a<<endl;
	cout<<"后b:"<<b<<endl;
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

4. Funktionsdeklaration

#include <iostream>
using namespace std;
//函数声明,提前告诉编译器函数的存在,可以利用函数的声明,避免主函数在上面报错
void change(int num1,int num2);
int main() 
{
	int a = 1;
	int b = 2;
	//当函数做值传递时,函数的形参会发生改变,并不会影响实参
	cout<<"前a:"<<a<<endl;
	cout<<"前b:"<<b<<endl;
	change(a,b); 
	cout<<"后a:"<<a<<endl;
	cout<<"后b:"<<b<<endl;
	return 0;
}
void change(int num1,int num2)//num1,num2为形参
{
	//交换前
	cout<<"交换前"<<endl;
	cout<<"num1:"<<num1<<endl;
	cout<<"num2:"<<num2<<endl;
	int num3 = num1;
	num1 = num2;
	num2 = num3;
	//交换后
	cout<<"交换后"<<endl;
	cout<<"num1:"<<num1<<endl;
	cout<<"num2:"<<num2<<endl;
	return ;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

8. Funktionen in separate Dateien schreiben

Schritt
1. Erstellen Sie eine Header-Datei mit dem Suffix .h
2. Erstellen Sie eine Quelldatei mit dem Suffix .cpp
3. Schreiben Sie die Funktionsdeklaration in die Header-Datei
4. Schreiben Sie die Funktionsdefinition in die Originaldatei

9. Zeiger

1. Verwenden Sie Zeigervariablen, um Adressen zu speichern

#include <iostream>
using namespace std;

int main() 
{
	//定义指针
	int a = 10;
	int *p;
	p = &a;//指针记录a的地址
	cout<<"a的地址"<<&a<<endl;
	cout<<"指针p的地址"<<p<<endl;
	//指针使用
	//可以通过解引用的方式来找到指针指向的内容
	//指针前加*代表解引用,找到指针指向的内存中的数据
	*p = 1000;
	cout<<"a的值"<<a<<endl;
	cout<<"指针p的值"<<*p<<endl;
	
	
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

2. Nullzeiger
Wird zum Initialisieren von Zeigervariablen verwendet
Auf Nullzeiger kann nicht zugegriffen werden

#include <iostream>
using namespace std;

int main() 
{
	//空用于给指针变量初始化
	int *p = NULL;
	//空用于给指针变量初始化
	//0-255之间的内存编号是系统占用的,因此不可以访问
	*p = 1000;//会报错
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3. Wilder Zeiger
Zeigt auf unzulässigen Speicherplatz

#include <iostream>
using namespace std;

int main() 
{
	//
	int *p = (int *)0x000;
	cout<<*p<<endl
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4. Zeigerkonstante, konstanter Zeiger

#include <iostream>
using namespace std;

int main() 
{
	//const修饰指针  常量指针
	int a = 10;
	int b = 20;
	const int *p = &a;
	//指针指向的值不能改,指针的指向可以改
	//*p = 20  //报错
	*p = &b;//正确
	
	//const修饰常量 指针常量
	//指针的指向不可以改,指针指向的值可以改
	int * const p2 = &a;
	*p2 = 100;//正确的
	p2 = &b;//错误,指向不能改
	
	
	//const 修饰指针和常量
	const int * const p3 = &a;
	//指针的指向和指针指向的值都不可以改
	*p3 = 1000;
	p3 = &b
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

5. Gemeinsame Verwendung von Zeigern und Arrays

#include <iostream>
using namespace std;

int main()
{
 	int arr[10] = {1,2,3,4,5,6,7,8,9,10};
	int * p = arr;
	for(int i=0;i<10;i++)
	{
		cout<<*p<<endl;
		p++;
	}
}	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

6. Zeiger und Funktionen

#include <iostream>
using namespace std;
void seap(int a,int b)
{
	int tem = a;
	a = b;
	b = tem;
}
void seap02(int *a,int *b)
{
	int tem = *a;
	*a = *b;
	*b = tem;
}
int main()
{
 	int a = 10;
	int b = 20;
	//值传递
	//seap(a,b);
	//cout<<a<<endl;
	//cout<<b<<endl;
	//地址传递,会改变实参的值
	seap02(&a,&b);
	cout<<a<<endl;
	cout<<b<<endl;

	
}	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

10. Struktur

1.Definition:

#include <iostream>
using namespace std;
struct Student
{
	string name;
	int age;
	int scroe;
}s3;
int main()
{
	//结构体调用
	//1.struct Student s1;
	struct Student s1;
	s1.name = "xuxu";
	s1.age = 20;
	s1.scroe = 30;
	cout<<"姓名"<<s1.name<<"年龄"<<s1.age<<"分数"<<s1.scroe;
	//2.struct Student s2 = {...};
	struct Student s2 = {"xuxu2",26,100};
	cout<<"姓名"<<s2.name<<"年龄"<<s2.age<<"分数"<<s2.scroe;
	//3在定义结构体时顺便创建结构体变量
	s3.name = "wangwu";
	s3.age = 50;
	s3.scroe = 2000;
	cout<<"姓名"<<s3.name<<"年龄"<<s3.age<<"分数"<<s3.scroe;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2. Strukturarray

#include <iostream>
using namespace std;
struct Student
{
	string name;
	int age;
	int scroe;
}s3;
int main()
{
	//创建结构体数组
	struct Student arr[3] = {
		{"张三",20,100},
		{"李四",21,101},
		{"王五",22,102},
	};
	//修改结构体数据
	arr[1].name = "liuliu";
	
	//遍历结构体数组
	for(int i=0;i<3;i++)
	{
		cout<<"姓名"<<arr[i].name<<"年龄"<<arr[i].age<<"分数"<<arr[i].scroe;
	}
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

3. Strukturzeiger

#include <iostream>
using namespace std;
struct Student
{
	string name;
	int age;
	int scroe;
};
int main()
{
	//创建结构体数组
	Student s1 = {"xuxu",22,1000};
	//定义结构体指针
	Student *p = &s1;
	//利用指针访问数据
	cout<<p->name<<p->age<<p->scroe<<endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

4. Strukturverschachtelung

#include <iostream>
using namespace std;
struct student
{
	string name;
	int age;
	int score;
};
struct Teacher
{
	int id;
	string name;
	int age;
	struct student stu;
};
int main()
{
   Teacher t;
	t.id = 666;
	t.name = "xuxu";
	t.age = 30;
	t.stu.age = 1000;
	cout<<t.id<<t.name<<t.age<<t.stu.age<<endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

5. Strukturparameterübergabe

#include <iostream>
using namespace std;
struct student
{
	string name;
	int age;
	int score;
};
//值传递
void printffun(struct student s)
{
	cout<<s.name<<s.age<<s.score<<endl;

}
//地址传递
void printffun01(struct student *s)
{
	cout<<s->name<<s->age<<s->score<<endl;

}
int main()
{
   student t;
	t.name = "xuxu";
	t.age = 30;
	t.score = 10000;
	printffun(t);
	printffun01(&t);
	//cout<<t.name<<t.age<<t.score<<endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

6. const in der Struktur
Um Missbrauch vorzubeugen

#include <iostream>
using namespace std;
struct student
{
	string name;
	int age;
	int score;
};
//地址传递
void printffun01(const struct student *s)
{
	s->age = 150;
	cout<<s->name<<s->age<<s->score<<endl;

}
int main()
{
   student t;
	t.name = "xuxu";
	t.age = 30;
	t.score = 10000;
	printffun01(&t);
	//cout<<t.name<<t.age<<t.score<<endl;
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25