编程一小时2023.4.16

发布时间 2023-04-16 21:54:35作者: Verneyyx

1.

#include "iostream"
using namespace std;
class Fu
{
public:
  double shi;
  double xv;
  void set()
  {
    cin>>this->shi;
    cin>>this->xv;
  }
  double fun1()
  {
    return this->shi;
  }
  double fun2()
  {
    return this->xv;
  }
  friend void operator>(Fu x,Fu y)
  {
    if(x.xv*x.xv+x.shi*x.shi>y.xv*y.xv+y.shi*y.shi)
    cout<<"true"<<endl;
    else
    cout<<"false"<<endl;
  }
};
int main()
{
  Fu x[1000];
  int i=0;
  while(1)
  {

    x[i].set();
    x[i+1].set();
    if(x[i].fun1() || x[i].fun2() || x[i+1].fun2() || x[i+1].fun1())
    {
      x[i]>x[i+1];
    }
    else return 0;
    i+=2;
  }
}

2.

#include <bits/stdc++.h>
using namespace std;
class number
{
  int fz,fm;
  friend number operator+(number &n1,number &n2);
public:
  number(int a=0,int b=1)
  {
    fz=a;
    fm=b;
  }
  friend int gcd(int a,int b);
  friend int min_gb(number &n1,number &n2);
  int show()
  {
    cout<<fz;
    if(fm!=1) cout<<" "<<fm;
  }
};

int gcd(int a,int b)
{
  if(b==0) return a;
  else return gcd(b,a%b);
}

int min_gb(number &n1,number &n2)
{
  return n1.fm*n2.fm/gcd(n1.fm,n2.fm);
}

number operator+(number &n1,number &n2)
{
  number nn;
  int a=min_gb(n1,n2);
  nn.fm=a;
  nn.fz=(a/n1.fm)*n1.fz+(a/n2.fm)*n2.fz;
  int b=gcd(nn.fz,nn.fm);
  if(b!=1)
  {
    nn.fz=nn.fz/b;
    nn.fm=nn.fm/b;
  }
  return nn;
}

int main()
{
  int a,b,c,d;
  cin>>a>>b>>c>>d;
  number num1(a,b);
  number num2(c,d);
  number num3;
  num3=num1+num2;
  num3.show();
}

3.

#include<iostream>
using namespace std;
class MyTime{
private:
  int hour;
  int minute;
public:
  MyTime(int a=0, int b=0): hour(a), minute(b){};
  int operator-( const MyTime& t){
    int temp;
    temp=(t.hour-hour)*60+t.minute-minute;
    return temp;
  }
};
int main(){
  int a,b,c,d;
  cin>>a>>b>>c>>d;
  while(a||b||c||d){
    MyTime t1(a,b),t2(c,d);
    cout<<t1-t2<<endl;
    cin>>a>>b>>c>>d;
  }
return 0;
}

4.

#include <iostream>
using namespace std;
class Complex
{
public:
  Complex(double r=0, double i=0):real(r), imag(i){ }
  Complex operator+(Complex c) const;
  Complex operator-=(Complex c);
  friend Complex operator-(Complex c1,Complex c2) ;
  void Display() const;
private:
  double real;
  double imag;
};
Complex Complex::operator+ (Complex c)const{
c.imag=c.imag+this->imag;
c.real=c.real+this->real;
return c;
}
Complex Complex::operator-=(Complex c){
this->imag=this->imag-c.imag;
this->real=this->real-c.real;
return *this;

}
Complex operator-(Complex c1,Complex c2){
c1.imag=c1.imag-c2.imag;
c1.real=c1.real-c2.real;
return c1;
}

void Complex::Display() const
{
  cout << "(" << real << ", " << imag << ")" << endl;
}

int main()
{
  double r, m;
  cin >> r >> m;
  Complex c1(r, m);
  cin >> r >> m;
  Complex c2(r, m);
  Complex c3 = c1+c2;
  c3.Display();
  c3 = c1-c2;
  c3.Display();
  c3 -= c1;
  c3.Display();
  return 0;
}