急求一个 C++ 大程序的源代码(代码量尽量大些),要求有注释·有流程
1个回答
2013-11-10
展开全部
头文件1:#include "IntContainer.h"template<class T>
IntContainer<T>::IntContainer(int size)
{
increaseSize = size;
quantity=increaseSize;
count=0;
storage = new T[quantity];
cout<<"构造函数"<<storage<<endl;
}template<class T>
void IntContainer<T>::add(T element)
{
if (count>= quantity)
increase();
storage[count] = element;
count++;
}template<class T>
T IntContainer::fetch(int index) const
{
if (index>=0&&index<count)
return storage[index];
else
return -1;
}
template<class T>
void IntContainer<T>::increase()
{
quantity += increaseSize;
T * newStorage = new T [quantity];
for(int i=0;i<count;i++)
newStorage[i]=storage[i];
delete[] storage;
storage = newStorage;
}
template<class T>
IntContainer<T>::~IntContainer()
{
cout<<"删除storage析构函数"<<storage<<endl;
delete[] storage;
}
头文件2:#ifndef INTCONTAINER
#define INTCONTAINER
#include <iostream>
using namespace std;template<class T>
class IntContainer
{
private:
T * storage;
int count;
int quantity;
int increaseSize; //IntContainer(IntContainer & tmp);public:
IntContainer(int size=100);
~IntContainer(); void add(T element);
T fetch(int index) const ;
int getCount() const {return count;} class iterator
{
private:
IntContainer* c;
int position;
public:
iterator(IntContainer * container, bool flag):c(container)
{
if (flag == true) position = 0;
if (flag == false) position = container->getCount();
}
bool operator==(iterator & right)
{
return this->position == right.position;
}
bool operator!=(iterator & right)
{
return this->position != right.position;
}
iterator & operator++()
{
position++;
return *this;
}
iterator operator++(int)
{
iterator tmp = *this;
position++;
return tmp;
}
int operator*()
{
return c->fetch(position);
}
}; iterator begin(){return iterator(this, true);}
iterator end(){return iterator(this, false);}
private:
void increase();
};template<class T>
IntContainer<T>::IntContainer(int size)
{
increaseSize = size;
quantity=increaseSize;
count=0;
storage = new T[quantity];
cout<<"构造函数"<<storage<<endl;
}template<class T>
void IntContainer<T>::add(T element)
{
if (count>= quantity)
increase();
storage[count] = element;
count++;
}template<class T>
T IntContainer<T>::fetch(int index) const
{
if (index>=0&&index<count)
return storage[index];
else
return -1;
}
template<class T>
void IntContainer<T>::increase()
{
quantity += increaseSize;
T * newStorage = new T [quantity];
for(int i=0;i<count;i++)
newStorage[i]=storage[i];
delete[] storage;
storage = newStorage;
}
template<class T>
IntContainer<T>::~IntContainer()
{
cout<<"删除storage析构函数"<<storage<<endl;
delete[] storage;
}
#endifmain函数#include "IntContainer.h"
#include <vector>
#include <iostream>
#include <numeric>
#include <functional>
using namespace std;int multiply(int x, int y){return x*y;}template<class T>
class multiplyClass
{
public:
T operator()(T x, T y){return x*y;}
};void main()
{
IntContainer<int> ic;
ic.add(1);
ic.add(2); IntContainer<int>::iterator first = ic.begin();
IntContainer<int>::iterator end = ic.end(); for( ; first != end; first++)
cout<<*first<<endl;
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
for ( vector<int>::iterator tmp = vec.begin();
tmp != vec.end();
tmp++)
{
cout<<*tmp<<endl;
} cout<<"________________"<<endl;
cout<<accumulate(vec.begin(), vec.end(),1,multiplies<int>())<<endl; double doubleArray[]={12,13.4,34,5,6.6};
cout <<accumulate(&doubleArray[0], &doubleArray[5],1, multiplies<double>())<<endl; vector<double> dArray(10);
partial_sum(&doubleArray[0], &doubleArray[5],dArray.begin(),minus<double>());
for(vector<double>::iterator first = dArray.begin();
first != dArray.end(); first++)
cout<<*first<<endl;
}
IntContainer<T>::IntContainer(int size)
{
increaseSize = size;
quantity=increaseSize;
count=0;
storage = new T[quantity];
cout<<"构造函数"<<storage<<endl;
}template<class T>
void IntContainer<T>::add(T element)
{
if (count>= quantity)
increase();
storage[count] = element;
count++;
}template<class T>
T IntContainer::fetch(int index) const
{
if (index>=0&&index<count)
return storage[index];
else
return -1;
}
template<class T>
void IntContainer<T>::increase()
{
quantity += increaseSize;
T * newStorage = new T [quantity];
for(int i=0;i<count;i++)
newStorage[i]=storage[i];
delete[] storage;
storage = newStorage;
}
template<class T>
IntContainer<T>::~IntContainer()
{
cout<<"删除storage析构函数"<<storage<<endl;
delete[] storage;
}
头文件2:#ifndef INTCONTAINER
#define INTCONTAINER
#include <iostream>
using namespace std;template<class T>
class IntContainer
{
private:
T * storage;
int count;
int quantity;
int increaseSize; //IntContainer(IntContainer & tmp);public:
IntContainer(int size=100);
~IntContainer(); void add(T element);
T fetch(int index) const ;
int getCount() const {return count;} class iterator
{
private:
IntContainer* c;
int position;
public:
iterator(IntContainer * container, bool flag):c(container)
{
if (flag == true) position = 0;
if (flag == false) position = container->getCount();
}
bool operator==(iterator & right)
{
return this->position == right.position;
}
bool operator!=(iterator & right)
{
return this->position != right.position;
}
iterator & operator++()
{
position++;
return *this;
}
iterator operator++(int)
{
iterator tmp = *this;
position++;
return tmp;
}
int operator*()
{
return c->fetch(position);
}
}; iterator begin(){return iterator(this, true);}
iterator end(){return iterator(this, false);}
private:
void increase();
};template<class T>
IntContainer<T>::IntContainer(int size)
{
increaseSize = size;
quantity=increaseSize;
count=0;
storage = new T[quantity];
cout<<"构造函数"<<storage<<endl;
}template<class T>
void IntContainer<T>::add(T element)
{
if (count>= quantity)
increase();
storage[count] = element;
count++;
}template<class T>
T IntContainer<T>::fetch(int index) const
{
if (index>=0&&index<count)
return storage[index];
else
return -1;
}
template<class T>
void IntContainer<T>::increase()
{
quantity += increaseSize;
T * newStorage = new T [quantity];
for(int i=0;i<count;i++)
newStorage[i]=storage[i];
delete[] storage;
storage = newStorage;
}
template<class T>
IntContainer<T>::~IntContainer()
{
cout<<"删除storage析构函数"<<storage<<endl;
delete[] storage;
}
#endifmain函数#include "IntContainer.h"
#include <vector>
#include <iostream>
#include <numeric>
#include <functional>
using namespace std;int multiply(int x, int y){return x*y;}template<class T>
class multiplyClass
{
public:
T operator()(T x, T y){return x*y;}
};void main()
{
IntContainer<int> ic;
ic.add(1);
ic.add(2); IntContainer<int>::iterator first = ic.begin();
IntContainer<int>::iterator end = ic.end(); for( ; first != end; first++)
cout<<*first<<endl;
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
for ( vector<int>::iterator tmp = vec.begin();
tmp != vec.end();
tmp++)
{
cout<<*tmp<<endl;
} cout<<"________________"<<endl;
cout<<accumulate(vec.begin(), vec.end(),1,multiplies<int>())<<endl; double doubleArray[]={12,13.4,34,5,6.6};
cout <<accumulate(&doubleArray[0], &doubleArray[5],1, multiplies<double>())<<endl; vector<double> dArray(10);
partial_sum(&doubleArray[0], &doubleArray[5],dArray.begin(),minus<double>());
for(vector<double>::iterator first = dArray.begin();
first != dArray.end(); first++)
cout<<*first<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询