C++自定义类似栈的模版类
代码如下,不知道为什么错了,求大神解释#include<iostream>usingnamespacestd;template<classT>classmyclass{T...
代码如下,不知道为什么错了,求大神解释
#include<iostream>using namespace std;template<class T>class myclass{ T * b; int size;public: void push(T x) { size++; if(b==NULL) b=&x; else{b++;b=&x;} } myclass(T * p=NULL) { b=p; size=0; } T sum(); bool find(T x); void display() { T *p=b; int i=size; while(i>0) { cout<<*p<<" "; p--; i--; } }};template<class T>T myclass<T>::sum(){ T sum=0; T * p=b; int i=size; while(i>0) { sum=sum+*p; p--; i--; } return sum;}template<class T>bool myclass<T>::find(T x){T * p=b; int i=size; while(i>0) { if((*p-x)<0.0000001) {cout<<i;return true;} cout<<*p<<endl; p--; i--; }return false;}int main(){ myclass<int> a1; myclass<double> a2; myclass<float> a3; for(int i=1;i<=100;i++) { a1.push(i); a2.push((double)i); a3.push((float)i); } a1.display(); cout<<"find 50 frome a1 is "<<a1.find(50)<<endl; cout<<"find 50 frome a2 is "<<a2.find(50)<<endl; cout<<"find 50 frome a3 is "<<a3.find(50)<<endl; cout<<"the sum of a1 is"<<a1.sum()<<endl; cout<<"the sum of a2 is"<<a2.sum()<<endl; cout<<"the sum of a3 is"<<a3.sum()<<endl; system("pause"); return 0;
} 展开
#include<iostream>using namespace std;template<class T>class myclass{ T * b; int size;public: void push(T x) { size++; if(b==NULL) b=&x; else{b++;b=&x;} } myclass(T * p=NULL) { b=p; size=0; } T sum(); bool find(T x); void display() { T *p=b; int i=size; while(i>0) { cout<<*p<<" "; p--; i--; } }};template<class T>T myclass<T>::sum(){ T sum=0; T * p=b; int i=size; while(i>0) { sum=sum+*p; p--; i--; } return sum;}template<class T>bool myclass<T>::find(T x){T * p=b; int i=size; while(i>0) { if((*p-x)<0.0000001) {cout<<i;return true;} cout<<*p<<endl; p--; i--; }return false;}int main(){ myclass<int> a1; myclass<double> a2; myclass<float> a3; for(int i=1;i<=100;i++) { a1.push(i); a2.push((double)i); a3.push((float)i); } a1.display(); cout<<"find 50 frome a1 is "<<a1.find(50)<<endl; cout<<"find 50 frome a2 is "<<a2.find(50)<<endl; cout<<"find 50 frome a3 is "<<a3.find(50)<<endl; cout<<"the sum of a1 is"<<a1.sum()<<endl; cout<<"the sum of a2 is"<<a2.sum()<<endl; cout<<"the sum of a3 is"<<a3.sum()<<endl; system("pause"); return 0;
} 展开
展开全部
#include<iostream>
using namespace std;
template<class T>
class myclass
{
T * b;
int size;
int capcity;
public:
void push(T x)
{
// 你的程序没有分配内存
if(capcity == size)
{
if(capcity == 0)
{
capcity = 10;
b = new T[capcity+1];
}
else
{
capcity *= 2;
T * n = new T[capcity+1];
memcpy(n, b, sizeof(T) * size);
delete [] b;
b = n;
}
}
b[size] = x;
size++;
}
myclass(T * p = NULL)
{
b = p;
size = 0;
capcity = 0;
}
T sum();
bool find(T x);
void display()
{
T * p = b;
T * ph = p + size;
while(p < ph) cout << *p++ << " ";
cout << endl;
}
};
template<class T>
T myclass<T>::sum()
{
T sum = 0;
T * p = b;
T * ph = p + size;
while(p < ph) sum += *p++;
return sum;
}
template<class T>
bool myclass<T>::find(T x)
{
T * p = b;
T * ph = p + size;
while(p < ph)
{
T m = *p++ - x;
if(-0.0000001 < m && m < 0.0000001 )
{
cout << (size - (ph - p) - 1) << " ";
return true;
}
}
return false;
}
int main()
{
myclass<int> a1;
myclass<double> a2;
myclass<float> a3;
for(int i = 1;i <= 100; i++)
{
a1.push(i);
a2.push((double)i);
a3.push((float)i);
}
a1.display();
cout << "find 50 frome a1 is " << a1.find(50) << endl;
cout << "find 50 frome a2 is " << a2.find(50) << endl;
cout << "find 50 frome a3 is " << a3.find(50) << endl;
cout << "the sum of a1 is "<< a1.sum() << endl;
cout << "the sum of a2 is "<< a2.sum() << endl;
cout << "the sum of a3 is "<< a3.sum() << endl;
system("pause");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询