|
|
@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
冒泡排序:
|
|
|
|
|
|
|
|
for(int i=0;i<len-1;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for(int j=0;j<len-i-1;j++)
|
|
|
|
|
|
|
|
{ if (arr[j]>arr[j+1])
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int temp = arr[j];
|
|
|
|
|
|
|
|
arr[j]=arr[j+1];
|
|
|
|
|
|
|
|
arr[j+1]=temp;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
函数高级
|
|
|
|
|
|
|
|
函数的默认参数:
|
|
|
|
|
|
|
|
1如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须要有默认值
|
|
|
|
|
|
|
|
2如果函数声明有默认值,函数实现的时候就不能有默认参数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
函数的占位参数:
|
|
|
|
|
|
|
|
语法:返回值类型 函数名(数据类型)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
结构体作为函数参数:
|
|
|
|
|
|
|
|
将结构体作为函数作为参数向函数中传递
|
|
|
|
|
|
|
|
传递方式有两种:
|
|
|
|
|
|
|
|
值传递:形参不会改变实参
|
|
|
|
|
|
|
|
地址传递:会改变实参
|
|
|
|
|
|
|
|
总结:如果不想修改主函数中的数据,用值传递,反之用地址传递
|
|
|
|
|
|
|
|
结构体中const的使用场景:
|
|
|
|
|
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
1 不带头结点的头插入法创建链表:
|
|
|
|
|
|
|
|
LinkList LinkListCreate(const int n)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
LinkList head;
|
|
|
|
|
|
|
|
Node *p;
|
|
|
|
|
|
|
|
head = NULL;
|
|
|
|
|
|
|
|
for(;i<n;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
p = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
if(NULL == p)
|
|
|
|
|
|
|
|
perror("ERROR");
|
|
|
|
|
|
|
|
scanf("%d",&p->data);
|
|
|
|
|
|
|
|
p->next = head;
|
|
|
|
|
|
|
|
head = p;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
不带头结点的尾插入法创建链表:
|
|
|
|
|
|
|
|
LinkList LinkListCreate(const int n)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int n= 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建一个一直指向头结点,一直指向尾结点的指针。
|
|
|
|
|
|
|
|
LinkList head;
|
|
|
|
|
|
|
|
Node *p,*rear;
|
|
|
|
|
|
|
|
rear = head = NULL;
|
|
|
|
|
|
|
|
for(;i<n;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
p = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
scanf("%d",&p->data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(NULL == head) head=p;//创建的第一个结点。
|
|
|
|
|
|
|
|
else rear->next = p;//链表非空
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rear = p;//移动尾指针
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rear->next = NULL;//最后将链表的结尾设置为NULL。
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
------------------------------------------------------------
|
|
|
|
|
|
|
|
3 创建带结点的头插入链表。
|
|
|
|
|
|
|
|
1 LinkList LinkListCreate(const int n)
|
|
|
|
|
|
|
|
2 {
|
|
|
|
|
|
|
|
3 int i = 0;LinkList p;
|
|
|
|
|
|
|
|
4 LinkList head = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
5 head->next = NULL;
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7 for(;i<n;i++)
|
|
|
|
|
|
|
|
8 {
|
|
|
|
|
|
|
|
9 p = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
10 scanf("%d",&p->data);
|
|
|
|
|
|
|
|
11 p->next = head->next;
|
|
|
|
|
|
|
|
12 head->next = p;
|
|
|
|
|
|
|
|
13 }
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15 return head;
|
|
|
|
|
|
|
|
16 }
|
|
|
|
|
|
|
|
-----------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 带头结点的尾插入法创建链表。
|
|
|
|
|
|
|
|
LinkList LinkListCreate(const int n)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int i = 0;Node *p,*rear;
|
|
|
|
|
|
|
|
LinkList head = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
rear = head;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(;i<n;i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
p = (Node*)malloc(sizeof(Node));
|
|
|
|
|
|
|
|
scanf("%d",&p->data);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rear->next = p; //尾指针 指向新建结点
|
|
|
|
|
|
|
|
rear = p;//rear指针移动
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
rear->next = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return head;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector容器
|
|
|
|
|
|
|
|
头文件<vector>
|
|
|
|
|
|
|
|
向vector中插入数据
|
|
|
|
|
|
|
|
v.push_back( );
|
|
|
|
|
|
|
|
通过迭代器访问容器中的数据
|
|
|
|
|
|
|
|
vector<数据类型>::iterator itBrgin = v.brgin();其实迭代器指向容器中第一个元素
|
|
|
|
|
|
|
|
vector<数据类型>::iterator itEnd = v.end();结束迭代器指向容器中最后一个元素的下一个位置
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
STL提供的for_each(起始迭代器,结束迭代器,函数名)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector容器中存放自定义数据类型
|
|
|
|
|
|
|
|
class person
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
数据
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<person>
|
|
|
|
|
|
|
|
vector<person>v1;
|
|
|
|
|
|
|
|
vector<person>v2;
|
|
|
|
|
|
|
|
vector<person>v3;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string构造函数
|
|
|
|
|
|
|
|
string() 创建一个空的字符串 例如:string str
|
|
|
|
|
|
|
|
string(const char* s); 使用字符串s初始化
|
|
|
|
|
|
|
|
string(const string& str) 使用一个string对象初始化另一个stringduixiang
|
|
|
|
|
|
|
|
string(int n,char c) 使用n个字符c初始化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
纯虚函数:virtual 函数类型 函数名 ( )= 0;
|
|
|
|
|
|
|
|
纯虚函数
|
|
|
|
|
|
|
|
类中只要有一个纯虚函数就是抽象类
|
|
|
|
|
|
|
|
抽象类不能创建对象
|
|
|
|
|
|
|
|
抽象类的子类要重写抽象类中的纯虚函数才能创建对象,否则他也是属于抽象类
|
|
|
|
|
|
|
|
虚析构和纯虚析构
|
|
|
|
|
|
|
|
共性:
|
|
|
|
|
|
|
|
可以解决父类指针释放子类对象
|
|
|
|
|
|
|
|
都要有具体的函数实现
|
|
|
|
|
|
|
|
区别:
|
|
|
|
|
|
|
|
纯虚析构无法构建对象
|
|
|
|
|
|
|
|
纯虚析构需要声明(在类内)也需要实现(类外 )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
一,操作符重载
|
|
|
|
|
|
|
|
实质是利用已有的c++中的关键字operator(操作符)来重写 操作符
|
|
|
|
|
|
|
|
1.成员函数重载加号
|
|
|
|
|
|
|
|
本质调用:
|
|
|
|
|
|
|
|
Person p3 =p1.operator+(p2)
|
|
|
|
|
|
|
|
2.全局函数重载加号
|
|
|
|
|
|
|
|
数据类型 operator+(数据类型 &p1,数据类型 &p2)
|
|
|
|
|
|
|
|
本质调用:Person p3 = p1+p2
|
|
|
|
|
|
|
|
运算符重载也可以发生函数重载
|
|
|
|
|
|
|
|
重载的操作符相同 传入的参数不同
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
二,左移运算符重载
|
|
|
|
|
|
|
|
成员函数重载
|
|
|
|
|
|
|
|
数据类型 operator<<(cout) 简化形式 p<<cout
|
|
|
|
|
|
|
|
通常不使用成员函数重载,因为无法实现cout在左边
|
|
|
|
|
|
|
|
全局函数重载
|
|
|
|
|
|
|
|
ostream& operator<<(ostream & cout,自定义数据类型 p) 简化形式cout<<p
|
|
|
|
|
|
|
|
可以直接输出一个自定义的数据类型
|
|
|
|
|
|
|
|
经常与友元配合使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
三,重载++运算符
|
|
|
|
|
|
|
|
前置
|
|
|
|
|
|
|
|
后置(有占位参数)
|
|
|
|
|
|
|
|
用占位参数来区分
|
|
|
|
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
函数模板
|
|
|
|
|
|
|
|
-c++另一种编程思想称为泛型编程,主要利用的技术就是模板
|
|
|
|
|
|
|
|
-c++提供两种模板机制:函数模板和类模板
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
函数模板语法
|
|
|
|
|
|
|
|
函数模板作用:
|
|
|
|
|
|
|
|
建立一个通用函数,其函数返回值类型和形参类型不具体指定,用一个虚拟的类型来代表
|
|
|
|
|
|
|
|
语法:template<typename T>
|
|
|
|
|
|
|
|
template ---声明创建模板 typename ---表明其后面的符号是一种数据类型,可以 用class代替
|
|
|
|
|
|
|
|
T---通用的数据类型,名称可以替换,通常为大写字母
|
|
|
|
|
|
|
|
两种方式使用函数模板
|
|
|
|
|
|
|
|
1.自动类型推导
|
|
|
|
|
|
|
|
2.显示指定类型
|
|
|
|
|
|
|
|
函数名<例如:int >()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-函数名模板关键字template
|
|
|
|
|
|
|
|
-使用函数模板有两种方式:自动类型推导、显式指定类型
|
|
|
|
|
|
|
|
-模板的目的是为了提高复用性,将类型参数化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
函数模板注意事项
|
|
|
|
|
|
|
|
1.自动类型推导,必须推导出一致的数据类型T才可以使用
|
|
|
|
|
|
|
|
2.模板必须要确定出T的数据类型,才可以使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
普通函数和函数模板的区别
|
|
|
|
|
|
|
|
1、普通函数调用可以发生隐式类型转换
|
|
|
|
|
|
|
|
2、函数模板 用自动类型推导,不可以发生隐式类型转换
|
|
|
|
|
|
|
|
3、 用显示指定类型,可以
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
总结:建议使用指定类型的方式,调用函数模板,因为可以自己确定通用类型T
|
|
|
|
|
|
|
|
------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
文件操作
|
|
|
|
|
|
|
|
程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放
|
|
|
|
|
|
|
|
通过文件可以将数据持久化
|
|
|
|
|
|
|
|
c++中对文件操作需要包含头文件<fstream>
|
|
|
|
|
|
|
|
文件类型分为两种
|
|
|
|
|
|
|
|
1文本文件 -文件以文本的ASCII码形式存储在计算机中
|
|
|
|
|
|
|
|
2二进制文件 -文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂他们
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
操作文件的三大类:
|
|
|
|
|
|
|
|
1ofstream:写操作
|
|
|
|
|
|
|
|
2ifstream:读操作
|
|
|
|
|
|
|
|
3fstream:读写操作
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
文本文件 写文件
|
|
|
|
|
|
|
|
步骤如下
|
|
|
|
|
|
|
|
1包含头文件#include<fstream>
|
|
|
|
|
|
|
|
2创建流对象 ofstream ofs;
|
|
|
|
|
|
|
|
3打开文件 ofs.open("文件路径",打开方式)
|
|
|
|
|
|
|
|
4写数据 ofs<<"写入的数据"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-读文件可以利用ifstream,或者fstream
|
|
|
|
|
|
|
|
-利用is_open函数可以判断文件是否打开成功
|
|
|
|
|
|
|
|
-close关闭文件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
二进制文件 写文件
|
|
|
|
|
|
|
|
1包含头文件#include<fstream>
|
|
|
|
|
|
|
|
2创建流对象ofstream ofs("文件路径",ios::out|ios::binary)
|
|
|
|
|
|
|
|
3打开文件ofs.open("person.txt",ios::out|ios::binary)
|
|
|
|
|
|
|
|
4写文件 代码
|
|
|
|
|
|
|
|
5关闭文件 ofs.close()
|
|
|
|
|
|
|
|
二进制文件 读文件
|
|
|
|
|
|
|
|
1包含头文件
|
|
|
|
|
|
|
|
2创建流对象
|
|
|
|
|
|
|
|
3打开文件 并判断文件是否打开成功
|
|
|
|
|
|
|
|
ifs.open("文件路径",ios::in|ios::binary)
|
|
|
|
|
|
|
|
if(!ifs.open_()){cout<<"文件打开失败"<<endl;
|
|
|
|
|
|
|
|
return;}
|
|
|
|
|
|
|
|
4读文件
|
|
|
|
|
|
|
|
Person p;
|
|
|
|
|
|
|
|
ifs.read((char* )&p,sizeof(Person))
|
|
|
|
|
|
|
|
cout<<吧啦吧啦。。。。。。<<endl;
|
|
|
|
|
|
|
|
5关闭文件 ifs.close
|
|
|
|
|
|
|
|
文件输入流对象,可以通过read函数,以二进制方式读数据
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clear;clc;close all
|
|
|
|
|
|
|
|
[N,T]=deal(12,210);
|
|
|
|
|
|
|
|
Dem = [39,36,38,40,37,33,40,39,33,37,43,34,30,39,42,36,35,38,36,35,41,38,36,36,48,34,35,39,38,36,40,40,40,34,39,40,30,36,40,34,36,37,41,36,41,41,38,29,43,33,31,40,42,42,30,40,35,36,38,33,35,37,41,43,35,42,37,36,33,39,38,32,41,36,40,31,34,37,37,41,39,38,35,38,38,38,33,42,42,29,33,39,37,44,38,35,36,38,40,39,38,38,37,34,44,35,36,38,39,39,39,39,43,28,39,41,38,30,38,35,37,40,41,40,35,41,36,35,40,41,37,38,36,37,38,39,41,38,37,44,37,37,37,36,39,33,41,39,37,42,37,36,28,43,40,32,35,45,40,34,43,38,36,37,36,40,28,45,38,40,38,36,35,40,42,31,31,44,36,31,36,40,40,36,34,43,35,32,39,33,33,36,41,34,38,40,35,34,37,37,39,36,40,37,41,39,41,36,32,44;zeros(11,210)];
|
|
|
|
|
|
|
|
Store = [5 2 5 3 6 1.5 4 5 1.7 3 2 3];
|
|
|
|
|
|
|
|
Cap = [4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500,4500,2500,2750,2100,2500,2750,1500];
|
|
|
|
|
|
|
|
A = [0 3 5 5 0 0 0 0 0 0 0 0];
|
|
|
|
|
|
|
|
Req =
|