oop
- 函数
- 编程
- 友元很简单2016final(成绩最高分) (15分)
- 2018Final友元函数(体检表) (10分)
- 复数类的操作 (20分)
- 计算高考状元 (30分)
- 2017final英文语句格式简单检查 (30分)
- 动态二维数组的转置 (15分)
- 友元函数的练习 (10分)
- 最小栈设计并实现一个MinStack类 (10分)
- 集合的模拟实现(类模板) (40分)
- 求两个日期之间相差的天数。 (10分)
- 程序猿和产品狗 (10分)
- 2017final函数模板,求两点间距离(整型 浮点型 point类型) (20分)
- 2018final复数求模的类模板 (20分)
- 数据的最大值问题(整型,浮点型,time类,date类)(重载+函数模板)
- 数据的间距问题(整型,浮点型,point类,time类,date类)(重载+函数模板) (60分)
- 数据的间距问题(整型,浮点型,复数类) (15分)
- 分数加法运算(重载) (30分)
- 矩阵的乘法运算 (20分)
- 时间换算 (60分)
- 计算时间相减 (30分)
- 复数相乘--运算符重载 (30分)
- 复数相加 (70分)
- 分数相加--运算符重载 (10分)
- 复数的比较 (30分)
函数
可变大小矩阵 (10分)
完成一个矩阵类,可以存放二维数据,在构造函数中通过参数指定行数和列数,具有矩阵转置的功能和显示数据的功能。要求实现该矩阵类,使得主函数中的测试代码可以正确运行并得到正确的结果。
输入样例:
首先输入两个整数代表行数和列数,然后输入矩阵元素。
3 2
1 2
3 4
5 6
输出样例:
分别输出转置之前和之后的矩阵数据。例如:
datas before:
1 2
3 4
5 6
datas after:
1 3 5
2 4 6
#include<bits/stdc++.h>
using namespace std;
class Matrix{
public:
int r;//hang
int c;//lie
int juzhen[100][100];
Matrix(int x, int y){
r = x;
c = y;
}
void show(){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
cout << " " << juzhen[i][j];
}
cout << "\n";
}
}
void input(){
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
cin >> juzhen[i][j];
}
}
}
void transform(){
int x[100][100];
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
x[j][i] = juzhen[i][j];
}
}
memset(juzhen,0,sizeof(juzhen));
for(int i = 0; i < c; i++){
for(int j = 0; j < r; j++){
juzhen[i][j]= x[i][j];
}
}
int t;
t=c;
c=r;
r=t;
}
};
int main(){
int r,c;
cin>>r>>c;
Matrix m(r,c); // 初始大小 2行3列
m.input();
cout<<"datas before:"<<endl;
m.show();
m.transform();
cout<<"datas after:"<<endl;
m.show();
}
对象指针与对象数组(拉丁舞) (15分)
对象指针与对象数组(拉丁舞)
怡山小学毕业文艺晚会上,拉丁舞是最受欢迎的节目。不过,每年为了排练这个节目,舞蹈组都会出现一些纠纷。有些同学特别受欢迎,有些却少人问津,因此安排舞伴成为舞蹈组陈老师最头疼的问题。
为了解决这一问题,今年陈老师决定让按先男生后女生,先低号后高号的顺序,每个人先报上自己期待的舞伴,每人报两位,先报最期待的舞伴。接下来按先男生后女生,先低号后高号的顺序,依次按以下规则匹配舞伴:
(1)每个人均按志愿顺序从前到后确定舞伴。如果第一志愿匹配不成功,则考虑第二志愿。
(2)如果A的当前志愿为B,则如果B未匹配舞伴,且有以下情形之一者,A和B匹配成功:
2a) B的期待名单中A。
2b) B的期待名单中没有A,但B期待的两位舞伴均已匹配成功,所以B只能与A凑合。
输入时先输入男生数m, 接下来m行,第一项为学生的姓名,后两项为期待舞伴的编号,编号从0开始,最大为女生数减1。接下来输入女生数f,接下来f行,第一项为学生的姓名,后两项为期待舞伴的编号,编号从0开始,最大为男生数减1。
输出时按男生的编号顺序输出 姓名:舞伴姓名
注意两个姓名间有冒号隔开
输入样例:
5
M0 3 1
M1 1 3
M2 1 4
M3 3 1
M4 0 3
5
F0 0 2
F1 2 0
F2 2 1
F3 2 4
F4 3 2
输出样例:
M0:F1
M2:F4
M4:F0
#include <iostream>
#include <string>
using namespace std;
const int K=2;
const int N=20;
class Student{
string name;
Student *welcome[K];
Student *pair;
public:
void init(string &name, Student *a, Student *b) {
this->name=name;
welcome[0]=a;
welcome[1]=b;
pair=NULL;
}
void printPair();
void addPair();
};
void Student::addPair(){
if(this->pair!=NULL){
return;
}
for(int i=0;i<2;i++){
if(this->welcome[i]->pair!=NULL){
continue;
}
for(int j=0;j<2;j++){
if(this->welcome[i]->welcome[j]==this) {
this->pair=this->welcome[i];
this->welcome[i]->pair=this;
return;
}
}
for(int j=0;j<2;j++){
if(this->welcome[j]->welcome[0]->pair!=NULL && this->welcome[j]->welcome[1]->pair!=NULL && this->welcome[j]->pair==NULL){
this->pair=this->welcome[j];
this->welcome[j]->pair=this;
return;
}
}
}
}
void Student::printPair(){
if(this->pair)
cout<<this->name<<":"<<this->pair->name<<endl;
}
int main(){
Student male[N], female[N];
int m, f, i, j, a, b;
string name;
cin>>m;
for(i=0;i<m;i++){
cin>>name>>a>>b;
male[i].init(name, &female[a], &female[b]);
}
cin>>f;
for(i=0;i<f;i++){
cin>>name>>a>>b;
female[i].init(name, &male[a], &male[b]);
}
for(i=0;i<m;i++) male[i].addPair();
for(i=0;i<f;i++) female[i].addPair();
for(i=0;i<m;i++) male[i].printPair();
return 0;
}
各省总销量及最高销量(对象数组) (10分)
某手机厂商对其在n个城市的销量进行统计分析,现依次输入n个城市的省份名称、城市名称及销量,题目保证同一省份的数据将连续输入,要求输出各省的总销量及各省销量最高的城市名称及其销量,请根据给出的部分代码及输入输出的要求,按照注释中的任务提示,将代码补充完整。
输入样例:
第一行为城市总数,从第2行开始,将依次输入各城市的销量,格式为 “省份名称 城市名称 销量”,同一省份的数据将在连续的几行中输入,如下所示:
5
gd foshan 8.5
gd guangzhou 13.6
gd shenzhen 10.5
zj hangzhou 11.3
zj jiaxing 12.3
输出样例:
以如下的格式输出各省的总销量及最高销量的城市名称和该城市的销量。
gd sum=32.6 max=guangzhou,13.6
zj sum=23.6 max=jiaxing,12.3
#include <iostream>
using namespace std;
class Sale {
private:
string prov,city;//省份,城市
double volume; //销量
public:
void setProv(string p);
void setCity(string c);
void setVolume(double v);
string getProv();
string getCity();
double getVolume();
};
int main() {
int n;//城市个数
cin>>n;
Sale s[n];
string prov;
string city;
double volume;
for(int i=0; i<n; i++) {
cin>>prov>>city>>volume;
s[i].setProv(prov);
s[i].setCity(city);
s[i].setVolume(volume);
}
double sum = s[0].getVolume();
int max = 0;
if (n == 1)
cout << s[max].getProv() << " " << "sum=" << sum << " " << "max=" << s[max].getCity() << "," << s[max].getVolume() << endl;
if (n > 1){
for (int i = 1; i < n; i++){
if (s[i].getProv() == s[max].getProv()){
sum += s[i].getVolume();
if (s[i].getVolume() > s[max].getVolume()){
max = i;
}
}
if (s[i].getProv() != s[max].getProv()){
cout << s[max].getProv() << " " << "sum=" << sum << " " << "max=" << s[max].getCity() << "," << s[max].getVolume() << '\n';
max = i;
sum = 0;
sum = s[max].getVolume();
for (int i = max + 1; i <= n; i++){
if (s[i].getProv() == s[max].getProv() && i != n){
sum += s[i].getVolume();
if (s[i].getVolume() > s[max].getVolume()){
max = i;
}
}
if (i == n)
cout << s[max].getProv() << " " << "sum=" << sum << " " << "max=" << s[max].getCity() << "," << s[max].getVolume() << '\n';
}
}
}
}
}
void Sale::setProv(string p){
prov = p;
}
void Sale::setCity(string c){
city = c;
}
void Sale::setVolume(double v){
volume = v;
}
string Sale::getProv(){
return prov;
}
string Sale::getCity(){
return city;
}
double Sale::getVolume(){
return volume;
}
体育俱乐部I(构造函数) (5分)
一个俱乐部需要保存它的简要信息,包括四项:名称(字符串),成立年份(整数),教练姓名(字符串)和教练胜率(0-100之间的整数)。用键盘输入这些信息后,把它们分两行输出:第一行输出名称和成立年份,第二行输出教练姓名和胜率。
输入样例:
Guanzhou 2006 Tom 92
输出样例:
Guanzhou 2006
Tom 92%
#include <iostream>
#include <string>
using namespace std;
class Coach{
string name;
int winRate;
public:
Coach(string n, int wr){
name=n; winRate=wr;
}
void show();
};
class Club{
string name;
Coach c;
int year;
public:
Club(string n1, int y, string n2, int wr);
void show();
};
Club::Club(string n1, int y, string n2, int wr):c(n2,wr){
name=n1;
year=y;
}
void Club::show(){
cout<<name<<" "<<year<<endl;
c.show();
}
void Coach::show(){
cout<< name <<" "<< winRate <<"%"<<endl;
}
int main(){
string n1, n2;
int year, winRate;
cin>>n1>>year>>n2>>winRate;
Club c(n1,year, n2, winRate);
c.show();
return 0;
}
面积计算器(函数重载) (10分)
实现一个面积计算器,它能够计算矩形或长方体的面积。
输入样例:
2
2 1 2
3 2 3 4
输出样例:
2
52
#include<iostream>
#include<string>
using namespace std;
int area(int,int);
int area(int,int,int);
int main()
{
int i, repeat, c, x, y, z;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>c;
if(c==2){
cin>>x>>y;
cout<<area(x,y)<<endl;
}
if(c==3){
cin>>x>>y>>z;
cout<<area(x,y,z)<<endl;
}
}
return 0;
}
int area(int x, int y){
return x*y;
}
int area(int x, int y, int z){
return (x*y+x*z+y*z)*2;
}
函数重载实现两数相加 (15分)
设计一个重载函数add,该函数有两个参数,可以实现两个类型相同的参数相加的操作,函数返回相加的结果。两个参数可以是整数、实数和字符串,但必须保证两个参数类型相同。
输入样例:
3 5
3.3333 5.555555
hello world
输出样例:
8
8.89
helloworld
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
template <class T>
T add(T a,T b){
return a+b;
}
int main()
{
int a, b;
double c, d;
string s1, s2;
cin >> a >> b;
cin >> c >> d;
cin >> s1 >> s2;
cout << add(a, b) << endl;
cout << fixed << setprecision(2) << add(c, d) << endl;
cout << add(s1, s2) << endl;
return 0;
}
整数求和(运算符重载) (15分)
BigInt类表示不超过100位的无符号大整数。试重载>>,<<和+,以支持无符号大整数的输入、输出与求和(假设结果仍是一个不超过100位的无符号大整数)。
输入样例:
123456789
987654321
输出样例:
123456789+987654321=1111111110
#include <bits/stdc++.h>
using namespace std;
class BigInt {
public:
BigInt() {}
BigInt(string s) {
str = s;
}
friend ostream& operator<<(ostream& os, BigInt& t) {
os << t.str;
return os;
}
friend istream& operator>>(istream& is, BigInt& t) {
is >> t.str;
return is;
}
friend BigInt operator+(BigInt& op1, BigInt& op2){
string a = op1.str;
string b = op2.str;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int num = 0;
int aa[110] = { 0 };
int bb[110] = { 0 };
int cc[110] = { 0 };
int len = max(a.size(), b.size());
for (int i = 0; i < a.size(); i++){
aa[i] = a[i] - '0';
}
for (int i = 0; i < b.size(); i++){
bb[i] = b[i] - '0';
}
int carry = 0;
for (int i = 0; i < len; i++){
num = aa[i] + bb[i] + cc[i];
carry = num / 10;
if (carry){
cc[i + 1] = carry;
cc[i] = num % 10;
}
else{
cc[i] = num;
}
}
string c;
for (int i = 0; i < len; i++){
char ch = cc[i] + '0';
c = c + ch;
}
if (carry != 0){
char ch = carry + '0';
c = c + ch;
}
reverse(c.begin(), c.end());
return BigInt(c);
}
private:
string str;
};
int main(){
BigInt a, b, c;
cin>>a>>b;
c=a+b;
cout<<a<<"+"<<b<<"="<<c<<endl;
return 0;
}
单目运算符重载(时钟类) (15分)
本题已给出时钟类及其成员函数实现,要求补充完整运算符++重载函数(前置和后置),使之能够实现时钟对象自增1秒。
输入样例:
在这里给出一组输入。例如:
10 10 10
输出样例:
在这里给出相应的输出。例如:
10:10:11
10:10:11
10:10:12
#include<iostream>
using namespace std;
class Clock {
public:
Clock(int NewH=0, int NewM=0, int NewS=0);
void ShowTime();
friend Clock operator++(Clock& op); //前置单目运算符重载
friend Clock operator++(Clock& op,int); //后置单目运算符重载
private:
int Hour, Minute, Second;
};
Clock::Clock(int NewH, int NewM, int NewS) {
Hour=NewH;
Minute=NewM;
Second=NewS;
}
void Clock::ShowTime() {
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
Clock operator++(Clock& op){
if(op.Second<59){
op.Second++;
}
else if(op.Second==59&&op.Minute<59){
op.Second=0;
op.Minute++;
}
else if(op.Second==59&&op.Minute==59&&op.Hour<23){
op.Second=0;
op.Minute=0;
op.Hour++;
}
else if(op.Second==59&&op.Minute==59&&op.Hour==23){
op.Second=0;
op.Minute=0;
op.Hour=0;
}
return op;
}
Clock operator++(Clock& op,int a){
Clock op1=op;
if(op.Second<59){
op.Second++;
}
else if(op.Second==59&&op.Minute<59){
op.Second=0;
op.Minute++;
}
else if(op.Second==59&&op.Minute==59&&op.Hour<23){
op.Second=0;
op.Minute=0;
op.Hour++;
}
else if(op.Second==59&&op.Minute==59&&op.Hour==23){
op.Second=0;
op.Minute=0;
op.Hour=0;
}
return op1;
}
int main() {
int h, m, s;
cin>>h>>m>>s;
Clock a(h,m,s);
(++a).ShowTime();
(a++).ShowTime();
a.ShowTime();
return 0;
}
学生成绩的输入和输出(运算符重载) (10分)
现在需要输入一组学生的姓名和成绩,然后输出这些学生的姓名和等级。
输入时,首先要输入学生数(正整数)N。接着输入N组学生成绩,每组成绩包括两项:第一项是学生姓名,第二项是学生的成绩(整数)。
输出时,依次输出各个学生的序号(从1开始顺序编号),学生姓名,成绩等级(不小于60为PASS,否则为FAIL)
输入样例:
3
Li 75
Zhang 50
Yang 99
输出样例:
- Li PASS
- Zhang FAIL
- Yang PASS
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student() {};
Student(string n, int s) {
name = n;
score = s;
}
friend istream& operator>>(istream& is, Student& t) {
is >> t.name >> t.score;
return is;
}
friend ostream& operator<<(ostream& os, Student& t) {
static int num = 0;
os << ++num << ". " << t.name << " ";
if (t.score >= 60)
os << "PASS";
else
os << "FAIL";
return os;
}
private:
string name;
int score;
};
int main(){
int i, repeat;
Student st;
cin>>repeat;
for(i=0;i<repeat;i++){
cin>>st;
cout<<st<<endl;
}
return 0;
}
编程
友元很简单2016final(成绩最高分) (15分)
C++考试正在进行。请设计一个学生类student,学号、本次考试成绩是其私有数据成员,同时有一个求本次考试成绩最高分的学生的友元函数 student* average(student *p,int count)
以上类名和友元函数的形式,均须按照题目要求,不得修改。
输入格式:
输入是 学号([00001,99999])和成绩,以0结束。(不超过100个学生)
输出格式:
输出是最高分学生的 学号 。 提示:如果是并列最高分,需要将并列最高分学生的学号都输出,以一个空格间隔。
输入样例:
在这里给出一组输入。例如:
10001 90
10002 93
0
输出样例:
在这里给出相应的输出。例如:
10002
#include<bits/stdc++.h>
using namespace std;
class student{
private:
int id;
int score;
public:
student(int x = 0,int y = 0) {
id = x;
score = y;
}
friend void top(student* p, int count) {
int max = -1, index = 0;
int i = 0;
for(i = 0; i < count; i++) {
if (p[i].score > max) {
max = p[i].score;
index = i;
}
}
cout << setfill('0') << setw(5) << p[index].id;
for (i = 0; i < count; i++) {
if (p[i].score == max && i != index) {
cout << " " << setfill('0') << setw(5) << p[i].id;
}
}
}
};
int main() {
student ss[150];
int id, score;
int i = 0, count = 0;
while (1) {
cin >> id;
if (id == 0) {
break;
}
else {
cin >> score;
ss[i] = student(id, score);
i++;
count++;
}
}
top(ss, count);
return 0;
}
2018Final友元函数(体检表) (10分)
体育已经成为一门重要的中考科目,但身体存在某些疾病的同学可以免考。
为此,凤湖中学每年需要整理一下毕业班同学(学生数不超过300人)的体检表中的体检结论,筛选出需要免考的同学,建议其申请免考。
要求建立一个类Exam,每个Exam类对象存储一位同学的体检信息,包括私有数据成员:
string num;//学号
int disease[10];//疾病信息
int count;//疾病数
该类还包括一些必要的公有成员函数。
此外,该类还有一个友元函数void print(Exam*e, int count, int *d) 用于输出需要申请免考的学生信息,这里e是一个指向Exam类对象数组元素的指针,count是学生数量,d是一个指向值得关注病病数组元素的指针。
输入格式:
首先输入学生的体检信息,每位学生一行,以0结束输入。首先输入学生的学号(000-999,每位同学的学号各不相同);接着输入疾病数T(T<=10);然后是T个疾病信息,每种疾病用一个正整数编号(100-999)表示,每种疾病对应的编号不同。 在输入学生体检信息后,输入一行值得关注的疾病编号(相邻的编号以单个空格隔开),最后一个编号为0,标识结束输入病病编号。
输出格式:
输出由于患有值得关注疾病,需要申请免考的同学的学号,以及所患疾病编号(在存在多个值得关注的疾病时,仅输出编号最小的那个),每位同学一行,按照输入顺序输出。
输入样例:
103 0
109 1 101
002 3 105 101 107
825 4 108 775 109 104
0
105 101 0
输出样例:
109 101
002 101
#include<bits/stdc++.h>
using namespace std;
int n = 0;
class exam{
private:
string num;
int disease[10];
int count;
public:
exam() {
num = "";
memset(disease, 0, sizeof(disease));
count = 0;
}
void set(string a, int b[], int c) {
num = a;
for(int i=0;i<c;i++){
disease[i] = b[i];
}
count = c;
}
friend void print(exam* e, int count, int* d) {
for (int i = 0; i < count; i++){
int k = 0;
while (e[i].disease[k] != 0) {
k++;
}
if (k == 0) {
continue;
}
int flag = 0;
for (int j = 0; j < n; j++){
for (int q = 0; q < k; q++){
if (e[i].disease[q] == d[j]){
flag = 1;
cout << e[i].num << " " << d[j] << endl;
break;
}
}
if (flag) {
break;
}
}
}
}
};
int main() {
exam std[1000];
string num;
int count, disease[10], d[1000], sum = 0;
while (1) {
cin >> num;
if (num == "0") {
break;
}
else {
cin >> count;
for (int i = 0; i < count; i++) {
cin >> disease[i];
}
std[sum].set(num, disease, count);
sum++;
}
}
while (1) {
int m;
cin >> m;
if (m == 0) {
break;
}
else {
d[n++] = m;
}
}
sort(d, d + n);
print(std, sum, d);
return 0;
}
复数类的操作 (20分)
1、声明一个复数类Complex(类私有数据成员为double型的real和image)
2、定义构造函数,用于指定复数的实部与虚部。
3、定义取反成员函数,调用时能返回该复数的相反数(实部、虚部分别是原数的相反数)。
4、定义成员函数Print(),调用该函数时,以格式(real, image)输出当前对象。
5、编写加法友元函数,以复数对象c1,c2为参数,求两个复数对象相加之和。
6、主程序实现:
(1)读入两个实数,用于初始化对象c1。
(2)读入两个实数,用于初始化对象c2。
(3)计算c1与c2相加结果,并输出。
(4)计算c2的相反数与c1相加结果,并输出。
输入格式:
输入有两行:
第一行是复数c1的实部与虚部,以空格分隔;
第二行是复数c2的实部与虚部,以空格分隔。
输出格式:
输出共三行:
第一行是c1与c2之和;
第二行是c2的相反数与c1之和;
第三行是c2 。
输入样例:
在这里给出一组输入。例如:
2.5 3.7
4.2 6.5
输出样例:
在这里给出相应的输出。例如:
(6.7, 10.2)
(-1.7, -2.8)
(4.2, 6.5)
#include<bits/stdc++.h>
using namespace std;
class Complex{
private:
double real;
double image;
public:
Complex() {
real = 0;
image = 0;
}
void set(double r, double i){
real = r;
image = i;
}
void inverse(){
real = -real;
image = -image;
}
void print(){
cout << "(" << real << ", " << image << ")" << endl;
}
friend void jia( Complex a, Complex b){
cout << "(" << a.real + b.real << ", " << a.image + b.image << ")" << endl;
}
};
int main(){
double r1, m1, r2, m2;
cin >> r1 >> m1;
cin >> r2 >> m2;
Complex aa, bb;
aa.set(r1, m1);
bb.set(r2, m2);
jia(aa, bb);
bb.inverse();
jia(aa, bb);
bb.inverse();
bb.print();
return 0;
}
计算高考状元 (30分)
高考成绩已经公布,大家正在填报志愿。设计一个学生类student,四门学科成绩是其私有成员,分别是语文、数学、英语、综合。有个计算高考状元的函数是其友元函数,其形式是 student top(const student *p, int count) 。
以上类名和友元函数的形式,均须按照题目要求,不得修改。
输入是姓名 和 四科成绩,以0结束。 (不超过100个学生) 输出是状元的总分。
输入样例:
Alice 105 107 107 230
Bob 112 120 120 250
0
输出样例:
602
#include<bits/stdc++.h>
using namespace std;
class student{
private:
string name;
double yu;
double shu;
double ying;
double zong;
public:
student(){
name = "";
yu = 0;
shu = 0;
ying = 0;
zong = 0;
}
void set(string n, double c, double m, double e, double z){
name = n;
yu = c;
shu = m;
ying = e;
zong = z;
}
friend student top(const student* p, int count){
double top = 0, sum = 0;
for (int i = 0; i < count; i++){
sum = p[i].shu + p[i].ying + p[i].yu + p[i].zong;
if (sum > top)
top = sum;
}
cout << top;
return *p;
}
};
int main(){
student std[1000];
string name;
int count = 0;
double yu, shu, ying, zong;
while (1){
cin >> name;
if (name == "0") {
break;
}
cin >> yu >> shu >> ying >> zong;
std[count].set(name, yu, shu, ying, zong);
count++;
}
top(std, count);
return 0;
}
2017final英文语句格式简单检查 (30分)
英文书写中,句首字母通常为大写,其余为小写,单词“I”除外,单词与单词之间用一个空格隔开,句中用“,”断句,句末用“.”结束,“,”和“.”与其前置单词间无需空格隔开。 Word等文本编辑器通常根据以上规则,对我们输入的英文语句进行自动修正。 请编写一个功能,可对输入的一句英文句子,根据以上规则,修订为正确格式后输入。 例如,对于输入的英文句子“This is an Example with one mistake.”, 由于单词“Example”中的字符"E"应该为小写"e",所以修订后输出该句子的正确格式 This is an example with one mistake.
注意:
1、 每组测试数据仅包括一个以字符“.”结束的英文句子。
2、 输入的英文句子中出现的字符包括二十六个大写英文字母(ASCII码65~90),二十六个小写英文字母(ASCII码97~122),“,”和“.”,空格字符。
输入格式:
以字符“."为结束字符的一个英文句子。
输出格式:
输入英文句子经过格式纠错后的输出。
输入样例:
This is an Example with one mistake.
输出样例:
This is an example with one mistake.
#include<bits/stdc++.h>
using namespace std;
char* transform(char* ch){
if ('A' <= *ch && *ch <= 'Z')
*ch += 32;
else if ('a' <= *ch && *ch <= 'z')
*ch -= 32;
return ch;
}
int main(){
string str, result, space = " ", point = ".";
bool flag = true;
int pos = 0;
getline(cin, str);
if ('a' <= str[pos] && str[pos] <= 'z') {
result.append(transform(&str[pos]), 1);
}
else {
result.append(&str[pos], 1);
}//句首大写
while (str[++pos] != '.'){
if (!flag && str[pos] != ' '){
result.append(space);
flag = true;
}
if (flag){
if (str[pos] == 'I' && (str[pos - 1] == ' ' || str[pos - 1] == ',') && (str[pos + 1] == ' ' || str[pos - 1] == ',' || str[pos - 1] == '.')){
result.append(&str[pos], 1);
}
else if (str[pos] == 'i' && (str[pos - 1] == ' ' || str[pos - 1] == ',') && (str[pos + 1] == ' ' || str[pos + 1] == ',' || str[pos + 1] == '.')){
result.append(transform(&str[pos]), 1);
}
else if (str[pos] >= 'A' && str[pos] <= 'Z'){
result.append(transform(&str[pos]), 1);
}
else if (str[pos] == ','){
for (int i = 1; str[pos + i] != '.'; i++){
if (str[pos + i] == ' ')
continue;
else if (str[pos + i] == ',' || str[pos + i] == '.'){
pos += i - 1;
break;
}
else{
result.append(&str[pos], 1);
flag = false;
break;
}
}
}
else if (str[pos] == ' '){
for (int i = 1; str[pos + i] != '.'; i++){
if (str[pos + i] == ' ')
continue;
else if (str[pos + i] == ',' || str[pos + i] == '.'){
pos += i - 1;
break;
}
else{
result.append(space);
pos += i - 1;
break;
}
}
}
else{
result.append(&str[pos], 1);
}
}
}
result.append(point);
cout << result;
return 0;
}
动态二维数组的转置 (15分)
设计一个矩阵类Matrix(整型动态二维数组类)类中成员函数有:构造函数(创建m行n列的矩阵);析构函数(释放动态申请数组空间);读入函数ReadMatrix(创建m行n列的矩阵元素的值);输出函数WriteMatrix(输出m行n列的矩阵元素的值,每个元素占5个字符宽度 );转置函数void Transpose(Matrix& B)(将当前对象矩阵转置为一个n行m列的B矩阵)。main()函数中输入m,n的值创建m行n列的矩阵A,矩阵B是A的转置矩阵。 int main() { int m,n; cin>>m>>n; Matrix A(m,n); A.ReadMatrix(); A.WriteMatrix(); Matrix B(n,m); A.Transpose(B);
B.WriteMatrix(); system(“pause”); return 0; }
输入格式:
第一行:读入连个整数m,n (中间空格隔开) 接下m行,每行读入n个整数(读入A矩阵)
输出格式:
先输出A矩阵 后输出B矩阵(A的转置矩阵)
输入样例:
在这里给出一组输入。例如:
4 3
1 2 3
2 3 4
3 4 5
4 5 6
输出样例:
在这里给出相应的输出。例如:
1 2 3
2 3 4
3 4 5
4 5 6
1 2 3 4
2 3 4 5
3 4 5 6
#include<bits/stdc++.h>
using namespace std;
class Matrix{
public:
int mx[100][100];
Matrix() {};
Matrix(int x, int y) {
r = x;
c = y;
}
void ReadMatrix() {
int x;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> mx[i][j];
}
}
}
void WriteMatrix() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << setw(5) << mx[i][j];
}
cout << endl;
}
}
Matrix Transpose(Matrix& B) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
B.mx[j][i] = mx[i][j];
}
}
return B;
}
private:
int r;
int c;
};
int main(){
int m, n;
cin >> m >> n;
Matrix A(m, n);
A.ReadMatrix();
A.WriteMatrix();
Matrix B(n, m);
A.Transpose(B);
B.WriteMatrix();
system("pause");
return 0;
}
友元函数的练习 (10分)
定义Boat与Car两个类,两者都有私有的整型weight属性,定义两者的一个友元函数getTotalWeight(),计算二者的重量和。
输入格式:
请在这里写输入格式。例如:输入在一行中给出2个整数m和n。
输出格式:
请在这里描述输出格式。例如:对每一组输入,在一行中输出:船和汽车共重M+n吨值。
输入样例:
在这里给出一组输入。例如:
40 30
输出样例:
在这里给出相应的输出。例如:
船和汽车共重70吨
#include <bits/stdc++.h>
using namespace std;
class car;
class boat {
private:
int weight;
public:
boat(int x) {
weight = x;
}
friend int getTotalWeight(boat&, car&);
};
class car {
private:
int weight;
public:
car(int x) {
weight = x;
}
friend int getTotalWeight(boat&, car&);
};
int getTotalWeight(boat& a, car& b) {
return a.weight + b.weight;
}
int main() {
int n, m;
cin >> n >> m;
boat boat(n);
car car(m);
cout << "船和汽车共重" << getTotalWeight(boat, car) << "吨" << endl;
return 0;
}
最小栈设计并实现一个MinStack类 (10分)
最小栈设计并实现一个MinStack类,要求至少含有以下三种方法
一、push方法:将一个整数进行压栈
二、pop方法:弹出栈顶元素
三、getmin方法:输出栈内最小元素
输入格式:
第一行输入一个N,表示接下来要操作的数量
随后N行表示操作:
push方法后跟要压栈的元素
pop方法、getmin方法后不跟任何元素
说明:假设不包含其他无效输入,以及在执行getmin、pop方法时栈不为空
输出样例:
输出栈内最小元素
输入样例:
5
push 2
push 3
push 1
pop
getmin
输出样例:
2
#include <bits/stdc++.h>
using namespace std;
class MinStack {
public:
MinStack() {};
void push(int& x) {
if (s1.empty() && s2.empty()) {
s1.push(x);
s2.push(x);
}
else {
if (s2.top() > x) {
s2.push(x);
}
s1.push(x);
}
}
void pop() {
if (s1.top() <= s2.top()) {
s2.pop();
}
s1.pop();
}
int getmin() {
return s2.top();
}
private:
stack<int>s1;
stack<int>s2;
};
int main() {
MinStack t;
int n;
cin >> n;
getchar();
while (n--) {
string op;
cin >> op;
getchar();
if (op == "push") {
int x;
cin >> x;
t.push(x);
}
if (op == "pop") {
t.pop();
}
if (op == "getmin") {
cout << t.getmin() << endl;
}
}
return 0;
}
集合的模拟实现(类模板) (40分)
我们可以用一个类来模拟集合及集合运算,add运算用以实现集合元素的增加,delete运算用于实现集合元素的删除,find运算用以实现集合元素的查找,但是目前集合元素类型未知,可以是int、char、double等基本数据类型,也可以是String、Time、Student等对象类型,要求采用类模板实现集合及集合运算,包括集合元素的增加、删除和查找的等基本功能。
集合模板类MySet包括数据如下:
T data[100];//用数组来存放所有的集合元素,最多不超过100个元素
int count;//表示目前集合中有多少个元素
包括成员函数如下:
构造函数若干个,集合运算函数如下:
int addSet( T elem)
int deleSet(T elem)
int findElem(T elem)
其中,addSet向集合中添加一个元素,deleSet从集合中删除一个元素,findElem判断elem是否是集合成员,三个函数分别返回元素插入位置,删除位置和存在位置。
主函数有如下数据成员 :
MySet<\int>\ intSet;(反斜杠是转义字符,使用时去掉)
MySet<\double>\ douSet;
MySet<\string>\ strSet;
分别是int类型、double类型、String的集合。
完成上述类模板和主函数,主函数根据输入的信息,建立初始的三种不同类型的空集合对象,调用成员函数分别对intSet、douSet和StrSet执行相应的操作,并输出对应的集合信息。
输入格式:
每一行为一个集合操作,每行的第一个数字为集合元素类型,1为整型元素,2为浮点型元素,3为String类型,第二个数字为集合操作类型,1为插入,2为删除,3为查找,第三个为集合元素,集合元素类型视第一个数字给定的集合元素类型而定。输入0时标志输入结束。
输出格式:
输出当前操作的执行位置(插入位置、删除位置和存在位置)
删除操作时,如果元素X不存在,输出“X is not exist!”。
插入操作时,如果集合已满,输出“Full Set.”若元素已存在,输出“X is already exist!”
查找操作时,如果找不到元素,输出“X is not exist!”。
输入:
1 1 1
1 1 2
1 3 1
1 2 1
1 2 3
1 3 1
2 1 1.1
2 1 2.2
2 1 3.3
2 3 1.1
2 2 2.2
2 2 2.2
3 1 abc
3 1 bcd
3 3 abc
3 2 abc
3 3 abc
0
输出:
0
1
0
0
3 is not exist!
1 is not exist!
0
1
2
0
1
2.2 is not exist!
0
1
0
0
abc is not exist!
#include <bits/stdc++.h>
using namespace std;
template <class T>
int addSet(T *myset, T elem,int len){
bool flag=false;
for(int i=0; i<=len; i++){
if(elem==myset[i]&&len!=i){
flag=true;
break;
}
}
if(flag==true){
cout<<elem<<" is already exist!"<<endl;
}
else if(flag==false){
myset[len]=elem;
cout<<len<<endl;
len++;
}
return len;
}
template <class T>
int deleSet(T * myset, T elem, int len){
bool flag=false;
int i=0;
for(i=0; i<len; i++){
if(myset[i]==elem){
flag=true;
break;
}
}
if(flag==true){
cout<<i<<endl;
for(int j=i; j<len-1&&len>1; j++){
myset[j]=myset[j+1];
}
}
else{
cout<<elem<<" is not exist!"<<endl;
}
return i;
}
template <class T>
int findElem(T * myset, T elem, int len){
int i=0;
bool flag=false;
for(i=0; i<len; i++){
if(myset[i]==elem){
flag=true;
cout<<i<<endl;
break;
}
}
if(flag==false){
cout<<elem<<" is not exist!"<<endl;
}
return i;
}
int main(){
int intSet[100] ;
double douSet[100];
string StrSet[100] ;
int temp,ch;
int intLen=0, douLen=0, strLen=0;
while(cin>>ch){
if(ch==0){
break;
}
if(ch==1){
cin>>temp>>intSet[intLen];
if(temp==1){
intLen=addSet(intSet,intSet[intLen],intLen);
}
if(temp==2){
deleSet(intSet, intSet[intLen], intLen);
intLen--;
if(intLen<0){
intLen=0;
}
}
if(temp==3){
findElem(intSet, intSet[intLen], intLen);
}
}
if(ch==2){
cin>>temp>>douSet[douLen];
if(temp==1){
douLen=addSet(douSet, douSet[douLen], douLen);
}
if(temp==2){
deleSet(douSet, douSet[douLen], douLen);
douLen--;
if(douLen<0){
douLen=0;
}
}
if(temp==3){
findElem(douSet, douSet[douLen], douLen);
}
}
if(ch==3){
cin>>temp>>StrSet[strLen];
if(temp==1){
strLen=addSet(StrSet,StrSet[strLen],strLen);
}
if(temp==2){
deleSet(StrSet,StrSet[strLen],strLen);
strLen--;
if(strLen<0){
strLen=0;
}
}
if(temp==3){
findElem(StrSet,StrSet[strLen],strLen);
}
}
}
return 0;
}
求两个日期之间相差的天数。 (10分)
设计一个日期类Date,包括日期的年份、月份和日号,编写一个友元函数,求两个日期之间相差的天数。该类中设计有3个友元函数;count_day()函数,它有两个参数,第2个参数是一个标志,当其值等于1 时,计算一年的开始到某日期的天数;否则计算某日期到年尾的天数。leap()函数用于判断指定的年份是否为闰年。subs()函数用于计算两个日期之间的天数。 当时间输入不正确时,输出“time error!”
输入样例:
在这里给出一组输入。例如:
2000 1 1
2002 10 1
输出样例:
在这里给出相应的输出。例如:
1004
#include <bits/stdc++.h>
using namespace std;
const int year_day[2][13] = { {0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31} };
class Date {
public:
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
friend int count_day(Date d, int flag);
friend int leap(int year);
friend int subs(Date d1, Date d2);
private:
int year, month, day;
};
int leap(int year){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
}
int count_day(Date d, int flag) {
int bl, x;
if (leap(d.year)) {
bl = 1;
}
else {
bl = 0;
}
if (flag) {
x = d.day;
for (int i = 1; i < d.month; i++) {
x += year_day[bl][i];
}
}
else {
x = year_day[bl][d.month] - d.day;
for (int i = d.month+1 ; i <= 12; i++) {
x += year_day[bl][i];
}
}
return x;
}
int subs(Date d1, Date d2) {
int days;
if (d1.year < d2.year) {
days = count_day(d1, 0) + count_day(d2, 1);
for (int y = d1.year + 1; y < d2.year; y++) {
if (leap(y)) {
days += 366;
}
else {
days += 365;
}
}
}
if (d1.year == d2.year) {
days = fabs(count_day(d2, 1) - count_day(d1, 1));
}
if (d1.year > d2.year) {
days = count_day(d1, 1) + count_day(d2, 0);
for (int y = d2.year + 1; y < d1.year; y++) {
if (leap(y)) {
days += 366;
}
else {
days += 365;
}
}
}
return days;
}
int main(){
int y1, y2, m1, m2, d1, d2;
cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
if (y1 > y2 || (y1 == y2 && m1 > m2) || (y1 == y2 && m1 == m2 && d1 > d2)) {
cout << "time error!" << endl;
return 0;
}
Date date1(y1, m1, d1), date2(y2, m2, d2);
cout << fabs(subs(date1, date2)) << endl;
return 0;
}
程序猿和产品狗 (10分)
在公司里面,程序猿经常有一堆todolist要做,而这些todolist是产品经理分配给他们的。但是当程序员遇到不懂技术的产品狗时,就悲剧了。产品经理经常修改他们的todolist,比如:添加,减少他们的todolist。
请设计一个类CodeMonkey ,表示程序猿,另一个类ProductDog,表示产品经理。
CodeMonkey类有私有成员 name,todolist。
构造函数初始化姓名和todolist,公有函数 int sizeof_todolist(), 来自ProductDog类的友元函数 add_todolist(CodeMonkey&,int),reduce_todolist(CodeMonkey& , int);
ProductDog类有公有函数 add_todolist(CodeMonkey&,int),reduce_todolist(CodeMonkey& , int)
Input Specification
每个测试文件包含一组测试用例,对于每个测试用例,第一行输入 n (1<= n <= 10^6)表示有n个程序员,接下去n行,每行为: name x 表示名为name的程序员的todolist的长度为x。 接下去一行 m (1<=m<=10^6),表示产品经理分配任务的次数。 接下去m行,每行为 name opt x , opt 为 0 表示名为name的程序猿的todolist增加x,opt为 1 表示减少x。 输入数据保证合法性。
Output Specification
对于每个程序员,请输出 name x,x表示最终的todolist长度。按输入顺序输出。
Sample Input:
3
Jack 1
Luck 2
Tom 3
4
Tom 0 100
Luck 0 50
Jack 0 25
Tom 1 50
Sample Output:
Jack 26
Luck 52
Tom 53
#include <bits/stdc++.h>
using namespace std;
class CodeMonkey;
class ProductDog
{
public:
void add_todolist(CodeMonkey&,int);
void reduce_todolist(CodeMonkey& , int);
};
class CodeMonkey
{
private:
string name;
int todolist;
public:
void set(string na,int todo)
{
name=na;
todolist=todo;
}
int sizeof_todolist()
{
cout<<todolist<<endl;
return 0;
}
friend void ProductDog::add_todolist(CodeMonkey&,int);
friend void ProductDog::reduce_todolist(CodeMonkey&,int);
};
void ProductDog::add_todolist(CodeMonkey &d,int x)
{
d.todolist+=x;
}
void ProductDog::reduce_todolist(CodeMonkey &d,int x)
{
d.todolist-=x;
}
int main()
{
CodeMonkey monkey[100];
ProductDog dog;
string nam[100],na;
int todos[100],y;
int n,m;
int opt;
cin>>n;
for(int i=0; i<n; i++)
{
cin>>nam[i]>>todos[i];
monkey[i].set(nam[i],todos[i]);
}
cin>>m;
for(int j=0; j<m; j++)
{
cin>>na>>opt>>y;
for(int i=0; i<n; i++)
{
if(na==nam[i])
{
if(opt==0)
{
dog.add_todolist(monkey[i],y);
}
else if(opt==1)
{
dog.reduce_todolist(monkey[i],y);
}
}
}
}
for(int i=0; i<n; i++)
{
cout<<nam[i]<<" ";
monkey[i].sizeof_todolist();
}
return 0;
}
2017final函数模板,求两点间距离(整型 浮点型 point类型) (20分)
数据的间距问题(函数模板) 类point有三个数据成员:x、y和z, 分别代表x坐标、y坐标和z坐标,并有若干构造函数和一个重载-(减号,计算两点距离)的成员函数。 要求设计一个函数模板,
template < class T> double dist(T a, T b)
对int,float,point或者其他类型的数据,返回间距。
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,若为整型元素,接着输入两个整型数据,若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 z1 x2 y2 z2),输入0时标志输入结束。
输出格式:
对每个输入,每行输出一个间距值。
输入样例:
1 2 5
3 2 4 7 5 9 7
2 2.2 9.9
0
输出样例:
3
5.83095
7.7
#include <bits/stdc++.h>
using namespace std;
class point {
public:
double x, y, z;
point(int xx = 0, int yy = 0, int zz = 0) {
x = xx;
y = yy;
z = zz;
}
friend double operator-(point op1, point op2) {
return sqrt((op1.x - op2.x) * (op1.x - op2.x) + (op1.y - op2.y) * (op1.y - op2.y) + (op1.z - op2.z) * (op1.z - op2.z));
}
};
template < class T>
double dist(T a, T b) {
return fabs(a - b);
}
int main() {
int ty;
cin >> ty;
while (ty != 0) {
if (ty == 1) {
int a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
if (ty == 2) {
double a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
if (ty == 3) {
point a, b;
cin >> a.x >> a.y >> a.z >> b.x >> b.y >> b.z;
cout << dist(a, b) << endl;
}
cin >> ty;
}
return 0;
}
2018final复数求模的类模板 (20分)
有一个复数的类模板,有两个私有数据成员,分别是 实部和虚部。有一个成员函数是求该复数的模。 请添加适当的构造函数。
输入格式:
输入仅一行,分别是三个数,以空格间隔。 第一个数是1或2或3(1表示int型,2表示float型,3表示double型),第二个数是该复数的实部,第三个数是该复数的虚部。
输出格式:
输出仅一行,输出该复数的模(in型的复数,输出int型的模; float型的复数,输出float型的模;double型的复数,输出double型的模)。
输入样例:
在这里给出一组输入。例如:
2 2.5 -3.1
输出样例:
在这里给出相应的输出。例如:
3.98246
#include <bits/stdc++.h>
using namespace std;
template <class T>
void mod(T* arr){
cin >> arr[0] >> arr[1];
cout << sqrt(arr[0] * arr[0] + arr[1] * arr[1]);
}
int main(){
int a[2];
float b[2];
double c[2];
int flag;
cin >> flag;
if(flag==1){
mod(a);
}
else if(flag==2){
mod(b);
}
else
mod(c);
}
数据的最大值问题(整型,浮点型,time类,date类)(重载+函数模板)
两个类如下设计:类time有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载>(<)(大于号或者小于号)的成员函数。
要求设计一个函数模板template <\class T>\ double maxn(T x[], int len) 对int,float,time和date或者其他类型的数据,返回最大值。
主函数有如下数据成员:
int intArray[100];
double douArray[100];time timeArray[100];
date dateArray[100];
其中,hh = 3600 ss, mm = 60 ss, year = 365 day, month = 30 day,对于time和date类型,数据在转换成ss或者day后进行运算。
输入格式:
每行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为time类型,4为date类型,若为整型元素,接着输入整型数据,以0结束。若为浮点型元素,接着输入浮点型数据,以0结束。若为time型元素, 输入time型数据(hh1 mm1 ss1 hh2 mm2 ss2),以0结束。若为date型数据,输入date型数据(year1 month1 day1 year2 month2 day2),以0结束。输入-1时表示全体输入结束。
输出格式:
对每次输入,输出一个最大值。
样例输入:
1 4 5 9 3 7 0
2 4.4 5.5 6.9 3.2 2.7 0
3 18 21 22 18 20 31 18 21 49 0
4 2013 5 14 2013 5 15 2013 4 1 0
-1
样例输出:
9
6.9
18 21 49
2013 5 15
#include<iostream>
using namespace std;
class Time{
public:
Time(){
hh = 0;
mm = 0;
ss = 0;
}
void set(int h,int m,int s){
hh=h;
mm=m;
ss=s;
}
friend bool operator >(Time &t1,Time &t2){
int re1=t1.hh*3600+t1.mm*60+t1.ss;
int re2=t2.hh*3600+t2.mm*60+t2.ss;
if(re1>re2){
return true;
}
else{
return false;
}
}
friend ostream & operator <<(ostream &os, Time &t){
os<<t.hh<<" "<<t.mm<<" "<<t.ss;
return os;
}
private:
int hh;
int mm;
int ss;
} ;
class date{
public:
date(){
year = 0;
month = 0;
day = 0;
}
void set(int y,int m,int d){
year=y;
month=m;
day=d;
}
friend bool operator >(date &d1,date &d2){
int re1=d1.year*365+d1.month*30+d1.day;
int re2=d2.year*365+d2.month*30+d2.day;
if(re1>re2){
return true;
}
else{
return false;
}
}
friend ostream & operator <<(ostream &os, date &d){
os<<d.year<<" "<<d.month<<" "<<d.day;
return os;
}
private:
int year;
int month;
int day;
};
template <class T>
T maxn(T x[], int len){
T max;
max=x[0];
for(int i=0; i<len; i++){
if(x[i]>max){
max=x[i];
}
}
cout<<max<<endl;
return max;
}
int main(){
int intArray[100];
double douArray[100];
Time TimeArray[100];
date dateArray[100];
int ch;
int i=0;
while(cin>>ch){
if(ch==-1){
break;
}
if(ch==1){
while(cin>>intArray[i]){
if(intArray[i]==0){
break;
}
i++;
}
maxn(intArray,i);
}
if(ch==2){
while(cin>>douArray[i]){
if(douArray[i]==0){
break;
}
i++;
}
maxn(douArray,i);
}
if(ch==3){
int hour,minute,second;
while(cin>>hour){
if(hour==0){
break;
}
cin>>minute>>second;
TimeArray[i].set(hour,minute,second);
i++;
}
maxn(TimeArray,i);
}
if(ch == 4){
int years,months,days;
while(cin>>years){
if(years == 0){
break;
}
cin>>months>>days;
dateArray[i].set(years,months,days);
i++;
}
maxn(dateArray,i);
}
}
return 0;
}
数据的间距问题(整型,浮点型,point类,time类,date类)(重载+函数模板) (60分)
三个类如下设计:类cTime有三个数据成员,hh,mm,ss,分别代表时,分和秒,并有若干构造函数和一个重载-(减号)的成员函数。类point有两个数据成员,x,y分别坐标,并有若干构造函数和一个重载-(减号)的成员函数。类date有三个数据成员,year,month,day分别代表年月日,并有若干构造函数和一个重载-(减号)的成员函数。 要求设计一个函数模板template <\class T>\ double dist(T a, T b) 对int,float,cTime,point和date或者其他类型的数据,返回间距。
其中,hh = 3600 ss, mm = 60 ss, year = 365 day, month = 30 day,对于cTime和date类型,数据在转换成ss或者day后进行运算。
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为point类型,4,为time类型,5为date类型,若为整型元素,接着输入两个整型数据,
若为浮点型元素,接着输入两个浮点型数据,若为point型元素,输入两个point型数据(x1 y1 x2 y2),若为time型元素, 输入两个cTime型数据(hh1 mm1 ss1 hh2 mm2 ss2),若为date型数据,输入两个date型数据(year1 month1 day1 year2 month2 day2)。输入0时标志输入结束。
输出格式:
对每个输入,每行输出一个间距值。
样例输入:
1 2 5
4 18 21 22 18 20 31
3 2 4 5 9
5 2013 5 14 2013 5 15
2 2.2 9.9
0
样例输出:
3
51
5.83095
1
7.7
#include<iostream>
#include<cmath>
using namespace std;
//实现Time类
class Time
{
private:
int hh;
int mm;
int ss;
public:
Time()//无参构造函数
{
hh = 0;
mm = 0;
ss = 0;
}
void set(int h,int m,int s)//赋值成员数据
{
hh=h;
mm=m;
ss=s;
}
friend int operator-(Time ,Time);
friend istream& operator>>(istream &,Time &);
};
//重载>>
istream & operator>>(istream & set,Time &t)
{
set>>t.hh>>t.mm>>t.ss;
return set;
}
//重载-
int operator-(Time t1,Time t2)
{
int totalSecond1=t1.hh*3600+t1.mm*60+t1.ss;
int totalSecond2=t2.hh*3600+t2.mm*60+t2.ss;
return totalSecond2-totalSecond1;
}
//实现date类
class date
{
private:
int year;
int month;
int day;
public:
date()
{
year = 0;
month = 0;
day = 0;
}
void set(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
friend int operator-(date, date);
friend istream& operator>>(istream &,date &);
};
//重载>>
istream & operator>>(istream & set,date &d)
{
set>>d.year>>d.month>>d.day;
return set;
}
//重载-
int operator-(date d1,date d2)
{
int totalDay1=d1.year*365+d1.month*30+d1.day;
int totalDay2=d2.year*365+d2.month*30+d2.day;
return totalDay1-totalDay2;
}
//实现point
class Point
{
private:
int x;
int y;
public:
Point()
{
x=y=0;
}
void set(int a,int b)
{
x=a;
y=b;
}
friend double operator-(Point,Point);
friend istream& operator>>(istream &,Point &);
};
//重载>>
istream & operator>>(istream & set,Point &p)
{
set>>p.x>>p.y;
return set;
}
//重载-
double operator-(Point p1,Point p2)
{
return sqrt((1.0*p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int main()
{
int classType;
while(cin>>classType)
{
if(classType==0)break;
switch(classType)
{
case 1://int
{
int a,b;
cin>>a>>b;
cout<<abs(b-a)<<endl;
}break;
case 2://double
{
double a,b;
cin>>a>>b;
cout<<abs(b-a)<<endl;
}break;
case 3://Point
{
Point a,b;
cin>>a>>b;
cout<<abs(b-a)<<endl;
}break;
case 4://Time
{
Time a,b;
cin>>a>>b;
cout<<abs(b-a)<<endl;
}break;
case 5://date
{
date a,b;
cin>>a>>b;
cout<<abs(b-a)<<endl;
}break;
}
}
return 0;
}
数据的间距问题(整型,浮点型,复数类) (15分)
复数类Complex有两个数据成员:a和b, 分别代表复数的实部和虚部,并有若干构造函数和一个重载-(减号,计算两个复数的距离)的成员函数。 要求设计一个函数模板
template < class T >
double dist(T a, T b)
对int,float,Complex或者其他类型的数据,返回两个数据的间距。
输入格式:
每一行为一个操作,每行的第一个数字为元素类型,1为整型元素,2为浮点型元素,3为Complex类型,若为整型元素,接着输入两个整型数据,若为浮点型元素,接着输入两个浮点型数据,若为Complex型元素,输入两个Complex型数据(a1 b1 a2 b2),输入0时标志输入结束。
输出格式:
对每个输入,每行输出一个间距值。
输入样例:
1 2 5
3 2 4 5 9
2 2.2 9.9
0
输出样例:
3
5.83095
7.7
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
private:
double a, b;
public:
Complex()
{
a = 0, b = 0;
}
Complex(double A, double B)
{
a = A, b = B;
}
void set(double A, double B)
{
a = A, b = B;
}
void display()
{
cout << a << " " << b << endl;
}
friend double operator - (Complex C1, Complex C2);
};
double operator -(Complex C1, Complex C2)
{
return sqrt(pow(C1.a - C2.a, 2.0) + pow(C1.b - C2.b, 2.0));
}
template<class T>
double dist(T a, T b)
{
return abs(a - b);
}
int main()
{
int flag;
while(cin >> flag, flag != 0)
{
if(flag == 1)
{
int a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
else if(flag == 2)
{
float a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
else if(flag == 3)
{
Complex a, b;
double a1, a2, b1, b2;
cin >> a1 >> a2 >> b1 >> b2;
a.set(a1, a2), b.set(b1, b2);
cout << dist(a, b) << endl;
}
}
return 0;
}
分数加法运算(重载) (30分)
相信同学们对复数运算符重载已经相当熟悉啦,那今天那我们来看看分数又该如何处理呢?定义一个分数类FS,有私有成员分子fz,分母fm。另有公有成员函数FS operator + (const FS &f)对运算符“+”进行重载,实现两个分数相加。题目首先给出一个整型数n,紧跟着2n行输入,输入形如3z4m,代表分子为3,分母为4。其中分母不为0,输入时分母可以为负数,但输出时分母必须为正数。 要求对分数进行两两求和,并化简。(备注说明:分数为0时,表示成0z1m,如果结果为负数,那么分子取负数,分母为正数)
示例如下:
输入:
3
4z9m
2z9m
4z5m
5z4m
2z-5m
1z-5m
输出:
2z3m
41z20m
-3z5m
#include <bits/stdc++.h>
using namespace std;
class FS {
public:
FS(int z = 0, int m = 0) {
fz = z;
fm = m;
}
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
FS operator + (const FS& f) {
int x = this->fz * f.fm + this->fm * f.fz;
int y = f.fm * this->fm;
int g = gcd(x, y);
x = x / g;
y = y / g;
if (y < 0) {
x = -x;
y = fabs(y);
}
return FS(x, y);
}
void disp() {
cout << fz << "z" << fm << "m" << endl;
}
private:
int fz;
int fm;
};
int main() {
int n;
cin >> n;
while (n--) {
int a, b, c, d;
char z, m;
cin >> a >> z >> b >> m;
cin >> c >> z >> d >> m;
FS op1(a, b), op2(c, d), op3;
op3 = op1 + op2;
op3.disp();
}
}
矩阵的乘法运算 (20分)
线性代数中的矩阵可以表示为一个row*column的二维数组,当row和column均为1时,退化为一个数,当row为1时,为一个行向量,当column为1时,为一个列向量。 建立一个整数矩阵类matrix,其私有数据成员如下:
int row;
int column;
int **mat;
建立该整数矩阵类matrix构造函数; 建立一个 *(乘号)的运算符重载,以便于对两个矩阵直接进行乘法运算; 建立输出函数void display(),对整数矩阵按行进行列对齐输出,格式化输出语句如下:
cout<<setw(10)<<mat[i][j];
主函数里定义三个整数矩阵类对象m1、m2、m3.
输入格式:
分别输入两个矩阵,分别为整数矩阵类对象m1和m2。 每个矩阵输入如下: 第一行两个整数 r c,分别给出矩阵的行数和列数 接下来输入r行,对应整数矩阵的每一行 每行输入c个整数,对应当前行的c个列元素
输出格式:
整数矩阵按行进行列对齐(宽度为10)后输出 判断m1和m2是否可以执行矩阵相乘运算。 若可以,执行m3=m1*m2运算之后,调用display函数,对m3进行输出。 若不可以,输出"Invalid Matrix multiplication!" 提示:输入或输出的整数矩阵,保证满足row>=1和column>=1。
输入样例:
4 5
1 0 0 0 5
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
5 5
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 8 9
5 6 7 8 9
输出样例:
26 32 38 44 50
4 6 8 10 12
9 12 15 18 21
16 20 24 32 36
#include <bits/stdc++.h>
using namespace std;
class matrix{
public:
matrix(int r = 0, int c = 0) {
row = r;
column = c;
mat = new int* [row];
for (int i = 0; i < row; i++) {
mat[i] = new int[column];
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
cin >> mat[i][j];
}
}
}
void display() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
cout << setw(10) << mat[i][j];
}
cout << endl;
}
}
friend matrix operator*(matrix& a, matrix& b) {
matrix c;
if (a.column == 1 && a.row == 1) {
c.row = b.row;
c.column = b.column;
c.mat = new int* [c.row];
for (int i = 0; i < c.row; i++) {
c.mat[i] = new int[c.column];
}
for (int i = 0; i < c.row; i++) {
for (int j = 0; j < c.column; j++) {
c.mat[i][j] = a.mat[0][0] * b.mat[i][j];
}
}
}
else {
c.row = a.row;
c.column = b.column;
c.mat = new int* [c.row];
for (int i = 0; i < c.row; i++) {
c.mat[i] = new int[c.column];
}
for (int i = 0; i < c.row; i++) {
for (int j = 0; j < c.column; j++) {
int sum = 0;
for (int k = 0; k < a.column; k++) {
sum += a.mat[i][k] * b.mat[k][j];
}
c.mat[i][j] = sum;
}
}
}
return c;
}
friend bool judge(matrix& a, matrix& b) {
if (a.column == b.row || a.column == 1 && a.row == 1) {
return true;
}
else {
return false;
}
}
private:
int row;
int column;
int** mat;
};
int main() {
int a, b;
cin >> a >> b;
matrix m1(a, b);
cin >> a >> b;
matrix m2(a, b);
if (judge(m1, m2)) {
matrix m3;
m3 = m1 * m2;
m3.display();
}
else {
cout << "Invalid Matrix multiplication!" << endl;
}
return 0;
}
时间换算 (60分)
定义一个时间类time,内有数据成员hour,minute,second,另有成员函数:构造函数用于初始化数据成员,输出函数,运算符重载+(加号),。编写主函数:创建时间对象,再输入秒数 n,通过运算符重载+(减号),计算该时间再过 n 秒后的时间值,时间的表示形式为时:分:秒,超过 24 时从 0 时重新开始计时。
测试输入包含若干测试用例,每个测试用例占一行。当读入0 0 0 0时输入结束,相应的结果不要输出。
输入示例:
括号内为说明
0 0 1 59 (时间为0:0:1,秒数n=59)
11 59 40 30 (时间为11:59:40,秒数n=30)
23 59 40 3011 (时间为23:59:40,秒数n=3011)
0 0 0 0
输出示例:
time:0:1:0↙(0:0:01加上59秒的新时间)
time:12:0:10↙(11:59:40加上30秒的新时间)
time:0:49:51↙(23:59:40加上3011秒的新时间)
#include <bits/stdc++.h>
using namespace std;
class Time {
public:
Time(int a = 0, int b = 0, int c = 0) {
hour = a;
minute = b;
second = c;
}
void display(){
cout << "time:" << hour << ":" << minute << ":" << second << endl;
}
friend Time operator+(const Time& a, int k);
private:
int hour;
int minute;
int second;
};
Time operator+(const Time& a, int k) {
Time c1;
c1.hour = a.hour;
c1.minute = a.minute;
c1.second = a.second + k;
int count;
if (c1.second + k > 59 || c1.second + k < 0) {
count = (a.second + k) / 60;
c1.second = (a.second + k) % 60;
c1.minute = a.minute + count;
if (c1.second < 0) {
c1.second = 60 + c1.second;
c1.minute--;
}
if (c1.minute > 59 || c1.minute < 0) {
count = (c1.minute) / 60;
c1.minute = (c1.minute) % 60;
c1.hour = a.hour + count;
if (c1.minute < 0) {
c1.minute = 60 + c1.minute;
c1.hour--;
}
if (c1.hour > 23 || c1.hour < 0) {
c1.hour = c1.hour % 24;
if (c1.hour < 0) {
c1.hour = 24 + c1.hour;
}
}
}
}
return c1;
}
int main() {
int a1, a2, a3, a4;
while (1) {
cin >> a1 >> a2 >> a3 >> a4;
if (a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0) {
break;
}
Time c1(a1, a2, a3);
c1 = c1 + a4;
c1.display();
}
return 0;
}
计算时间相减 (30分)
题目描述:定义一个时间类,小时和分钟是其两个私有成员数据。输入一个起始时间和一个结束时间(起始时间早于结束时间),通过运算符重载-(减号),计算这两个时间相隔多少分钟。说明:这两个时间在同一天之内,且采用24小时计时分式,即从00:00-23:59。
输入格式:
测试输入包含若干测试用例,每个测试用例占一行。每个测试用例包括四个数,用空格间隔,每个数都是由两位数字组成,第一个数和第二个数分别表示起始时间的小时和分钟,第三个数和第四个数分别表示结束时间的小时和分钟。当读入一个测试用例是00 00 00 00时输入结束,相应的结果不要输出。
输出格式:
对每个测试用例输出一行。输出一个数即可,表示两者之间间隔的分钟数。
输入样例:
12 11 12 58
00 13 16 00
09 07 23 59
00 00 00 00
输出样例:
47
947
892
#include <bits/stdc++.h>
using namespace std;
class Time {
public:
Time(int a = 0, int b = 0) {
hour = a;
minute = b;
}
operator int() {
return hour * 60 + minute;
}
private:
int hour;
int minute;
};
int main() {
int a1, a2, a3, a4;
while (1) {
cin >> a1 >> a2 >> a3 >> a4;
if (a1 == 0 && a2 == 0 && a3 == 0 && a4 == 0) {
break;
}
Time c1(a1, a2);
Time c2(a3, a4);
cout << c2 - c1 << endl;
}
return 0;
}
复数相乘–运算符重载 (30分)
建立一个复数类Complex,实数和虚数是其私有数据成员;
建立复数类的无参和参数化构造函数;
建立一个 (乘号)的运算符重载,以便于对两个复数直接进行乘法运算;
建立输出函数void display(),对复数进行输出;
主函数里定义三个复数类对象c1、c2、c3.
输入格式:
输入一共一行,每行两个复数,分别为复数类c1和c2。
输出格式:
执行c3=c1c2运算之后,调用display函数,对c3进行输出。
提示:输入或输出的复数,可能只包含实部或虚部。
输入样例:
1+2i 2+3i
输出样例:
-4+7i
#include <bits/stdc++.h>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
void set(int r, int i)
{
real = r;
imag = i;
}
friend Complex operator *(Complex& c1, Complex& c2);
void display();
};
Complex operator *(Complex& c1, Complex& c2)
{
int re, im;
Complex c3;
c3.real = c1.real * c2.real - (c1.imag * c2.imag);
c3.imag = c1.real * c2.imag + c1.imag * c2.real;
return c3;
}
void Complex::display()
{
if (imag < 0)
{
if (imag == -1)
{
if (real == 0)
{
cout << "-i" << endl;
}
else
{
cout << real << "-i" << endl;
}
}
else
{
if (real == 0)
{
cout << imag << "i" << endl;
}
else
{
cout << real << imag << "i" << endl;
}
}
}
else if (imag > 0)
{
if (imag == 1)
{
if (real == 0)
{
cout << "i" << endl;
}
else
{
cout << real << "+i" << endl;
}
}
else
{
if (real == 0)
{
cout << imag << "i" << endl;
}
else
{
cout << real << "+" << imag << "i" << endl;
}
}
}
if (imag == 0)
{
cout << real << endl;
}
}
int main()
{
Complex a, b, c;
string str[3];
int re[20] = { 0 }, im[20] = { 0 };
for (int i = 0; i < 2; i++)
{
int count5 = 1;
cin >> str[i];
int o = 0;
int k = -1;
if (str[i][str[i].size() - 1] != 'i')
{
for (int l = str[i].size() - 1; l >= 0; l--)
{
re[i] = str[i][l] - '0';
}
}
else
{
for (int j = 0; j < str[i].size(); j++)
{
if (str[i][j] == '-')
{
k = j;
}
if (str[i][j] == '+')
{
o = 1;
}
}
if (o == 0 && k == -1)
{
re[i] = 0;
if (str[i][str[i].size() - 1] == str[i][0])
{
im[i] = 1;
}
else
{
for (int l = str[i].size() - 2; l >= 0; l--)
{
im[i] += (str[i][l] - '0') * count5;
count5 *= 10;
}
}
}
else if (o == 0 && k == 0)
{
if (str[i] == "-i")
{
im[i] = 1;
}
else
{
for (int l = str[i].size() - 2; l > 0; l--)
{
im[i] += (str[i][l] - '0') * count5;
count5 *= 10;
}
}
im[i] = -1 * im[i];
}
else
{
for (int j = 0; j < str[i].size(); j++)
{
int count1 = 1;
int count2 = 1;
int count3 = 1;
int count4 = 1;
if (str[i][j] == '+')
{
for (int t = j - 1; t >= 0; t--)
{
re[i] += (str[i][t] - '0') * count1;
count1 *= 10;
}
if (str[i][j + 1] == 'i')
{
im[i] = 1;
}
else
{
for (int t = str[i].size() - 2; t > j; t--)
{
im[i] += (str[i][t] - '0') * count2;
count2 *= 10;
}
}
}
if (str[i][j] == '-' && j != 0)
{
for (int t = j - 1; t >= 0; t--)
{
re[i] += (str[i][t] - '0') * count3;
count3 *= 10;
}
if (str[i][j + 1] == 'i')
{
im[i] = 1;
}
else
{
for (int t = str[i].size() - 2; t > j; t--)
{
im[i] += (str[i][t] - '0') * count4;
count4 *= 10;
}
}
im[i] = -1 * im[i];
}
if (str[i][0] == '-')
{
re[i] = -1 * re[i];
}
}
}
}
}
a.set(re[0], im[0]);
b.set(re[1], im[1]);
c = a * b;
c.display();
return 0;
}
复数相加 (70分)
题目:一个复数类,运算符重载 + ,实现复数和复数的相加。输入一组复数,每行一个复数,直到输入0结束。 输出这组复数的结果。
提示: 复数的输入和输出符合数学书写规范
输入示例
3+2i
2+3i
0
输出示例
5+5i
#include <bits/stdc++.h>
using namespace std;
class Complex{
private:
int real;
int image;
public:
Complex(int a=0,int b=0):real(a),image(b){};
void display(); //输出使用函数
void get(int ,int ); //设置私有成员大小
friend Complex operator+(const Complex &c1,const Complex &c2); //重载+运算符
};
Complex operator+(const Complex &c1,const Complex &c2){
Complex c3;
c3.real=(c1.real+c2.real); //重载+实现
c3.image=(c1.image+c2.image);
return c3;
}
void Complex::get(int a,int b){
real=a; //设置大小
image=b;
}
void Complex::display(){ //输出函数
if(real!=0&&image>0){
if(image!=1){
cout<<real<<"+"<<image<<"i"<<endl;
}else{ //注意虚数是1或者-1的时候不能直接输出虚数
cout<<real<<"+"<<"i"<<endl;
}
}else if(real!=0&&image<0){
if(image!=-1){
cout<<real<<image<<"i"<<endl;
}else{
cout<<real<<"-i"<<endl;
}
}else if(real!=0&&image==0){
cout<<real<<endl;
}else if(real==0&&image==0){
cout<<"0"<<endl;
}else if(real==0&&image>0){
if(image!=1){
cout<<image<<"i"<<endl;
}else{
cout<<"i"<<endl;
}
}else if(real==0&&image<0){
if(image!=-1){
cout<<image<<"i"<<endl;
}else{
cout<<"-i"<<endl;
}
}
}
void getnumber(string s,int &a,int &b){ //难点,输入字符串,a,b,来返回具体实数虚数的大小
int i,j,k;
a=0;
b=0;
int count1=0,count2=0;
int flag1=0,flag2=0;
for(i=0;i<s.size();i++){
if(s[i]=='i'){
flag2=1; //flag2代表i出现出现个数
}
}
for(i=0;i<s.size();i++){
if(s[i]=='+'){
count1++; //count1,count2记录+-个数
}
if(s[i]=='-'){
count2++;
}
}
if(count2==2){
int flag=0;
for(i=1;i<s.size();i++){
if(s[i]=='-'){
i++;
break;
}
a=(s[i]-'0')+a*10;
}
for(;i<s.size();i++){ //负实负虚
if(s[i]=='i'){
break;
}
b=(s[i]-'0')+b*10;
flag++;
}
a=-a;
if(flag==0){
b=1;
}
b=-b;
}else if(count1==0&&count2==0){
a=0;
b=0;
int flag=0;
if(flag2==0){
for(i=0;i<s.size();i++){ //纯正实数
a=(s[i]-'0')+a*10;
}
}else if(flag2==1){
for(i=0;i<s.size()-1;i++){
b=(s[i]-'0')+b*10; //纯正虚数
flag++;
}
if(flag==0){
b=1;
}
}
}else if(count1==1&&count2==0){
a=0;
b=0;
int flag=0;
for(i=0;i<s.size();i++){
if(s[i]=='+'){ //正实正虚
i++;
break;
}
a=(s[i]-'0')+a*10;
}
for(;i<s.size();i++){
if(s[i]=='i'){
break;
}
b=(s[i]-'0')+b*10;
flag++;
}
if(flag==0){
b=1;
}
}else if(count1==0&&count2==1){
a=0;
b=0;
int flag=0;
if(flag2==1){
if(s[0]=='-'){
for(i=1;i<s.size()-1;i++){
b=(s[i]-'0')+b*10;
flag++; //纯负虚数
}
if(flag==0){
b=1;
}
b=-b;
}else{
for(i=0;i<s.size();i++){
if(s[i]=='-'){
i++;
break; //正实负虚
}
a=(s[i]-'0')+a*10;
}
for(;i<s.size()-1;i++){
flag++;
b=(s[i]-'0')+b*10;
}
if(flag==0){
b=1;
}
b=-b;
}
}else{
a=0;
b=0;
for(i=1;i<s.size();i++){ //纯负实数
a=(s[i]-'0')+a*10;
}
a=-a;
}
}else if(count1==1&&count2==1){
a=0;
b=0;
int flag=0;
for(i=1;i<s.size();i++){
if(s[i]=='+'){ //负实正虚
i++;
break;
}
a=(s[i]-'0')+a*10;
}
for(;i<s.size()-1;i++){
flag++;
b=(s[i]-'0')+b*10;
}
a=-a;
if(flag==0){
b=1;
}
}
}
int main(){
Complex c1,c2;
string s1,s2;
int a=0,b=0,c=0,d=0;
cin>>s1;
getnumber(s1,a,b);
c1.get(a,b);
while(1){
cin>>s2;
if(s2=="0"){
break;
}
getnumber(s2,c,d);
c2.get(c,d);
c1=c1+c2;
}
c1.display();
return 0;
}
分数相加–运算符重载 (10分)
请定义一个分数类,拥有两个整数的私有数据成员,分别表示分子和分母(分母永远为正数,符号通过分子表示)。 重载运算符加号"+",实现两个分数的相加,所得结果必须是最简分数。
输入:
第一行的两个数 分别表示 第一个分数的分子和分母(分母不为0)。 第二行的两个数 分别表示 第二个分数的分子和分母。
输出:
第一个数表示分子,第二个数表示分母(若分数代表的是整数,则不输出分母)。
输入样例:
1 5
2 5
输出样例:
3 5
#include <bits/stdc++.h>
using namespace std;
class Fs{
int fz,fm;
public:
void display();
Fs(int x = 0, int y = 0);
Fs operator+(Fs &op2)
{
int max = (this->fm > op2.fm) ? this->fm : op2.fm;
do
{
if (max % this->fm == 0 && max % op2.fm == 0)
{
break;
}
else
++max;
} while (true);
Fs temp;
this->fz *= max / this->fm;
op2.fz *= max / op2.fm;
this->fz += op2.fz;
temp.fz=this->fz;
temp.fm=max;
max = 1;
for (int i = 1; i <= abs(this->fz); i++)
{
if (temp.fz % i == 0 && temp.fm%i == 0)
max = i;
}
temp.fm/=max;
temp.fz/=max;
return temp;
}
};
Fs::Fs(int x,int y){
fz = x;
fm = y;
}
void Fs::display(){
cout << fz;
if(fm!=1&&fz!=0)
cout << " " << fm;
}
int main()
{
int a[4];
cin >> a[0] >> a[1] >> a[2] >> a[3];
Fs x1(a[0],a[1]), x2(a[2],a[3]),x3;
x3 = x1 + x2;
x3.display();
}
复数的比较 (30分)
题目描述:建立一个复数类,实数和虚数是其私有数据成员。建立一个>(大于号)的运算符重载,比较两个复数间模的大小。
输入格式:
测试输入包含若干测试用例,每个测试用例占一行。每个测试用例包括四个数字,前两个数字分别表示第一个复数的实部和虚部,第三个和第四个数字分别表示第二个复数的实部和虚部。每个数字之间用空格间隔。当读入一个测试用例是0 0 0 0时输入结束,相应的结果不要输出。
输出格式:
对每个测试用例输出一行。当第一个复数的模大于第二个复数的模时,输出 true ,当第一个复数的模小于或等于第二个复数的模时,输出false
输入样例:
3 5 4 0
0 3 4 1
0 0 0 0
输出样例:
true
false
#include <bits/stdc++.h>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0)
{
real = r;
imag = i;
}
double modulus()
{
double modulus;
modulus = sqrt(real *real + imag * imag);
return modulus;
}
bool friend operator>(Complex o,Complex p)
{
double a = o.modulus();
double b = p.modulus();
if (a > b)
{
return true;
}
else if (a <=b)
{
return false;
}
}
};
int main()
{
double a, b, c, d;
while(cin >> a >> b >> c >> d)
{
if (a == 0 && b == 0 && c == 0 && d == 0)
break;
Complex p1(a, b);
Complex p2(c, d);
if (p1 > p2)
{
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}
return 0;
}