Condivisione della tecnologia

Studio C

2024-07-12

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

1. Commenti

/*多行
。。。
。。。
注释*/
//单行注释
#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. Definizione delle variabili

#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. Costanti

Utilizzato per registrare dati non modificabili nel programma

#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. Regole di denominazione

  • L'identificatore non può essere una parola chiave
  • Gli identificatori sono costituiti da lettere, numeri e trattini bassi
  • La prima lettera dell'identificatore può essere solo una lettera o un carattere di sottolineatura
  • Gli identificatori fanno distinzione tra maiuscole e minuscole

5. Tipo di dati

1. Tipo intero
Inserisci qui la descrizione dell'immagine

#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

Inserisci qui la descrizione dell'immagine
2. Corda

#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

Inserisci qui la descrizione dell'immagine
3.Tipo booleano
vero vero
falso falso

#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

Inserisci qui la descrizione dell'immagine
4. Tipo a virgola mobile

#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

Inserisci qui la descrizione dell'immagine
5. Valore immesso dalla tastiera

#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. Operatori

1. Operatori aritmetici
Inserisci qui la descrizione dell'immagine
Nota: due leoncini non possono eseguire le operazioni rimanenti.

#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. Operatore di assegnazione
Inserisci qui la descrizione dell'immagine
3. Operatori di confronto
Inserisci qui la descrizione dell'immagine
4. Operatori logici
Inserisci qui la descrizione dell'immagine

7. Funzione

1.Formatta

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

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

2. Implementazione e chiamata della funzione

#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. Trasferimento di valore

#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. Dichiarazione di funzione

#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. Scrittura di funzioni in file separati

fare un passo
1. Creare un file di intestazione con il suffisso .h
2. Creare un file sorgente con il suffisso .cpp
3. Scrivere la dichiarazione della funzione nel file header
4. Scrivere la definizione della funzione nel file originale

9. Puntatore

1. Utilizzare variabili puntatore per salvare gli indirizzi

#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. Puntatore nullo
Utilizzato per inizializzare le variabili puntatore
Non è possibile accedere ai puntatori nulli

#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. Puntatore selvaggio
Indica uno spazio di memoria non valido

#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. Puntatore costante, puntatore costante

#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. Uso combinato di puntatori e array

#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. Puntatori e funzioni

#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. Struttura

1.Definizione:

#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. Matrice di struttura

#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. Puntatore della struttura

#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. Nidificazione delle strutture

#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. Trasferimento dei parametri della struttura

#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 nella struttura
Per prevenire abusi

#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