C++实验四

1.定义一个复数类Complex,重载运算符“+”、“-”、“*”、“-”“==”,使之能用于复数的加、减、乘、求反以及比较的等运算。要求分别重载运算符函数为Complex类的成员函数和非成员函数(友元函数)。在主函数中进行测试。

#include <iostream>
#include <cstdio>
using namespace std;

class Complex
{
    public:
      Complex(){}
      Complex(double r, double i){ real = r; img = i;}
      Complex operator+(const Complex &a);
      Complex operator-(const Complex &a);
      bool operator==(const Complex &a);
      void display()
	  {
	      printf(" %f%+fi\n", real, img);
      }
      friend Complex operator*(const Complex &a, const Complex &b);
      friend Complex operator-(const Complex &a);
    private:
      double real;
      double img;
};
int main()
{
	double a1, a2, b1, b2;
	bool flag = 1;
	cin>>a1>>a2>>b1>>b2;
    Complex a(a1, a2);
    Complex b(b1, b2);
    Complex c;
    
    cout<<"a =";
    a.display();
    cout<<"b =";
    b.display();
    
    c = a + b;
    cout<<"a + b =";
    c.display();
    
    c = a - b;
    cout<<"a - b =";
    c.display();
    
	c = a * b;
	cout<<"a * b =";
    c.display();
        
	c = -b;
	cout<<"-b =";
    c.display();
    
	flag = (a==b);
    if(flag==1) cout<<"a==b"<<endl;
    else cout<<"a!=b"<<endl;
    return 0;
}
Complex Complex::operator+(const Complex &a)
{
    Complex c(real+a.real, img+a.img);
	return c;
}
Complex Complex::operator-(const Complex &a)
{
    Complex c(real-a.real, img-a.img);
	return c;
}
bool Complex::operator==(const Complex &a)
{
	if(real==a.real && img==a.img)
	  return true;
	else
	  return false;
}


Complex operator*(const Complex &a, const Complex &b)
{
    Complex c(a.real*b.real-a.img*b.img, 
			  a.real*b.img+a.img*b.real);
	return c;
}
Complex operator-(const Complex &a)
{
    Complex c(-a.real, -a.img);
	return c;
}

2.声明Point类,有坐标x和y两个成员变量;对Point类重载“++”“--”自增、自减(前置、后置)运算符,实现对坐标值的改变。

#include <iostream>
using namespace std;

class Point
{
    public:
      Point(){}
      Point(int xx, int yy):x(xx), y(yy){}
      Point &operator++();
      Point operator++(int);
      Point &operator--();
      Point operator--(int);
      void showP() const
      {
        cout<<"( "<<x<<" , "<<y<<" )"<<endl;
      }
	private:	
	  int x;
	  int y;
};

int main()
{
    int x1, y1, x2, y2;
    cin>>x1>>y1>>x2>>y2;
    Point a(x1, y1);
	Point b(x2, y2);
	
	cout<<"a   : ";
	a.showP();
	cout<<"b   : ";
	b.showP();
	
	cout<<"++a : ";
	(++a).showP();
	cout<<"a   : ";
	a.showP();
	
	cout<<"b++ : ";
	(b++).showP();
	cout<<"b   : ";
	b.showP();
	
	cout<<"a-- : ";
	(a--).showP();
	cout<<"a   : ";
	a.showP();
	
	cout<<"--b : ";
	(--b).showP();
	cout<<"b   : ";
	b.showP();
	
    return 0;
}

Point & Point::operator++()
{
    x++;
    y++; 
    return *this;
}
Point Point::operator++(int)
{
	Point old = *this;
	++(*this); 
    return old;
}
Point & Point::operator--()
{
    x--;
    y--; 
    return *this;
}
Point Point::operator--(int)
{
	Point old = *this;
	--(*this); 
    return old;
}


3.设计一个一维的int数组类IntArray(属性:下标下限、下标上限、int型指针),可以任意指定下标范围(初始化时要判断下标是否正确),并重载下标访问运算符“[]”实现数组类的下标访问。在主函数中(创建一个下标1-10的数组对象,初始化并输出)进行测试。

#include <iostream>
using namespace std;

class IntArray
{
    public:
      IntArray(int l, int h):low(l), high(h)
	  {
	      int size = high-low+1;
		  p = new int [size];
	  	  for(int i=0;i<size;i++)
	        {
	  	      p[i] = 0;
	        }
	  }
	  IntArray(int l, int h, int *a):low(l), high(h)
	  {
	      int size = high-low+1;
		  p = new int [size];
	  	  for(int i=0;i<size;i++)
	        {
	  	      p[i] = a[i];
	        }
	  } 
	  void setArray(int i, int n);
      int operator[](int n);
    private:
      int low;
      int high;
      int *p;
};

int main()
{
    IntArray a(1, 10);
    for(int i=1;i<=10;i++) 
      cout<<a[i]<<" ";
    cout<<endl;
    for(int i=1;i<=10;i++) 
      a.setArray(i, i);
    for(int i=1;i<=10;i++) 
      cout<<a[i]<<" ";
    cout<<endl;
    int b[5] = {6, 7, 8, 9, 10};
	IntArray c(10, 14, b);
	for(int i=10;i<=14;i++) 
      cout<<c[i]<<" ";
    cout<<endl;
    return 0;	
}

int IntArray::operator[](int i)
{
    return p[i-low];
}
void IntArray::setArray(int i, int n)
{
    p[i-low] = n;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值