题目
阅读以下说明和C++码,将相应的文字填入(n)处,写在对应栏内。
[说明]
以下代码实现了对象引用作为函数参数,仔细阅读以下代码,分析运行结果,填入(n)处。
[代码]
include<iostream.h>
class Sample
{
int x,y;
public:
Sample() {x=y=0; }
Sample (int i, int j ) {x=i; y=j; }
void copy (Sample &s ).,
void setxy (int i, int j ) {x=i; y=j; }
void print ({cout<<"x="<<x<<end1 ; cout <<"y="<<y<<end1;
};
void Sample: copy (Sample &s )
{
X=S.X;
y=s.y;
}
void func (Sample s1, Sample &s2 )
{
s1.setxy (10,20 );
s2.setxy (30,40 );
}
void main ()
{
Sample p (1,2 ) ,q;
q.copy (p );
time (p,q );
p.print ();
q.print ();
}
运行结果
(1)
(2)
(3)
(4)
第1题
阅读以下说明和c++码,将应填入(n)处的字名写在的对应栏内。
[说明] 以下函数完成求表达式
的值,请填空使之完成此功能。
float sum (float x )
{ float s=0.0;
int sign = 1;
(1);
for(inti=1;(2); i+ +)
{
t=t*x;
s=s+(3);
sign = - sign;
(4);
}
第2题
阅读以下说明和C++码,将应填入(n)处的字名写在对应栏内。
从下列的3道试题(试题五至试题七)中任选1道解答。
如果解答的试题数超过1道,则题号小的1道解答有效。
[说明] 编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a: xxk1. dat”中。
(1)
include <fstream. h >
include < stdlib. h >
void main () {
(2)
if (! four) {
cerr < <“文件没有找开!” < <end1;
exit (1);
}
int x;
cin > >x;
while((3)){
(4)
cin> >x;
}
(5)
}
第3题
阅读以下说明和c++码,将应填入(n)处的字名写在对应栏内。
[说明] 从键盘输入一个字符ch,输出该字符在文本文件input.txt 的每一行中出现的次数。(必须调用函数鳋统计ch的出现次数,函数ff (str,ch)的功能是统计并返回字符ch在字符串str 中出现的次数。)。
例如:如果文件input. txt 中存放了下列数据:
every
121
I am a student
运行程序,并输入e后,输出:
2
0
1
int ff(char * str, char ch)
{ int count =0;
while ((1)) {
if(*str= =ch) count++;
str ++;
}
return count;
}
include < stdio. h >
include < stdlib. h >
void main()
{ char ch, c, s [80];
int k;
FILE *fp;
if((2)){
printf(“打不开文件!n”); return;
}
ch = getchar();
k=0;
while(! feof(fp) ) {
c = fgete(fp);
if (3)
s[k++ ] =c;
else {
s[k]= ";
printf ("%dn" ,ff(s, ch) );
k=0;
}
}
(4)
printf("% dn", ff(s, ch ) );
}
第4题
阅读以下说明和C++码,填入(n)处。
[说明]
建立一个分数类,使之具有下述功能:建立构造函数,它能防止分母为0,当分数不是最简形式时进行约分以及避免分母为负数。
[C++代码]
include<iostream.h>
include<math.h>
class Num
{
public:
Num (int a,int b);
private:
int num1;
int num2;
}:
Num:: Num (int a,int b)
{
if((1) )
{
cout<<"ERROR"<<endl;
return;
}
int min=fabs(a)<fabs (b)?fabs (a): fabs (b);
int x=1;
for (int i=1;i<=min;i++)
if((2) )
x=i;
a/=X;
b/=x;
if((3) )
{
a=-a;
b=-b;
}
(4)
(5)
}
第5题
() 阅读下列说明和C++代码,将应填入空(n)处的字句写在答题纸的对应栏内。【说明】 某中学开展中外中学生野外生存夏令营活动,由于中外学生的语言障碍,随队为外籍学员配置一名翻译。以下代码采用适配器(Adapter)模式模拟翻译适配器。其类图如下:
第6题
阅读以下说明和 C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。 【说明】 在股票交易中,股票代理根据客户发出的股票操作指示进行股票的买卖操作。其类图如图5-1所示,相应的c++代码附后。图5-1 类图
【C++代码】 include <iostream> include <string> include <vector> using namespace std; class Stock { private: string name; int quantity; public: Stock(string name ,int quantity) { this->name= name;this->quantity = quantity; } void buy() { cout<<" [买进]股票名称: "<< name << ",数量: "<< quantity << endl;} void sell() { cout<<" [卖出]股票名称: " << name << ",数量:"<< quantity <<endl; } }; class Order { public: virtual void execute() = 0; }; classBuyStock: (1) { private: Stock* stock; public: BuyStock(Stock* stock) { (2) = stock; } void execute() { stock->buy () ; } }; //类SellStock的实现与BuyStock类似,此处略 c1ass Broker { private: vector < Order*> orderList; pub1ic: void takeOrder((3) order) { orderList.push_back(order);} void p1aceOrders() { for (inti=O; i<orderList.size(); i++) { (4) -> execute () ; } orderList.c1ear(); } }; c1ass StockCommand { pub1ic: void main () { Stock* aStock = new Stock("股票 A" ,10); Stock* bStock = new Stock("股票 B" ,20); Order* buyStockOrder = new BuyStock(aStock); Order* se11StockOrder = new Se11Stock(bStock); Broker* broker = new Broker(); broker->takeOrder(buyStockOrder); broker->takeOrder(se11StockOrder); broker-> (5) () ; } }; int main() { StockCommand* stockCommand = new StockCommand(); stockCommand->main(); de1ete stockCommand; }
第7题
阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写在对应栏内。
【说明】
阅读下面几段C++程序回答相应问题。
比较下面两段程序的优缺点。
①for (i=0; i<N; i++ )
{
if (condition)
//DoSomething
…
else
//DoOtherthing
…
}
②if (condition) {
for (i =0; i<N; i++ )
//DoSomething
}else {
for (i=0; i <N; i++ )
//DoOtherthing
…
}
第8题
阅读以下说明,以及用C++在开发过程中所编写的程序代码,将应填入(n)处的字句写在对应栏内。
【说明】
在下面函数横线处填上适当的字句,使其输出结果为:
构造函数.
构造函数.
1,2
5,6
析构函数
析构函数.
【C++代码】
include "iostream.h"
class AA
{ public;
AA(int i,int j)
{A=i; B=j;
cout<<"构造函数.\n";
}
~AA(){(1);}
void print();
private:
int A, B;
};
void AA∷print()
{cout<<A<<","<<B<<endl;}
void main()
{
AA *a1, *a2;
(2)=new AA(1, 2);
a2=new AA(5, 6);
(3);
a2->print();
(4) a1;
(5) a2;
}
第9题
阅读以下说明和C++代码,将应填入(n)处。
[说明]
在一公文处理系统中,开发者定义了一个公文类OfficeDoc,其中定义了公文具有的属性和处理公文的相应方法。当公文的内容或状态发生变化时,关注此OfficeDoc类对象的相应的DocExplorer对象都要更新其自身的状态。一个OfficeDoc对象能够关联一组 DocExplorer对象。当OfficeDoc对象的内容或状态发生变化时,所有与之相关联的 DocExplorcr对象都将得到通知,这种应用被称为观察者模式。以下代码写在一个C++源文件中,能够正确编译通过。
[C++代码]
include <iostream>
const OBS_MAXNUM=20;//最多与OfficeDoc对象相关联的DocExplorer对象的个数
(1);
class DocExplorer{ //关注OfficeDoc公文对象的类
public:
DocExplorer((2) *doc); //构造函数
(3) void update(OfficeDoc *doc)=0;//更新自身状态的函数
//其他相关属性和方法省略
};
class OfficeDoc{ //公文类
private:
DocExplorer *myObs[OBS_MAXNUM];
//关注此公文类的DocExplorer类对象指针数组
int index; //与OfficeDoc对象关联的DocExplorer对象的个数
public:
OfficeDoe()
index=0;
}
void attach(DocExplorer *o){
//将一DoeExplorer对象与OfficeDoe对象相关联
if(index >=OBS_MAXNUM ||o=NULL)return;
for(int loop=0; loop<index; loop++)
if(myObs[loop]==o) return;
myObs[index]=o;
index++;
}
void detaeh(DocExplorer *o){
//接触某DoeExplorer对象与OfficeDoc对象的关联
if(0==NULL) return;
for(int loop=0; loop<index; loop++){
if(myObs[loop]==o){
if(loop<=index-2)myObs[loop]=myObs[index-1];
myObs[index-1]=NULL;
index--;
break;
}
}
}
private:
void notifyObs(){ //通知所有的DocExplorer对象更改自身状态
for(int loop=0; loop<index; loop++){
myObs[loop]->(4); //DocExplorer对象更新自身状态
}
}
//其他公文类的相关属性和方法
};
DocExplorer::DocExplorer(OfficeDoc *doc){//DocExplorer 类对象的构造函数
doc->(5); //将此DocExplorer对象与doc对象相关联
}
第10题
阅读以下说明和C++代码,将应填入(n)处的字句写在对应栏内。
【说明】
C++标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为std。vector模板类的部分方法说明如下表所示:
【C++代码】
include <iostream>
include <vector>
using namespace (1);
typedef vector< (2) > INTVECTOR;
const int ARRAY_SIZE = 6;
void ShowVector (INTVECTOR &theVector);
int main() {
INTVECTOR theVector;
// 初始化 theVector, 将theVector的元素依次设置为0至5
for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++}
theVector.push_back((3));
ShowVector(theVector); // 依次输出theVector中的元素
theVector.erase (theVector.begin () + 3};
ShowVector(theVector);
}
void ShowVector (INTVECTOR &theVector) {
if (theVector.empty ()) {
cout << "theVector is empty." << endl; return;
}
INTVECTOR::iterator (4);
for (theIterator=theVector.begin(); theIterator !=theVector.end(); theIterator++) {
cout << *theIterator;
if (theIterator != theVector.end()-1) cout << ", ";
}
cout << end1;
}
该程序运行后的输出结果为:
0,1,2,3,4,5
(5)
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!