首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数
admin
2018-03-13
25
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删除指定的元素(如果集合中存在该元素),成员函数sort的作用是将集合中的整数按升序进行排序。请编写这个sort函数。此程序的正确输出结果应为:
5 28 2 4 5 3 2 75 27 66 31
5 28 2 4 5 3 2 75 27 66 31 6
5 28 2 4 5 3 2 75 27 66 31 6 19
5 28 4 5 3 2 75 27 66 31 6 19
5 28 4 5 3 2 75 27 66 31 6 19 4
2 3 4 4 5 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“//*********333*********”与“//*********666*********”之间。不得修改程序的其他部分。
注意:相关文件包括:main.cpp、Integers.h。
程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件。
//Integers.h
#ifndef INTEGERS
#define INTEGERS
#include
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class Integers{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;
//用于记录集合中元素个数的计数器
public:
Integers():counter(O){)
//创建一个空集合
Integers(int data[],intSize);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
void remove(int element);
//删除集合中指定的元素
int getCount()const{return counter;)
//返回集合中元素的个数
int getElement(int i)const{return elem
;}
//返回集合中指定的元素
void sort();
//将集合中的整数按由小到大的次序进行排序
void show()const;
//显示集合中的全部元素
};
void writeToFile(const char*path);
#endif
//main.cpp
#include"Integers.h"
#include
Integers::Integers(int data
[],int size):counter(0){
for(int i=0;i
add(data
);
}
void Integers:: add (int
element){
i f(counter
elem[counter ++]
=element;
}
void Integers::remove(int element){
int j;
for(j=counter-1;j>=0;J--)
if(elem[j]==element)
break;
for(int i=j;i
elem
=elem[i+1];
counter --;
}
void Integers::sort(){
//*********333*********
//*********666*********
}
void Integers::show()const{
for(int i=0;i
cout<
cout<
}
int main(){
int d[]={5,2 8,2,4,5,3,2,75,27,66,31);
Integers S(d,11);
s.show();
s.add(6); s.show();
s.add(19); s.show();
s.Eemove(2); s.show();
s.add(4); s.show();
s.sort;(); s.show();
writeToFile("");
return 0;
}
选项
答案
for(int i=0;i
i;j--) //从最后一位到i到前一位遍历elem if(elem[i]>elem[j]) //如果elem[i]大于elem[j],则两值替换 { int temp=elem[i]; //定义整形变量temp并赋值为elem[i]; elem[i]=elem[j]; //给elem[i]赋值elem[i] elem[j]=temp; //给elem[j]赋值temp }
解析
主要考查考生对排序算法的掌握,要排序的数组为elem,元素个数为counter,在这里使用下标i和j进行比较,当elem
>elem[j]时,数组元素通过中间变量temp进行交换。
转载请注明原文地址:https://jikaoti.com/ti/iLt0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
若调用一个函数,且此函数中没有return语句,则正确的说法是该函数
若有语句int*point,a=4;和point=&a;下面均代表地址的一组选项是
数据库DB、数据库系统DBS、数据库管理系统DBMS之间的关系是()。
数据独立性分为逻辑独立性与物理独立性。当数据的存储结构改变时,其逻辑结构可以不变,所以,基于逻辑结构的应用程序不必修改,称为______。
执行语句序列intn:cin>>n:switch(n){case1:case2:cout<<’1’;case3:case4:cout<<’2’:break;
结构化程序设计的主要特点是
在下面程序的横线处填上适当的内容,使程序执行后的输出结果为1/2005。#include<iostream.h>usingnamespacestd:classDatepubl
以下函数模板max()的功能是:返回数组a中最大元素的值。请将横线处缺失部分补充完整。template<typenameT>Tmax(Ta[],intn){Tm=a[0];for(in
______允许用户为类定义一种模式,使得类中的某些数据成员及某些成员函数的返回值能取任意类型。
随机试题
在分配稀有卫生资源时,不应该坚持的是
甲与乙有仇,某日甲远远看见乙手持匕首朝自己走来,心想先下手为强,于是随手拾起一根本棒冲上去猛击乙头部,致其重伤,后来发现乙拿的是一把玩具匕首。甲的行为属于:()
钎焊时( )。
甲公司2007年期初发行在外的普通股为60000万股;4月30日新发行普通股32400万股;12月1日回购普通股14400万股,以备将来奖励职工之用。该公司当年度实现净利润为24120万元。则甲公司2007年度基本每股收益为()元。
宏观调控手段系统是由税收杠杆、信贷杠杆、外汇杠杆、价格杠杆组成的。()
个体倾向于利用自己身体或内部参照作为信息加工依照的认知风格称为()。
作为一名建筑师,莱伊恩并不是最出色的。但作为一个人,他无疑非常伟大。他始终恪守自己的原则,给高贵的心灵一个美丽的住所,哪怕是遭遇到最大的阻力,也要想办法抵达胜利彼岸。下列表述错误的是()。
材料一:本世纪初开始,我国广大社会成员公共需求的全面、快速增长同公共产品的短缺、基本公共服务不到位的问题成为日益突出的阶段性矛盾。第一,城乡基本公共服务的过大差距形成城乡协调发展的巨大压力。第二,地区间基本公共服务的过大差距形成区域协调发展的巨大压力。第三
Recordset对象属性中,用来移动到下一条记录的是()。
将考生文件夹下FVP文件夹中的文件ZUO.ARJ复制到同一文件夹下,并命名为BEER2.ARJ。
最新回复
(
0
)