面向对象实验报告
title:面向对象实验报告
date:2020-07-05 00:00:00
面向对象实验报告
/* RhInEs_liao */
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string.h>
#include <algorithm>
#include<iomanip>
#include <iterator>
#include<fstream>
#include<map>
#include<string>
#include<set>
using namespace std;
//综合应用程序CMat类
template<typename T>
class CMat {
private:
int row; //行
int col; //列
T* data; //指向数据的一维指针
public:
CMat();
CMat(int r, int c, int value);
CMat(int r, int c, T* arr);
CMat(int r, int c);
CMat(const CMat& M);
~CMat();
void printMat() const;
void saveMat(const char* filename);
void loadMat(const char* filename);
int getRows() const;
int getCols() const;
bool isSquare() const;
void reSize(int p, int q);
T operator () (int r,int c);
CMat<T> operator +(const CMat<T>M1);
CMat<T> operator -(const CMat<T>M1);
friend CMat<T> operator *(const CMat<T>M1, const CMat<T>M2) {
{
if (M1.col * M1.row == 0 || M2.col * M2.row == 0 || M1.col != M2.row) {
cout << "数据非法" << '\n';
CMat<T> temp(0, 0, 0);
temp.data = nullptr;
return temp;
}
else {
int r3 = M1.row;
int c3 = M2.col;
T* arr = nullptr;
CMat<int> ans(r3, c3, arr);
for (int i = 0; i < M1.row; i++) {
for (int j = 0; j < M2.col; j++) {
ans.data[i * M2.col + j] = 0;
for (int k = 0; k < M1.col; k++) {
ans.data[i * M2.col + j] += M1.data[i * M1.col + k] * M2.data[k * M2.col + j];
}
}
}
return ans;
}
}
}
bool operator !=(CMat<T>&M);
bool operator ==(CMat<T>&M);
CMat<T> operator=(const CMat<T> M);
CMat<T> fetchRow(int i);
CMat<T> fetchCol(int i);
void swapRows(int a,int b);
void swapCols(int a,int b);
static CMat<T> createEyeMat(int n) {
CMat<int> MatI(n, n, 0);
for (int i = 0; i < n * n; i += (n + 1)) {
MatI.data[i] = 1;
}
return MatI;
}
static double detMat(const CMat<T>M,int n) {
double sum = 0;
if (n == 2) {
sum = M.data[0] * M.data[3] - M.data[1] * M.data[2];
}
else {
for (int i = 0; i < n; i++) {
T* arr = new T[(n - 1) * (n - 1)];
CMat<T> temp(n - 1, n - 1, arr);
for (int j = 0; j < n - 1; j++) {
for (int k = 0; k < n - 1; k++) {
temp.data[j * (n - 1) + k] = M.data[(j + 1) * (n - 1) + (k < i ? k : k + 1)];
}
}
if (M.data[i]) {
sum += M.data[i] * detMat(temp, n - 1) * (((2 + i) % 2) ? -1 : 1);
}
}
}
return sum;
}
static CMat<T> convMat(CMat<T> M1, CMat<T> M2) {
int n = M1.row;//被卷矩阵规模 n*n
int f = M2.row;//卷积核规模 f*f
int p = (f - 1) / 2;//卷积填充维度
int s = (n + 2 * p - f) / (n - 1);//步长
CMat<T> M3(n, n, 0);
CMat<T> M4(n + f - 1, n + f - 1, 0);
//same输出格式;
//M3.row==M1.row,M3.col==M1.col,用n代替ANS的行列信息;
//填充:
for (int i = 0; i < M1.row; i++) {
for (int j = 0; j < M1.col; j++) {
(M4.data + (i + p) * M4.col)[j + p] = (M1.data + i * n)[j];
}
}
//卷积:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < f; k++) {
for (int z = 0; z < f; z++) {
(M3.data + i * n)[j] += (M4.data + (i + k) * M4.col)[j + z] * (M2.data + k * M2.col)[z];
}
}
}
}
return M3;
}
};
template<typename T>
CMat<T>::CMat() {
row = 0;
col = 0;
}
template<typename T>
CMat<T>::CMat(int r, int c, int value) {
row = r;
col = c;
data = new T[r * c];
for (int i = 0; i < r*c; i++) {
data[i] = value;
}
}
template<typename T>
CMat<T>::CMat(int r, int c, T* arr) {
row = r;
col = c;
arr = new T[r * c];
for (int i = 0; i < r * c; i++) {
arr[i] = 1;
}
data = arr;
}
template<typename T>
CMat<T>::CMat(int r, int c) {
row = r;
col = c;
data = new T[r * c];
for (int i = 0; i < r*c; i++) {
cin >> data[i];
}
}
template<typename T>
CMat<T>::~CMat() {
delete[]data;
data = nullptr;
}
template<typename T >
CMat<T>::CMat(const CMat& M) {
row = M.row;
col = M.col;
data = new T[M.row * M.col];
for (int i = 0; i < M.row * M.col; i++) {
data[i] = M.data[i];
}
}
template<typename T>
void CMat<T>::printMat()const {
for (int i = 0; i < row*col; i++) {
cout << data[i] << " ";
if ((i + 1) % col == 0) {
cout << '\n';
}
}
}
template<typename T>
void CMat<T>::saveMat(const char* filename) {
ofstream input;
input.open(filename, ios::out);
if (!input) {
cout << "文件打开失败"<<'\n';
}
for (int i = 0; i < col*row; i++) {
input << data[i] << " ";
if ((i + 1) % col == 0) {
input << '\n';
}
}
input.close();
}
template<typename T>
void CMat<T>::loadMat(const char* filename) {
ifstream fin(filename,ios::in);
string str1;
int old_col = 0;
int row1 = 0;
int new_col = 0;
if (!fin)cout << "error" << '\n';
while (getline(fin, str1)) {
new_col = 0;
row1++;
for (int i = 0; i < str1.size(); i++) {
while (str1[i] != ' ' && i < str1.size()) {
i++;
}
new_col++;
}
if (old_col != 0 && old_col != new_col) {
cout << "矩阵错误" << '\n';
return;
}
old_col = new_col;
}
fin.close();
fin.open(filename);
col = old_col;
row = row1;
data = new T[row * col];
T t;
T* temp = data;
while (fin >> t) {
*temp = t;
temp++;
}
cout << "提取的矩阵为:" << '\n';
for (int i = 0; i < row * col; i++) {
cout << data[i] << " ";
if ((i + 1) % col == 0) {
cout << '\n';
}
}
fin.close();
}
template<typename T>
int CMat<T>::getRows()const {
return row;
}
template<typename T>
int CMat<T>::getCols()const {
return col;
}
template<typename T>
bool CMat<T>::isSquare()const {
if (row == col) {
return true;
}
else {
return false;
}
}
template<typename T>
void CMat<T>::reSize(int p, int q) {
if (p * q == row * col) {
T* temp;
temp = new T[q * p];
for (int i = 0; i < q * p; i++) {
temp[i] = data[i];
}
delete[]data;
data = temp;
row = p;
col = q;
}
else {
cout << "无法进行resize" << '\n';
}
}
template<typename T>
T CMat<T>::operator ()(int r, int c) {
if (r + 1 <= row && c + 1 <= col) {
return*(data + r * c);
}
else {
cout << "行列超出范围,将返回-1。" << '\n';
return -1;
}
}
template<typename T>
CMat<T> CMat<T>::operator +(const CMat<T>M1) {
if (col != M1.col || row != M1.row) {
cout << "行列不一致无法运算" << '\n';
CMat<T> temp(0, 0);
temp.data = nullptr;
return temp;
}
else {
CMat<int>M3(row,col,0);
for (int i = 0; i < row * col; i++) {
M3.data[i] = this->data[i] + M1.data[i];
}
return M3;
}
}
template<typename T>
CMat<T> CMat<T>::operator -(const CMat<T>M1) {
if (col != M1.col || row != M1.row) {
cout << "行列不一致无法运算" << '\n';
CMat<T> temp(0, 0);
temp.data = nullptr;
return temp;
}
else {
CMat<int>M3(row, col, 0);
for (int i = 0; i < row * col; i++) {
M3.data[i] = this->data[i] - M1.data[i];
}
return M3;
}
}
template<typename T>
CMat<T> operator*(const CMat<T>obj1, T a) {
CMat<T>temp(obj1.row, obj1.col, obj1.data);
for (int i = 0; i < temp.col; i++) {
temp.data[i] = temp.data[i] * a;
}
return temp;
}
template<typename T>
CMat<T> operator * (const CMat<T> M1, const CMat<T> M2);
template<typename T>
bool CMat<T>::operator ==(CMat<T>& M) {
for (int i = 0; i < row * col; i++) {
if (data[i] != M.data[i]) {
return 0;
}
}
return 1;
}
template<typename T>
bool CMat<T>::operator !=(CMat<T>& M) {
bool flag = 0;
for (int i = 0; i < row * col; i++) {
if (data[i] != M.data[i]) {
flag= 1;
}
}
return flag;
}
template<typename T>
CMat<T> CMat<T>::operator=(const CMat<T>M)
{
if (this->data != NULL) {
delete[]this->data;
row = 0;
col = 0;
}
data = new T[M.col * M.row];
row = M.row;
col = M.col;
for (int i = 0; i < M.col * M.row; i++)
{
this->data[i] = M.data[i];
}
return *this;
}
template<typename T>
CMat<T> CMat<T>::fetchRow(int i) {
CMat<T> temp(1, col,0);
for (int j = i * col,k=0; j < (i + 1) * col; j++,k++) {
temp.data[k] = data[j];
}
return temp;
}
template<typename T>
CMat<T> CMat<T>::fetchCol(int i) {
CMat<T> temp(1, row,0);
for (int j = i, k = 0; j < row*col; j+=col, k++) {
temp.data[k] = data[j];
}
return temp;
}
template<typename T>
void CMat<T>::swapRows(int a, int b) {
if (a < row && b < row && a >= 0 && b >= 0) {
T* temp = new T[col];
for (int i = 0; i < col; i++) {
temp[i] = fetchRow(b).data[i];
}
for (int i = 0; i < col; i++) {
data[b * col + i] = data[a * col + i];
}
for (int i = 0; i < col; i++) {
data[a * col + i] = temp[i];
}
}
else
{
cout << "超出交换范围" << endl;
}
}
template<typename T>
void CMat<T>::swapCols(int a, int b) {
if (a < col && b < col) {
T* temp = new T[row];
for (int i = 0; i < row; i++) {
temp[i] = fetchCol(b).data[i];
}
for (int i = 0; i < row; i++) {
data[b + i * col] = data[a + i * col];
}
for (int i = 0; i < row; i++) {
data[a + i * col] = temp[i];
}
}
else {
cout << "超出交换范围" << endl;
}
}
int main() {
cout << "构造函数:" << '\n';
CMat<int>M1(3, 2, 2);
cout << "矩阵M1:" << '\n';
M1.printMat();
CMat<int>M2(2,3);
cout << "矩阵M2:" << '\n';
M2.printMat();
int* arr = nullptr;
CMat<int>M3(2, 3, arr);
cout << "矩阵M3:" << '\n';
M3.printMat();
cout << "***************************************************" << '\n';
cout << "调用getRows、getCols()、isSquare()、reSize函数" << endl;
cout << "矩阵M1列数row=" << M1.getCols() << endl;
cout << "矩阵M1行数col=" << M1.getRows() << endl;
cout << "矩阵M1是否为方阵:" << M1.isSquare() << endl;
cout << "矩阵M2 resize前:" << endl;
M2.printMat();
cout << "矩阵M2 resize后:" << endl;
M2.reSize(3, 2);
M2.printMat();
M2.reSize(2, 3);//恢复原样;*/
cout << "***************************************************" << '\n';
cout <<"重载运算符函数:()、+、-、*、==、!=" << '\n';
CMat<int>ans(1, 1, 0);
cout << "M1矩阵如下:" << '\n';
M1.printMat();
cout << "M2矩阵如下:" << '\n';
M2.printMat();
cout << "输入行数列数,获取M1矩阵的元素:";
int r, c;
cin >> r >> c;
cout << "M2矩阵的第" << r << "行" << c << "列元素:" << M2(r, c) << '\n';
cout << "矩阵2与矩阵3相加后,结果如下" << '\n';
ans = M2 + M3;
ans.printMat();
cout << "矩阵2与矩阵3相减后,结果如下" << '\n';
ans = M2 - M3;
ans.printMat();
cout << "矩阵1与矩阵2相乘后,结果如下" << '\n';
ans = M1 * M2;
ans.printMat();
cout << "判断矩阵1与矩阵2是否相等" << '\n';
if (M1 == M2) {
cout << "相等" << '\n';
}
else cout << "不等" << '\n';
cout << "***************************************************" << '\n';
cout << "fetch函数、swap函数功能测试:" << '\n';
CMat<int>temp1(1, 1, 0);
cout << "M2矩阵的第0行:";
temp1 = M2.fetchRow(0);
temp1.printMat();
cout << "M2矩阵的第1列:";
temp1 = M2.fetchCol(1);
temp1.printMat();
cout << "交换矩阵M2的第0行和第1行" << '\n';
cout << "交换前:" << '\n';
M2.printMat();
cout << "交换后:" << '\n';
M2.swapRows(0, 1);
M2.printMat();
cout << "交换矩阵M2的第0列和第2列" << '\n';
cout << "交换前:" << '\n';
M2.printMat();
cout << "交换后:" << '\n';
M2.swapCols(0, 2);
M2.printMat();
cout << "***************************************************" << '\n';
cout << "createEyeMat,detMat函数功能测试:" << '\n';
cout << "输入单位矩阵的阶数:";
int n;
cin >> n;
CMat<int>M4(n, n, 0);
M4=CMat<int>::createEyeMat(n);
cout << n << "阶单位矩阵:" << '\n';
M4.printMat();
cout << "detMat计算行列式" << '\n';
cout << "输入矩阵M5的4*4个元素:";
CMat<double>M5(4, 4);
cout << "结果:" << CMat<double>::detMat(M5, 4) << '\n';
cout << "***************************************************" << '\n';
cout << "saveMat函数、loadMat函数功能测试:" << '\n';
CMat<double>M6(3, 3);
char* file1, * file2;
file1 = new char[40];
file2 = new char[40];
cout << "输入保留文件的名字";
cin >> file2;
M6.saveMat(file2);
cout << "输入读取文件的名字";
cin >> file1;
M6.loadMat(file1);
cout << "***************************************************" << '\n';
cout << "convMat测试" << '\n';
cout << "目标矩阵为" << endl;
M5.printMat();
cout << "卷积核为" << endl;
M6.printMat();
CMat<double>M7(4, 4, 1);
M7 = CMat<double>::convMat(M5, M6);
cout << "卷积计算结果为:" << endl;
M7.printMat();
cout << "***************************************************" << '\n';
return 0;
}
//模版(1)
template<typename T1>
T1 MAX( T1 x, T1 y, T1 z) {
return (x > y ? x : y) > z ? (x > y ? x : y) : z;
}
int main() {
double M1 = MAX<double>(-5.55, 0.55, 4.44);
char M2 = MAX<char>('a', 'z', 'A');
int M3 = MAX<int>(-100, 5, 0);
cout << "MAX(-5.55, 0.55, 4.44)=" << M1 << '\n';
cout << "MAX('a', 'z', 'A')=" << M2 << '\n';
cout << "MAX(-100, 5, 0)=" << M3 << '\n';
return 0;
}
//模版(2)
template<typename T1>
class Data {
public:
Data(int i) {
size = i;
a = new T1[size];
}
virtual ~Data() {
delete[]a;
}
void showdata() {
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
cout << '\n';
}
void putdata(int i) {
cout << a[i] << '\n';
}//功能1
void setdata(int index,int num) {
a[index] = num;
}//功能2
void insert(int index,T1 b) {
T1* temp3 = new T1[size+1];
for (int i = 0; i <= index; i++) {
temp3[i] = a[i];
}
temp3[index + 1] = b;
for (int i = index + 2; i < size+1; i++) {
temp3[i] = a[i - 1];
}
delete[]a;
a = temp3;
size++;
}//功能3(下标后插)
void remove(int index ) {
T1* temp4 = new T1[size - 1];
for (int i = 0; i < index; i++) {
temp4[i] = a[i];
}//下标前面数据
for (int i = index; i < size-1; i++) {
temp4[i] = a[i + 1];
}
delete[]a;
a = temp4;
size--;
}//功能4
void add(T1 b) {
T1* temp5 = new T1[size + 1];
for (int i = 0; i < size; i++) {
temp5[i] = a[i];
}
temp5[size] = b;
delete[]a;
a = temp5;
size++;
}//功能5
private:
int size;
T1* a;
};
int main() {
int len;
cout << "输入数组长度:";
cin >> len;
Data<int>Array(len);
int i;
for (int j = 0; j < len; j++) {
cin >> i;
Array.setdata(j,i);
}
cout << "数组为:";
Array.showdata();
cout << "******************************************" << '\n';
cout << "功能一:根据下标取数组元素的值,请输入下标:";
int index1;
cin >> index1;
cout << "该元素值为:";
Array.putdata(index1);//功能1
cout << "功能二:根据下标设置数组元素的值,请输入下标以及设置值:";
int index2,num1;
cin >> index2>>num1;
cout << "下标" << index2 << "原值为:";
Array.putdata(index2);
Array.setdata(index2, num1);
cout << "下标" << index2 << "设置后的值:";
Array.putdata(index2);
cout << "数组为:";
Array.showdata();
cout << "功能三:根据下标在指定位置插入数组元素,请输入下标和插入的元素:";
int index3, num2;
cin >> index3 >> num2;
Array.insert(index3, num2);
cout << "数组为:";
Array.showdata();
cout << "功能四:根据下标删除指定位置的数组元素,请输入下标:";
int index4;
cin >> index4;
Array.remove(index4);
cout << "数组为:";
Array.showdata();
cout << "功能五:在数组尾部增加元素,请要添加元素的个数:";
int count, tempnum;
cin >> count;
while (count--) {
cout << "输入要增加的元素:";
cin >> tempnum;
Array.add(tempnum);
}
cout << "数组为:";
Array.showdata();
}
//模版(3)
typedef map<string,set<int>> maps;//map字典,键是单词,值是一个set容器(存储int类型,存储的是该单词所在的行数的集合)
int main()
{
maps ans; string word;
ifstream files("dict.txt");
int line = 0;
string dict = "";
while (getline(files, word)){
for (int i = 0; i < word.size(); i++)
{
if (word[i] != ' ')dict += word[i];
else {
ans[dict].insert(line);
dict = "";
}
}
//对该行最后一个
ans[dict].insert(line);
line++; dict = "";
}
cout << "单词\t出现在第几行\n";
maps::iterator iter1;
iter1 = ans.begin();
while (iter1 != ans.end()) {
cout << iter1->first << '\t';
for (set<int>::iterator iter2 = ans[iter1->first].begin(); iter2 != ans[iter1->first].end(); iter2++) {
cout << *iter2 << " ";
}
for (auto a : ans[iter1->first]) {
cout << a << " ";
}
cout << '\n';
iter1++;
}
files.close();//结束时关闭文件
return 0;
}
//继承与多态性(1)
class Student {
private:
string name;
long long number;
string school;
public:
Student(string name1,long long num1,string school1): name(name1),number(num1),school(school1){}
//缺省↓
Student() {
name = "RhInEs";
number = 1906100071;
school = "Guangzhou University";
}
void print() {
cout << "————————————————" << '\n';
cout << "学生信息: " << '\n';
cout << "姓名: " << name << '\n';
cout << "学号: " << number << '\n';
cout << "学校: " << school << '\n';
}
};
class Postgraduate :public Student {
private:
string study_direction;
string teacher;
public:
Postgraduate(string name1,long long num1,string school1,string direction1,string teacher1)
:Student(name1,num1,school1),study_direction(direction1),teacher(teacher1){}
void print() {
cout << "————————————————" << '\n';
cout << "研究生信息:" << '\n';
cout << "研究方向:" << study_direction << '\n';
cout << "导师:" << teacher << '\n';
}
};
int main() {
Student stu1;
stu1.print();
Student stu2("Tom", 1906100999, "GZHU");
stu2.print();
Postgraduate stu3("Jerry", 1906100111, "GZHU", "Computer Science", "Jacky");
stu3.Student::print();
stu3.print();
return 0;
}
//继承与多态性(2)
class Base {
public:
virtual void area() {
//虚函数
}
};
class Triangle :public Base {
public:
Triangle(double x,double y,double z):a(x),b(y),c(z){}
void area() {
double areas = 0;
int p = (this->a + this->b + this->c) / 2;
areas = sqrt(p * (p - this->a) * (p - this->b) * (p - this->c));
cout << "三角形面积是:" << areas << '\n';
}
protected:
double a;
double b;
double c;
};
class Circle :public Base {
public:
Circle(double n) :r(n){}
void area() {
double areas = 0;
areas = 3.14 * r * r;
cout << "圆形面积是:" << areas << '\n';
}
protected:
double r;
};
int main() {
Base* s;
double x,a,b,c;
cout << "输入圆的半径,三角形的三条边,4个为0时停止计算。" << '\n';
while (cin >> x >> a >> b >> c) {
if (x == 0&& 0== b&& 0== c&&a == 0) break;
s = new Circle(x);
s->area();
s = new Triangle(a, b, c);
s->area();
}
return 0;
}
//继承与多态性(3)
class Employee {
public:
virtual void display() = 0;
virtual void getWage() = 0;
Employee(double a) {
cout << " 输入编号: " << '\n';
cin >> num;
cout << " 输入姓名: " << '\n';
cin >> name;
money = a;
}
protected:
int num;
string name;
double money;
};
//经理↓
class Manager :public Employee {
public:
Manager(double a):Employee(a){}
virtual void display() {
cout << " 编号:" << num << " " << " 经理:" << name << '\n';
}
virtual void getWage() {
cout << " 月薪:" << money << '\n';
}
};
//兼职技术人员↓
class Technician :public Employee {
public:
Technician(double a) :Employee(a) {}
virtual void display() {
cout << " 编号:" << num << " " << " 兼职技术人员:" << name << '\n';
}
virtual void getWage() {
cout << " 月薪:" << money << '\n';
}
};
//销售员↓
class Saleman :public Employee {
public:
Saleman(double a) :Employee(a) {}
virtual void display() {
cout << " 编号:" << num << " " << " 销售员:" << name << '\n';
}
virtual void getWage() {
cout << " 月薪:" << money << '\n';
}
};
//销售经理↓
class Salemanager :public Employee {
public:
Salemanager(double a) :Employee(a) {}
virtual void display() {
cout << " 编号:" << num << " " << " 销售经理:" << name << '\n';
}
virtual void getWage() {
cout << " 月薪:" << money << '\n';
}
};
class Node {
public:
Employee& temp;
Node* next, * pre;
Node(Employee &x):temp(x),next(NULL),pre(NULL){}
};
class EmployeeList {
private:
Node* head;
int sizenum;
public:
EmployeeList() {
head = NULL;
sizenum = 0;
}
~EmployeeList() {
Node* p=head;
for (; p = head; delete p) {
head = head->next;
}
}
void append(Employee &x) {
Node* p = new Node(x);
if (head!=0) {
p->next = head;
head->pre = p;
}
head = p;
sizenum++;
}
void print() {
Node* p = head;
while (p != NULL) {
(p->temp).display();
(p->temp).getWage();
cout << "*****************" << '\n';
p = p->next;
}
}
};
int main() {
EmployeeList pai;
Employee* m=NULL;
int i;
double a=8000.0, b=4000.0, c=6000.0, d=10000.0;
cout << " 职工信息查询 " << '\n';
cout << "1------经理" << '\n';
cout << "2------兼职技术人员" << '\n';
cout << "3------销售员" << '\n';
cout << "4------销售经理" << '\n';
while (cin >> i) {
switch (i) {
case 1: {
m = new Manager(a);
pai.append(*m);
break;
}
case 2: {
m = new Technician(b);
pai.append(*m);
break;
}
case 3: {
m = new Saleman(c);
pai.append(*m);
break;
}
case 4: {
m = new Salemanager(d);
pai.append(*m);
break;
}
}
cout << "=========================================" << '\n';
pai.print();
delete m;
}
}
//重载运算符(3)
class Time {
private:
int hour;
int minute;
int second;
public:
bool flag;//判断合法性
Time():hour(0),minute(0),second(0),flag(1){}
friend ostream& operator <<(ostream& output, Time&A) {
output << A.hour << ":" << A.minute << ":" << A.second ;
return output;
}
friend istream& operator >>(istream& input, Time& A) {
input >> A.hour >> A.minute >> A.second;
if (A.hour < 0 || A.hour > 59 || A.minute < 0 || A.minute > 59 || A.second < 0 || A.second > 59)
{
A.hour = 0, A.minute = 0, A.second = 0;
A.flag = 0;
cout << "时间输入不合法!" << '\n';
}
return input;
}
friend bool operator >(Time& A, Time& B) {
if (A.hour > B.hour)
return true;
else if (A.hour == B.hour && A.minute > B.minute)
return true;
else if (A.minute == B.minute && A.second > B.second)
return true;
else
return false;
}
friend bool operator <(Time& A, Time& B) {
if (A.hour < B.hour)
return true;
else if (A.hour == B.hour && A.minute < B.minute)
return true;
else if (A.minute == B.minute && A.second < B.second)
return true;
else
return false;
}
friend bool operator ==(Time& A, Time& B) {
if (A.hour == B.hour && A.minute == B.minute && A.second == B.second) {
return true;
}
else
return false;
}
friend bool operator !=(Time& A, Time& B) {
if (A.hour == B.hour && A.minute == B.minute && A.second == B.second) {
return false;
}
else
return true;
}
Time operator +(int m) {
Time A;
int minutes;
m %= 1440;//预处理大于24小时的部分,余下24小时内的部分;
minutes = this->hour * 60 + this->minute; //将当前时间全部化为分钟
int temp1 = minutes + m;
A.hour = temp1 / 60;
A.minute = temp1 % 60;
A.second = this->second;
if (A.hour >= 24) {
A.hour %= 24;
}
return A;
}
Time operator -(int m) {
Time A;
int minutes;
m %= 1440;//预处理大于24小时的部分,余下24小时内的部分;
minutes = this->hour * 60 + this->minute; //将当前时间全部化为分钟
int temp1 = minutes - m;
A.hour = temp1 / 60;
A.minute = temp1%60;
A.second = this->second;
if (A.minute < 0) {
A.minute += 60;
}
if (A.hour <= 0 && temp1 < 0) {
A.hour += 23;
}
return A;
}
Time operator++() {
hour++;
if (hour >= 24) {
hour -= 24;
}
return *this;
}
Time operator--() {
hour--;
if (hour < 0) {
hour += 24;
}
return *this;
}
};
int main() {
Time A,B,C;
string s2;
char s1;
int x;
cout << "输入'#'进入比较操作,输入'$'进入运算操作,输入'%'退出操作:" << '\n';
while (cin >> s1&&s1!='%') {
//比较
if (s1 == '#') {
cin >> A >> B;
if (A.flag == 0 || B.flag == 0) {
cout << "请再次输入2个合法的时间!" << '\n';
A.flag = 1;
B.flag = 1;
continue;
}
cout << "*********************************" << '\n';
if (A != B) {
cout << A << " " << "不等于" << B << " " << '\n';
if (A > B) {
cout << A << " " << "大于" << B << " " << '\n';
}
else if (A < B) {
cout << A << " " << "小于" << B << " " << '\n';
}
}
else {
cout << A << " " << "等于" << B << " " << '\n';
}
cout << "*********************************" << '\n';
}
//运算
else if(s1=='$'){
cin >> C;
if (C.flag == 0) {
cout << "请再次输入1个合法的时间!" << '\n';
C.flag = 1;
continue;
}
cout << "请输入操作:" << '\n';
cin >> s2;
if (s2 == "+") {
cout << "输入推后的分钟数x:" << '\n';
cin >> x;
C = C + x;
cout << C;
}
else if (s2 == "-") {
cout << "输入提前的分钟数x:" << '\n';
cin >> x;
C = C - x;
cout << C;
}
else if (s2 == "++") {
++C;
cout << C;
}
else if (s2 == "--") {
--C;
cout << C;
}
else {
cout << "操作符输入错误,请再次输入时间!" << '\n';
continue;
}
}
}
return 0;
}
//重载运算符(2)
class Point {
private:
int x;//横坐标
int y;//纵坐标
public:
Point(int a,int b) :x(a), y(b) {};
void Display();
friend Point operator++(Point &A);
friend Point operator++(Point &A, int);
friend Point operator--(Point &A);
friend Point operator--(Point &A, int);
};
void Point::Display(){
cout << '(' << x << ',' << y << ')' << '\n';
}
Point operator++(Point &A) {
A.x++;
A.y++;
return A;
}
Point operator++(Point &A, int) {
Point temp(A);
A.x ++;
A.y ++;
return temp;
}
Point operator--(Point &A) {
A.x--;
A.y--;
return A;
}
Point operator--(Point &A, int) {
Point temp(A);
A.x --;
A.y --;
return temp;
}
int main() {
int x, y;
cout << "输入A点横坐标、纵坐标,输入'#'结束输入:" << '\n';
while (cin >> x >> y ||!'#') {
Point A(x, y);
Point B(0, 0);
cout << "*********后置自增" << '\n';
cout << "A="; A.Display();
B = A++;
cout << "A++="; B.Display();
cout << "*********后置自减" << '\n';
cout << "A="; A.Display();
B = A--;
cout << "A-- ="; B.Display();
cout << "*********前置自增" << '\n';
cout << "A="; A.Display();
B = ++A;
cout << "++A="; B.Display();
cout << "*********前置自减" << '\n';
cout << "A="; A.Display();
B = --A;
cout << "--A="; B.Display();
}
return 0;
}
//重载运算符(1)
class Matrix {
private:
int M[2][3]={0};
public:
Matrix( ) { }
friend ostream& operator <<(ostream& output, Matrix& A) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
output << A.M[i][j];
if (j != 2) {
output << ' ';
}
else
{
output << '\n';
}
}
}
return output;
}
friend istream& operator >>(istream& input, Matrix& A) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
input>> A.M[i][j];
}
}
return input;
}
friend Matrix operator + (Matrix &a, Matrix &b) {
Matrix c;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
c.M[i][j]=a.M[i][j] + b.M[i][j];
}
}
return c;
}
friend Matrix operator - (Matrix& a, Matrix& b) {
Matrix c;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
c.M[i][j] = a.M[i][j] - b.M[i][j];
}
}
return c;
}
friend Matrix operator ++(Matrix& a) {
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
a.M[i][j]++;
}
}
return a;
}
friend Matrix operator --(Matrix& a) {
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
a.M[i][j]--;
}
}
return a;
}
friend bool operator ==(Matrix& a, Matrix& b) {
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a.M[i][j] != b.M[i][j]) {
return false;
}
}
}
return true;
}
friend bool operator !=(Matrix& a,Matrix &b) {
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
if (a.M[i][j] == b.M[i][j]) {
return false;
}
}
}
return true;
}
};
int main() {
Matrix A, B, C;
cout << "输入矩阵A与矩阵B,输入$计算开始:" << '\n';
cin >> A>>B;
char t;
cin >> t;
if (t == '$') {
cout << "********加法" << '\n';
C = A + B;
cout << C;
cout << "********减法" << '\n';
C = A - B;
cout << C;
cout << "********A自增" << '\n';
C = ++A;
cout << C;
cout << "********B自减" << '\n';
C = --B;
cout << C;
cout << "********A与B是否相等" << '\n';
int flag = 2;
if (A == B) {
flag = 1;
cout << flag<<'\n';
cout << "A矩阵与B矩阵相等" << '\n';
}
else if (A != B) {
flag = 0;
cout << flag<<'\n';
cout << "A矩阵与B矩阵不相等" << '\n';
}
}
return 0;
}
/*类与对象(3);*/
class imaginary {
private:
double x;//实部;
double y;//虚部;
public:
imaginary() {
x = 0;
y = 0;
}
imaginary(double a, double b) {
x = a;
y = b;
//重载构造函数,传入参数a和b;
}
friend imaginary add(imaginary *imag1, imaginary *imag2) {
double x1 = imag1->x + imag2->x;
double y1 = imag1->y + imag2->y;
imaginary ans = imaginary(x1, y1);
return ans;
}
friend imaginary sub(imaginary *imag1, imaginary *imag2) {
double x2 = imag1->x - imag2->x;
double y2 = imag1->y - imag2->y;
imaginary ans = imaginary(x2, y2);
return ans;
}
friend imaginary mul(imaginary *imag1, imaginary *imag2) {
double x3 = imag1->x * imag2->x - imag1->y * imag2->y;
double y3 = imag1->y * imag2->x + imag1->x * imag2->y;
imaginary ans = imaginary(x3, y3);
return ans;
}
friend imaginary div(imaginary *imag1, imaginary *imag2) {
double x4 = (imag1->x * imag2->x + imag1->y * imag2->y) / (imag2->x * imag2->x + imag2->y * imag2->y);
double y4 = (imag1->y * imag2->x - imag1->x * imag2->y) / (imag2->x * imag2->x + imag2->y * imag2->y);
imaginary ans = imaginary(x4, y4);
return ans;
}
void static show(imaginary *ptr);
};
void imaginary::show(imaginary *ptr) {
if (ptr->x == 0) {
cout << ptr->y << 'i';
}
else if (ptr->y < 0) {
cout << ptr->x << ptr->y << 'i';
}
else if (ptr->y > 0) {
cout << ptr->x << '+' << ptr->y << 'i';
}
else {
cout << ptr->x;
}
cout << '\n';
}
int main()
{
cout.setf(ios_base::fixed), cout.precision(2);
char s;
double a, b, c, d;
while (cin >> a >> b >> c >> d >> s) {
imaginary x(a, b), y(c, d);
imaginary ans;
if (s == '+') {
ans = add(&x, &y);
}
else if (s == '-') {
ans = sub(&x, &y);
}
else if (s == '*') {
ans = mul(&x, &y);
}
else {
ans = div(&x, &y);
}
ans.show(&ans);
}
return 0;
}
/*类与对象(2);*/
class TEST{
public:
TEST() { cout<< "调用构造函数" << endl;
x = 2; y = 50; z = 9; }
TEST( int a,int b,int c ) {
cout << "调用重载构造函数"<< endl;
x = a; y = b; z = c;
}
void display(){
cout << "x=" << x << '\t' << "y=" << y <<'\t' << "z=" << z << endl;
}
int max( int a,int b ){
if ( a>b ) return a; else return b;
}
~TEST( ){
cout << "调用析构函数" << endl;
cout << x << "," << y <<"和" << z << "最大值是:" << max( max( x,y ),z) << endl;
}
private:
int x,y,z;
};
void main()
{
TEST obj1;
obj1.display ( ) ;
TEST obj2( 33, 20, 80);
obj2.display();
}
/*类与对象(1);*/
class Score{
private:
int num;//学号
int Math, English, Programming;//数学,英语,程序设计
double avg;//平均成绩
public:
const int interval = 15;//setw的间隔
void inscore();
void showscore();
Score() :num(0), Math(0), English(0), Programming(0), avg(0) {};//数据初始化
};
void Score::inscore() {
cout << "输入学号:";
cin >> num;
cout << "输入高等数学成绩:";
cin >> Math;
cout << "输入大学英语成绩:";
cin >> English;
cout << "输入程序设计成绩:";
cin >> Programming;
avg = (Math + English + Programming) / 3;
}
void Score::showscore() {
cout << setw(5) << "学生学号" << setw(interval) << "高等数学成绩" << setw(interval) << "大学英语成绩" << setw(interval) << "程序设计成绩" << setw(interval) << "平均成绩" << '\n';
cout << setw(5) << num << setw(interval) << Math << setw(interval) << English << setw(interval) << Programming << setw(interval) << avg << '\n';
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);cout.tie(0);
cout.setf(ios_base::fixed), cout.precision(2);
int n;
cout << "学生数量:";
cin >> n;
Score* k = new Score[n];
for (int i = 0; i < n; i++)
{
k[i].inscore();
}
for (int j = 0; j < n; j++)
{
k[j].showscore();
}
delete[]k;
return 0;
}