C++笔记

好记性不如烂笔头

//1//
//10层汉诺塔递归解法,过程输出到文件
共移动 2的n次方减1次

#include

#include

using namespace std;

ofstream fout(“out.txt”);

void Move(int n, char i, char j)
{
fout << “把” << n << “号从” << i << “移动到” << j << endl;
}

void Hannoi(int n, char a, char b, char c)
{
if (n == 1)
{
Move(1, a, c);
}
else
{
Hannoi(n - 1, a, c, b);
Move(n, a, c);
Hannoi(n - 1, b, a, c);
}
}

int main()
{
fout << “以下是10层汉诺塔的解法:” << endl;
Hannoi(2, ‘a’, ‘b’, ‘c’);
fout.close();
cout << “输出结果完毕。” << endl;
system(“pauses”);
return 0;
}

//2//
//容器 迭代器的使用

#include

#include
using namespace std;

int main()
{
vectorobj;
for (int i = 0; i < 10; i++)
obj.push_back(i);
//for (int i=0; obj.size(); i++) //尽量不要用下标的形式
// cout << obj[i] << “\t”;

cout << endl;
//容器
//vector <int>::iterator p;
//p = obj.begin();
//for (; p != obj.end();p++)
//    cout << *p << "\t";
//方法二,使用迭代器将容器中数据输出 
vector<int>::iterator it;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素 
for (it = obj.begin(); it != obj.end(); it++)
{
    cout << *it << " ...";
}
system("pause");

}

//3//
c++双向队列的使用
2019年04月10日 14:58:39 reaching_dream 阅读数 155更多
分类专栏: 编程 C++
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42056625/article/details/89181657
首先我们先简单说明c++的双向队列
c++的双向队列比队列具有更加灵活的特性,可以对队列中两端的元素进行操作。
包含文件为 #include
声明为: deque d; type为声明的变量类型,其中d为声明的变量名
常见的操作有以下几种方式:
push_back: 从尾部添加元素
push_front: 从头部添加元素
pop_back: 从尾部删除元素
pop_front: 从头部删除元素
insert: 插入一个元素
erase 删除一个元素
更加具体的函数可以参考c++的官方网站对于deque的说明:
http://www.cplusplus.com/reference/deque/deque/?kw=deque


下面我们来看一下这里例子,这里具体的说明如何使用上面的函数,程序里具体说明了每一步的解释:

#include

#include
using namespace std;

int main()
{
deque d;
//从尾部插入0到4
for (int i = 0; i < 5; i++)
{
d.push_back(i);
cout << i << “ “;
}
//从头部插入20到16
for (int i = 20; i>15; i–)
{
d.push_front(i);
cout << i << “ “;
}
cout << endl;
//现在双向队列中按从头到位的顺序为16 17 18 19 20 0 1 2 3 4
//输出双向队列中的所有元素
cout << “按照一头一尾的方式输出双向队列中的所有元素” << endl;
//正确的输出结果应该是16 4 17 3 18 2 19 1 20 0
while (!d.empty())
{
cout << d.front() << “ “;
d.pop_front();
cout << d.back() << “ “;
d.pop_back();
}
cout<<endl;
deque::iterator it = d.begin();
//删除了所有元素以后我们重新对双向队列赋值
for (int i = 0; i < 10; i++)
{
d.push_back(i);
}
//输出现在队列中的元素
for (it = d.begin(); it != d.end(); it++)
{
cout <<*it << “ “;
}
cout << endl;
//接下来我们演示在第3个元素中插入5个10,然后删除倒数第2个元素

it = d.begin();//让it指向第三个元素
d.insert(it+2,5,10);//这里的5代表插入的元素个数
it = d.end();
d.erase(it - 2);//删除倒数第二个元素
//查看现在双向队列中的元素
for (it = d.begin(); it != d.end(); it++)
{
    cout <<*it << " ";
}

system("pause");
return 0;

}
下面是对应的输出截图:

//4//
//8进制

#include
using namespace std;

int main()
{
int oo=0655;
cout << oo << “\t”; //429

system("pause");

}

//5//
//小数格式

#include

#include
using namespace std;

int main()
{
cout << setiosflags(ios::fixed) << setiosflags(ios::right);
cout << setw(10) << 34.3434 << endl;
cout << setw(10) << 4.8989888 << endl;
cout << setw(10) << 0.5454 << endl;
cout << setw(10) << 232.7878 << endl;
system(“pause”);
}

//6//
//if使用

#include

#include
using namespace std;

//分段函数
//x>200 y=89;
//x>100 y=88;
//other y=87;
int main()
{
int n=340;//给定默认
if (n > 200)cout << 89 << endl;
else if(n>100)cout << 88 << endl;
else cout << 87 << endl;
system(“pause”);
}

//7//
//求π

#include

#include
using namespace std;

int main()
{
double sum = 0.0, t = 1.0, n = 1.0;
int s = 1;
for (; fabs(t) > 1e-7; )
{
//cout << n << endl;
t = s / n;
sum = sum + t;
s = -s;
n += 2;//不能放到for的第三个条件
}
cout << sum * 4;

system("pause");

}

//8//
//斐波那契数列

#include

#include
using namespace std;

int main()
{
int a[100];
a[0] = 1, a[1] = 1;
int x = a[0] + a[1];
cout << a[1] << endl;
for (int i = 2; i < 20; i++)
{
a[i] = a[i - 1] + a[i - 2];
cout << a[i] << endl;
}

system("pause");

}

//10//
//100-200间的素数

#include

#include
using namespace std;

int main()
{
int num = 0;
for (size_t i = 101; i < 200; i+=2)
{
int flag = 1;
for (size_t j = 2; j <= sqrt(i); j++)
{
if (i%j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
cout << i << “\t” << ++num << endl;
}

system("pause");
return 0;

}

//11//
//1000 0000 0内的完数

#include

#include
using namespace std;

int main()
{
int num = 0;
for (size_t i = 2; i < 10000; i+=2)//太大了算不出来
{
int flag = 1, sum = 0;
for (size_t j = 1; j < i; j++)
{
if (i%j == 0)
{
sum += j;
//cout << sum << “\t”;
}
}
///cout << endl;
if (sum==i)
cout << i << “\t” << ++num << endl;
}

system("pause");
return 0;

}

//12//
//4个数中最大

#include

#include
using namespace std;

int main()
{
int num = 0;
int a = 9, b = 19, c = 1, d = 88;
if (a<b)
a = b;
if (c>d)
d = c;
if (a<d)
a = d;
cout << “max = “ << a << endl;
system(“pause”);
return 0;
}

//12//
//两队都有3人 a不与x,c不与下x,z 问对手

#include

#include
using namespace std;

int main()
{
char x, y, z;
for (x = ‘x’; x <= ‘z’; x++)
for (y = ‘x’; y <= ‘z’; y++)
if (x != y)
for (z = ‘x’; z < ‘z’; z++)
if (x!=z&&y != z)
if (x != ‘x’&&z != ‘x’&&z != ‘z’)
{
cout << “a–>” << x << endl;
cout << “b–>” << y << endl;
cout << “c–>” << z << endl;
}
system(“pause”);
return 0;
}

//13//
//两个特殊的重载函数

#include

#include
using namespace std;
class MyClass
{
public:
void overload() //只能写在类里
{
cout << “测试1” << endl;
}

void overload()const
{
    cout << "测试2" << endl;
}

private:

};

int main()
{
MyClass obj1;
obj1.overload();//默认调用不加const的函数
/obj.overload()const;///错误 不能这样调用

MyClass const obj2;
obj2.overload();

system("pause");
return 0;

}

//14//
//函数模板

#include

#include
using namespace std;

template
void f(T a,T b)
{
T c = a - b;
cout << c << endl;
}

int main()
{
f(34, 444);
f(‘g’,’a’);
system(“pause”);
return 0;
}

//15//
//类模板 及在类外定义模板函数

#include

#include
using namespace std;

template
class MyClass
{
public:
MyClass(T aa, T bb) { a = aa; b = bb; }
T a;
T b;
void f1()
{
cout << a << endl << b << endl;
}
T f2();
private:

};

template
T MyClass :: f2()
{
T c;
c = a + b;
cout << c;
return c;
}
int main()
{
MyClass obj(44, 43434);
obj.f1();
obj.f2();
system(“pause”);
return 0;
}

//17//
//不加头文件使用系统函数

#include
int main()
{
double sin(double x);
std::cout << sin(3.14);
system(“pause”);
return 0;
}

//18//
//数字转字符串

#include
using namespace std;

int f(int a)
{
char c;
if (a < 10)
{

    cout << a;
    return a;
}
else
    f(a / 10);
c = (a % 10)+48;   //原来的数字对应实际的ASCII ,如个位数为4,ASCII为4,在此基础上增加48转为对应的ASCII的数字
cout << c;

}
int main()
{
int a;
a = 1234;

cout << a << "表示成字符串为:";
f(a);
system("pause");
return 0;

}

//20//
//调用函数 求i的阶乘

#include
using namespace std;

int f(int a)
{
int res= a;
if (a == 1)
return a;
else
{
res *= f(a-1);
return res;
}
}
int main()
{
int a = 5;
cout << f(a) << ‘\t’;
system(“pause”);
return 0;
}

//21//
//调用函数 求i^2 求和

#include
using namespace std;

int f(int a) //函数内if和else都要有返回值
{
int res= a;
if (a == 1)
return a;
else
{
res *= res;
res += f(a-1);
return res;
}
}
int main()
{
int a = 4;
cout << f(a) << ‘\t’;
system(“pause”);
return 0;
}

//23//
//最大公约数 最小公倍数

#include
using namespace std;

int f1(int a,int b)//最大公约数
{
int temp;
if (a < b)
{
temp = a;
a = b;
b = temp;
}
if ((a % b) == 0)
{
return b;
}
else
{
return f1(b, a - b);
}
}

int f2(int a,int b)//最小公倍数
{
int res;
res = a*b / f1(a, b); //原数的乘积除以最大公约数
return res;
}

int main()
{
int a = 40;
int b = 20015;
cout << f1(a, b) << ‘\t’;
cout << f2(a,b) ;
system(“pause”);
return 0;
}

//24//
//cin.getline 单独无法使用

#include

#include
using namespace std;

int main()
{
char a[34] = {“dfdf”};
cin.getline(a,33);
cout << a;
system(“pause”);
return 0;
}

//25//
//二维数组指针

#include

#include

#pragma warning (disable :4996)
using namespace std;

int main()
{
//方式1
int a[2][3] = { 23,34 };
int p;
p = a[0];
cout << p[0] << endl;//23
cout <<
p << endl;//23

//方式2
int *p1[3] = { a[0] ,a[1]};
cout <<p[0];//23

//方式3
int(*pp)[3] = a;
cout << p[0];//23
cout << (*pp)[0];//23
//cout << p[0][1];//23   //error

system("pause");
return 0;

}

//26//
//const指针

#include

#include

#pragma warning (disable :4996)
using namespace std;

int main()
{
//No1 指向变量的指针变量
int a = 23;
const int pa = &a;//不能通过指针修改原来指向的变量的值
//
p = 34; //error 错误信息 :必须是可赋值的左值

//No2 常指针
int b = 23;
int bb = 34;
int * const pb = &a;//不能修改指针指向
//pb = &bb;    //error 错误信息 :必须是可赋值的左值


//No3
//以上两种的综合
system("pause");
return 0;

}

//27//
//指向对象成员函数的指针

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
void f();
private:

};

void MyClass ::f()
{
cout << “lalala”;
}

int main()
{
//1
void (MyClass :: *p2)(); //指向对象成员函数的指针 的定义
//p2 = &MyClass::f(); //error
p2 = &MyClass::f; //注:无括号

//2
//直接写是指向对象的指针:
MyClass *pp;

system("pause");
return 0;

}

//28//
//对象的动态建立与初始化

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(int aa) { a = aa; }
void f();
private:
int a;
};

void MyClass ::f()
{
cout << “lalala” << a;
}
int main()
{
MyClass *pp;
pp = new MyClass;

MyClass *pp2;
pp2 = new MyClass(666);
pp2->f();
system("pause");
return 0;

}

//29//
//普通函数作为友元

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(int aa) { a = aa; }
void f();
friend void myfriend(MyClass & obj);
private:
int a;
};

void MyClass ::f()
{
cout << “lalala” << a;
}

void myfriend(MyClass & obj)
{
cout << “开心” << obj.a;
}

int main()
{
MyClass *pp;
pp = new MyClass;

MyClass *pp2;
pp2 = new MyClass(666);
pp2->f();

MyClass obj(888);
myfriend(obj);

system("pause");
return 0;

}

//30//
//原来的字符个数,加上%d代表的个数

#include<stdio.h>

int main()
{

int x=77797;
x=printf("tt%d",x);//x是输出的字符的个数
//原来的字符个数,加上%d代表的个数

printf("%d",x);



}

//31//
//静态成员函数使用
//一般来说
//静态成员函数用来调用静态成员(非绝对)
//非静态成员函数来调用非静态成员(非绝对)

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(int aa) :a(aa) {}
void static f();
static int sum;
static int count;
private:
int a;

};

void MyClass ::f()
{
cout << sum/count ;
}

int MyClass::sum = 50;
int MyClass::count = 50;

int main()
{
cout << MyClass::sum;
MyClass::f();
system(“pause”);
return 0;
}

//32//
//“+-*/“ 成员函数重载 实现复数运算

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(double real,double image) :a(real),b(image) {}
MyClass operator+(MyClass &c2);
MyClass MyClass::operator-(MyClass &c2);
MyClass MyClass::operator*(MyClass &c2);
MyClass MyClass::operator/(MyClass &c2);
void disp()
{
if (b > 0)
{
cout << “(“ << a << “+” << b << “i)” << endl;
}
else
{
cout << “(“ << a << “” << b << “i)” << endl;
}
}
private:
double a;
double b;
};

MyClass MyClass::operator+(MyClass &c2)
{
MyClass temp;
temp.a = a + c2.a;
temp.b = b + c2.b;
return temp;
}

MyClass MyClass::operator-(MyClass &c2)
{
MyClass temp;
temp.a = a - c2.a;
temp.b = b - c2.b;
return temp;
}

MyClass MyClass::operator(MyClass &c2)
{
MyClass temp;
temp.a = a
c2.a - b c2.b;
temp.b = b
c2.a + a * c2.b;
return temp;
}

MyClass MyClass::operator/(MyClass &c2)
{
MyClass temp;
double c;
c = c2.ac2.a + c2.bc2.b;
temp.a = a c2.a + b c2.b;
temp.a = temp.a / c;

temp.b = b * c2.a - a * c2.b;
temp.b = temp.b / c;

return temp;

}

int main()
{
MyClass obj1(23, 44), obj2(111, 222), obj3;
obj3 = obj1 + obj2;
obj3 = obj1 - obj2;
obj3 = obj1 / obj2;
obj3.disp();
system(“pause”);
return 0;
}

//显示调用成员函数重载符
//int main()
//{
// MyClass obj1(1, 1), obj2(2, 2), obj3;
// obj3 = obj2.operator+(obj1);
// obj3.disp();
// system(“pause”);
// return 0;
//}

//33//
//“+-*/“ 友元重载 实现复数运算

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(double real,double image) :a(real),b(image) {}
friend MyClass operator+(MyClass &, MyClass &);
friend MyClass operator-(MyClass &, MyClass &);
friend MyClass operator*(MyClass &, MyClass &);
friend MyClass operator/(MyClass &, MyClass &);

void disp()
{
    if (b > 0)
    {
        cout << "(" << a << "+" << b << "i)" << endl;
    }
    else
    {
        cout << "(" << a << "" << b << "i)" << endl;
    }
}

private:
double a;
double b;
};

MyClass operator+(MyClass &c1, MyClass &c2)
{
MyClass temp;
temp.a = c1.a + c2.a;
temp.b = c1.b + c2.b;
return temp;
}

MyClass operator-(MyClass &c1, MyClass &c2)
{
MyClass temp;
temp.a = c1.a - c2.a;
temp.b = c1.b - c2.b;
return temp;
}

MyClass operator(MyClass &c1, MyClass &c2)
{
MyClass temp;
temp.a = c1.a
c2.a - c1.b c2.b;
temp.b = c1.b
c2.a + c1.a * c2.b;
return temp;
}

MyClass operator/(MyClass &c1, MyClass &c2)
{
MyClass temp;
double c;
c = c2.ac2.a + c2.bc2.b;
temp.a = c1.a c2.a + c1.b c2.b;
temp.a = temp.a / c;
temp.b = c1.b c2.a - c1.a c2.b;
temp.b = temp.b / c;
return temp;
}

int main()
{
MyClass obj1(23, 44), obj2(111, 222), obj3;
obj3 = obj1 + obj2;
obj3 = obj1 - obj2;
obj3 = obj1 / obj2;
obj3 = obj1 * obj2;
obj3.disp();
system(“pause”);
return 0;
}

//32//
//“== >” 友元重载 实现复数运算

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(string str11) :str1(str11){}
friend bool operator==(MyClass &, MyClass &);
friend int operator>(MyClass &, MyClass &);

void disp()
{
    cout << "string:" << str1 << endl;
}

private:
string str1;
};

bool operator==(MyClass &obj1, MyClass &obj2)
{
if(obj1.str1 == obj2.str1)
return 1;
else
return 0;
}

int operator>(MyClass &obj1, MyClass &obj2)
{
if (obj1.str1 > obj2.str1)
return 1;
else if (obj1.str1 < obj2.str1)
return 2;
else return 0;
}

int main()
{
MyClass obj1(“good”), obj2(“well”), obj3;
int flag = obj1 > obj2;
obj1.disp();
obj2.disp();
cout << “第” << flag << “大” << endl;

system("pause");
return 0;

}

//33//
//“++” 成员函数重载 实现自加(前置 后置)

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

class MyClass
{
public:
MyClass() {}
MyClass(double x) :a(x){}
MyClass operator++();
MyClass operator++(int);
double a;
private:

};

MyClass MyClass::operator++()
{
++a;
return *this;
}

MyClass MyClass::operator++(int)
{
a++;
return *this;
}

int main()
{
MyClass obj1(665);
++obj1;
cout << obj1.a; //666
obj1++;
cout << obj1.a; //667
system(“pause”);
return 0;
}

注意问题:
重载符必须和原来的操作对象个数相同,双目重载时还是双目,单目重载时是单目

//32//
//选择法排序(10个数)

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

void f(int *a)//
{
int min, t;
size_t j;
size_t i;
for (i = 0; i < 10-1; i++)
{
min = i;
for (j = i+1; j < 10; j++)
{
if (a[min] > a[j])
min = j;
}
swap(a[min], a[i]);

}

for (size_t i = 0; i < 10; i++)
{
    cout << a[i] << "\t";
}

}

void swap(int&a, int &b)
{
int t=0; //需要赋初值
a = t;
t = b;
b = t;
}

int main()
{
int a[10] = { 23,45,56,35,34,65,57,23,1,7 };
f(a);
system(“pause”);
return 0;
}

//33//
//鞍点

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

void f(int (*a)[3])//
{
int flag = 1;
for (size_t i = 0; i < 3; i++)
{
int max = a[i][0], maxj = 0; //假设行最大值为max 最大值的下标为maxj
for (size_t j = 0; j < 3; j++)
{
if (max < a[i][j])
{
max = a[i][j];
maxj = j;
}
}
flag = 1; //假设每一行的最大值都有可能是鞍点
for (int k = 0; k < 3; k++)
{
if (max > a[k][maxj])
{
flag = 0;
continue;//次句可不要 增加只是减少运算次数,意为只要出现小于max的数停止本次计算
}
}
if (flag)
{
cout << “鞍点坐标为:” << i + 1 << “,” << maxj + 1;
cout << “ 其数值为:” << max;
}
}
}

int main()
{
int a[3][3] = { 1,2,3,1,2,4,2,2,5};
f(a);
system(“pause”);
return 0;
}

//34//
//折半查找(二分法) 一串数已经有序 则查找item在一串数中的位置比较次数为logn+1

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

//非递归
void f1(int *a,int low,int high,int key)//a[]数组 key为要查找的数
{
int mid;
while (low<=high)
{
mid = (low + high) / 2;
if (a[mid] == key)
{
cout << key << “下标为(从1开始):” << mid + 1 << endl;
break;
}
else if (a[mid] < key)
{
low = mid + 1;
}
else high = mid - 1;
}
}

//递归
int f2(int *a, int low, int high, int key)//a[]数组 key为要查找的数
{
int mid;
if (low > high)
return -1;
else
{
mid = (low + high) / 2;
if (a[mid] == key)
{
cout << key << “下标为(从1开始):” << mid + 1 << endl;
return 1;
}
else if (a[mid] < key)
{
f2(a, mid + 1, high, 8);
}
else f2(a, low, mid - 1, 8);
}
}

int main()
{
int a[10]= { 1,2,3,4,5,6,7,8,9,10};
f1(a,0,9,8); //方法1
f2(a, 0, 9, 8);// //方法2
system(“pause”);
return 0;
}

//35//
//指针运用:后移部分数据
//如后移1,4
//移动前:1 4 4 7 8 4
//移动后:4 7 8 4 1 4

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

void f(int a,int n,int m)//a[]数组 n为数据总数 m为后移个数
{
int
p;
int a_end;
a_end = (a+n-1);
//步骤1:前移后面n-1个元素
for (p = a + n - 1; p > a; p–)
{
p = (p - 1);
}
//步骤2:开始时的第一个元素放最后
a = a_end;
//步骤3:一共移动m次
m–;
if (m > 0)f(a, n, m);
}

int main()
{
int a[10]= { 1,2,3,4,5,6,7,8,9,10};
f(a,10,3);
for (size_t i = 0; i < 10; i++)
{
cout << a[i] << “\t”;
}
system(“pause”);
return 0;
}

//36//
//指针运用:约瑟夫 (报数问题)
//从第k个人报数,凡报到3的人退出。问最后剩余的那一人的编号

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;

void f(int a,int n)// a[]为一圈人的编号 n总人数 k为开始报数的编号 m为每次报到出圈的人3
{
int i, m, k;
m = 0; i = 0; k = 0;//初始化
int
p = a;
while (m < n - 1)//m累加是出圈总人数(在数组中重置为0即出圈)
{
if ((p + i) != 0)
{
k++;
}
if (k == 3)
{
(p + i) = 0;
k = 0;
m++;
}
i++;
if (i == n)i = 0; //次句相当于循环,即到末尾时重置
}
while (p == 0)
{
p++;
}
cout <<
p;

}

int main()
{
int a[8]= { 1,2,3,4,5,6,7,8};//给每个人编号(从1开始),假设共10个人
f(a,8);
system(“pause”);
return 0;
}

//37//
//字典排序 https://blog.csdn.net/qq_34672688/article/details/79557380
//总结得出字典排序算法四步法:
//
//字典排序:
//第一步:从右至左找第一个左邻小于右邻的数,记下位置i,值list[a]
//第二部:从右边往左找第一个右边大于list[a]的第一个值,记下位置j,值list[b]
//第三步:交换list[a]和list[b]的值
//第四步:将i以后的元素重新按从小到大的顺序排列
//
//
//举例:125643的下一个字典序列
//第一步:右边值大于左边的3<4, 4<6, 6>5, 则i = 2(从0开始),list[a] = 5
// 第二步:从右往左找出第一个右边大于list[a] = 5的值,找到6>5, j = 3; list[b] = 6;
//第三步:交换list[a]和list[b]的值,序列125643->126543
//第四步:将位置2以后的元素重新排序, 126543->126345;
//结束: 126345即125643的下一个序列

#include “myiostream.h”

#pragma warning (disable :4996)
using namespace std;
//注意宏函数不能不能写为swap(int a,int b)
//注意宏函数{}不能换行

#define swap(a,b) {int t;t = a;a = b;b = t;}

//冒泡排序对数组中的一段数据进行排序
void sort(int *a, int start, int end)//形参:数组 开始位置(从0开始) 结束位置
{
for (size_t i = 0; i <= end - start; i++)
{
for (size_t j = start; j < end - i; j++)
{
if (a[j] > a[j+1])
swap(a[j], a[j+1]);
}
}
}

void 字典排序(int a, int n)
{
int 阶乘=1;
int xx, jj;//记录要交换的位置
size_t j;
size_t i;
size_t k;
for (i = 1; i <= n; i++)
{
阶乘
= i;
}
sort(a,0,n-1);
for (i = 0; i < 阶乘; i++)
{
for (j = 0; j < n; j++)
{
cout << a[j];
}
cout << endl;
for (k = n - 1; k > 0; k–)//字典步骤1 (不能写成k >= 0)
{
if (a[k - 1] < a[k])
{
xx = k-1;//不能写成xx=k
break;
}
}
for (k = n - 1; k >= 0; k–)//字典步骤2
{
if (a[k] > a[xx])
{
jj = k;
break;
}
}
swap(a[xx], a[jj]);//字典步骤3
//注意是xx后的元素进行排序 xx+1
sort(a, xx+1, n - 1);////字典步骤4
}
}

int main()
{
int a[4]= {1,2,3,4};//给每个人编号(从1开始),假设共10个人
字典排序(a,4);
system(“pause”);
return 0;
}

//38//
//3个等长字符串排序输出

#include

#include
using namespace std;

//冒泡 string方式
void sort(string a[3])
{
for (size_t j = 0; j < 3-1; j++)
{
for (size_t i = 0; i < 3-j-1; i++)
{
string temp;
if (a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}

    }
}

}

int main()
{
string a[3] = { “zrer”,”mfgs”,”pppp” };
sort(a);
for (size_t i = 0; i < 3; i++)
{
cout << a[i] << “\t”;
}
system(“pause”);
return 0;
}

//40//
//计算积分 一元积分 二重积分

#include

#include

#include
using namespace std;

//被积函数
double f(double x)
{
return pow(2.71,x);
}

//被积函数(重载,计算二重积分)
double f(double x, double y)
{
return x*y;
}

//一元积分 微元:矩形 = 长
//积分=Sigma(a+(b-a)/n
i)b-a)/n
void f1(double a, double b, int n) //积分下限 积分上限 分割份数
{
//累加法求面积(定积分定的精确定义)
double s = 0.0;
double t = (b - a) / n;
for (size_t i = 0; i < n; i++)
{
s += f(a + it)t;
}
cout << s << endl;
}

//二重积分 SigmaSigma(a+(b-a)/n i (b-a)/n,c+(d-c)/ni))d-c)/n
void f2(double a, double b, double c, double d, int n)//x,y的下限 上限
{//精度较高 时计算速度较慢
//累加法求面积(定积分定的精确定义)
double s = 0.0;
double t1 = (b - a) / n;
double t2 = (d - c) / n;
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
s += f(a + i
t1, c + jt2)t1*t2;
}
}
cout << s << endl;
}

int main()
{
f1(0, 2, 100000);
f2(0, 1, 0, 1, 1000);
system(“pause”);
return 0;
}

//41//
//输入一串字符 输出字符中的数字及个数 如sd 123hfj555sdf333sfhj 输出123 555 333 3个

#include

#include

#include
using namespace std;

void convert(char a,int n)//字符转数字,形参为字符串及长度
{
int sum=0;
for (size_t i = 0; i < n; i++)
{
sum += (a[i]-48)
pow(10, n - 1 - i);
}
cout << sum << endl;
}
void f(char a)
{
char
p, r;
int i=0;//统计每次找到的数字的位数
for (p = a;
p != ‘\0’; p++)
{
if ((p >= ‘0’)&&(p <= ‘9’))
{
i++;
}
else if( (p != ‘0’) && (p != ‘9’))
{
if (i > 0)
{
r = p - i;
convert(r, i);
i = 0;
}
}
if (*(p+1) == ‘\0’) //不能写为else if 这是一个特殊情况
{
if (i > 0)
{
r = p - i + 1; //此处和上边有细微差别
convert(r, i);
i = 0;
}
}
}
}

int main()
{
char a[] = { “ddfn2334nfgjn78” };
f(a);
system(“pause”);
return 0;
}

//42//
//ABCDEFG7位医生 每人每周值班一次 要求:
/
(1)A比C晚一天
(2)D比E晚两天
(3)G比B晚3天
(4)F在B与C之间,F在周四
/

#include

#include
using namespace std;

enum MyEnum
{
sun=1, mon, tus, wed, thu, fri, sat //此处结尾无分号
};

//根据每位医生的限制条件
void f()
{
int f = thu;//F医生
for (size_t a = sun; a <= sat; a++)
if (a != f)
for (int b = sun; b <= sat; b++)
if (b != a&&f > b)
for (int c = sun; c <= sat; c++)
if (c != a&&c > f&&c + 1 == a&&c!=b)
for (int d = sun; d <= sat; d++)
if (d != a&&d!=b&&d!=c&&a!=f)
for (int e = sun; e <= sat; e++)
if (e != a&&e!=b&&e!=c&&d == e + 2&&e!=f)
for (int g = sun; g <= sat; g++)
if(g!=a&&g==b+3&&g!=c&&g!=d&&g!=e&&g!=f)
cout << a << “,” << b << “,” << c << “,” << d << “,” << e << “,” << f << “,” << g << endl;

}

int main()
{
f();
system(“pause”);
return 0;
}

//43//
//重载流提取和流输入 << >>

#include

#include
using namespace std;

class MyClass
{
public:
MyClass(int aa):a(aa){}
friend istream &operator>>(istream&, MyClass&);
friend ostream &operator<<(ostream&, MyClass&);

private:
int a;
};

istream &operator >>(istream&mycin, MyClass&obj1)
{
mycin >> obj1.a;
return mycin;
}
ostream &operator<<(ostream&mycout, MyClass&obj2)
{
mycout << “这是一个对象输出:” << obj2.a << endl;
return mycout;
}
int main()
{
MyClass obj(666);
cin >> obj;
cout << obj;
system(“pause”);
return 0;
}

//43//
//转换构造函数

#include
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(int a) { real = a; imag = 0; }//转换构造 只有一个形参
MyClass(int a,int b) { real = a; imag = b; }
MyClass operator+(MyClass&obj)
{
real += obj.real;
imag += obj.imag;
return *this;
}
void dis()
{
cout << real << “,” << imag;
}
private:
int real, imag;
};

int main()
{
MyClass obj1(3),obj2;
obj2 = obj1 + MyClass(3.5); //注意转换函数的使用是:类名(参数)
obj2.dis();
system(“pause”);
return 0;
}

//44//
//类型转换函数

#include
using namespace std;

class MyClass
{
public:
MyClass(){}
MyClass(int a) { real = a; imag = 0; }//转换构造 只有一个形参
MyClass(int a,int b) { real = a; imag = b; }
operator int()//没有参数 返回类型为int 只能做为成员函数
{
return real;
}
friend MyClass operator+(MyClass&,MyClass&);
void dis()
{
cout << real << “,” << imag << endl;
}
private:
int real, imag;
};

MyClass operator+(MyClass&obj1,MyClass&obj2)
{
MyClass obj;
obj = obj1.real + obj2.real;
obj = obj1.imag += obj2.imag;
return obj;
}

int main()
{
MyClass obj1(3),obj2;
obj2 = obj1 + MyClass(3); //注意转换函数的使用是:类名(参数)
obj2 = 3 + obj1;
obj2 = obj1 + 3;
obj2.dis();

MyClass obj(12,13);
int a = 45;
a = a + obj;
a = obj + 56;
cout << a;
system("pause");
return 0;

}

//45//
//重载流提取和流输入 << >> 重定向输出到文件

#include

#include
using namespace std;

class MyClass
{
public:
MyClass(int aa) :a(aa) {}
friend istream &operator>>(istream&, MyClass&);
friend ostream &operator<<(ostream&, MyClass&);

private:
int a;
};

istream &operator >>(istream&mycin, MyClass&obj1)
{
cout << “输入一个整数:” << endl;
mycin >> obj1.a;
return mycin;
}
ostream &operator<<(ostream&mycout, MyClass&obj2)
{
mycout << “这是一个对象输出:” << obj2.a << endl;
return mycout;
}
int main()
{
FILE *stream; //建立一个流对象作为freopen_s()的第一个参数 freopen()无需第一个参数
freopen_s(&stream,”test.txt”, “w”, stdout);//没有文件时自动建立
MyClass obj(666);
cin >> obj; //还是要求从键盘输入 但不会在屏幕输出
cout << obj;
system(“pause”);
return 0;
}

//46//
//存在派生类和的子对象的构造函数
//顺序为:
/
基类
子对象
派生类
/

#include

#include
using namespace std;

class MyClass1
{
public:
MyClass1(int aa): a1(aa){}
void dis()
{
cout << a1 << endl;
}
private:
int a1;
};

class MyClass2:public MyClass1
{
public:
MyClass2(int aa,int bb,int cc):MyClass1(aa),obj1(cc)
{
a2 = bb;
}
void dis()
{
MyClass1::dis();//若不加限定符存在二义性
cout << a2 << endl;
obj1.dis();
}
private:
int a2;
MyClass1 obj1;
};

int main()
{
MyClass2 obj2(23,26,666);
obj2.dis();
system(“pause”);
return 0;
}

//47//
//存在派生类和的子对象的构造函数
//顺序为:
/
基类
子对象
派生类
/

#include

#include
using namespace std;

class MyClass3
{
public:
MyClass3(int aa) :a3(),aa3(aa) {}
void dis()
{
cout << a3 << endl << aa3 << endl;
}
int aa3;
private:
int a3;
};

class MyClass1
{
public:
MyClass1(int aa): a1(aa){}
void dis()
{
cout << a1 << endl;
}
private:
int a1;
};

class MyClass2:public MyClass1
{
public:
MyClass2(int aa,int bb,int cc):MyClass1(aa),obj1(cc),obj3(cc)
{
a2 = bb;
}
void dis()
{
MyClass1::dis();//若不加限定符存在二义性
cout << a2 << endl;
obj1.dis();
obj3.dis();
}
private:
int a2;
MyClass1 obj1;
MyClass3 obj3;
};

int main()
{
MyClass2 obj2(23,26,666);
obj2.dis(); //输出:23,26,666 0 666 ,知,无法对非基类的子对象进行访问私有数据
system(“pause”);
return 0;
}

//48//
//虚基类的构造函数
//虚基类:在继承间接共同基类时只保留一份成员 至少4个类之间的使用

#include

#include
using namespace std;

class MyClass1
{
public:
MyClass1(int aa): a(aa){}
protected: //private时派生类无法访问
int a;
};

class MyClass2:public virtual MyClass1 //virtual和public的顺序无所谓
{
public:
MyClass2(int bb):b(bb),MyClass1(bb){}
protected://private时派生类无法访问
int b;
};

class MyClass3:virtual public MyClass1
{
public:
MyClass3(int cc) :c(cc), MyClass1(cc) {}
protected://private时派生类无法访问
int c;
};

//class MyClass4:public MyClass2,public MyClass3 //和下一行写法不同但用法相同,加virtual是为了派生类不会产生不明确的继承,此处的MyClass4没有派生类 故无需添加virtual
class MyClass4 :public virtual MyClass2, virtual public MyClass3
{
public:
MyClass4(int dd) :d(dd), MyClass1(dd),MyClass2(dd),MyClass3(dd) {} //此处要对基类的基类的构造函数MyClass1(int aa)初始化
void dis()
{
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;

}

protected:
int d;
};

int main()
{
MyClass4 obj(666);
obj.dis();
system(“pause”);
return 0;
}

//49//
//基类与派生类之间的转换

#include

#include
using namespace std;

class MyClass1
{
public:
MyClass1(int aa): a(aa){}
int a;
};

class MyClass2:public virtual MyClass1 //virtual和public的顺序无所谓
{
public:
MyClass2(int bb):b(bb),MyClass1(bb){}
int b;
};

void f(MyClass1 &obj)
{
cout << obj.a << endl;
//cout << obj.b; error 无法访问派生类的成员,即使调用时实参是MyClass2的对象
}

int main()
{
MyClass2 obj(666);
f(obj);
system(“pause”);
return 0;
}

//50//
//多态性的使用(虚函数)
/
类:点;线;正方形;正方体
/

#include

#include
using namespace std;

class point
{
public:
point(double x,double y): a(x),b(y){}
double a,b;
void virtual dis()
{
cout << “坐标为:(“ << a << “,” << b << “)” << endl;
}
};

class line:public point
{
public:
line(double x1,double y1,double x2,double y2):a(x1),b(y1), c(x2), d(y2), point(x1,y1){}
double a,b,c,d;

void virtual dis()
{
    double k, m;//y=kx+m
    k = (b - d) / (a - c);
    m = b - k*a;
    if (m == 0)
        cout << "直线为:y=" << k << "x" << endl;
    else
        cout << "直线为:y=" << k << "x+" << m << endl;
}

};

class zhengfangxing:public line
{
public:
zhengfangxing(int x,int y):a(y-x), line(x,y,x,y){}
int a;//边长
void virtual dis()//面积
{
double s;//y=kx+m
s = a*a;
cout << “面积为:s=” << s << endl;
}
};

class lifangti :public zhengfangxing
{
public:
lifangti(int x, int y) :a(y - x), zhengfangxing(x, y) {}
int a;//边长
void virtual dis()//体积
{
double v;//y=kx+m
v = aaa;
cout << “体积为:v=” << v << endl;
}
};

int main()
{
//一般使用
point obj1(666,44);
line obj2(3, 8, 0, 1);
zhengfangxing obj3(4,8);
lifangti obj4(4,8);
obj1.dis();
obj2.dis();
obj3.dis();
obj4.dis();

//使用虚函数(以下输出和上边完全相同)
point *p; //基类指针分别指向不同的派生类
p = &obj1;
p->dis();
p = &obj2;
p->dis();
p = &obj3;
p->dis();
p = &obj4;
p->dis();

system("pause");
return 0;

}

//51//
//多态性的使用(纯虚函数)
/
抽象基类:存在纯虚函数的基类 shape
类:点;正方形;正方体
/

#include

#include
using namespace std;

class shape
{
public:
virtual void point() { cout << “点坐标为:” << 0 << “,” << 0 << endl; }
virtual void area() { cout << “面积为:” << 0 << endl; }
virtual void v() { cout << “体积为:” << 0 << endl; }
virtual void shapeName() = 0;
};
class mypoint:public shape
{
public:
mypoint(double x,double y): a(x),b(y){}
double a,b;
virtual void shapeName()
{
cout << “this is a point. “ << endl;
point();
area();
v();
}
void point()
{
cout << “坐标为:(“ << a << “,” << b << “)” << endl;
}
};

class myzhengfangxing:public shape
{
public:
myzhengfangxing(int x,int y):a(y-x){}
int a;//边长
void area()//面积
{
double s;//y=kx+m
s = a*a;
cout << “面积为:s=” << s << endl;
}
virtual void shapeName()
{
cout << “this is a zhengfangxing. “ << endl;
point();
area();
v();
}
};

class mylifangti :public shape
{
public:
mylifangti(int x, int y) :a(y - x){}
int a;//边长
void v()//体积
{
double v;//y=kx+m
v = aaa;
cout << “体积为:v=” << v << endl;
}
virtual void shapeName()
{
cout << “this is a lifangti. “ << endl;
point();
area();
v();
}
};

int main()
{
//一般使用
mypoint obj1(666,44);
myzhengfangxing obj2(4,8);
mylifangti obj3(4,8);
obj1.shapeName();
obj2.shapeName();
obj3.shapeName();

cout << endl;
//使用虚函数(以下输出和上边完全相同)
shape *p; //基类指针分别指向不同的派生类
p = &obj1;
p->shapeName();
p = &obj2;
p->shapeName();
p = &obj3;
p->shapeName();
system("pause");
return 0;

}

//52//
//main.cpp
//头文件的使用 在classmypoint.h 中声明mypoint类

#include

#include “classmypoint.h” //自己的头文件必须双引号
using namespace std;

int main()
{
mypoint obj1(666,44);
obj1.point();
system(“pause”);
return 0;
}

classmypoint.h

#include //此文件中用到的库要加到此文件的头文件
using namespace std;
class mypoint
{
public:
mypoint(double x, double y) : a(x), b(y) {}
double a, b;
void point()
{
cout << “坐标为:(“ << a << “,” << b << “)” << endl;
}
};

//53//
//流对象控制输出 特殊函数(字符函数)输入输出

#include
using namespace std;

void f(char t)
{
//输出一个字符的两个函数
cout.put(65); //等价putchar(65); 等价cout.put(‘A’);
cout.put(‘\n’);
//输入函数get的3中用法
/
cin.get();//①取一个字符,包括空白(空格)
char ch;
cin.get(ch);//②赋值给ch
char str1[50];
cin.get(str1,7,’a’);//③赋值给str[]数组 参数分别是:指针 读取个数(n-1) 终止字符(不论是否达到终止个数,遇到结束符即刻终止)
cout << str1 << endl;
*/
//char str2[50];
//cin.getline(str2, 7, ‘a’); //参数分别是:指针 读取个数(n - 1) 终止字符(不论是否达到终止个数,遇到结束符即刻终止) 输出时不包括终止字符
//cout << str2 << endl;

//控制符
int a = 21;
cout.setf(ios::showbase); //显示基数符号(0x 0)
cout << a << endl;
cout.unsetf(ios::dec);//终止10进制
cout.setf(ios::hex);//16进制 必须对10进制的设置终止,否则不起作用
cout << a << endl;
cout.unsetf(ios::hex);//终止10进制
cout.setf(ios::oct);//8进制 必须对16进制的设置终止,否则不起作用
cout << a << endl;
cout.width(10); //仅对一个输出变量有效
cout << t << t << endl; //第一个t不足10位时左边自动以空格替代
cout.fill('*');
cout.width(10);
cout << t << t << endl; //第一个t不足10位时左边以*替代
double b = 0.31415926;
cout.setf(ios::scientific);
cout << b << endl;
cout.unsetf(ios::scientific);
cout.setf(ios::fixed);//默认小数点后6位输出
cout << b << endl;
cout.precision(20);
cout.setf(ios::fixed);//小数点后10位输出 原数小于10时,0补充或取近似值 如1取为0.99999
cout << b << endl;
cout.setf(ios::showpos | ios::right);
cout << b << endl;

}

void f2()
{
//eof
char c;
while (!cin.eof())
{
c = cin.get();
if ( c == ‘a’)
break;
cout.put(c);
}
}
int main()
{
char t[111] = {“dfdgg”};
f(t);
f2();
system(“pause”);
return 0;
}

//54//
//文件操作 (ASCII 、二进制)

#include

#include
using namespace std;
/*
f1()输入输出ASCII
f2()输入输出二进制
f3()二进制的输入输出同时操作 及修改磁盘二进制数据后再次存放到磁盘

istream& read(char buffer,int len)为从文件以二进制方式读出
ostream& write(const char
buffer,int len)以二进制方式写入到文件
*/

void f1()//对ASCII输出到文件
{
ofstream outfile(“test.txt”,ios::out); //文件不存在会自动创建 若原来存在会覆盖
if (!outfile)
{
cerr << “open error!” << endl;
exit(1);
}
outfile << “dfsdf” << endl << 999;
outfile.close();
}

void f11()//用 ASCII输入
{
ifstream infile(“test.txt”,ios::in | ios::_Nocreate);//不创建新文件 若不存在则打开失败
if (!infile)
{
cerr << “fail” << endl;
exit(1);
}
int a[2];
for (size_t i = 0; i < 2; i++) //若与文件信息数据类型不匹配,出现意想不到的结果
{
infile >> a[i];
cout << a[i] << endl;
}
infile.close();
}

void f12()//用 ASCII输入
{
ifstream infile(“test.txt”, ios::in | ios::_Nocreate);//不创建新文件 若不存在则打开失败
if (!infile)
{
cerr << “fail” << endl;
exit(1);
}
char ch;
for (; infile.get(ch);) //空格 空行 均可读入
{
cout.put(ch);
}
infile.close();
}

void f2()//二进制方式输出到文件
{
struct student
{
char name[20];
int num;
int age;
};
student stu[2] = {“wang”,10086,18,”zhao”,10084,18 };
ofstream outfile(“stu.txt”,ios::binary);
if (!outfile)
{
cerr << “error…” << endl;
abort();
}
outfile.write((char*)&stu[0], sizeof(stu)); //stu[0]是起始地址 sizeof(stu)是整个结构体变量的大小
outfile.close();
}

void f21()//二进制方式输出到文件
{
struct student
{
char name[20];
int num;
int age;
};
student stu[2];
ifstream infile(“stu.txt”, ios::binary);
if (!infile)
{
cerr << “error…” << endl;
abort();
}
infile.read((char*)&stu[0], sizeof(stu));
infile.close();
for (size_t i = 0; i < 2; i++)
{
cout << stu[i].name << endl;
cout << stu[i].num << endl << endl;

}

}

void f3()
{
struct student
{
char name[20];
int num;
int age;
};
student stu[5] = { “q”,100,18,”w”,101,18 ,”e”,102,18,”r”,103,18 ,”t”,104,18};
fstream iofile(“stu.txt”, ios::binary | ios::in | ios::out); //ios::in | ios::out必须添加,否则无法操作
if (!iofile)
{
cerr << “error…” << endl;
abort();
}
//存入到磁盘
iofile.write((char*)&stu[0], sizeof(stu));

//从磁盘文件读取第1,3,5个学生数据
student stu_1[5];
for (size_t i = 0; i < 5; i+=2)
{
    iofile.seekg(i*sizeof(stu[i]), ios::beg);//定位在1,3,5学生数据开头
    iofile.read((char*)&stu_1[i/2],sizeof(stu_1[0]));//读取存放
    cout << stu_1[i / 2].name << "," << stu_1[i / 2].num << endl;
}

//修改第3人的信息
/*stu_1[2].name = "WTO";*/ //直接重新复制出错,应使用strcpy(str1,str2)
strcpy_s(stu[2].name, "WTO");
stu[2].num = 666;
iofile.seekg(2 * sizeof(stu[0]), ios::beg); //定位
iofile.write((char*)&stu[2], sizeof(stu[2])); //写入更新 stu[2]为开始位置
iofile.seekg(0, ios::beg);//重定位到文件开头
iofile.read((char*)&stu[0], sizeof(stu)); 
cout.put('\n');
for (size_t i = 0; i < 5; i++)
{
    cout << stu[i].name << ",";
    cout << stu[i].num << endl;
}
iofile.close();

}

int main()
{
//f1(); //f11();
//f12();

/*f2();
f21();*/

f3();
system("pause");
return 0;

}

//55//
//文件操作2

#include

#include
using namespace std;
/*要求
①输入4个数,存放在两个文件(1.txt,2.txt),每个文件2个数
②把1.txt中的2个数存放在2.txt后
③对2.txt中的4个数排序,排序后从新存放到2.txt

istream& read(char buffer,int len)为从文件以二进制方式读出
ostream& write(const char
buffer,int len)以二进制方式写入到文件
*/

void f1()//ASCII方式
{
int a;
ofstream outfile(“1.txt”);//默认以ios::out
if (!outfile)
{
cerr << “error…” << endl;
abort();
}
for (size_t i = 0; i < 2; i++)
{
cin >> a;
outfile << a << “ “;
}
outfile.close(); //不关闭直接打开下一个文件,提示error…

outfile.open("2.txt");//默认以ios::out
if (!outfile)
{
    cerr << "error..." << endl;
    abort();
}
for (size_t i = 0; i < 2; i++)
{
    cin >> a;
    outfile << a << " ";
}
outfile.close();


ifstream infile("1.txt");
if (!infile)
{
    cerr << "error..." << endl;
    abort();
}
int t[2];
for (size_t k = 0; k < 2; k++)
{
    infile >> t[k];
}
infile.close();

outfile.open("2.txt",ios::app); //打开文件,并使指针指向末尾
if (!outfile)
{
    cerr << "error..." << endl;
    abort();
}
for (size_t k = 0; k < 2; k++)
{
    outfile << t[k] << " ";
}
outfile.close();


infile.open("2.txt");
if (!infile)
{
    cerr << "error..." << endl;
    abort();
}
int t2[4];
for (size_t k = 0; k < 4; k++)
{
    infile >> t2[k];
}
infile.close();

int temp;  //冒泡
for (size_t i = 0; i < 4-1; i++)
{
    for (size_t j = 0; j < 4 - i - 1; j++)
        if (t2[j]>t2[j + 1])
        {
            temp = t2[j];
            t2[j] = t2[j + 1];
            t2[j + 1] = temp;
        }
}

cout << "排序后的数据:" << endl;
for (size_t i = 0; i < 4; i++)
{
    cout << t2[i] << endl;
}

outfile.open("2.txt"); //打开文件,重新写入,覆盖原有数据
if (!outfile)
{
    cerr << "error..." << endl;
    abort();
}
for (size_t k = 0; k < 4; k++)
{
    outfile << t2[k] << " ";
}
outfile.close();

}

int main()
{

f1();
system("pause");
return 0;

}

//56//
//异常处理和命名空间 可以多个try catch 一个throw

#include

#include

#include
using namespace std;
/知三角形三边求面积 /
namespace myname
{
void f1(double a, double b, double c)//面积求解
{
double s = (a + b + c) / 2;
if ((a + b <= c) || (a + c <= b) || (c + b <= a))
throw a;
cout << “面积:” << sqrt(s(s - a)(s - b)*(s - c)) << endl;

}

void f2(int a)
{
    try
    {
        if (a<0)throw a;
        cout << a << endl;
    }
    catch (char b)
    {
        cout << "a error...";
    }
}

}

using namespace myname;//只能在主函数前声明 不能在文件开头声明

int main()
{
try
{
//f1(11, 78, 89); //只能捕获一个异常,即遇到异常就转到catch 不再执行以下内容
f2(-2);
}
catch (int a) //参数类型找不到时(不匹配),等于没写
{
cout << “error…” << endl << a << endl;;
}
system(“pause”);
return 0;
}

//57//
//运算符的优先级

#include

#include

#include
using namespace std;

void f()
{
//优先级
//1.:: void MyClass::ff(); //类内声明成员函数 (MyClass可省略)
//2.() [] -> . ++(后置) –(后置)
//3.++ – ~ ! -(负号) +(正号) (指针) &(取地址) (类型转换) sizeof new delete
//4.
(乘) % /
//5.+(加) -
//6.<<(按位左移) >>
//7. < <= > >=
//8.== !=
//9.&(按位与运算)
//10.^ (异或)
//11.| (按位或)
//12.&&
//13.||
//14.三目
//15.赋值 = +=……
//16.throw
//17.,

//级别判断(高—低):初等运算符 单目 算术 关系 逻辑 条件 赋值 逗号
cout << "blank.." << endl;

}

int main()
{

system("pause");
return 0;

}

-------------本文结束感谢您的阅读-------------