?? main.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 + -