首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数
admin
2017-07-04
37
问题
请使用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已经编译为obi文件。
//Integers.h
#ifndef INTEGERS
#define INTEGERS
#include
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class Integers{
int e lem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;
//用于记录集合中元素个数的计数器
public:
Integers():counter(0){)
//创建一个空集合
Integers(int data[],int size);
//利用数组提供的数据创建一个整数集合
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
ta
);
}
void Integers::add(int element){
if(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,28,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://kaotiyun.com/show/PLAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列程序中的重载函数disp()发生错误,错误原因是【 】。#include<iostream,h>classSample{priva
以下有关类与结构体的叙述不正确的是
存取指针所指的值时,采用的运算符是
如使用setw()的操纵符对数据进行格式输出时,应包含( )。
如果定义了一个类myclass,则为myclass类对象以成员函数形式重载后++运算符的声明应该为( )。
有如下数组声明:intvalue[30];下标值引用错误的是()。
在关系数据库中,用来表示实体之间联系的是【 】。
C++语言中的基本符号可以分为_______、_________、________三类。
下列运算符中,在C++语言中不能重载的是
以下选项中合法的用户标识符是
随机试题
男婴,1岁,出生时体重3kg,单纯母乳喂养,10个月断乳后不愿进食,渐消瘦,大便呈不消化状,每天2次~3次。体重6.5kg,身长73cm。腹壁皮下脂肪0.3cm对其进行急救处理应选择()
发生食物中毒后,通常采取的紧急救治措施有
根据电泳的分离特点和工作方式,电泳法可分为
急性链球菌感染后肾小球肾炎电镜下的典型表现是
教师反思旧思想,并积极寻找新思想与新策略来解决所面临的问题,属于教学反思的()阶段。
要实现人与自然的和谐发展,下列措施最合理的是()。
目前,国家税务总局为了加强增值税一般纳税人开具普通发票的管理,正在试点推行“一机多票”。请谈谈你对推行“一机多票”的认识和看法。
Wheremostlikelydoesthewomanwork?
A、Thebrandimage.B、Themarketingstrategy.C、Thesalesplan.D、Thecompanyculture.A信息明示题。女士询问男士如何提升品牌形象,男士说他刚刚与Susan讨论了关于品牌形
A、Cookthemilkautomatically.B、Sendthemilkbacktothesupermarket.C、Movethemilktothefrontoftheshelf.D、Orderfresh
最新回复
(
0
)