本程序提示用户输入一系列整型数字(没有长度限制),然后排序,然后输出。
程序自定义了一个类,用于存储用户输入的数字,类中用数组存储数字,数组会动态调整大小。
#include "stdafx.h"
#include
#include
using namespace std;
const int new_cell=100;
class iArray
{
public:
int * p;
int count;
int max_count;
int new_count;
iArray()
{
p = new int[new_cell];
count = 0;
new_count = 1;
max_count = new_count * new_cell;
}
~iArray()
{
delete p;
}
void new_again()
{
int * tmp;
tmp = p;
new_count++;
p = new int[new_cell * new_count];
max_count += new_cell;
for (int i=0;ip[i]=tmp[i];
delete tmp;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
iArray my_array;
int value;
cout << "Please input integer, if enter -1, end."<do {
cin >> value;
if (value == -1)
break;
my_array.p[my_array.count++] = value;
if (my_array.count >= my_array.max_count)
my_array.new_again();
} while (1);
for (int i=0; i< my_array.count; i++)
for (int j =0; j{
if (my_array.p[j] < my_array.p[j+1])
{
int b = my_array.p[j];
my_array.p[j]=my_array.p[j+1];
my_array.p[j+1]=b;
}
}
for (int i =0; i< my_array.count; i++)
{
if (i%8 == 0)
cout << endl;
cout << setw(5)<};
return 0;
}