**
关于源文件与头文件:代码:头文件代码
**:#includenamespace pers {
 struct person {
 std::string fname;
 std::string lname;};
 
 void getperson(person&);
 void show(const person&);}
 namespace debts {
 using namespace pers;
 struct debt {
 person name;
 double amount;
 };
 void getdebt(debt&);
 void showdebt(const debt&);
 double sum(const debt ar[], int n);}源文件(两个):第一个:通过包含头文件,导入了原来的名称空间,然后该文件将函数定义添加到两个名称空间中。#include 
 #include “源.cpp"using namespace debts;
 namespace pers {
 using std::cout;
 using std::cin;
 void getperson(person& rp) {
 cout << “enter fname”;
 cin >> rp.fname;
 cin >> rp.lname;
 }
 void show(const person & rp) {
 cout << rp.fname;
 cout << rp.lname << “\t”; } 
  }
 namespace debts {void getdebt(debt&rp) {
 getperson(rp.name);
 cout << “please enter debt”;
 cin >> rp.amount;}
 void showdebt(const debt&rp)
 {
 show(rp.name);
 cout << rp.amount;
 }double sum(const debt ar[], int n) {
 double am = 0;
 for (int i = 0; i < n; i++)
 am += ar[i].amount;
 return am;
 
 }}第二个:含有主函数main#include
 #include"源.cpp”
 void other(void);
 void another(void);
 using namespace std;
 using namespace pers;
 using namespace debts;
 int main() {
 debt golf = { {“ben”,“jieming”},32.1 };
 showdebt(golf);
 other();}
 void other() {
 person dg = { “ii”,“derw” };
 show(dg);
 cout << endl;
 debt dogg[3];
 int i;
 for (i = 0; i < 3; i++)
 getdebt(dogg[i]);
 for (i = 0; i < 3; i++)
 showdebt(dogg[i]);
 cout << “tatol debt $” << sum(dogg, 3);}关于从键盘正确输入一个东西并检查是否合法
include 
 double x;
 int main() {
 std::cout << “enter!”;while (!(std::cin >> x))
 {
 std::cout << “should double”;
 std::cin.clear();
 while (std::cin.get() != ‘\n’)
 continue;
 } std:: cout << “the value is” << x;
 }关于排列:定义一个描述三种颜色的枚举类型,编程输出这三种颜色的全排列
include 
 using namespace std;
 enum color{red,blue,green};
 enum color pri;
 int n,i, j, k, loop;
 int main()
 {
 n = 0;
 for (i = red ;i <= green ;i++)
 for (j = red; j <= green; j++) {
 for (k = red; k <= green; k++)
 if (i != j)
 {
 if ((k != i) && (k != j))
 {
 n++;
 cout << n << “:”; for (loop = 1; loop <= 3; loop++)
 {
 switch (loop)
 {
 case(1):pri = (enum color)i; break;
 case(2):pri = (enum color)j; break;
 case(3):pri = (enum color)k; break;
 }
 switch (pri)
 {
 case(red):cout << "red "; break;
 case(blue):cout << "blue "; break;
 case(green):cout << "green "; break;
 }}
                cout << endl;
            }
        }
}关于将一个数组移位:先写一个函数向右移动一位,再写一个函数调用前面的函数移动n位
。#includeusing namespace std;
 void move(int p[], int n)
 {
 int t;
 t = p[n - 1];
 if (n == 0) return;
 for (int i = n-1; i > 0; i–)//注意i的取值
 {
 p[i] = p[i - 1];
 }
 p[0] = t;}
 void movep(int p[], int k, int n)
 {
 for (int i = 0; i < k; i++)
 move(p, n);//调用move函数移动k位
 }
 int main() {
 int p[5];
 int n;
 cin >> n;
 for (int i = 0; i < n; i++)
 {
 cin >> p[i];
 }
 for (int i = 0; i < n; i++)
 {
 cout << p[i];
 }
 int k;
 cout << “the k:”;
 cin >> k;
 movep(p, k, n);
 cout << “after:”;
 for (int i = 0; i < n; i++)
 cout << p[i];}输入字符串,调用函数取出字符串中最长的单词并存入另一个字符串。
(有待思考)#define _CRT_SECURE_NO_WARNINGS
 #include
 #include
 #includeusing namespace std;
 void index(char* s1, char* s2)
 {
 int len ,max = 0;
 char temp[50];
 while (*s1)
 {
 while (*s1 == ’ ’ && *s1 != ‘\0’) s1++;//过滤空格;len = 0;
while (*s1 != ' ' && *s1 != '\0')
{    
    *(temp + len) = *s1;//不能用*temp=*s1,why?
    len++;
    s1++;
}

*(temp + len) = '\0';//注意这种方式。
if (len > max)
{
    max = len;
    strcpy(s2, temp);
}

if (*s1 == '\0') break;}}
 int main()
 {
 char s1[50],s2[50];cin.get(s1,50); index(s1, s2);
  cout << “s2:” << s2;
 }用队列的方法输出杨辉三角:#include 
 using namespace std;
 const int maxsize = 100;
 typedef struct {
 int Q[maxsize];//存放数据
 int front, rear;
 }sequeue;
 sequeue qu;
 void setkong(sequeue& qq)
 {
 qq.front = 0;
 qq.rear = 0;
 }//置队空
 void rudui(sequeue& qq, int x)
 {
 if (qq.front == (qq.rear + 1) % maxsize)
 cout << “overflow\n”;
 else
 {
 qq.Q[qq.rear] = x;
 qq.rear = (qq.rear + 1) % maxsize;
 }
 }
 void chudui(sequeue &qq, int& x)
 {
 if (qq.front == qq.rear)
 {
 cout << “underflow\n”;
 }
 else
 {
 x = qq.Q[qq.front];
 qq.front = (qq.front + 1) % maxsize;
 }
 }
 void getfront(sequeue qq, int &x)//读取队头元素
 {
 if (qq.front == qq.rear)
 {
 cout << “error!\n”;
 }
 
 else
 {
 x = qq.Q[qq.front];
 }
 }
 int empty(sequeue qq)//判断队列是否为空
 {
 if (qq.front == qq.rear)
 return 1;
 else
 return 0;
 }
 void yanghui(int n,sequeue qu)
 {
 int i, j,s,t;
 setkong(qu);
 rudui(qu, 1); rudui(qu, 1);
 cout << endl;
 cout.width(4); cout << 1;
 cout.width(4); cout << 1<<endl;
 for (i = 2; i <= n; i++)//生成并输出杨辉三角第i~n行的数据
 {
 rudui(qu, 1);
 cout.width(4); cout << 1;
 chudui(qu, s);
 for (j = 2; j <= i; j++)//处理第i行中间的各数据
 {
 chudui(qu, t);
 rudui(qu, s + t);
 cout.width(4); cout << s + t;
 s = t;
 }
    rudui(qu, 1);
    cout.width(4); cout << 1<<endl;
  
}
cout << endl;
 }
 int main()
 {
 int m;
 cin >> m;
 yanghui(m, qu);
 }最长的不重复的字符子串
include
 #include
 #include<unordered_set>
 using namespace std;
 int lengthOfLongestSubstring(string s);
 
 int lengthOfLongestSubstring(string s)
 {
 // 哈希集合,记录每个字符是否出现过
 unordered_set occ;
 int n = s.size();
 // 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
 int rk = -1, ans = 0;
 // 枚举左指针的位置,初始值隐性地表示为 -1
 for (int i = 0; i < n; ++i)
 {
 if (i != 0)
 {
 // 左指针向右移动一格,移除一个字符
 occ.erase(s[i - 1]);
 }
 while (rk + 1 < n && !occ.count(s[rk + 1]))
 {
 // 不断地移动右指针
 occ.insert(s[rk + 1]);
 ++rk;
 }
 // 第 i 到 rk 个字符是一个极长的无重复字符子串
 ans = max(ans, rk - i + 1);
 }
 return ans;
 };
 int main() {
 string mm;
 cin >> mm;
 int num;
 num = lengthOfLongestSubstring(mm);
 cout << num;
 }
 
 彩票案例
(二维数组初始化)//彩票案例
 //随机生成1-49之间的苏子 程序中每一组中按照升序排序
 //检查某个数字在用户选择的所有彩票中出现的次数
 //七行六列随机数
 //注意初始化二维数组 int table = new int**[7]
 //int table[i] = new int*[6]
 //下标越界时会导致错误
 #include
 #include
 #include
 using namespace std;
 const int column = 6;
 const int maxx = 49;
 void iniarray(int**, int);
 int isnum(int**, int, int);
 void sort(int**, int);
 void print(int**);
 int numoccur(int** table, int mm);
 inline void memerror() {
 cout << “error\n”;
 exit(1);
 }
 void genloto(int**, int);
 int main() {
 int** table;
 int numcheck;
 int i;
 int client;
 char answer;
 srand((unsigned)time(0));
 table = new int* [7];
 if (!table)
 memerror();
 for (i = 0; i < 7; i++) {
 table[i] = new int[6];
 if (!table[i])
 memerror();//check for an allocation error
 }
 iniarray(table, 7);
 genloto(table, 7);
 for (i = 0; i < 7; i++)
 {
 sort(table, i);//sort numbers in a row
 }
 print(table);
 cin >> client;//用户输入数字
 numcheck = numoccur(table, client);
 cout << "the num = " << numcheck;
 }
 void iniarray(inttable, int)
 {
 int i, j;
 for (i = 0; i < 7; i++)
 {
 for (j = 0; j < 6; j++)
 table[i][j] = 0;
 }
 }
 
 void genloto(inttable, int)
 {
 int i, j;
 int newnum;
 for (i = 0; i < 7; i++) {
 for (j = 0; j < 6; j++)
 {
 do
 {
 newnum = rand() % maxx + 1;
 } while (isnum(table, i, newnum));
 table[i][j] = newnum;
 }
 }
 }
 int isnum(int**table, int num, int n) {
 for (int i = 0; i < 6; i++) {
 if (table[num][i] == 0)
 return 0;
 if (table[num][i] == n)
 return 1;
 
 }
 return 0;
 }
 //升序排列的代码
void sort(int** table, int row) {
 int temp;
 for (int i = 0; i < 5; i++) {//注意i j 一定不能大于5
 for (int j = 0; j < 5; j++)
 {
 if (table[row][j] > table[row][j + 1])
 {
 temp = table[row][j];
 table[row][j] = table[row][j + 1];
 table[row][j + 1] = temp;
 }
 
 }
 }
 }
 void print(inttable)
 {
 cout << endl;
 cout << "**\n";
 for (int i = 0; i < 7; i++) {
 for (int j = 0; j < 6; j++)
 cout << table[i][j] << “\t”;
 cout << endl;
 }
 
 }
 int numoccur(int table, int num) {
 int count = 0;
 for (int i = 0; i < 7; i++)
 {
 for (int j = 0; j < 6; j++)
 {
 if (table[i][j] == num)
 count++;
 }
 }
 return count;
 }银行账户管理系统
include 
 #include
 using namespace std;
 class saving {
 private:
 int id;
 double balance;//余额
 double rate;//年利率
 int lastdate;//上次变更余额的时期
 double accumulation;//余额按日期累加之和
 void record(int date, double amount);
 double accumulate(int date) const {
 return accumulation + balance * (date - lastdate);
 }
 public:
 saving(int date, int id, double rate);//构造函数
 int getid() { return id; }
 double getbalance() { return balance; }
 double getrate() { return rate; }
 void deposit(int date, double amount);//存入现金
 void withdraw(int date, double amount);//取出现金
 void settle(int date);//结算利息
 void show();//显示账号信息
 };
 saving::saving(int date, int id, double rate):id(id),balance(0),rate(rate),lastdate(date),accumulation(0)
 {
 cout << id << “is created\n”;
 }
 void saving::record(int date, double amount)
 {
 accumulation = accumulate(date);
 lastdate = date;
 amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
 balance += amount;
 cout << date << “—” << id <<“amount:”<< amount<<“balance:” << balance;}
 void saving::deposit(int date, double amount)
 {
 record(date, amount);}
 void saving:: withdraw(int date, double amount)
 {
 if (getbalance() < 0)
 cout << “error\n”;
 else
 record(date, -amount);
 }
 void saving:: settle(int date)
 {
 double interest = accumulate(date) * rate / 365;
 if (interest != 0)
 record(date, interest);
 accumulation = 0;
 }
 void saving::show()
 {
 cout << id << balance;
 }
 int main()
 {
 saving s1(1, 4234, 0.15);
 s1.deposit(3, 5000);
 s1.show();
 s1.deposit(5, 3000);
 cout << endl;
 s1.show();
 cout << endl;
 s1.settle(80);
 s1.show();
 cout << endl;
 }
 容器嵌套#include #include
 #include
 using namespace std;
 void test()
 {
 vector<vector> mm;
 vectorp1;
 vectorp2;
 vectorp3;
 vectorp4;
 for (int i = 0; i < 4; i++)
 {
 p1.push_back(i);
 p2.push_back(i + 1);
 p3.push_back(i + 2);
 p4.push_back(i + 3);
 }
 mm.push_back(p1);
 mm.push_back(p2);
 mm.push_back(p3);
 mm.push_back(p4);
 for (vector<vector>::iterator mg = mm.begin(); mg != mm.end(); mg++){
	for (vector<int>::iterator yj = (*mg).begin(); yj != (*mg).end(); yj++)
		cout << *yj;
	cout << endl;
}}
 int main()
 {
 test();
 }
 访问单个字符:string str = “hello”;for(int i = 0;i < str.size() ;i++)cout<<str[i];多态性变步长梯形积分
include
class function//抽象类function的定义
 {
 public:
 virtual double operator()(double x) const = 0;//纯虚函数重载运算符()
 virtual ~function() {};
 };
 class myfunction :public function {
 public:
 double operator()(double x) const;//覆盖虚函数
 };
 class integrate {
 public:
 virtual double operator()(double a, double b, double eps)const = 0;
 virtual ~integrate() {}
 };
 class grate :public integrate {
 public:
 double operator()(double a, double b, double eps)const;
 grate(function&mm):f(mm){}//构造函数
 private:
 function& f;//私有成员
 };
 //类实现
 double myfunction::operator()(double x) const {
 return log(1.0 + x) / (1.0 + x * x);
 }
 double grate::operator()(double a, double b, double eps)const
 {
 myfunction yy;
 grate nice(yy);
 bool judge = false;
 double h = b - a;
 double tn = (yy(a) + yy(b)) * h / 2;
 double t2n;
 int n = 1;
 do{
 double sum = 0;
 for (int k = 0; k < n; k++) {
 double x = a + (k + 0.5) * h;
 sum += f(x);
 }
 t2n = (tn + h * sum) / 2.0;//变步长梯形法计算
 if (fabs(t2n - tn) < eps)//判断积分误差
 judge = true;
 else
 {
 tn = t2n;
 n *= 2;
 h /= 2;
 }
 } while (!judge);
 return t2n;}
array类模板(重载指针下标运算符 等号)trap.h:动态数组类模板array
#include
 template 
 class Array {
 T* mm;//T类型指针 用于存放动态分配的数组首地址
 int size;//数组大小
 public:
 Array(int siz = 50);
 Array(const Array& yy);//复制构造函数
 ~Array();
 Array& operator = (Array& rhs);//重载“=”
 T& operator [](int i);//重载[];
 operator T* ();
 //重载指针转化运算符T* 要想对象名能像数组名一样使用下标,还要重载下标运算符
 int getsize()const;
 void resize(int sz);//修改数组大小
 };
 template 
 Array::Array(int siz)
 {
 assert(siz >= 0);
 size = siz;
 mm = new T[size];//动态分配size个T类型的元素空间
 }
 template 
 Array& Array:: operator = (Array& rhs)
 //重载“=” 将对象rhs赋给本对象 实现对象之间整体赋值
 {
 if (size != rhs.size) {//如果本对象中数组大小与rhs不同 则删除数组原有内存然后重新分配delete[]mm;//删除数组原有内存
	size = rhs.size;//设置本对象数组大小
	mm = new T[size];//重新分配size个元素的内存
}

for (int i = 0; i < size; i++)
{
	mm[i] = rhs.mm[i];
}
return *this;//返回当前对象的引用}
 template 
 Array::~Array()
 {
 delete[]mm;
 }
 template 
 Array::operator T* ()
 {
 return mm;//返回当前对象中私有数组的首地址
 }template 
 T& Array:: operator [] (int i)
 {
 assert(i >= 0 && i < size);//检查下标是否越界
 return mm[i];
 }
 template
 int Array::getsize()const
 {
 return size;
 }
 template
 void Array::resize(int sz)
 {
 assert(sz >= 0);//检查sz是否非负
 if (size == sz)
 return;//如果指定的大小与原有大小一样什么也不做
 T* newlist = new T[sz];//申请新的内存
 int n = (sz < size) ? sz : size;//将sz 与size中较小的值赋给n
 for (int i = 0; i < n; i++)
 {
 newlist[i] = mm[i];//将原有数组的前n个元素复制到新数组中
 }
 delete[] mm;//删除原数组
 mm = newlist;//使mm指向新数组
 size = sz;//更新size}
从键盘输入n,计算2~n中的质数并且输出
:#include 
 #include"trap.h"
 #includeusing namespace std;
 int main()
 {
 int n;
 cin >> n;
 int count = 0;
 Arraynice(10);
 int i;for (i = 2; i < n; i++)
{//检查i能否被比它小的质数整除
	bool judge = true;
	for (int j = 0; j < count; j++)//如果i被nice[j]整除说明i不是质数
	{
		if (i % nice[j] == 0)
		{
			judge = false;
			break;
		}
	}
	if (judge)
	{
		if (count == nice.getsize())//如果质数表满了将其空间加倍
			nice.resize(count * 2);
		nice[count++] = i;
	}
}
for (i = 0; i < count; i++)
	cout <<setw(8)<< nice[i];}//个人认为a[0]一开始应该是一个随机数
vector构造函数//奇偶排序 先奇数再偶数 从大到小的顺序
#include
 #include
 #include
 #include
 #include
 using namespace std;
 int main()
 {
 istream_iterators1(cin), s2;//建立一对输入流迭代器
 vector i1(s1, s2);//通过输入流迭代器从标准输入流中输入数据
 sort(i1.begin(), i1.end());//将输入的整数排序
 dequem1;
 //循环遍历i1
 for (vector::iterator ite = i1.begin(); ite != i1.end(); ++ite)
 {
 if (*ite % 2 == 0)//偶数放m1尾部
 m1.push_back(*ite);
 else
 m1.push_front(*ite);
 }
 copy(m1.begin(), m1.end(), ostream_iterator(cout, “-”));//输出结果 以-符号分隔
 }
 结点类模板#include using namespace std;
 //结点类模板
 template
 class node {
 private:
 node* next;//指向后继结点的指针
 public:
 T data;//数据域
 node(const T& data, node* next = 0);//构造函数
 void insert(node* p);//在本结点之后插入一个同类结点
 node* deleteafter();//删除本结点的后继结点并且返回其地址
 node* getlocation();//获取后继结点的地址
 const node* getlocation() const;//获取后继结点的地址
 };
 //类的实现部分
 template
 node::node(const T& data, node* next ):data(data),next(next){}
 template
 void node::insert(node* p)
 {
 p->next = next;//p的指针域指向当前结点的后继结点
 next = p;//当前结点指针域指向p}
 template
 nodenode:: deleteafter()
 {
 node temp;
 temp = next;//将欲删除的结点地址存储在temp中
 if (next == 0)//如果当前结点没有后继结点 返回空指针
 return 0;
 next = temp->next;//使当前结点指向temp的后继结点
 return temp;//返回被删除的结点的地址
 }
 template
 node* node::getlocation()
 {
 return next;//返回后继结点的指针
 }
 template
 const node* node::getlocation() const
 {
 return next;
 }细胞分裂
:#include
 #include
 #include
 #include
 using namespace std;
 const int maxx = 2000;
 const int minn = 500;
 class cell;
 priority_queuecellque;class cell {
 private:
 static int total;//细胞总数
 int id;//当前细胞编号
 int time;//细胞分裂时间
 public:
 cell(int birth) :id(total++) { time = birth + (rand() % (maxx - minn) )+ minn; }
 //初始化获得细胞分裂时间
 int getid() const{ return id; }//获取细胞编号
 int gettime() const{ return time; }//获取细胞分裂时间
 bool operator <(const cell& s)const { return time > s.time; }//定义"<"
 void spilit()const
 {
 cell xibao1(time), xibao2(time);//得到两个细胞
 cout << time << “cell” << id << “splits to” << xibao1.getid() << " " << xibao2.getid() << endl;
 cellque.push(xibao1);//将第一个子细胞压入优先级队列
 cellque.push(xibao2);//}};
 int cell::total = 0;
 int main()
 {
 srand(static_cast(time(0)));
 int t;
 cout << “input\n”;
 cin >> t;
 cellque.push(cell(0));
 while (cellque.top().gettime() <= t)
 {
 cellque.top().spilit();
 cellque.pop();
 }
 }
 //栈操作#include
 #include
 using namespace std;
 template<class T, int size = 50>
 class stack {
 private:
 T list[size];
 int top;
 public:
 stack();
 void push(const T& mm){
 assert(!isfull());//如果栈满了则报错
 list[++top] = mm;//将新元素压入栈顶}
 T pop()//将栈顶元素弹出栈
 {
 assert(!isempty());
 return list[top–];//返回栈顶元素并将其弹出栈
 }
 void clear()
 {
 top = -1;//清空栈
 }
 const T& peek()const//访问栈顶元素
 {
 assert(!isempty());//如果为空则报错
 return list[top];
 }
 bool isempty()const
 {
 return top == -1;
 }bool isfull()const
 {
 return top == size - 1;
 }
 };
 template<class T, int size>
 stack<T, size>::stack() :top(-1) {};//构造函数 栈顶初始化为-1.class cal {
 stacks;
 void enter(double num)
 {
 s.push(num);
 }//操作数弹出栈 如果栈中没有两个操作数则返回错误信息
 bool gettwo(double& o1, double& o2)
 {
 bool mod;
 if (s.isempty())
 {
 cout << “error”;
 mod = false;
 }
 else
 o1 = s.pop();
 if (s.isempty())
 mod = false;
 else
 {
 o2 = s.pop();
 mod = true;
 }
 }
 void ope(char op)//连续将两个操作数弹出并放在o1和o2中 如果成功 执行运算并将结果压入栈
 {
 double oo1, oo2;
 bool result = gettwo()}//执行由操作符op指定的运算
 public:
 void run()
 void clear()
 };