忘了为什么这么起名字了,可能是期末复习题。

[A.cpp]
#include<iostream>
using namespace std;
class Member { //普通会员类
protected:
int number, credit;
string name;
public:
Member() {};
Member(int n, string s, int c) :number(n), name(s), credit(c) {};
virtual void Add(int money) {
credit += money;
}
virtual int Exchange(int c) {
int temp;
if (c <= credit) {
temp = c / 100;
credit = credit - temp*100;
}
else {
temp = credit / 100;
credit = 0;
}
return temp;
}
virtual void print() {
cout << "普通会员" << number << "--" << name << "--" << credit << endl;
}
~Member() {
}
//....代码自行编写
};
class VIP :public Member { //贵宾会员类
private:
int ratio1, ratio2;
public:
VIP() {};
VIP(int n, string s, int c, int r1, int r2) :Member(n, s, c), ratio1(r1), ratio2(r2) {};
virtual void Add(int money) {
credit += money * ratio1;
}
virtual int Exchange(int c) {
int temp;
if (c <= credit) {
temp = c / ratio2;
credit = credit - temp*ratio2;
}
else {
temp = credit / ratio2;
credit -= credit * ratio2;
}
return temp;
}
virtual void print() {
cout << "贵宾会员" << number << "--" << name << "--" << credit << endl;
}
//....代码自行编写
};
int main()
{
Member* pm; //创建一个基类对象指针
int n, c; string name;
int r1, r2, cre, cc;//....其他变量自行编写
cin >> n >> name >> c;
Member mm(n, name, c);//输入数据,创建普通会员对象mm
//使用指针pm执行以下操作:
pm = &mm;//1、pm指向普通会员对象mm
cin >> cre >> cc;
pm->Add(cre);//2、输入数据,通过pm执行积分累加和积分兑换
pm->Exchange(cc);
pm->print();//3、通过pm调用打印方法输出
cin >> n >> name >> c >> r1 >> r2;
VIP vv(n, name, c, r1, r2);//输入数据,创建贵宾会员对象vv
//使用指针pm执行以下操作:
pm = &vv;//1、pm指向贵宾会员对象vv
cin >> cre >> cc;
pm->Add(cre);//2、输入数据,通过pm执行积分累加和积分兑换
pm->Exchange(cc);
pm->print();//3、通过pm调用打印方法输出
return 0;
}
[B.cpp]
#include<iostream>
using namespace std;
class Metal {
private:
int hardness, weigh, volume;
public:
Metal() {};
Metal(int h, int w, int v) :hardness(h), weigh(w), volume(v) {};
Metal operator+(const Metal& a) {
Metal temp;
temp.hardness = a.hardness + hardness;
temp.volume = a.volume + volume;
temp.weigh = a.weigh + weigh;
//cout<<"硬度"<<temp.hardness<<"--重量"<<temp.weigh<<"--体积"<<temp.volume<<endl;
return temp;
}
Metal operator*(int bei) {
Metal temp;
temp.volume = volume * bei;
temp.hardness = hardness;
temp.weigh = weigh;
return temp;
}
Metal operator++ (int) {
Metal temp;
temp.hardness = hardness+1;
temp.volume = volume * 1.1;
temp.weigh = weigh * 1.1;
return temp;
}
Metal operator-- (int) {
Metal temp;
temp.hardness = hardness-1;
temp.volume = volume * 0.9;
temp.weigh = weigh * 0.9;
return temp;
}
void print() {
cout << "硬度" << hardness << "--重量" << weigh << "--体积" << volume << endl;
}
};
int main() {
int a1, a2, a3, b1, b2, b3, big;
cin >> a1 >> a2 >> a3 >> b1 >> b2 >> b3 >> big;
Metal m1(a1, a2, a3);
Metal m2(b1, b2, b3);
(m1 + m2).print();
(m1* big).print();
m1++.print();
m2--.print();
}
[C.cpp]
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
template <class T>
T Max(T* a, int len) {
T mm=0 ;
for (int i = 0; i < len; i++) {
if (*(a + i) > mm) { mm = *(a + i); }
}
return mm;
};
template <class T>
class Cryption {
T ptxt[100]; //明文
T ctxt[100]; //密文
T key; //密钥
int len; //长度
public:
Cryption(T tk, T *tt, int tl) {
//memset(ptxt, -1, sizeof(ptxt));
len = tl;
key = tk;
for (int i = 0; i < len; i++) { ptxt[i] = tt[i]; }
}; //参数依次对应密钥、明文、长度
void Encrypt() {
/*sort(ptxt, ptxt + len);
T mm = *(ptxt + len -1);*/
T mmax = Max(ptxt, len);
T pianli[100];
for (int i=0; i < len; i++) {
*(ctxt+i)= mmax - *(ptxt + i)+key;
};
}; //加密
void Print() //打印,无需改造
{
int i;
for (i = 0; i < len - 1; i++)
cout << ctxt[i] << " ";
cout << ctxt[i] << endl;
}
};
//支持三种类型的主函数
int main()
{
int i;
int length; //长度
int ik, itxt[100];
double dk, dtxt[100];
char ck, ctxt[100];
//整数加密
cin >> ik >> length;
for (i = 0; i < length; i++)
cin >> itxt[i];
Cryption<int> ic(ik, itxt, length);
ic.Encrypt();
ic.Print();
//浮点数加密
cin >> dk >> length;
for (i = 0; i < length; i++)
cin >> dtxt[i];
Cryption<double> dc(dk, dtxt, length);
dc.Encrypt();
dc.Print();
//字符加密
cin >> ck >> length;
for (i = 0; i < length; i++)
cin >> ctxt[i];
Cryption<char> cc(ck, ctxt, length);
cc.Encrypt();
cc.Print();
return 0;
}
[D.cpp]
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class fur {
protected:
int bian, fv;
public:
fur(int bian1, int fv1) :bian(bian1), fv(fv1) { cout << "编号" << bian << "--功率" << fv <<"W" << endl; };
};
class fan :public fur {
protected:
int xiang, li;
public:
fan(const fur& f, int x1, int l1) :fur(f), xiang(x1), li(l1) {};
void FX(int n) { xiang = n; }
void FL(int n) { li = n; }
};
class shi :public fur {
protected:
float real, max;
public:
shi(const fur& f, float x1, float l1) :fur(f), real(x1), max(l1) {};
int warn(){
if (real >= max * 0.5) { return 1; };
if (real < max * 0.5 && real >= max * 0.1) { return 2; }
if (real < max * 0.1) { return 3; }
}
};
class shifan :public fan, public shi {
int dw;
public:
shifan(const fan& f, const shi& s, int x1) :fan(f),shi(s), dw(x1){};
void opr(int n) {
dw = n;
if (dw == 0) { ; }
if (dw == 1) { xiang = 0; li = 1; }
if (dw == 2) { xiang = 1; li = 2; }
if (dw == 3) { xiang = 1; li = 3; }
}
void show() {
if (xiang == 0) { cout << "定向吹风--风力" << li <<"级" << endl; }
if (xiang == 1) { cout << "旋转吹风--风力" << li << "级"<< endl; }
int n = warn();
if (n == 1) { cout << "实际水容量" << real <<"升--水量正常" << endl; }
if (n == 2) { cout << "实际水容量" << real << "升--水量偏低" << endl; }
if (n == 3) { cout << "实际水容量" << real << "升--水量不足" << endl; }
}
};
int main() {
int t; cin >> t;
while (t--) {
int bian, fv, xiang, li,dw;
float real, max;
cin >> bian >> fv >> xiang >> li >> real >> max >> dw;
int dw1; cin >> dw1;
cout << "加湿风扇--档位" << dw1 << endl;
fur f(bian, fv);
fan fan1(f, xiang, li);
shi s1(f, real, max);
shifan s2(fan1, s1, dw1);
s2.opr(dw1);
s2.show();
}
}
[E.cpp]
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
class CN; //提前声明
class EN; //提前声明
class Weight { //抽象类
protected:
char kind[20]; //计重类型
int gram; //克
public:
Weight() {};
Weight(const char* tk, int tg = 0) {
strcpy(kind, tk);
gram = tg;
}
virtual void Print(ostream& out) = 0; //输出不同类型的计重信息
};
class CN : public Weight { //中国计重
//....类定义自行编写y
int jin, lia, qian;
public:
CN(int jin1, int lia1, int qian1, int gram1, const char k[]) : jin(jin1), lia(lia1), qian(qian1), Weight(k, gram1) {};
void Convert(int n) {
gram = n;
jin = gram / 500;
lia = (gram - (jin * 500)) / 50;
qian = (gram - (jin * 500) - (lia * 50)) / 5;
gram = gram - (jin * 500) - (lia * 50) - qian * 5;
}
CN& operator=(EN& e1);
virtual void Print(ostream& out) {
out << "中国计重:" << jin << "斤" << lia << "两" << qian << "钱" << gram << "克" << endl;
}
};
class EN : public Weight { //英国计重
//....类定义自行编写
int bang, ans, dala;
public:
EN(int i, int j, int k, int g, const char tk[]) : Weight(tk, g) {
bang = i;
ans = j;
dala = k;
}
void Convert(int n) {
gram = n;
bang = gram / 512;
ans = (gram - (bang * 512)) / 32;
dala = (gram - (bang * 512) - (ans * 32)) / 2;
gram = gram - (bang * 512) - (ans * 32) - dala * 2;
}
int sum() {
return gram + bang * 512 + ans * 32 + dala * 2;
}
virtual void Print(ostream& out) {
out << "英国计重:" << bang << "磅" << ans << "盎司" << dala << "打兰" << gram << "克" << endl;
}
};
//以全局函数方式重载输出运算符,代码3-5行....自行编写
//重载函数包含两个参数:ostream流对象、Weight类对象,参数可以是对象或对象引用
//重载函数必须调用参数Weight对象的Print方法
ostream& operator<<(ostream& out, Weight& w) {
w.Print(out);
return out;
}
CN& CN::operator=(EN& e1) {
int gram = e1.sum();
Convert(gram);
return *this;
}
int main()//主函数
{
int tw;
//创建一个中国计重类对象cn
//构造参数对应斤、两、钱、克、类型,其中克和类型是对应基类属性gram和kind
CN cn(0, 0, 0, 0, "中国计重");
cin >> tw;
cn.Convert(tw); //把输入的克数转成中国计重
cout << cn;
//创建英国计重类对象en
//构造参数对应磅、盎司、打兰、克、类型,其中克和类型是对应基类属性gram和kind
EN en(0, 0, 0, 0, "英国计重");
cin >> tw;
en.Convert(tw); //把输入的克数转成英国计重
cout << en;
cn = en; //把英国计重转成中国计重
cout << cn;
return 0;
}
这篇文章展示了C++编程中如何设计和实现会员类,包括普通会员和贵宾会员,以及它们的积分累加和兑换功能。同时,还涵盖了C++中的类继承和多态性。此外,还提到了一种自定义加密算法,支持对不同类型数据进行加密处理。
11万+

被折叠的 条评论
为什么被折叠?



