C++ 修改的p369-17题 数组类

#include <iostream.h>

class CArray
{
private:
 int nSizeofInt;     //整型数组的大小
 int nNumofInt;  //整型数组中实际存放的元素个数
 int nSizeofFloat; //浮点数组的大小
 int nNumofFloat; //浮点数组中实际存放的元素个数
 int *pInt;   //指向整型数组,动态分配内存空间
 double *pFloat;  //指向浮点数组,动态分配内存空间
public:
 CArray(int nIntSize=100,int nFloatSize=200)
 {
  pInt=new int[nIntSize];
  pFloat=new double[nFloatSize];

  nSizeofInt=nIntSize;
  nSizeofFloat=nFloatSize;
  nNumofInt=0;
  nNumofFloat=0;
 }

 void put(int n)     //将n加入到整型数组中
 {
  if(nNumofInt<nSizeofInt)
  {
   pInt[nNumofInt]=n;
   nNumofInt+=1;
  }
 }
 void put(double x)    //将x加入到浮点数组中
 {
  if(nNumofFloat<nSizeofFloat)
  {
   pFloat[nNumofFloat]=x;
   nNumofFloat+=1;
  }
 }

 int getInt(int index)   //取整型数组中第index个元素,index从0开始
 {
  return pInt[index];
 }

 double getFloat(int index)  //取浮点数组中第index个元素,index从0开始
 {
  return pFloat[index];
 }

 ~CArray()      //析构,释放动态分配的空间
 {
  delete [100]pInt;
  delete [200]pFloat;
 }

 void print()     //分别输出整型数组合浮点数组中的所有元素
 {
  cout<<"/n整型数组大小 "<<nNumofInt;

  if(nNumofInt>0)
  {
   cout<<" 输出数组元素:"<<endl;
  }

  for(int i=0;i<nNumofInt;i++)
  {
   cout<<"["<<pInt[i]<<"] ";
  }
  
  cout<<"/n/n浮点数组大小 "<<nNumofFloat;
  
  if(nNumofFloat>0)
  {
   cout<<" 输出浮点数组元素:"<<endl;
  }

  for(i=0;i<nNumofFloat;i++)
  {
   cout<<"["<<pFloat[i]<<"] ";
  }

  cout<<endl;
 }
};


void main()
{
 int inti;
 double doublef;
 CArray A1;
 
 cout<<"向整型数组添加元素:/n";
 for(int i=0;i<100;i++)
 {
  cout<<"输入[1]确定输入元素,进行[下一步]输入任意键:";
  cin>>inti;
  
  if(inti==1)
  {
   cout<<"/n请输入整型数组元素:";
   cin>>inti;
   A1.put(inti);
  }
  else
   break;
 }

 

 cout<<"向浮点数组添加元素:/n";
 inti='/0';
 for(i=0;i<200;i++)
 {
  cout<<"输入[1]确定输入元素,进行[下一步]输入任意键:";
  cin>>inti;
  
  if(inti==1)
  {
   cout<<"/n请输入浮点数组元素:";
   cin>>doublef;
   A1.put(doublef);
  }
  else
   break;
 }
 
 inti='/0';
 cout<<"/n输入要查看整形数组的值的位置:";
 cin>>inti;
 cout<<inti<< " 位置的值是:";
 cout<<A1.getInt(inti)<<endl;

 inti='/0';
 cout<<"/n输入要查看整形数组的值的位置:";
 cin>>inti;
 cout<<inti<< " 位置的值是:";
 cout<<A1.getFloat(inti)<<endl;
 A1.print();

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

物欲添彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值