#define day 7 const int month 12;
数据类型:给变量分配合适的内存空间
float f1 = 3.14f; double d1 = 3.14;
c风格: char str[] = "hello word"; c++风格: string str = "hello word";
c++风格 要加入头文件
#include <string>
bool flag = ture; flag = false;
整型: cin >> a; 浮点型: cin >> f; 字符型: cin >> ch; 字符串型: cin >> str; 布尔型: cin >> flag;
只有整型变量才可以进行取模运算
前置递增:先让变量+1,在进行表达式运算 a=2; b=++a; a=3; b=3; 后置递增 a=2; b=a++; a=3; b=2; 前置递减 a=2; b=--a; a=1; b=1; 后置递减 a=2; b=a--; a=1; b=2;
返回值为假值或真值
返回值为假值或真值
单行if语句 if(...){ } 多行if语句 if(...){ } else{ } 多条件if语句 if(...){ } else if(...){ } ... else{ } 嵌套if语句 if(...){ if(...){ } ... else{ } } ...
运算符返回的是变量
int a = 10; int b = 20; (a > b ? a : b) = 100; a=10 b=100
switch(){ case 结果1: 执行语句; break; case 结果2: 执行语句; break; ... default: 执行语句; break; }
判断只能是整型或是字符型
while(循环条件){ }
猜数: #include<ctime> srand((unsigned int)time(NULL)); //随机数种子,防止每次一样 int num = rand() % 100 + 1; //随机生成1~100的数 int num1 = 0; //输入猜测的数 int num2 = 0; //猜测次数 cout << "猜测" << endl; while (num2<5) { num2++; cin >> num1; if (num1>num) { cout << "过大" << endl; } else if(num1<num){ cout << "过小" << endl; } else { cout << "恭喜" << endl; break; } } cout << "未猜中" << endl;
do{ } while(...);
//水仙花数 int num = 100; int num_1 = 0; int num_2 = 0; int num_3 = 0; do{ //取个位 num_1 = num % 10; //取十位 num_2 = (num / 10) % 10; //取百位 num_3 = num / 100; if(num_1*num_1*num_1+num_2*num_2*num_2+num_3* num_3*num_3 == num ) { cout << "水仙花数:" << num << endl; } num++; } while (num < 1000);
//敲桌子 1~100的数字 含7和7的倍数 int num_1 = 0; int num_2 = 0; int num_3 = 0; for (int num = 1;num < 101; num++) { //个位有7 num_1 = num % 10; //十位有7 num_2 = num / 10; //7的倍数 num_3 = num % 7; if (num_1==7 || num_2==7 || num_3==0 ) { cout << "敲桌子" << endl; } else { cout << num << endl; } }
//乘法表 for (int i = 1; i < 10; i++) { for (int j = 1; j <= i; j++) { cout << j << "*" << i << "=" << i * j <<"\t"; } cout << endl; }
for(...){ if(...){ break; } }
for(...){ if(...){ continue; } }
... goto FLAG; ... FLAG: ...
int arr[5]; int arr[5] = {1,2,3,4,5}; int arr[] = {1,2,3,4,5,6,7,8,9};
sizeof(arr)
//称重 int arr[] = { 300,350,200,400,250 }; int max = 0; for (int i = 0; i < 5; i++) { if (arr[i] > max) { max = arr[i]; } } cout << "最重: " << max << endl;
//数组倒置 int arr[10] = { 0,1,2,3,4,5,6,7,8,9 }; int num = sizeof(arr) / sizeof(arr[0]);//数组长度 int start = 0;//数组起始脚标 int end = sizeof(arr) / sizeof(arr[0]) - 1;//数组末位脚标 int temp = 0;//临时存储 for (int i=0; i<num; i++) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; if (start >= end) { break; } } for (int j = 0; j < num; j++) { cout << arr[j] << endl; }
//冒泡排序 int arr[] = { 4,2,8,0,5,7,1,3,9,6,3 }; int num = sizeof(arr) / sizeof(arr[0]); int temp = 0; for (int i = 0; i < num-1; i++){ for (int j = 0; j < num-i-1; j++){ if (arr[j] > arr[j + 1]){ temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int k = 0; k < num; k++) { cout << arr[k] << endl; }
int arr[2][3]; int arr[2][3]={{1,2,3},{4,5,6}}; int arr[2][3]={1,2,3,4,5,6}; int arr[][3]={1,2,3,4,5,6};
二维数组的行数: sizeof(arr)/sizeof(arr[0]) 二维数组的列数: sizeof(arr[0])/sizeof(arr[0][0])
//成绩统计 int scores[3][3] = { {100,100,100}, {90,50,100}, {60,70,80} }; string names[3] = { "张三","李四","王五" }; for (int i = 0; i < 3; i++) { int sum = 0; for (int j = 0; j < 3; j++) { sum += scores[i][j]; } cout << names[i] << "的总成绩: " <<sum<< endl; }
int add(int num_1,num_2){ int sum = num_1 + num_2; return sum; }
int a=10; int b=20; int c = add(a,b); 定义函数的num_1,num_2为形参 调用函数的a,b为实参
如果函数不需要返回值,声明时可以写void //换值函数 void swap(int num_1,num_2){ int temp = num_1; num_1 = num_2; num_2 = temp; return; } int a=10; int b=20; swap(a,b); 值传递中,实参的值不会发生变化,只影响形参
1.无参无返 void test1(){ cout << "..." << endl; } test1(); 2.有参无返 void test1(int a){ cout << "..." << endl; return; } test2(10000); 3.无参有返 int test3(){ cout << "..." << endl; return 10000; } int num1=test3(); 4.有参有返 int test4(int a){ cout << "..." << endl; return a; } int num2=test4(10000);
函数声明:swap.h #include<iostream> using namespace std; void swap(int num_1, int num_2); 函数定义:swap.cpp #include"swap.h" void swap(int num_1, int num_2) { int temp = num_1; num_1 = num_2; num_2 = temp; cout << "a=" << num_1 << endl; cout << "b=" << num_2 << endl; } 函数调用:main() int a = 10, b = 20; swap(a, b);
下篇:
int a = 10; 1、指针的定义:数据类型 * 变量名 int * p; p = &a; //指针指向变量a的地址 2、指针的使用:通过 * 进行解引用,找到指针指向地址中的数据 cout << " *p = " << *p << endl;
指针所占内存:32位操作系统4位,64位操作系统8位。
1、空指针:指针初始化 int * p = NULL;
2、野指针:尽量避免 int * p = (int *)0x1100;
1、常量指针:指针的指向可以修改,但指向的值不可以修改 const int * p = &a; *p = 100; (×) p = &b; (√) 2、指针常量:指针的指向的值可以修改,但指向不可以修改 int * const p = &a; *p = 100; (√) p = &b; (×) 3、既修饰指针,又修饰常量:指针指向及其指向的值都不可以修改 const int * const p = &a; *p = 100; (×) p = &b; (×)
//指针访问数组 int arr[10] = { 0,1,2,3,4,5,6,7,8,9 }; int * p = arr; for (int i=0; i < 10; i++){ cout << *p << endl; p++; }
****值传递不改变实参值,地址传递可以修改实参的值**** void swap1(int * p1 , int * p2) { int temp = *p1; *p1 = *p2; *p2 = temp; } main(){ ... int a = 10; int b = 20; swap1(&a, &b); cout << a << endl; cout << b << endl; ... }
//指针、数组、函数的冒泡排序 void maopao(int * adr,int num) { int temp = 0; for (int i = 0; i < num - 1; i++) { //存下数组首地址 int * p = adr; for (int j = 0; j < num - i - 1; j++) { if (*p > *(p+1)) { temp = *p; *p = *(p+1); *(p+1) = temp; } p++; } } } main(){ ... int arr[] = { 4,2,8,0,5,7,10,1,3,9,6 }; int num = sizeof(arr) / sizeof(arr[0]); //传入数组的首地址和长度 maopao(&arr[0], num); for (int k = 0; k < num; k++) { cout << arr[k] << endl; } ... }