每日一小时
//#include<iostream>
//using namespace std;
//class veter3D
//{
//private:
// float x, y, z;
//public:
// veter3D(float x = 0, float y = 0, float z = 0)
// {
// this->x = x, this->y = y, this->z = z;
// }
// friend istream& operator>>(istream&, veter3D&);
// friend ostream& operator<<(ostream&, veter3D&);
// veter3D operator+(veter3D A);
// veter3D operator-(veter3D A);
// veter3D operator*(float A);
// void display()
// {
// cout << "(" << x << "," << y << "," <<z << ")";
// }
//};
//istream& operator>>(istream&input, veter3D&A)
//{
// input >> A.x >> A.y >> A.z;
// return input;
//}
//ostream& operator<<(ostream&output, veter3D&A)
//{
// output << A.x << A.y << A.z;
// return output;
//}
//veter3D veter3D::operator+(veter3D A)
//{
// return veter3D(this->x + A.x, this->y + A.y, this->z + A.z);
//}
//veter3D veter3D::operator-(veter3D A)
//{
// return veter3D(this->x - A.x, this->y - A.y, this->z - A.z);
//}
//veter3D veter3D::operator*(float A)
//{
// return veter3D(this->x*A, this->y*A, this->z*A);
//}
//int main()
//{
// veter3D v1, v2, v3, v4, v5;
// int x;float y;
// cin >> x;
// if (x == 1)
// {
// cin >> v1;
// }
// if (x == 2)
// {
// cin >>v1>>v2;
// }
// cin >> y;
// v3 = v1 + v2;
// v3.display();
// cout << endl;
// v4 = v1 - v2;
// v4.display();
// cout << endl;
// v5 = v1 *y;
// v5.display();
// cout << endl;
// return 0;
//}
//#include<iostream>
//using namespace std;
//class veter_N
//{
//private:
// int n, *p;
//public:
// veter_N(int n=0, int* p=NULL)
// {
// this->n = n, *p = *p;
// }
// friend istream& operator>>(istream&, veter_N&);
// friend ostream& operator<<(ostream&, veter_N&);
// friend veter_N operator+(veter_N A,veter_N B);
// friend veter_N operator-(veter_N A);
// friend veter_N operator*(int C, veter_N A);
// veter_N operator[](int n);
//};
//istream& operator>>(istream&input, veter_N&B)
//{
// input >> B.n;
// if (B.n % 1 != 0 && B.n > 0)
// {
// cout << "Error Length!";
// exit(0);
// }
// else
// {
// B.p = &B.n;
// }
//}
//ostream& operator<<(ostream&output, veter_N&B)
//{
// output << B.n << *B.p;
//}
//veter_N operator+(veter_N A, veter_N& B)
//{
//
//}
//veter_N operator-(veter_N A, veter_N& B)
//{
//
//}
//veter_N operator*(int C, veter_N A)
//{
//
//}
//veter_N veter_N::operator[](int n)
//{
//
//}
#include<iostream>
#include<cmath>
using namespace std;
class fenshu
{
private:
int x, y;
public:
fenshu(int x=1, int y=1)
{
this->x = x, this->y = y;
}
void huajian()
{
int m, n; int z; int count;
m = fabs(x), n = fabs(y); z = m < n ? m : n;
if (x == 0&&y!=0)
{
cout << "0";
}
if (y == 0)
{
cout << "错误";
}
else {
do
{
{
count = 0;
for (int i = 2; i <= z; i++)
{
if (m % i == 0 && n % i == 0)
{
m = m / i;
n = n / i;
count++;
}
z = m < n ? m : n;
}
}
if (x * y > 0)
{
if (n == 1)
{
cout << m;
}
else
{
cout << m << "/" << n << endl;
}
}
if (x * y < 0)
{
if (n == 1)
{
cout << -m;
}
else
{
cout << -m << "/" << n << endl;
}
}
} while (count == 0);
}
}
// if(x*y<0)
// {
// m = fabs(x);
// n = fabs(y); z = m < n ? m : n;
// do
// {
// count = 0;
// for (int i = 2; i <= z; i++)
// {
// if (m % i == 0 && n % i == 0)
// {
// m = m / i;
// n = n / i;
// count++;
// }
// z = m < n ? m : n;
// }
// } while (count == 0);
// if (n == 1)
// {
// cout << -m;
// }
// else
// {
// cout << -m << "/" << n;
// }
// }
//}
void display()
{
cout << x << "/" << y<<endl;
}
friend istream& operator>>(istream&, fenshu&);
friend ostream& operator<<(ostream&, fenshu&);
fenshu operator+(fenshu A);
fenshu operator-(fenshu A);
fenshu operator*(fenshu A);
/*fenshu operator==(fenshu A);
fenshu operator!=(fenshu A);
fenshu operator>=(fenshu A);
fenshu operator<=(fenshu A);*/
};
istream& operator>>(istream& input, fenshu&A)
{
input >> A.x >> A.y;
return input;
}
ostream& operator<<(ostream& output, fenshu& A)
{
output << A.x << A.y;
return output;
}
fenshu fenshu::operator+(fenshu A)
{
return fenshu(this->x*A.y+A.x*this->y,this->y*A.y);
}
fenshu fenshu::operator-(fenshu A)
{
return fenshu((this->x * A.y - A.x * this->y), this->y * A.y);
}
fenshu fenshu::operator*(fenshu A)
{
return fenshu(this->x * A.x, this->y * A.y);
}
//fenshu fenshu:: operator==(fenshu A)
//{
//
//}
//fenshu fenshu:: operator!=(fenshu A)
//{
//
//}
//fenshu fenshu:: operator>=(fenshu A)
//{
//
//}
//fenshu fenshu:: operator<=(fenshu A)
//{
//
//}
int main()
{
fenshu a1, a2, a3, a4, a5, a6;
cin >> a1; cin >> a2;
a3 = a1 + a2;
a3.huajian();
a4 = a1 - a2;
a4.huajian();
a5 = a1 * a2;
a5.huajian();
}
本文来自博客园,作者:赵千万,转载请注明原文链接:https://www.cnblogs.com/zhaoqianwan/p/17307767.html
千万千万赵千万