首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删
请打开考生文件夹下的解决方案文件proj3,其中包含了类Integers和主函数main的定义。一个Integers对象就是一个整数的集合,其中包含0个或多个可重复的整数。成员函数add的作用是将一个元素添加到集合中,成员函数remove的作用是从集合中删
admin
2020-06-29
41
问题
请打开考生文件夹下的解决方案文件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<iostream>
using namespace std;
const int MAXELEMENTS=100;
//集合最多可拥有的元素个数class Integers{
int elem[MAXELEMENTS];
//用于存放集合元素的数组
int counter;
//用于记录集合中元素个数的计数器
public:
Integers( ):counter(0){}
//创建一个空集合
Integers(int data[ ],int size);
//利用数组提供的数据创建一个整数集合
VOid add(int element);
//添加一个元素到集合中
void remove(int element);
//删除集合中指定的元素
int getCount( )const{returncounter;}
//返回集合中元素的个数
int getElement(int i)const{return elem
;}
//返回集合中指定的元素
void sort( );
//将集合中的整数按由小到大的次序进行排序
void show( )const;
//显示集合中的全部元素
};
void writeTOFile(const char*path);
#endif
//main.cpp
#include"Integers.h"
#include<iomanip>
Integers::Integers(int data
[ ],int size):counter(0){for(inti=0;i<size;i++)
add(data
);
}
void Integers::add(int element){
if(counter<MAXELEMENTS)
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<counter-1;i++)
elem
=elem[i+1];
counter--;
}
void Integers::sort( ){
//********333********
//********666********
}
void Integers::show( )const{
for(int i=0;i<getCount( );i++)
cout;<<seLw(4)<<getElemerit;(i);
tout;<<endl;
}
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.remove(2);s.show( );
s.add(4);s.show( );
s.sort( );s.show( );
writeToFile(" ");
return0;
}
选项
答案
for(inti=0;1<counter;i++)//遍历数组elem for(int j=counter-1;j>i;j-)//从最后一位到i到前一位遍历elem if(elem[i]>elem[j]) //如果elem[i]大于elem[j],则两值替换 { in ttemp=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/yMA0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
以下程序的输出结果是()。#includeusingnamespacestd;voidfun(char**q){++q;cout
使用VC6打开考生文件夹下的源程序文件modi2.cpp。阅读下列函数说明和代码,补充空出的代码。函数convert(char*des,char*str)的功能是去掉字符串str中相同的字母,并将处理后的结果存到des所指的字符串中。例如:输入:T
下列选项中,正确的C++表达式是
数据库设计中反映用户对数据要求的模式是( )。
某二叉树共有7个结点,其中叶子结点只有1个,则该二叉树的深度为(假设根结点在第1层)( )。
结构化程序所要求的基本结构不包括( )。
某二叉树有5个度为2的结点,则该二叉树中的叶子结点数是( )。
某二叉树共有845个结点,其中叶子结点有45个,则度为1的结点数为
某二叉树的中序序列为BDCA,后序序列为DCBA,则前序序列为
随机试题
"Doyoulikebeingmarried?""Yes,butI’mnotused______everynight."
流感病毒的成分中变异性强的是
首选用于治疗小儿风疹邪毒内盛证的方剂是
以下不属于风邪致病特点的是
证券公司从事上市公司并购重组财务顾问业务应当具备的条件包括()。Ⅰ.公司净资本符合中国证监会的规定Ⅱ.财务顾问主办人不少于3人Ⅲ.建立健全的尽职调查制度,具备良好的项目风险评估和内核机制Ⅳ.公司控股股东、实际控制人信誉良好且最近3年无重大
(2011年)下列税务处理中,符合个人独资企业和合伙企业征税规定的有()。
—HowmuchisthetickettoCentralPark?—Aone-wayticket________$40,andyoucan________another$20foraround-trip.
小学三年级学生还不能够根据行为的原因或从行为的因果关系上做出判断。()
巴拿马运河丑闻
In1971researchershopingtopredictearthquakesintheshorttermbyidentifyingprecursoryphenomena(thosethatoccurafew
最新回复
(
0
)