】编写void put(int n)函数时,注意考虑数组满的情况
2017-01-19 09:51
751 查看
#include <iostream>
using namespace std;
class ListArray{
private:
int size;
int elem;
int *p;
public:
ListArray (int s = 100);
void put (int n);
void print ();
~ListArray();
};
ListArray::ListArray (int s)
{
size = s;
elem = 0;
p = new int[s];
}
void ListArray::put (int n)
{
int *pn;
int i;
if (elem == size)
{
size++;
pn = new int [size];
for (i = 0; i < elem; i++)
{
pn[i] = p[i];
}
pn[elem++] = n;
delete[] p;
p = pn;
}
else
{
p[elem++] = n;
}
}
void ListArray::print ()
{
int j;
cout << "所有元素:";
for (j = 0; j < elem; j++)
{
cout << p[j] << " ";
}
cout << endl;
cout << "size = " << size << endl;
}
ListArray::~ListArray()
{
delete [] p;
}
int main ()
{
int b;
ListArray test (2);
while (1)
{
cout << "输入n:";
cin >> b;
test.put (b);
test.print();
}
/*ListArray test (2);
test.put(1);
test.put(2);
test.print();
test.put(3);
test.print();
test.put(5);
test.print();*/
return 0;
}
4.设定义一个类:
class ListArray
{
private:
int size; //整型数组的大小,表示可放元素的个数
int elem; //整型数组当前的元素个数,初始应为0,当elem等于size时,数组满
int *p; //指向整型数组,动态分配内存空间
public:
ListArray(int s=100); //用s初始化整型数组的大小
void put(int n); //将n加入整型数组,elem自增1
void print(); //输出整型数组所有元素
~ListArray();
};
请完成该类成员函数的定义和测试程序的设计。
【提示】编写void put(int n)函数时,注意考虑数组满的情况。如果数组已满,则没有空间存储n,此时需动态申请新的内存空间,其大小应比原数组大小大1,并将原数组元素备份到新数组,新数组的最后一个元素存放n,当然不要忘记释放为原数组动态分配的内存空间。
using namespace std;
class ListArray{
private:
int size;
int elem;
int *p;
public:
ListArray (int s = 100);
void put (int n);
void print ();
~ListArray();
};
ListArray::ListArray (int s)
{
size = s;
elem = 0;
p = new int[s];
}
void ListArray::put (int n)
{
int *pn;
int i;
if (elem == size)
{
size++;
pn = new int [size];
for (i = 0; i < elem; i++)
{
pn[i] = p[i];
}
pn[elem++] = n;
delete[] p;
p = pn;
}
else
{
p[elem++] = n;
}
}
void ListArray::print ()
{
int j;
cout << "所有元素:";
for (j = 0; j < elem; j++)
{
cout << p[j] << " ";
}
cout << endl;
cout << "size = " << size << endl;
}
ListArray::~ListArray()
{
delete [] p;
}
int main ()
{
int b;
ListArray test (2);
while (1)
{
cout << "输入n:";
cin >> b;
test.put (b);
test.print();
}
/*ListArray test (2);
test.put(1);
test.put(2);
test.print();
test.put(3);
test.print();
test.put(5);
test.print();*/
return 0;
}
4.设定义一个类:
class ListArray
{
private:
int size; //整型数组的大小,表示可放元素的个数
int elem; //整型数组当前的元素个数,初始应为0,当elem等于size时,数组满
int *p; //指向整型数组,动态分配内存空间
public:
ListArray(int s=100); //用s初始化整型数组的大小
void put(int n); //将n加入整型数组,elem自增1
void print(); //输出整型数组所有元素
~ListArray();
};
请完成该类成员函数的定义和测试程序的设计。
【提示】编写void put(int n)函数时,注意考虑数组满的情况。如果数组已满,则没有空间存储n,此时需动态申请新的内存空间,其大小应比原数组大小大1,并将原数组元素备份到新数组,新数组的最后一个元素存放n,当然不要忘记释放为原数组动态分配的内存空间。
相关文章推荐
- 编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分
- 有一个整数数组,请编写一个函数,找出索引m和n,只要将m和n之间的元素排好序,整个数组就是有序的。注意:n-m应该越小越好,也就是说,找出符合条件的最短序列。 给定一个int数组A和数组的大小n,请
- 编写函数 void count(char a[],char w[][10],int n,int b[]).功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字符字符看作单词分割
- 编写函数void count(char a[],char w[][10],int n,int b[])。功能是:统计w指向的数组中的n个单词在a指向的字符串中各自出现的次数(将非字母字符看作单词分
- 编写一个函数,它从一个字符串中提取一个子字符串。函数原型如下: Int substr(char dst[], char src[],int start, int len) {} 目标是:从src数组
- 编写一个函数,返回一个int数组中存储的最大数值,并在一个简单的程序中测试这个函数。(有缺陷)
- 请编写一个函数jsValue(int m,int k,int xx[]),该函数的功能是:将大于整数m且紧靠m的k个素数存入数组xx传回
- 编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格,和其他字符的个数,在主函数中输入字符串以及输出上述统计的结果。再考虑将算的的结果放在一个数组中
- 编写一个函数 求出int数组中最大的数
- 编写函数int stat(int a[],int n,int c[][2])。a指向的数组中保存了由n个1位整数组成的数列(n为偶数)。函数从前至后依次将a数组中每两个相邻元素拼成一个不超过2位
- 程序员面试金典——解题总结: 9.17中等难题 17.6给定一个整数数组,编写一个函数,找出索引m和n,只要将m和n之间的元素排好序,整个数组就是有序的。注意:n - m越小越好,也就是说,找出
- 编写函数void count(char a[],char w[][10],int n,int b[])。
- 编写一个void sort(int*x,int n)实现将x数组中的n个数据从大到小排序。n及数组元素在主函数中输入。将结果显示在屏幕上并输出到文件
- 编写函数fun(int *a, int n, int *odd, int *even),功能是:求出数组a[]中所有奇数之和以及所有偶数之和。并利用指针odd返回奇数之和,利用指针even返回偶数
- 2、编写单元测试用例,对用户注册功能的DAO层进行测试。(注意:测试用例应考虑成功和失败的情况)
- 编写函数 void fun(int a[2][3],int b[3][2]),将数组 a 的内容转置存入数组 b 中?
- (2)int A[nSize],其中隐藏着若干0,其余非0整数,写一个函数int Func(int* A, int nSize),使A把0移至后面,非0整数移至数组前面并保持有序,返回值为原数据中第一个元素为0的下标。(尽可能不使用辅助空间且考虑效率及异常问题,注释规范且给出设计思路)
- 编写函数void change(char *a,char *b,char*c)。 函数功能是首先把b指向的字符串逆向存放,然后将a指向的字符串和b指向的字符串按排列顺序交叉合并到c指向的数组中,两个字
- 二、 编写函数int stat(int a[],int n,int c[][2])。a指向的数组中保存了由n个1位整数组成的数列(n为偶数)。函数从前至后依次将a数组中每两个相邻元素拼成一个不超过2位
- 编写函数int stat(int a[],int n,int c[][2]).a指向的数组中保存了由n个1位整数组成的数列(n为偶数)。