目录
3.编写递归函数模板,要求找出最小值,并返回该值的元素下表。
5.对于数组Array类的一个对象,通过重载()运算符,实现chess(row,col)代替chess[row,col]。
1.计算e^x
科普一下iomanip头文件
io代表输入输出,manip是manipulator(操纵器)的缩写。
主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制头文件,就像C里面的格式化输出一样。
dec 置基数为10 相当于"%d"
hex 置基数为16 相当于"%X"
oct 置基数为8 相当于"%o"
setfill(c) 设填充字符为c
setprecision(n) 设显示小数精度为n位
setw(n) 设域宽为n个字符 setbase(n)以n进制输出
setiosflags(ios::fixed) 固定的浮点显示
setiosflags(ios::scientific) 指数表示
setiosflags(ios::left) 左对齐
setiosflags(ios::right) 右对齐
setiosflags(ios::skipws 忽略前导空白
setiosflags(ios::uppercase) 16进制数大写输出
setiosflags(ios::lowercase) 16进制小写输出
setiosflags(ios::showpoint) 强制显示小数点
setiosflags(ios::showpos) 强制显示符号
#include<iostream>
#include <iomanip>
#include<cmath>
using namespace std;
int main() {
double x ;
double ans=1;//结果
int n = 1;
cout << "请输入x的值:";
cin >> x;
double temp=1;
do{
temp = temp*x/n;
ans += temp;
n++;
} while (fabs(temp) >= 10e-10);
cout << "e^" <<x<<"=" << fixed << setprecision(10) << ans << endl;
return 0;
}
2.计算Π
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main() {
double ans = 4;
double i = 3;
double sign = -1;//正负号
double temp;
do {
temp = sign * 4 / i;
ans += temp;
sign = -sign;
i += 2;
} while (fabs(temp) >= 10e-10);
cout << "Π的值为:"<<setprecision(10) << ans << endl;
}
3.编写递归函数模板,要求找出最小值,并返回该值的元素下表。
#include<iostream>
using namespace std;
template<class T>
int getMin(T a[], int i,int n) {
static int min = i;
if (a[i] < a[min])
min = i;
if(i<n)
return getMin(a, i + 1, n);
return min;
}
int main() {
int a[5] = { 1,2,3,4,5 };
double b[5] = { 1.2,2.1,4.5,8.1,0.2 };
int pos1 = getMin(a, 0, 4);
int pos2 = getMin(b, 0, 4);
cout << pos1 << pos2;
}
4.字符串数组的插入排序和选择排序
1选择排序
//选择排序
#include<iostream>
#include<string>
using namespace std;
void Sort2(string a[], int n) {
int minPos;
string temp;
for (int i = 0; i < n; i++) {
minPos = i;
for (int j = i; j < n; j++) {
if (a[j] < a[minPos])
minPos = j;
}
temp = a[i];
a[i] = a[minPos];
a[minPos] = temp;
}
}
int main() {
string b[4] = { "nantong","taizhou","yangzhou","suzhou" };
Sort2(b, 4);
for (string item : b)
cout << item << endl;
}
nantong
suzhou
taizhou
yangzhou
D:\vs\Project1\x64\Debug\Project1.exe (进程 45528)已退出,代码为 0。
按任意键关闭此窗口. . .
2.插入排序
#include<iostream>
#include<string>
#include"Sort2.cpp"
using namespace std;
void sort1(string a[], int n) {
string temp;
int j;
for (int i = 0; i < n; i++) {
temp = a[i];
j = i - 1;
while (j >= 0 && temp < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
int main() {
string a[3] = { "shanghai","nanjing","hangzhou" };
sort1(a, 3);
for (string item : a)
cout << item << endl;
}
hangzhou
nanjing
shanghai
D:\vs\Project1\x64\Debug\Project1.exe (进程 53816)已退出,代码为 0。
按任意键关闭此窗口. . .
5.对于数组Array类的一个对象,通过重载()运算符,实现chess(row,col)代替chess[row,col]。
1.Array类的构造、析构、拷贝构造和基本数据成员的实现
#include<iostream>
using namespace std;
class Array {
private:
int rows;
int cols;
int* ptr;
public:
Array(int, int);
Array(const Array&);
~Array();
int& operator()(int = 0, int = 0)const;
};
Array::Array(int row, int col) {
rows = row;
cols = col;
ptr = new int[rows * cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
ptr[i * cols + j] = 0;
}
Array::Array(const Array& a) {
rows = a.rows;
cols = a.cols;
ptr = new int[rows * cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
ptr[i * cols + j] = a.ptr[i * cols + j];
}
Array::~Array() {
delete[]ptr;
}
2.()的重载
int& Array:: operator()(int row, int col)const{
if (row > rows || col > cols)
cerr << "out of range" << endl;
else
return ptr[row * cols + col];
}
3.<<和>>的重载
参考大佬的代码,还运用友元函数重载了<<和>>,方便了输入和输出,我觉得非常有价值,体现了更加的多态。
istream& operator>>(istream& input, Array& a) {//声明这里就不赘述了。
for (int i = 0; i < a.rows; i++)
for (int j = 0; j < a.cols; j++)
input >> a.ptr[i * a.cols + j];
return input;
}
ostream& operator<<(ostream& output, Array& a) {
for (int i = 0; i < a.rows; i++){
for (int j = 0; j < a.cols; j++)
output<< a.ptr[i * a.cols + j]<<ends;
cout << endl;
}
return output;
}
4主函数和结果
int main() {
Array a(4, 3);
cout << "请输入:";
cin >> a;
cout << "a:" << a<<endl;
Array chess(a);
cout << "chess:";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++)
cout << chess(i, j)<<ends;
cout << endl;
}
}
请输入:1 2 3 4 5 6 7 8 9 1 2 3
a:123
456
789
123
chess:123
456
789
123
这里其实有一个小问题,我不知道为什么我的ends用起来没有空格的效果,有知道的朋友可以说一说。
6.多态基类
1.建立抽象类然后继承,比较初级。
#define PI 3.14
#include<cmath>
#include<iostream>
using namespace std;
class Shape{
public:
virtual double calculate() const = 0 ;
};
class Circle :public Shape {//派生类圆 重写calculate()
private:
double O[2] = { 0,0 };
double radius;
public:
Circle(double x, double y, double r) {
O[0] = x;
O[1] = y;
radius = r;
}
double calculate() const {
return PI * radius * radius;
}
};
class Triangle :public Shape {
private:
double A[2] = { 0,0 };
double B[2] = { 0,0 };
double C[2] = { 0,0 };
public:
Triangle(double a1, double a2, double b1, double b2, double c1, double c2) {
A[0] = a1;
A[1] = a2;
B[0] = b1;
B[1] = b2;
C[0] = c1;
C[1] = c2;
}
double calculate() const{
return 0.5 * abs(A[0] * B[1] + B[0] * C[1] + C[0] * A[1] - A[0] * C[1] - B[0] * A[1] * B[1] * C[0]);
}
};
class Rectriangle :public Shape {
private:
double A[2] = { 0,0 };
double B[2] = { 0,0 };
public:
Rectriangle(double a1, double a2, double b1, double b2) {
A[0] = a1;
A[1] = a2;
B[0] = b1;
B[1] = b2;
}
double calculate() const{
return abs((A[0] - B[0]) * (A[1] * B[0]));
}
};
int main() {
Circle c(1, 2, 1.25);
Triangle t(1, 2, 2, 2, 0, 0);
Rectriangle r(1, 2, 0, 3);
cout << "圆的面积:" << c.calculate() << endl << "三角形面积:" << t.calculate() << endl << "矩形面积:" << t.calculate() << endl;
}
圆的面积:4.90625
三角形面积:1
矩形面积:1
2.读字符串,实现虚类指针分别指向圆等对象。
void dealCircle(string& info, Shape* p) {
double x; double y, r;
x = stod(info.substr(info.find("C") + 2, info.find(",") - 2));
info.erase(info.find("C"), info.find(",") - info.find("C") + 1);
y = stod(info.substr(0, info.find(",")));
info.erase(0, info.find(",") + 1);
r= stod(info.substr(0, info.find(",")));
info.erase(0, info.find(";") + 1);
Circle cir(x, y, r);
p = ○
cout << "圆的面积为:"<<p->calculate();
}
}
int main() {
/*Circle c(1, 2, 1.25);
Triangle t(1, 2, 2, 2, 0, 0);
Rectriangle r(1, 2, 0, 3);
cout << "圆的面积:" << c.calculate() << endl << "三角形面积:" << t.calculate() << endl << "矩形面积:" << t.calculate() << endl;*/
ifstream is("D:/word.txt");
if (!is) {
cerr << "File cannnot be opened!";
exit(EXIT_FAILURE);
}
string line;
Shape* ptr=nullptr;
while (is >> line) {
cout << "Read a Line:" << line << endl;
}
dealCircle(line, ptr);
}
2011年最难的点就在于运算符重载和最后一问的代码量,对于多态和继承的考察一直是553的重点。