看来自己的编程基本功还差的很远,简单的插入排序和线性查找都调了很久才调处正确的结果,得加强这方面的训练。嗯,今天下午看到的一道题目,自己改了改。题目源自于谭浩强的《C++程序设计实践指导》的第一题。以后慢慢会改出更完善的程序。加油。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
题目要求:任意一个数组,如array[] = {1,8,2,3,2,2,4,2,3,8,8,3,7,2,9,3,1,4,5,6,6,7,8,8,8,9,10,10},试建立一个
类ARR,完成删除数组中相同的数,并统计出数组中不同的数字以及他们出现的次数。
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include<iostream>
using namespace std;
const int SIZE = 50;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class ARR
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
private:
int size; //the size of the array
int a[SIZE]; //the array
public:
ARR(int *x, int s) //x: the form parameter of the array; s:the size of the array to be passed-in.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
size = s;
for (int i = 0; i < size; i ++)
a[i] = x[i];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Print() //function to print the array.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for (int i = 0; i < size; i ++)
cout << a[i] << " ";
cout << endl;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void DelSame(void); //delete the same elements of the array.
void DelElement(int); //delete a[k] from the array.
void Sort(); //sort the array in increase order.
void EleNumber(); //compute the repeat times of each element.
};
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void ARR::DelElement(int k)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
for (int i = k; i < size - 1; i ++)
a[i] = a[i + 1];
size = size -1;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void ARR::DelSame()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
int pindex, findex; //two pivot index which point to two adjacent elements of the array
for (pindex = 0; pindex < size -1; )
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
findex = pindex + 1;
if (a[pindex] != a[findex])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
pindex ++;
continue;
}
else
DelElement(pindex);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void ARR::Sort() //inserting-sort.
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
int t;
if(a[0] > a[1])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
t = a[0];
a[0] = a[1];
a[1] = t;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int j = 2; j < size; j ++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int temp = a[j];
for(int i = 0; i <= j - 1; i ++)
if(temp >= a[i] && temp < a[i+1])
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(int k = j - 1; k >= i; k --)
a[k + 1] = a[k];
a[i + 1] = temp;
break;
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void ARR::EleNumber() //cout the different elements and their number
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
struct EleNum
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int element;
int number;
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EleNum Ele[SIZE];
int s;
int EleSize;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Ele[0].element = a[0];
Ele[0].number = 1;
EleSize = 1;
for (int i = 1; i < size; i ++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
s = a[i];
bool find = 0;
for (int j = 0; j < EleSize; j ++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if (s == Ele[j].element)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Ele[j].number ++;
find = 1;
break;
}
}
if (find == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
EleSize ++;
Ele[EleSize - 1].element = s;
Ele[EleSize - 1].number = 1;
}
}
for (int j = 0; j < EleSize; j ++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cout << Ele[j].element << " ";
}
cout << endl;
for (int j = 0; j < EleSize; j ++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cout << Ele[j].number << " ";
}
cout << endl;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void main()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) int array[] = {1,8,2,3,2,2,4,2,3,8,8,3,7,2,9,3,1,4,5,6,6,7,8,8,8,9,10,10}; //the input array
int asize = sizeof(array)/sizeof(int); //the size of the input array
ARR a1(array, asize);
a1.Sort();
a1.Print();
a1.EleNumber();
//a1.DelSame();
//a1.Print();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif) /**//*
程序的改进之处:
1、可以利用标准模板库STL中的vector类代替数组,完成程序的重写。
2、对于程序中的查找排序等算法考虑更新,用效率更高的算法代替。
3、可以考虑用链表重写此程序。
*/
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
发表于
2008-12-08 21:45
Lox
阅读( 241)
评论()
编辑
收藏
举报
|
|
|