⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 实现运算符的重载!!让出学者有个模版来参考
💻 CPP
字号:
#include <iostream>
#include<conio.h>

using namespace std;

const int SIZE=10;

template<class T>class Cstack
{
public:
	void init(){position=0;};
	T*push(T ch);
	T*pop();
private:
	int position;
	T   stk[SIZE];
};
template<class T>
T*Cstack<T>::push(T ch)
{
	if (position==SIZE)
	{
		cout<<endl<<"stack is full"<<endl;
		return NULL;
	}
	stk[position++]=ch;
	return stk+position;
}

template<class T>
T*Cstack<T>::pop()
{
	if (position==0)
	{
		cout<<"stack is NULL"<<endl;
		return NULL;
	}
	position--;
	return  stk+position;
}
class complex
{
public:
	complex();
	complex(complex &a);
	complex(double r,double i);
	void display();
	void operator=(complex a);
	complex operator+(complex a);
	complex operator-(complex a);
	complex operator+(double r);
	complex operator-(double r);
private:
	double real;
	double img;


};
complex::complex()
{
	real=0;
	img=0;
}
complex::complex(complex &a)
{
	real=a.real;
	img=a.img;
}
complex::complex(double r , double i)
{
	real=r;
	img=i;
}
void complex::display()
{
	cout<<real<<(img>=0?"+":"")<<img<<"i"<<endl;
}
void complex::operator=(complex a)
{
	real=a.real;
	img=a.img;
}
complex complex::operator+(complex a)
{
	complex temp(real+a.real,img+a.img);
	return temp;
}
complex complex::operator+(double r)
{
	complex temp(real+r,img);
	return temp;
}
complex complex::operator-(complex a)
{
	complex temp(real-a.real,img-a.img);
	return temp;
}
complex complex::operator-(double r)
{
	complex temp(real-r,img);
	return temp;
}
void main()
{
	Cstack<char>s;
	s.init();
	char*p,ch;
	cout<<"please input characters"<<endl;
	cin>>ch;
	while (ch!='!'&&s.push(ch))
		cin>>ch;
	cout<<"data  in stack"<<endl;
	while(p=s.pop())
		cout<<*p<<endl;
	Cstack<float> fs;
	fs.init();
	float *pf,f;
	cout<<"please input float number"<<endl;
	cin>>f;
	while(f!=0&&fs.push(f))
		cin>>f;
	while(pf=fs.pop())
		cout<<*pf;
		
	getch();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -