首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成
请打开考生文件夹下的解决方案文件proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成
admin
2018-07-06
62
问题
请打开考生文件夹下的解决方案文件proj3,其中包含了类IntegerSet和主函数main的定义。一个IntegerSet对象就是一个整数的集合,其中包含0个或多个无重复的整数;为了便于进行集合操作,这些整数按升序存放在成员数组elem的前若干单元中。成员函数add的作用是将一个元素添加到集合中(如果集合中不存在该元素),成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。请编写成员函数remove。在main函数中给出了一组测试数据,此时程序的正确输出结果应为:
2 3 4 5 27 28 31 66 75
2 3 4 5 6 27 28 31 66 75
2 3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
3 4 5 6 19 27 28 31 66 75
要求:
补充编制的内容写在“//********333********”与“//********666********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//IntegorSet.h
#ifndef INTEGERSET
#define INTEGERSET
#include<iostream>
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数
class IntegerSet{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;
//用于记录集合中元素个数的计数器
public:
IntegerSet( ):counter(0){}
//创建一个空集合
IntegerSet(intdata[ ],intsize);
//利用数组提供的数据创建一个整数集合
void add(int element);
//添加一个元素到集合中
VOid remove(int element);
//删除集合中指定的元素
int getCount( )const{return counter;)
//返回集合中元素的个数
int getElement(int i)const{return elem
;}//返回集合中指定的元素
void show( )const;
};
void WriteToFile(char*);
#endif
//main.cpp
#include"IntegerSet.h"
#include<iomanip>
IntegerSet::IntegerSet(intdata[ ],int size):counter(0){
for(int i=0j;i<size;i++)
add(data
);
}
void IntegerSet::add(int element){
int j;
//从后往前寻找第一个小于等于element的元素
for(J=counter;j>0;j--)
if(element>=elem[j-1])
break;
//如果找到的是等于element的元素,说明要添加的元素已经存在,直接返回
if(j>0)
if(element==elem[j-1])
return;
//如果找到的是小于element的元素,j就是要添加的位置
//该元素及其后面的元素依次后移,腾出插入位置
for(int k=counter;k>J;k--)
elem[k]=elem[k-1];
elem[j]=element;
//将element插入到该位置
counter++;//计数器加1
}
void IntegerSet::remove(intelement){
//********333********
//********333********
}
void IntegerSet::show( )const{
for(int i=0;i<getCount( );i++)
cout<<setw(4)<<getElement(i);
cout<<endl;
}
int main( ){
int d[ ]={5,28,2,4,5,3,2,75,27,66,31};
IntegerSet s(d,11);s.show( );
s.add(6);s.show( );
s.add(19);s.show( );
s.remove(2);s.show( );
s.add(4);s.show( );
WriteToFile(" ");
return0;
}
选项
答案
for(int i=0;i<counterj;1++)//遍历整个集合(数组elem) if(element==elem[i])//如果element等于elem[i] { for(int j=i;j<counter-i;j++)//从i开始遍历集合elem elem[j]=elem[j+1];//把elem[j+1]赋值给elem[j] counter--;//elem长度自减1 return;//返回 }
解析
主要考查考生对有序数组的掌握,题目要求成员函数remove从集合中删除指定的元素(如果集合中存在该元素)。遍历数组elem中的元素,找出与形参element相等的元素,并将其删除,每删除一个元素,即将该元素之后的每个元素前移一位,如果不存在与形参element相等的元素则没有操作。使用下标i遍历数组,if语句判断是否与element相等。
转载请注明原文地址:https://kaotiyun.com/show/HAAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列关于类和对象的叙述中,错误的是()。
下列程序的输出的结果是()。#include<iostream.h>voidmain(){inti,k,a[10],p[3];k=5;for(i=0;i<9;i++)a[i]=i;
有如下程序:#include<iostream>usingnamespacestd;classBase{public:voidfun(){cout<<"Base::fun"<<e
有如下类定义:classAA{inta;public:intgetRef()const{return&a;}//①intgetValue(
有如下程序:#include<iostream>usingnamespacestd;intmain(){int*p;*p=9;cout<<"Thev
在下面程序的横线处填上适当的内容,使程序执行后的输出结果为ABCD。#include<iostream.h>usingnamespacestd;classA{public:A(){cout<<‘A’;}};
如下程序声明了一个电话号码类PhoneNumber,重载了流插入运算符<<,以便于电话号码的输出。请将程序补充完整。#include<iostream>#include<iomanip>#include<string>using
如果要把返回值为void的函数A声明为类B的友元函数,则应在类B的定义中加入的语句是______。
设有下定义的语句:inta[3][2]={10,20,30,40,50,60};intb[3][2]={10,20,30,40,50,60};则a[1][1]*b[2][1)的结果为()。
随机试题
感染性发热在发热病因中占
其性黏滞的邪气是
为了空调系统节能,应对空调房间内的()进行合理选定。
下列关于会计人员岗位的表述符合规定的有()。
企业因员工工资的集中发放而进行筹资的动机属于()。
1978年我国居民总消费额约为()亿元。
请举例说明跨国并购的优势。并说明我国进行跨国并购的发展趋势。[河北大学2015国际商务硕士]
[*]
有如下程序#include<stdio.h>main(){inti;for(i=0;i<3;i++)putchar(’A’+i*2);}程序运行后的输出结果是()。
AstheTitanicwassinkingandwomenandchildrenclimbedintolifeboats,themusiciansfromtheship’sbandstoodandplayed.
最新回复
(
0
)