标签:运算 pre .cpp ipc 大小 divide mpm == 运算符
#pragma once
#ifndef CHIPA_H
#define CHIPA_H
class chipA {
protected:
int m, n;
public:
chipA(int tempm, int tempn) :m(tempm), n(tempn) {}
int add() {
return m + n;
}
int minus() {
return m - n;
}
};
#endif
#pragma once
#ifndef CHIPB_H
#define CHIPB_H
#include"chipA.h"
class chipB :public chipA {
public:
chipB(int tempm, int tempn):chipA(tempm,tempn){}
int multiply() {
return m * n;
}
};
#endif // !CHIPB_H
#pragma once
#ifndef CHIPC_H
#define CHIPC_H
#include "chipA.h"
class chipC :public chipA {
public:
chipC(int tempm, int tempn) :chipA(tempm, tempn) {}
int divide() {
return m / n;
}
};
#endif // !CHIPC_H
// 芯片.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "chipA.h"
#include "chipB.h"
#include "chipC.h"
#include <iostream>
using namespace std;
int main()
{
chipA temp1(3, 6);
cout << "chipA测试:" << endl;
cout << "加法:" << temp1.add() << endl << "减法:" << temp1.minus() << endl;
chipB temp2(8, 4);
cout << "chipB测试:" << endl;
cout << "加法:" << temp2.add() << endl << "乘法:" << temp2.multiply() << endl;
chipC temp3(10, 10);
cout << "chipC测试:" << endl;
cout << "加法:" << temp3.add() << endl << "除法:" << temp3.divide() << endl;
return 0;
}
#pragma once
#ifndef VEHICLE_H
#define VEHICLE_H
class vehicle {
protected:
int maxspeed, weight;
public:
vehicle(int newmaxspeed,int newweight)
:maxspeed(newmaxspeed),weight(newweight){}
void run();
void stop();
};
#endif // !VEHICLE_H
#pragma once
#ifndef BICYCLE_H
#define BICYCLE_H
#include"vehicle.h"
class bicycle :virtual public vehicle {
protected:
bicycle(int newmaxspeed, int newweight, int newheight)
:vehicle(newmaxspeed, newweight),height(newheight) {}
int height;
};
#endif // !BICYCLE_H
#pragma once
#ifndef MOTORCAR_H
#define MOTORCAR_H
#include"vehicle.h"
class motorcar :virtual public vehicle {
protected:
motorcar(int newmaxspeed, int newweight, int newseatnum)
:vehicle(newmaxspeed, newweight), seatnum(newseatnum) {}
int seatnum;
};
#endif // !MOTORCAR_H
#pragma once
#ifndef MOTORCYCLE_H
#define MOTORCYCLE_H
#include"bicycle.h"
#include"motorcar.h"
class motorcycle :virtual public bicycle, virtual public motorcar {
public:
motorcycle(int newmaxspeed, int newweight, int newheight, int newseatnum)
:bicycle(newmaxspeed, newweight, newheight),
motorcar(newmaxspeed, newweight, newseatnum),
vehicle(newmaxspeed,newweight) //由于无法调用更上层的虚基类,因此更上层的虚基类需要自己构造
{}
};
#endif // !MOTORCYCLE_H
#include"vehicle.h"
#include<iostream>
void vehicle::run() {
std::cout << "run" << std::endl;
}
void vehicle::stop() {
std::cout << "stop" << std::endl;
}
// 2.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "motorcycle.h"
#include <iostream>
using namespace std;
int main()
{
motorcycle m(100, 200, 2, 2);
m.run();
m.stop();
return 0;
}
#pragma once
#ifndef FRACTION_H
#define FRACTION_H
class fraction
{
public:
fraction(int t0, int b0);
fraction(int t0);
fraction();
~fraction();
//运算符重载
fraction operator+(const fraction &f1);
fraction operator-(const fraction &f1);
fraction operator*(const fraction &f1);
fraction operator/(const fraction &f1);
void compare(fraction &f1); //比较大小
void input();
void output();
protected:
int top;
int bottom;
};
#endif // !FRACTION_H
#pragma once
#ifndef IFRACTION_H
#define IFRACTION_H
#include"Fraction.h"
class ifraction :public fraction {
public:
ifraction(int t0, int b0);
ifraction(int t0);
ifraction();
void show();
friend void convertF(ifraction &f0);
private:
int extra = 0;//用于保存将假分数变为真分数时产生的前缀数字
};
#endif // !IFRACTION_H
#include "fraction.h"
#include <iostream>
using namespace std;
//构造函数
fraction::fraction(int t0, int b0) :top(t0), bottom(b0)
{}
fraction::fraction(int t0) : top(t0),bottom(1)
{}
fraction::fraction():top(1),bottom(1)
{}
//析构函数
fraction::~fraction()
{}
//重载运算符
fraction fraction::operator+(const fraction &f1) {
int newtop = top * f1.bottom + f1.top * bottom;
int newbottom = bottom * f1.bottom;
return fraction(newtop, newbottom);
}
fraction fraction::operator-(const fraction &f1) {
int newtop = top * f1.bottom - f1.top * bottom;
int newbottom = bottom * f1.bottom;
return fraction(newtop, newbottom);
}
fraction fraction::operator*(const fraction &f1) {
int newtop = top * f1.top;
int newbottom = bottom * f1.bottom;
return fraction(newtop, newbottom);
}
fraction fraction::operator/(const fraction &f1) {
int newtop = top * f1.bottom;
int newbottom = bottom * f1.top;
return fraction(newtop, newbottom);
}
void fraction::compare(fraction &f1) //比较大小
{
if (top * f1.bottom > bottom * f1.top)
cout << top << "/" << bottom << endl;
else if (top * f1.bottom < bottom * f1.top)
cout << f1.top << "/" << f1.bottom << endl;
else if (top * f1.bottom == bottom * f1.top)
cout << "一样大" << endl;
}
void fraction::input()
{
cin >> top >> bottom;
}
void fraction::output()
{
cout << top << "/" << bottom << endl;
}
#include"iFraction.h"
#include<iostream>
using namespace std;
ifraction::ifraction(int t0, int b0):fraction(t0,b0){}
ifraction::ifraction(int t0):fraction(t0,1){}
ifraction::ifraction():fraction(1,1){}
void ifraction::show() {
if (bottom == 0) {
cout << top << endl;
}
else if (top == 0) {
cout << extra << endl;
}
else if (extra != 0) {
cout << extra << "又" << top << "/" << bottom << endl;
}
else {
cout << top << "/" << bottom << endl;
}
}
// Fraction.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "fraction.h"
#include "iFraction.h"
using namespace std;
int max(int a, int b)
{
if (a < b)
swap(a, b);
int x;
while (b != 0)
{
x = a % b;
a = b;
b = x;
}
return a;
}
void convertF(ifraction &f0) {
//约分
int max0 = max(f0.top, f0.bottom);
f0.top /= max0;
f0.bottom /= max0;
//化为真分数
if (f0.top == f0.bottom) {
f0.top = 1;
f0.bottom = 0;
}
else if (f0.top > f0.bottom) {
f0.extra = f0.top / f0.bottom;
f0.top %= f0.bottom;
}
}
int main()
{
fraction a;
fraction b(3, 4);
fraction c(5);
fraction result;
cout << "函数输出测试" << endl;
cout << "分数a为:"; a.output();
cout << "分数b为:"; b.output();
cout << "分数c为:"; c.output();
cout << "加减乘除比较测试" << endl;
cout << "a + b = "; result = a + b; result.output();
cout << "a - b = "; result = a - b; result.output();
cout << "b * c = "; result = b * c; result.output();
cout << "b / c = "; result = b / c; result.output();
cout << " a和b中较大的是:"; a.compare(b);
cout << "c和c比较:"; c.compare(c);
cout << "请输入分数a(分子和分母中间以空格分隔):";
a.input();
cout << "a的大小为:"; a.output();
//ifraction测试
ifraction temp1(155, 5), temp2(20, 8);
cout << "转化前:";
temp1.output();
temp2.output();
cout << "转化后:";
convertF(temp1);
convertF(temp2);
temp1.show();
temp2.show();
return 0;
}
标签:运算 pre .cpp ipc 大小 divide mpm == 运算符
原文地址:https://www.cnblogs.com/MrWang-nextdoor/p/9129628.html