贵州大学oj 第四次作业(仅供参考)

矩阵类Matrix

#include<iostream>
using namespace std;
class Matrix{
   private:
     int** ali;
     int r,c;
   public:
     Matrix(int a,int b):r(a),c(b){
         ali = new int*[r];
         for(int i=0;i<r;i++)
         ali[i] = new int[c];
     }
     int *operator[](int row)
     {
         return ali[row];
     }
     int getRows()
     {
     	return r;
	 }
	 int getColumns()
	 {
	 	return c;
	 }
    friend ostream& operator<<(std::ostream& cout, const Matrix& m) {  
    for (int i = 0; i < m.r; i++) {  
        for (int j = 0; j < m.c; j++) {  
            cout << m.ali[i][j] << " ";  
        }  
        cout << std::endl;  
    }  
    return cout; // 返回流对象以支持链式操作  
}  
     friend Matrix operator+(const Matrix a,const Matrix b)
     {
     	Matrix re(a.r,a.c);
         int i,j;
         for(i=0;i<a.r;i++)
         {
             for(j=0;j<a.c;j++)
             {
                 re[i][j]=a.ali[i][j]+b.ali[i][j];
             }
         }
         return re;
     }
     Matrix operator*(const Matrix b)
     {
         
         int i,j;
         Matrix re(r,r); 
         for(i=0;i<r;i++)
         {
             for(j=0;j<r;j++)
             {
                 int k;
                 int sum=0;
                 for(k=0;k<c;k++)
                 {
                 	sum+=ali[i][k]*b.ali[k][j];
				 }
				 re[i][j]=sum;
             }
         }
         return re;
     }

};

点类Point的运算符重载

#include<iostream>
#include<cmath>
using namespace std;
class Point{
   private:
     int x,y;
   public:
     Point(int a,int b):x(a),y(b){}
     friend double operator+(const Point a,const Point b)
     {
         double x1 = static_cast<double>(a.x - b.x);
         double y1 = static_cast<double>(a.y - b.y);
         return sqrt(x1*x1+y1*y1);
     }
     Point operator+(int n)
     {
         return Point(x+n,y+n);
     }
     friend ostream& operator<<(std::ostream& cout, const Point& m)
	 {
	 	cout<<"["<<m.x<<","<<m.y<<"]";
	 	 return cout;
	  } 
	 
     
};

二进制非负整数类Bina

#include<iostream>
#include<cmath>
using namespace std;
class Binary{
   private:
     int a,b,c,d;
   public:
     Binary(int a,int b,int c,int d):a(a),b(b),c(c),d(d){
     }
     int check()
     {
     	int sum=0;
     	if((a>1||a<0)||(b>1||b<0)||(c>1||c<0)||(d>1||d<0))
     	return -1;
     	else
     	{
		 sum+=a*pow(2,3);
     	sum+=b*pow(2,2);
     	sum+=c*pow(2,1);
     	sum+=d*pow(2,0);
     	return sum;}
     }
     void operator+(Binary t)
     {
         d = d+t.d;
         if(d>=2)
         {
		 d%=2;
		 c++;}
		 c = c+t.c;
         if(c>=2)
         {
		 c%=2;
		 b++;}
		 b = b+t.b;
         if(b>=2)
         {
		 b%=2;
		 a++;}
		 a = a+t.a;
         if(a>=2)
         {
		 a%=2;}
     }
     friend ostream& operator<<(std::ostream& cout, const Binary& c)
     {
         cout<<c.a<<c.b<<c.c<<c.d;
         return cout;
     }
};

MyArray类的运算符重载

#include<iostream>
#include<cmath>
using namespace std;
class MyArray{
   private:
     int size;
     int *ptr;
   public:
   	MyArray(){
	   }
     MyArray(int n):size(n){
         ptr = new int[size];
     }
    MyArray(const MyArray& m) : size(m.size) {  
        ptr = new int[size];  
        for (int i = 0; i < size; ++i) {  
            ptr[i] = m.ptr[i];  
        }  
    }  
  
    MyArray& operator=(const MyArray& m) {  
        if (this != &m) {  
            size = m.size;  
            ptr = new int[size];  
            for (int i = 0; i < size; ++i) {  
                ptr[i] = m.ptr[i];  
            }  
        }  
        return *this; 
    }  
     int &operator[](int index) const
     {
         return ptr[index];
     }
     int &operator[](int index)
     {
         return ptr[index];
     }
     friend MyArray operator+(const MyArray &a,const MyArray &b)
     {
         int l = a.size;
         MyArray p(l);
         for(int i=0;i<l;i++)
         p.ptr[i] = a.ptr[i] + b.ptr[i];
         return p;
     }
     friend MyArray operator-(const MyArray a,const MyArray b)
     {
         int l = a.size;
         MyArray p(l);
         for(int i=0;i<l;i++)
          p.ptr[i] = a.ptr[i] - b.ptr[i];
         return p;
     }
     friend int operator*(const MyArray a,const MyArray b)
     {
         int sum=0;
         for(int i=0;i<a.size;i++)
         {
             sum+=a.ptr[i] * b.ptr[i];
         }
         return sum;
     }
     friend ostream& operator<<(ostream& os,const MyArray &a)
     {
         int i;
         for(i=0;i<a.size-1;i++)
         {
             os<<a[i]<<",";
         }
         os<<a[i]<<endl;
         return os;
     }
     int getSize() const {  
        return size;  
    }  
};

MyString类的运算符

 ostream& operator<<(ostream& out, const MyString& str)
{
    out << str.ptr;
    return out;
}
int operator==(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) == 0)return 1;
    else return 0;
}
int operator!=(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) == 0)return 0;
    else return 1;
}
int operator<(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) < 0)return 1;
    else return 0;
}
int operator<=(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) > 0)return 0;
    else return 1;
}
int operator>(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) > 0)return 1;
    else return 0;
}
int operator>=(const MyString& s1, const MyString& s2)
{
    if (strcmp(s1.ptr, s2.ptr) < 0)return 0;
    else return 1;
} 
MyString operator+(const MyString& s1, const MyString& s2)
{
    MyString s;
    s.ptr = new char[strlen(s1.ptr) + strlen(s2.ptr) + 1];
    strcpy(s.ptr, s1.ptr);
    strcat(s.ptr, s2.ptr);
    return s;
}
MyString &MyString :: operator=(const MyString& s)
{
    ptr = new char[strlen(s.ptr) + 1];
    strcpy(ptr, s.ptr);
    return *this;
}
MyString &MyString :: operator+=(const MyString& s)
{
    char* temp;
    temp = ptr;
    ptr = new char[strlen(temp) + strlen(s.ptr) + 1];
    strcpy(ptr, temp);
    delete[]temp;
    strcat(ptr, s.ptr);
    return *this;
}
 char MyString::operator[](int i)
{
    return this->ptr[i];
}

Complex的运算符重载

#include<iostream>
using namespace std;
class Complex{
   private:
     double real,imag;
   public:
     Complex(double a,double b):real(a),imag(b){}
     friend Complex operator*(Complex &a,Complex &b)
     {
         double real1 = a.real*b.real - a.imag*b.imag;
         double imag1 = a.real*b.imag + a.imag*b.real;
         Complex p(real1,imag1);
         return p;
     }
     void operator+(int n)
     {
         real+=n;
         imag+=n;
     }
     friend ostream &operator<<(ostream& os, const Complex& a) {  
    os << a.real << "+" << a.imag << "i" << endl;  
    return os;  
}  
};

 

Circle类的运算符重载

#include<iostream>
using namespace std;
const double pi = 3.14159265;
class Circle{
   private:
     double x,y,radius;
   public:
     Circle(double a,double b,double c):x(a),y(b),radius(c){}
     friend double operator+(Circle a,Circle b)
     {
         double sum = 3.14159265*a.radius*a.radius + 3.14159265*b.radius*b.radius;
         return sum;
     }
     void operator+(double n)
     {
         x+=n;
         y+=n;
         radius+=n;
     }
     friend ostream& operator<<(ostream& out,Circle &a)
     {
         out<<"圆心:"<<a.x<<","<<a.y<<" 半径:"<<a.radius<<endl;
         return out;
     }
};

 Time类的运算符重载

#include<iostream>
#include<cmath>
using namespace std;
class Time{
   private:
     int hour,minute,second;
     int sumsecond;
   public:
     Time(int a,int b,int c):hour(a),minute(b),second(c){
         int sum = 0;
         sum+=hour*60*60;
         sum+=minute*60;
         sum+=second;
         sumsecond = sum;
     }
     Time operator+(int n)
     {
     	sumsecond+=n;
     	sumsecond%=(60*60*24);
         int hourn = n/(60*60);
         n%=60*60;
         int minuten = n/60;
         n%=60;
         int secondn = n;
         hour += hourn;
         minute += minuten;
         second += secondn;
         if(second>=60)
         {
             minute += second/60;
             second%=60;
         }
         if(minute>=60)
         {
             hour += minute/60;
             minute%=60;
         }
         if(minute>=24)
         hour%=24;
         return *this;
     }
     Time operator-(int n)
     {
     	n%=(60*60*24);
         int sum = sumsecond - n;
         if(sum<0)
         sum+=60*60*24;
        sumsecond = sum;
         int h = sum/(60*60);
         sum%=3600; 
         int m = sum/60;
         sum%=60;
         int s = sum;
         Time a(h,m,s);
         return a;
     }
    int operator-(Time a)
    {
    	return sumsecond - a.sumsecond;
	}
     friend ostream &operator<<(ostream& os,Time &m)
     {
         os<<m.hour<<":"<<m.minute<<":"<<m.second<<endl;
         return os;
     }
};

自定义字符串类MyStri

#include <iostream>  
#include <cstring>  
#include <string>  
using namespace std;  
  
class MyString {  
private:  
    char *sptr;  
    int n = 0;  
  
public:  
    MyString() {  
        cout << "Object Constructed. No Memory Allocated." << endl;  
        sptr = NULL;  
    }  
  
    MyString(const string &a) {  
        n = a.size();  
        sptr = new char[n + 1];  
        strcpy(sptr, a.c_str());   
        cout << "Object Constructed. " << n + 1 << " Bytes Allocated." << endl;  
    }  
  
    MyString(const MyString &a)   
    {  
        n = a.n;  
        sptr = new char[n + 1];  
        strcpy(sptr, a.sptr);   
        cout << "Object Constructed. "<<n+1<<" Bytes Allocated." << endl;  
    }  
  
    MyString& operator=(const MyString &a)  
    {  
        if (this != &a) {   
            delete[] sptr;  
            n = a.n;  
            sptr = new char[n + 1];  
            strcpy(sptr, a.sptr);  
        }  
        return *this;  
    }  
  
    void printString() {  
        if (n == 0)  
            cout << "No Memory Allocated In This Object." << endl;  
        else  
            cout << sptr << endl;   
    }  
  
    int getSize() {  
        return n;  
    }  
  
    void stringCopy(const MyString &a)  
    {  
        delete[] sptr;
		int l=n;  
        n = a.n;  
        sptr = new char[n + 1];  
        strcpy(sptr, a.sptr); 
        cout <<"String Copy, "<<n+1<<" Bytes Reallocated And "<<l+1<<" Bytes Freed."<< endl;  
    }  
  
    void stringCat(const MyString &a) {  
        int newSize = n + a.n + 1; 
		int l = n; 
        char *t = new char[newSize];  
  
  
        strcpy(t, sptr);  
  
 
        strcpy(t + n, a.sptr);  
  
       
        delete[] sptr;  
        sptr = t;  
        n = newSize - 1;   
  
        cout <<"String Connection, "<<n+1<<" Bytes Reallocated And "<<l+1<<" Bytes Freed."<< endl;  
    }  
    
     ~MyString()
     {
     	if(n==0)
     	cout<<"Object Destructed. No Memory Freed."<<endl;
     	else
     	cout<<"Object Destructed. "<<n+1<<" Bytes Freed."<<endl;
         delete []sptr;
     }
};

 this的使用

#include<iostream>
using namespace std;
class Test{
   private:
     int x,y;
   public:
     Test(int a,int b):x(a),y(b){}
     Test *setXY1(int a,int b)
     {
         x+=a;
         y+=b;
         return this;
     }
     Test &setXY2(int a,int b)
     {
         x-=a;
         y-=b;
         return *this;
     }
     void showXY()
     {
         cout<<x<<" "<<y<<endl;
     }
};

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
吉林大学oj考试系统是吉林大学为学生提供的在线考试平台。该系统便于学生在任何时间和地点参加各类考试,为学生带来了诸多便利性和效率提升。 首先,吉林大学oj考试系统的在线性质使得学生可以在自己方便的时间参加考试。不再受限于传统考试的固定时间和地点,学生可以根据自己的学习进度和个人安排,合理安排考试时间,提高自己的学习效率。 其次,该系统具备灵活性和多样性。学生可以根据自己的学科需求选择参加不同类型的考试,如选择题、填空题、简答题等。系统还可以根据考试的难易程度进行分类,使学生能够有针对性地进行学习和准备。这为学生提供了更多的学习选择空间,并能够更好地帮助他们提高自己的学科水平。 此外,吉林大学oj考试系统还具备快速反馈和评分系统。在学生完成考试后,系统能够立即给出评分和反馈,使学生能够及时了解自己的考试成绩和不足之处。这有助于学生及时发现自己的学习不足,做出相应的调整和改进,提升学习效果。 最后,吉林大学oj考试系统还可以提供学科竞赛和学术竞赛的机会。该系统可以举办不同层次和类型的竞赛,吸引更多的学生积极参与,提高大家的学科水平和学术能力。 总之,吉林大学oj考试系统具有灵活性、便捷性和多样性等优势,使学生能够更好地进行学习和考试。该系统为学生提供了更多的学习选择空间和机会,有助于提高学生的学习效率和成绩表现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值