c++学习笔记之 数组类与负数类

//

#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstring>
class array
{
	public:
		//friend std::ostream &operator<<(std::ostream &os, const array &a);
		//friend array& operator+(const array& a, const array& b); 
		array& operator+(const array& a);
		array &operator+=(const array &a);
		bool operator>(const array &a) const;
		//friend bool operator<=(const array&a,const array&b);
		bool operator<=(const array&a) const;
		bool operator>=(const array&a) const;
		bool operator<(const array&a) const;
		bool operator==(const array&a) const;
		int& operator[](int index);
		//array();
		//array();
		void operator=(const array& a);
		array(const array & a);
		array(int n);
		//void operator=(const array &a);
		~array();
		void print();
	private:
		int* arr_;
		int len_;




};



#endif

#include "array.h"
using namespace std;
int& array::operator[](int index)
{
	return *(arr_+index);
}

// array::array():arr_(new int[1])
// {
// }
void array::operator=(const array& a)
{
	int l=a.len_;
	arr_=new int[l];
	for(int i=0;i<l;++i)
	{
		*(arr_+i)=*(a.arr_+i);//cout<<"exe"<<endl;
	}
}

array::array(const array & a)
{
	int l=a.len_;
	arr_=new int[l];
	for(int i=0;i<l;++i)
	{
		*(arr_+i)=*(a.arr_+i);//cout<<"exe"<<endl;
	}
}

// void array::operator=(const array &a)
// {
// 	arr_=new int[1];
// }

array::array(int n)
{
	arr_=new int[n];
	len_=n;
}

array::~array()
{
	delete[] arr_;
}

void array::print()
{
	cout<<*arr_<<endl;
}

// ostream &operator<<(std::ostream &os, const array &a)
// {
// 	for(int i=0;i<a.len_;++i)
// 	{
// 		os<<*(a.arr_+i)<<" ";
// 	}
// }




array & array::operator+=(const array &a)
{
    // "foo" + "bar"
    int *pt=new int[a.len_+len_];
    int j=0;
    for(int i=0;i<a.len_+len_;++i)
    {
    	if(i<len_)
    	{
    		*(pt+i)=*(arr_+i);
    	}else
    	{
    		*(pt+i)=*(a.arr_+j);
    		j++;
    	}
    }
    delete[] arr_;
    arr_=pt;
    return *this;
}

array& array::operator+(const array&  a)
{
    array* c=new array(a.len_+len_);
    int *pt=new int[a.len_+len_];
    int j=0;
    for(int i=0;i<a.len_+len_;++i)
    {
    	if(i<len_)
    	{
    		*(pt+i)=*(arr_+i);
    	}else
    	{
    		*(pt+i)=*(a.arr_+j);
    		j++;
    	}
    }
    c->arr_=pt;
	return *c;
}

//bool array::operator<=(const array&a,const array&b)
//{
// 	string bs="";
// 	string as="";
// 	char buf[5];
// 	for(int i=0;i<a.len_;++i)
// 	{
// 		memset(buf,0,10);
// 		sprintf(buf,"%d",*(a.arr_+i));
// 		as+=buf;
// 	}
// 	for(int i=0;i<b.len_;++i)
// 	{
// 		memset(buf,0,10);
// 		sprintf(buf,"%d",*(b.arr_+i));
// 		bs+=buf;
// 	}
// 	return as<=bs;
//	return ture;
//}

bool array::operator<=(const array &a) const
{
	string ps="";
	string as="";
	char buf[1];
	for(int i=0;i<a.len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(a.arr_+i));
		as+=buf;
	}
	for(int i=0;i<len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(arr_+i));
		ps+=buf;
	}
	
	return ps<=as;
}
bool array::operator>(const array &a) const
{
	string ps="";
	string as="";
	char buf[1];
	for(int i=0;i<a.len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(a.arr_+i));
		as+=buf;
	}
	for(int i=0;i<len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(arr_+i));
		ps+=buf;
	}
	
	return ps>as;
}

bool array::operator>=(const array&a) const
{
	string ps="";
	string as="";
	char buf[1];
	for(int i=0;i<a.len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(a.arr_+i));
		as+=buf;
	}
	for(int i=0;i<len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(arr_+i));
		ps+=buf;
	}
	return ps>=as;
}
bool array::operator<(const array&a) const
{
	string ps="";
	string as="";
	char buf[1];
	for(int i=0;i<a.len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(a.arr_+i));
		as+=buf;
	}
	for(int i=0;i<len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(arr_+i));
		ps+=buf;
	}
	return ps<as;
}
bool array::operator==(const array&a) const
{
	string ps="";
	string as="";
	char buf[1];
	for(int i=0;i<a.len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(a.arr_+i));
		as+=buf;
	}
	for(int i=0;i<len_;++i)
	{
		memset(buf,0,1);
		sprintf(buf,"%d",*(arr_+i));
		ps+=buf;
	}
	
	return ps==as;
}

///

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>

class complex
{
	public:
		complex();
		complex(int real, int img);
		complex(const complex &c);
		friend std::ostream &operator<<(std::ostream &os,const complex &c);
		//std::ostream &operator<<(std::ostream &os);
		complex& operator=(const complex &c);
		complex operator+(const complex &c);
		complex operator-(const complex &c);
		complex operator*(const complex &c);
		void print();
		int mod();
	private:
		int real_;
		int img_;
};


#endif

#include "complex.h"
using namespace std;

complex::complex()
{
	real_=0;
	img_=0;
}

complex::complex(int real, int img):real_(real),img_(img)
{

}

void complex::print()
{
	cout<<real_<<"+"<<img_<<"i"<<endl;
}

complex::complex(const complex &c)
{
	real_=c.real_;
	img_=c.img_;
}
complex& complex::operator=(const complex &c)
{
	real_=c.real_;
	img_=c.img_;
	return *this;
}
complex complex::operator+(const complex &c)
{
	complex ns;
	ns.real_=real_+c.real_;
	ns.img_=img_+c.img_;
	return ns;
}
complex complex::operator-(const complex &c)
{
	complex ns;
	ns.real_=real_-c.real_;
	ns.img_=img_-c.img_;
	return ns;
}
complex complex::operator*(const complex &c)
{
	complex ns;
	ns.real_=real_*c.real_;
	ns.img_=img_*c.img_;
	return ns;
}
int complex::mod()
{
	return real_*real_+img_*img_;
}

// ostream &complex::operator<<(ostream &os)
// {
// 	os<<real_<<"+"<<img_<<"i";
// 	return os;
// }

ostream& operator<<(ostream &os,const complex &c)
{
	os<<c.real_<<"+"<<c.img_<<"i"<<endl;
	return os;
}
二元操作写成友元的形式更好,便于查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值