目录
-
一、输入输出
scanf与printf的回顾练习:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
int lovely;
double dinner;
scanf("%d,%lf",&lovely,&dinner);//必须按照 2,2.5 的格式输入
printf("%d lovely people can eat %lf dinner\n", lovely, dinner); //&为取址符
scanf("%d-%lf", &lovely, &dinner);//必须按照 2-2.5 的格式输入
printf("%d lovely people can eat %lf dinner\n", lovely, dinner);
system("pause");
return 0;
}
关于强制数据类型转换的回顾练习:
(float)只对紧挨着变量或括号起作用:
明白下列表达式的不同,你就得到了精髓
#include<iostream>
using namespace std;
int main()
{
int a = 5.2;
cout << "(3 - 2) / (5 - 2)=";
cout << (3 - 2) / (5 - 2) << endl;
cout << "(float)((3 - 2) / (5 - 2))=";
cout << (float)((3 - 2) / (5 - 2)) << endl;
cout << "(float)(3 - 2) / (float)(int a=5.2 - 2)=";
cout << (float)(3 - 2) / (float)(a - 2) << endl;
cout << "(float)(3 - 2) / (5 - 2)=";
cout << (float)(3 - 2) / (a - 2) << endl;
cout << "(float)(3 - 2) / (float)(5 - 2)=";
cout << (float)(3 - 2) / (float)(5 - 2) << endl;
system("pause");
return 0;
}
-
二、逻辑运算
或与非的回顾练习:
!(非)→ &&(与)→ ||(或)
如下为一次试验,自己动手,丰衣足食,尝试一下不同的组合:
cout << (4 && 0 || -1) << endl;// 结果为1
条件运算符的回顾练习:
格式:(条件)?(条件为真的输出) : (条件为假的输出)
cout << ((a>b)?a:b) << endl;// 结果为max=11
int x=0;
cout << (x ? 'a' : 'b') << endl;// 结果为b·
cout << ((a>b)?a:b) << endl;// 结果为max=11
关于 或与非 if判断 函数声明-定义-调用 的回顾练习:
两个数比较大小,输出大数
#include<stdio.h>
#include<iostream>
using namespace std;
double max(double a, double b);
int main()
{
double x,y,m;
cin >> x>>y;
m = max(x, y);
cout << "max number=" << m<<endl;
system("pause");
return 0;
}
double max(double a, double b)
{
if (a > b) return a;
else if (a < b || a==b) return b; //逻辑或
}
-
三、基本数据类型
关于赋值表达式:
"=" 的运算顺序与 "+-*/" 不一致,为先右后左:
int a, b, c;
a = b = c = 5; //(赋值表达式值为 5,a,b,c 值均为 5)
cout << a << " " << b << " " << c << endl;
a = 5 + (c = 6); //(表达式值为 11,a 值为 11,c 值为 6)
cout << a << " " << b << " " << c << endl;
a = (b = 4) + (c = 6); //(表达式值为 10,a 值为 10,b 等于 4,c 等于 6)
cout << a << " " << b << " " << c << endl;
a = (b = 10) / (c = 2); //(表达式值为 5,a 等于 5,b 等于 10,c 等于 2)
cout << a << " " << b << " " << c << endl;
//赋值表达式也可以包含复合的赋值运算符。如
a += a -= a * a; //也是一个赋值表达式。如果 a 的初值为 5,此赋值表达式的求解步骤如下:
//先进行“a -= a * a”的运算,它相当于 a = a - a * a = -20
//再进行“a += -20”的运算,它相当于 a = a + (-20) = -40
cout << a << " " << b << " " << c << endl;
关于char的 输入格式 回顾练习:
字符串以'\0'结尾,采用ascⅡ码存储,
以及,输入回车、单引号等特殊字符的方法如下:
#include<iostream>
using namespace std;
int main()
{
char a='\\',b='\'',c='\"';
cout << a << " " << b << " " << c << endl << endl;
char d = 'a', e = 33;
char f[] = "\\\"\33\0!";
cout << d << " " << e << " " << f << endl << endl; //\33之后全部不输出
system("pause");
return 0;
}
(11条消息) C语言之 字符串长度的计算方法_CFY的博客-CSDN博客_字符串长度
关于 常用字符串函数 的回顾练习:
字符串比大小、复制、连接、求字符串长度:
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char a[30] = "LaiXingYu", b[] = "LiuPeiWen", c[] = "LaiXingYu";
int n = 3;
//按照ascⅡ比较字符串,从左到右比较,字母靠前的小
cout << strcmp(a, b) << endl;//-1
cout << strcmp(a, c) << endl;// 0
cout << strcmp(b, a) << endl;// 1
cout << strcpy(a, b) << endl;//把b复制到a,返回a
cout << strncpy(a, c, n) << endl;//把b的前n项复制到a,返回a
cout << strcat(a, b) << endl;//把b连接到a,返回a
cout << strlen(a) << endl;//返回字符串长度
system("pause");
return 0;
}
关于 枚举类型 的回顾练习:
用奇怪的表达方式(Monday,Tuesday,...)代替默认首项为0,差为1的等差数列:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
enum week { Monday=1, Tuseday, Wednesday, Thursday, Friday, Saturday, Sunday };
//枚举变量默认输出值为从0开始的整数数组,进行Monday=1初始化后,后面元素自行加1
week weekend_2 = Sunday, weekend_1 = Saturday;
week workday[] = { Monday, Tuseday, Wednesday, Thursday, Friday };
cout << "weekend: " << weekend_1 << " " << weekend_2 << endl;
cout << "workday: ";
for (int i = Monday; i <= Friday; i++)
{
cout << workday[i-1] << " ";
}
cout << endl;
week Happy;
int a;
cout << "Which day is your favourite day?\n";
cin >> a;
Happy = (week)a;
cout << Happy;
return 0;
}
-
四、函数
递归法 实现斐波那契数列的回顾练习:
#include<iostream>
using namespace std;
int feb(int i)
{
if (i == 1)
{return 0;}
else if (i == 2)
{return 1;}
else
{
return feb(i - 1) + feb(i - 2); //递归
}
}
int sum(int n)
{
int i,S=0;
for (i = 1; i <= n; i++)
{
cout<<feb(i)<<" ";
S = S + feb(i);
}
return S;
}
int main()
{
int n;
cout << "输入斐波那契数列前几项?" << endl;
cin >> n;
cout << endl << "前" << n << "项和 S=" << sum(n) << endl;
system("pause");
return 0;
}
关于全局变量与函数声明定义的回顾练习:
#include<iostream>
#include<string>
using namespace std;
int a = 3;
int sum(int x);
int main()
{
int b=123;
cout << a - b << endl;
cout << sum(b) << endl;
system("pause");
return 0;
}
int sum(int b)
{
return a + b;
}
关于 函数重载 的回顾练习:
函数名称一致,但变量类型或个数不同,即为函数重载
乘方函数power:
#include<iostream>
using namespace std;
int power(int x); //x^2
int power(int x, int a); //x^a
double power(double x); //x^2
double power(double x, int a); //x^a
int main()
{
int b, n; double c;
cout << "int: "; cin >> b;
cout << power(b) << endl;
cout << "int: "; cin >> b;
cout << "int: "; cin >> n;
cout << power(b,n) << endl;
cout << "double: "; cin >> c;
cout << power(c) << endl;
cout << "double: "; cin >> c;
cout << "int: "; cin >> n;
cout << power(c, n) << endl;
system("pause");
return 0;
}
int power(int x)
{
return x * x;
}
double power(double x)
{
return x * x;
}
int power(int x, int a)
{
int result=1;
if (a > 0)
{
for (int i = 1; i <= a; i++)
{
result = result * x;
}
}
else if (a < 0)
{
for (int i = 1; i <= -a; i++)
{
result = result * x;
}
result = 1 / result;
}
return result;
}
double power(double x, int a)
{
double result = 1;
if (a > 0)
{
for (int i = 1; i <= a; i++)
{
result = result * x;
}
}
else if (a < 0)
{
for (int i = 1; i <= -a; i++)
{
result = result * x;
}
result = 1 / result;
}
return result;
}
关于 默认参数 的回顾练习:
只在 函数声明 时指定形式参数默认值
规则:指定默认值的参数必须放在形参表列中的 最右端,
一个函数不能既作为重载函数,又作为有默认参数的函数
乘方函数power:
#include<iostream>
using namespace std;
int power(int x, int a=2); //x^a
double power(double x, int a=2); //x^a
int main()
{
int b, n; double c;
cout << "int: "; cin >> b;
cout << power(b) << endl;
cout << "int: "; cin >> b;
cout << "int: "; cin >> n;
cout << power(b,n) << endl;
cout << "double: "; cin >> c;
cout << power(c) << endl;
cout << "double: "; cin >> c;
cout << "int: "; cin >> n;
cout << power(c, n) << endl;
system("pause");
return 0;
}
int power(int x, int a)
{
int result=1;
if (a > 0)
{
for (int i = 1; i <= a; i++)
{
result = result * x;
}
}
else if (a < 0)
{
for (int i = 1; i <= -a; i++)
{
result = result * x;
}
result = 1 / result;
}
return result;
}
double power(double x, int a)
{
double result = 1;
if (a > 0)
{
for (int i = 1; i <= a; i++)
{
result = result * x;
}
}
else if (a < 0)
{
for (int i = 1; i <= -a; i++)
{
result = result * x;
}
result = 1 / result;
}
return result;
}
-
五、数组
声明:
数组大小必须固定,定义为常量:
double a[5];
定义:
a[0] = 10;
一维数组初始化(3种):
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int main()
{
double a[5] = { 1,2,3,4,5 };
double b[] = { 5,4,3,2,1 };
double c[5] = { 1,3,5 };
cout << sizeof(c) << " " << sizeof(c[0]) << endl;//40 8
for (int i = 0; i < 5; i++)
{
cout << c[i] << " ";//1 3 5 0 0
}
system("pause");
return 0;
}
数组与函数:
数组可将地址传递到函数,因此函数中对数组的操作会返回到主函数中:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
double aver(double a[],int N);
int main()
{
const int n = 100;
double a[n];
int i;
cout << "输入一组数,以\\0结尾" << endl; //\\为’\’
cin >> a[0];
for (i = 1; i < n; i++)
{
if (a[i - 1] != '\0')
{
cin >> a[i];
}
else break;
}
cout << aver(a, i - 1) << endl;
system("pause");
return 0;
}
double aver(double a[], int N)
{
double sum=0;
for (int j = 0; j < N; j++)
{
sum += a[j];
}
return sum / N;
}
int a[]还可以有另一种形式:int *a,两者等价。详见 后续 指针变量 的文章。
二维数组:
定义与存储:
输出每一行首地址:
double m[4][1] = { 0 };
cout << m << endl << m + 1 << endl << m + 2 << endl << m + 3 << endl;
初始化:
未初始化元素默认赋值为0:
int a[2][3] = { 1,2,3,4,5,6 };
int b[2][3] = { 1,2,3 };
int c[2][3] = { {1,2,3},{4,5,6} };
int d[2][3] = { {1,2},{3} };
引用:
引用可直接传递变量地址到函数,从而真正改变变量的值
本例中swap函数为引用写法
矩阵转置练习:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
const int MAX_M = 10;
const int MAX_N = 10;
void swap(double &a,double &b);//引用可直接传递地址,用来更改变量
//二维数组初始化列表时可以省略行的长度,但是不能省略列的长度
void transpose(double a[][MAX_N],int M,int N);
void output(double a[][MAX_N], int M, int N);
int main()
{
double matrix[MAX_M][MAX_N] = { 0 };
int m, n;
int i, j;
cout << "input the number of row and column:" << endl;
cin >> m >> n;
cout << "input the matrix:" << endl;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
}
transpose(matrix, m, n);
output(matrix, n, m);
system("pause");
return 0;
}
void swap(double& a, double& b)//实参 a[i][j] 和 形参 &a 共同使用同一个存储空间
{
double mid=a;
a = b;
b = mid;
}
void transpose(double a[][MAX_N], int M, int N)
{
int n = (M < N) ? M : N;
int i, j;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
swap(a[i][j], a[j][i]);//实参 a[i][j] 和 形参 &a 共同使用同一个存储空间
}
}
for (i = 0; i < n; i++)
for (j = n; j < ((M > N) ? M : N); j++)
{
swap(a[i][j], a[j][i]);
}
}
void output(double a[][MAX_N], int M, int N)
{
cout << "transpose matrix" << endl;
int i, j;
for (i = 0; i < M; i++)
{
for (j = 0; j < N; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
查找:
折半查找
排序:
冒泡排序 相邻元素比较交换
选择法
插入法
字符数组和字符串:
初始化:
比较不同类型数组输出内容:究竟是地址还是内容?请看如下代码
int a[3];
cout << sizeof(a) << endl;//4byte*3=12
double d[2];
cout << sizeof(d) << endl;//8byte*2=16
char ch[10] = { 'r','e','s','p','e','c','t' };
cout << ch[8] << endl;//ch[8]为空
cout << ch << " " << sizeof(ch) << endl;//respect 10byte*1=10
char ah[] = { "respect" };//等价于 ah[] = {'r','e','s','p','e','c','t','\0'};
cout << ah << " " << sizeof(ah) << endl;//respect (7+1)byte*1=8
char bh[] = "respect";
cout << bh << " " << sizeof(bh) << endl;//respect (7+1)byte*1=8
char diamond[5][5] = { {' ',' ','*'},{' ','*',' ','*'},{'*',' ',' ',' ','*'},{' ','*',' ','*'},{' ',' ','*'} };
cout << diamond << endl;//与一维字符数组不同,输出值为地址
输入:
三种常用输入方法:
char ch[8];
cin >> ch; //遇到空格或回车结束,且最多存放 7 个有效字符
cin.getline(s,8);//默认终止符为'\n'
cin.getline(s, 8, ' '); //终止符为’ ’
cout << s << endl; //遇到第一个空字符’\0’将认为字符串结束,停止输出。
数组的输出:
除一维字符数组外,都要利用循环结构输出:
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int arr[6] = { 1,2,3 };
cout << arr << "\n";//address
for (int i = 0; i < 6; i++)
{
cout << arr[i] << " ";
}
cout << "\n\n";
int a[][6] = { {1,2,3},{3},{1,0} };
cout << a << " " << a + 1 << "\n";//address
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 6; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
char ch[6] = "acc";
cout << ch << endl;//"acc"
cout << ch[0] << endl << endl;
char chh[][6] = { "()","\\","*&%@#" };
cout << chh << " " << chh + 1 << endl;//address
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 6; j++)
{
cout << chh[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
字符串与数值型的转换
函数定义在下述头文件中:
#include<cstdlib>
整数转换为字符串的函数:
针对itoa警报问题的解决方法:
1._itoa_s(n, a, 10);
2.在代码最前面加入两行
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
函数调用格式与用处如下:
itoa(n, a, 10); //int n → char a[]
ltoa(n, a, 10); //long int n → char a[]
ultoa(n, a, 10); //unsigned long int n → char a[]
浮点型转换为字符串的函数:
针对itoa警报问题的解决方法:
在代码最前面加入两行
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
函数调用格式与用处如下:
gcvt(d, 5, a); //double d → char a[] 前五位
字符串转换为数值的函数:
函数调用格式与用处如下:
d=atof(a);//char a[]=”3.14” →int d=3 保留整数
d=atoi(a);//char a[]=”3.14” →int d=3 保留整数
C 的通用函数实现数值与字符串的转换:
#define _CRT_NONSTDC_NO_DEPRECATE
利用C的 格式化输入输出函数 实现:
char sn[10];
sprintf(sn, "%d-%d-%d", 2016, 1, 5); //2016-1-5 → char sn[]
cout << sn << endl; //输出:2016-1-5
double f = -123.45;
char sf[20];
sprintf(sf, "%lf", f); // f=-123.450000 → sf[]
cout << sf << endl;
char a[] = "12.345";
sscanf(a, "%d", &n); //char a → int n 保留整数
sscanf(a, "%f", &f); //char a → float f 保留单精度
printf("Integer= %d\n", n); //输出结果:Integer=12
printf("Real= %f\n", f); //输出结果:Real=12.345000