目录
必考算法
求和
均值
逆序
最大/最小值——打擂法
排序——冒泡/选择法
查找——顺序/折:有序
插入——边找边插
删除
常用算法:
水仙花数
迭代 :Fibonacci 数列
回文:整数各位分离,逆序
素数:设置counter,并清 0 ,满足条件即增 1
取余算法
逆序算法:
反转算法:
c++无string类型,只提供了string类(不是基本数据类)
变量命名规则 首字母不能是数字
char不能赋值汉字,因为一个汉字占两个字符
?:为三目运算
a>b ? a:b;
表示 若a>b取a,否则取b
2.6904e+124 是科学计数法
’\t’用于换行
‘ ’用于字符,“ ”用于字符串
表整除 1/2=0
5.0/9不等于0!
字符会自动转为acsii值计算
y=x++ //先给y赋值x再自增
y=++x //x先自增再给y赋值
‘ = ’ 从右向左执行:
|
|
|
|
| 冒泡排序函数 void sort(int a[5],int b,int c) { if(c==0) { for(int i=0;i<b-1;i++) { for(int j = 0;j<b-i-1;j++) { if(a[j]>a[j+1]) { int t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } }//升序
if(c==1){ for(int i=0;i<b-1;i++){ for(int j = 0;j<b-i-1;j++){ if(a[j]<a[j+1]){ int t=a[j]; a[j]=a[j+1]; a[j+1]=t; }}}}//降序
}
结果为1:
只能用&&表示和,不能直接按上面照片连表达大小
可以通过编译:
switch语句 记得加break
“a”内存里其实有\0
进制转换: 16进制转化为10进制 #include <iostream> #include <cstring> using namespace std; int trs(char q) { int num; if(q>='0'&&q<='9'){ num=q-'0'; } else num=q-'A'+10; return num; } int main() { int i=0; char hex[1000]; cin.getline(hex,1000); int len=strlen(hex); int dec=0; for(i=0;i<len;i++){ dec = dec*16 + trs(hex[i]); } cout<<dec<<endl; return 0; }
第四章 //数组与指针
只能这样初始化,因为a是常量值 方法三只初始化1后面的自动初始化为为234 但如果初始化0,后面也全自动为0
动态数组 #include<iostream> using namespace std; int main() { int n; cin >> n;//定义数组的长度 int * a = new int[n];//动态一维数组的定义方法 for (int i = 0; i < n; i++)//循环给数组赋值 { cin >> a[i]; } for (int i = 0; i < n; i++)//循环输出数组 { cout << a[i] << " "; } return 0; }
多次相加
循环取位
第一个数组为a[0]
include<cstdlib> a[i]=rand()%100 //100以内的随机数
排序用选择法,冒泡法 srand((time(0))
冒泡211 int a [2][3] 行号 列号 字符型变量
#include<cstring>
strcpy()复制
strcat(a,b)a连接b
strcmp(a,b)比较ascii大小
strlen()长度
t[i]!=“\0”
字符数组的输入:cin>>t; 若输入abcd则分别输到4个数组位置里(不能有空格) 可以用cin.getline(数组,长度)弥补cin不能用空格的缺陷
cin.get()用于cin和cin.getline同时使用时
a等价于a[0]
&a; 取a的地址(16进制) *a 取a的内容(值) *(p+9)=p[9] 野指针要小心,用指针变量一定要记得赋值
int*p = &i;这样赋值,或者int i ,*p=&i;
c是对的 int i ,*p; p = &i;
*p表地址方法 p=&p
引用:变量的别名 new 的用法
|
|
|
|
| ||
|
|
|
函数
原型声明语句(定义函数在mian前时可省略)
例 : int fac(int a);
形参(只能是变量)/实参(常量,地址,变量,表达式)
传引用法
形参在原型声明中可以不写名字仅指明类型
形参不可以被其它函数访问
内联函数inline:加快函数执行速度
重载函数:多个名字相同的函数只有参数个数和类型,(return类型不影响)不同
一个函数不能既是有默认值的函数又是重载函数
递归:函数自己调用自己
//第六章构造数据类型
(*p).id等于p->id
struct BOOK{
Int id;
double price;
int sale;
}
类: -> 对象:
构造函数 void
析构函数 void
”先进后出“
Carray::CArray (int n1){
n=n1;
Data= new int[n];
}
#include <iostream>
using namespace std;
class CArray{
public:
CArray(int = 5);
CArray(CArray&);
~CArray();
void setAt(int,int);//index, value
int getAt(int); //index
int len();
private:
int *data; // address
int n; // length
};
CArray::CArray(int n1){
n = n1;
data = new int[n];
for(int i = 0;i < n; i++){
*(data+i) = 0;
}
cout <<"In constructor: "<<n<<endl;
}
CArray::~CArray(){
delete []data;
cout <<"In destructor: " << n<<endl;
}
CArray::CArray(CArray &t){
n = t.n;
data = new int [n];
for (int i = 0;i < n; i++){
data[i] = t.data[i];
}
cout<<"In copy constructor"<<endl;
}
void CArray::setAt(int i, int value){
data[i] = value;
}
int CArray::getAt(int i){
return data[i];
}
int CArray::len(){
return n;
}
int main(){
CArray t(3);
CArray a = t;
/*
int value , i;
for(i = 0;i < t.len(); i++){
cout <<"Enter a value:";
cin>>value;
t.setAt(i, value);
}
int sum = 0;
for(i = 0;i < t.len(); i++){
sum += t.getAt(i);
}
cout <<sum<< endl;
// CArray x; */
return 0;
}
类和对象
类的 | |
类成员可以被定义为 public、private 或 protected。默认情况下是定义为 private。 | |
构造函数前无类型,构造函数的名称与类的名称是完全相同的 例: CPoint(); | |
类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。拷贝构造函数的最常见形式如下:
classname (const classname &obj) { p = new int; *p = *obj.p; // 拷贝值 } 调用: classname a=b;
| |
友元函数可以访问类的 private 和 protected 成员。 类的友元函数是定义在类外部,但有权访问类的所有私有(private)成员和保护(protected)成员。尽管友元函数的原型有在类的定义中出现过,但是友元函数并不是成员函数。
| |
通过内联函数,编译器试图在调用函数的地方扩展函数体中的代码。 | |
每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数。因此,在成员函数内部,它可以用来指向调用对象。 友元函数没有 this 指针 | |
指向类的指针方式如同指向结构的指针。实际上,类可以看成是一个带有函数的结构。 | |
使用 static 关键字来把类成员定义为静态的 我们不能把静态成员的初始化放置在类的定义中,但是可以在类的外部通过使用范围解析运算符 :: 来重新声明静态变量从而对它进行初始化
|
--------------------------------------------------------------------------------------------------------
在调用private前往不要重定义
private:
int m;
int year;
public:
CTimeDeposit(int m1,int year1)
{
int m=m1;
- int year=year1; 这个int是错的,不能加
};
--------------------------------------------------------------------------------------------------------
private :
int *p;
int num1;
};
CArray::CArray(int num1){
p=new int[num1]; 注意p的类型只能在private定义,在函数内再次定义会引起溢出!
zero(num1);
};
---------------------------------------------------------------------------------------------------------
友元函数示例
class CCircle{
private:
int x;
int y;
double radious;
public:
CCircle(int x1,int y1,double radious1){
x=x1;
y=y1;
radious=radious1;
};
friend int isIntersected(CCircle c1,CCircle c2);
};
int isIntersected(CCircle c1,CCircle c2){
int b=0;
if(c1.x*c1.x+c1.y*c1.y+c2.x*c2.x+c2.y*c2.y<c1.radious+c2.radious)
b=1;
return b;
};
---------------------------------------------------------------------------------
类模板示例
#include <iostream>
using namespace std;
template < class Type >
class CComplex{
private :
Type real;
Type unreal;
public :
CComplex(Type real1,Type unreal1){
real=real1;
unreal=unreal1;
};
Type getReal(){
return real;
};
Type getImg(){
return unreal;
};
void print (){
cout<<real<<'+'<<unreal<< "i"<<endl;
};
};
//主程序
int main()
{
//定义一个实部虚部为整数的复数类对象并输出
CComplex <int> a(1,2);
cout<<a.getReal() << "+" << a.getImg() << "i" <<endl;
//定义一个实部虚部为实数的复数类对象并输出虚部
CComplex <double> b(1.2,3.4);
b.print();
//我们甚至可以定义一个实部虚部为 char 的复数类,当然,这在数学上是无意义的
CComplex <char> c('a','b');
c.print();
return 0;
}
运算符重载
类名 operator+(const Box&);
双目:一个形参
return *this
i++ 后增 / 先增 :是否有int
友元函数:双目需要两个参数
不能重载的运算符 共五个:
- .成员访问运算符
- .*, ->*成员指针访问运算符
- ::域运算符
- sizeof长度运算符
- ?:条件运算符
- # 预处理符号
---------------------------------------------------------------------------------
一元运算符重载示例
#include <iostream>
using namespace std;
//此处编写CPoint类函数的定义
class CPoint{
private :
int x;
int y;
public:
CPoint(int x1,int y1){
x=x1;
y=y1;
};
int getX(){
return x;
};
int getY(){
return y;
};
CPoint operator ++() {
++x;
++y;
return *this;
}
CPoint operator ++(int) {
CPoint temp =*this;
++*this;
return temp;
}
};
//主函数
int main()
{
int x, y;
cout <<"Enter x and y of Point: ";
cin >> x >> y;
CPoint p1(x, y);
CPoint p(0, 0);
p = p1++;
cout <<"x and y of the new point is: ";
cout << p.getX() << "," << p.getY() << endl;//输出后增的结果
p = ++p1;
cout <<"x and y of the new point is: ";
cout << p.getX() << "," << p.getY() << endl; //输出先增的结果
return 0;
}
---------------------------------------------------------------------------------
鉴定
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
private:
int x;
int y;
public:
CPoint(int x1=0,int y1=0)
{
x=x1;
y=y1;
};
int getx()
{
return x;
};
int gety()
{
return y;
};
CPoint operator -( CPoint &b)
{
CPoint c;
c.x =this->x-b.x;
c.y =this->y-b.y;
return c;
};
};
class CCircle
{
private:
int radius;
int x;
int y;
public:
CCircle(CPoint p,int radius1)
{
x=p.getx();
y=p.gety();
radius=radius1;
};
int operator &(CCircle &c1){
CCircle c2=*this;
int b=0;
if((c1.x-c2.x)*(c1.x-c2.x)+(c1.y-c2.y)*(c1.y-c2.y)<=c1.radius*c1.radius+c2.radius*c2.radius)
b=1;
return b;
};
};
int main()
{
//生成三个测试圆
int x,y,r;
cout <<"Enter x,y,r of the 1st circle: ";
cin >> x >> y >> r;
CCircle c1(CPoint(x, y), r); //创建第一个圆类对象
cout <<"Enter x,y,r of the 2nd circle: ";
cin >> x >> y >> r;
CCircle c2(CPoint(3,1),1); //创建第二个圆类对象
cout <<"Enter x,y,r of the 3rd circle: ";
cin >> x >> y >> r;
CCircle c3(CPoint(8,8),2); //创建第三个圆类对象
int intersect = c1 & c2;
if(intersect == 1) //c1和c2是否相交?
cout << "c1 and c2 is interseted"<< endl;
else
cout << "c1 and c2 is not interseted" << endl;
intersect = c1 & c3;//c1和c3是否相交?
if(intersect == 1)
cout << "c1 and c3 is interseted"<< endl;
else
cout << "c1 and c3 is not interseted" << endl;
return 0;
}
---------------------------------------------------------------------------------
继承和派生
已有的类称为基类,新建的类称为派生类
---------------------------------------------------------------------------------
// 基类
Class Animal {
// eat() 函数
// sleep() 函数
};
//派生类
Class Dog :public Animal {
// bark() 函数
};
---------------------------------------------------------------------------------
三种继承方式(降级):public private protected 类的外部只能用public
基类成员 比较 b.CA::x 与 b.x
重名时用:this->
派生类至少有一个基类
派生类不能继承构造函数和析构函数
不写类型默认是 private
派生类 先人后己执行
基类与派生类的类型转换:赋值,指针,引用三种方式
同一个类 | yes | yes | yes |
派生类 | yes | yes | no |
外部的类 | yes | no | no |
pub protected private
---------------------------------------------------------------------------------
#include <iostream>
#include <cmath>
using namespace std;
//此处编写CRectangle和CSquare类的定义
class CRectangle{
protected:
int width;
int height;
public:
CRectangle(int width1=0,int height1=0){此处//必须赋初值,否则报错
width=width1;
height=height1;
};
int getPerimeter(){
int l;
l=2*(width+height);
return l;
};
int getArea(){
int s;
s=width*height;
return s;
};
};
class CSquare:public CRectangle{
public:
CSquare(int width1){
width=width1;
height = width1;
};
};
//主函数
int main()
{
//test class CRectangle
int w, h;
cout <<"please enter w and h:";
cin>>w>>h;
CRectangle rect(w, h);
cout <<"the perimeter of Rect is "<< rect.getPerimeter () << endl;
//test for class CSquare
cout<<"please enter side:";
cin>>w;
CSquare sq(w);
cout << "the area of the Square is "<<sq.getArea() << endl;
//is the CSquare inherited from CRenctangle ?
CRectangle *root = &sq;
cout << root->getArea() << endl;
return 0;
}
---------------------------------------------------------------------------------
多态性与虚函数
Virtual void run()
纯虚函数?
#include <iostream>
using namespace std;
class CShape{
public:
CShape (double x) { r = x; }
virtual double getArea()=0;
protected:
double r;
};
//此处编写CCricle类和CCylinder类
class CCircle:public CShape{
public:
CCircle (double x):CShape(x){ r = x;};//为什么这样写?
virtual double getArea(){
return 3.14*r*r;
};
};
class CCylinder:public CShape{
private:
double h;
public:
CCylinder (double x,double hh):CShape(x){ r = x;h=hh; };
virtual double getArea(){
return 2*3.14*r*r+2*3.14*r*h;
};
};
//主程序
int main(){
double r, h;
cout <<"Enter r and h: ";
cin >> r >> h;
CShape *p1, *p2; //创建CSape类指针变量
CCircle c(r); //创建CCircle类对象
CCylinder y(r, h); //创建CCylinder对象
p1 = &c;
p2 = &y;
cout << "Area: "<<p1->getArea() << endl;
cout <<"Area: "<<p2->getArea() << endl;
return 0;
}