首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义的MyString类是一个用于表示字符串的类。假设字符串由英文单词组成,单词与单词之间使用一个空格作为分隔符。成员函数wordCount的功能是计算组成字符串的单词的个数。
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义的MyString类是一个用于表示字符串的类。假设字符串由英文单词组成,单词与单词之间使用一个空格作为分隔符。成员函数wordCount的功能是计算组成字符串的单词的个数。
admin
2015-06-27
30
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中定义的MyString类是一个用于表示字符串的类。假设字符串由英文单词组成,单词与单词之间使用一个空格作为分隔符。成员函数wordCount的功能是计算组成字符串的单词的个数。
例如,字符串“dog”由1个单词组成;字符串“thequickbrownfoxjumpsoverthelazydog”由9个单词组成。请编写成员函数wordCount。在main函数中给出了一组测试数据,此时程序应显示:
读取输入文件…
STR1=1
STR2=9
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数WriteToFile已经编译为obj文件,并且在本程序中调用。
//mystring.h
#include
#include
usingnamespacestd;
classMyString{
public:
MyString(constchar*s)
{
str=newchar[strlen(S)+1];
strcpy(str,S);
};
—MyString(){delete[]str;)
intwordCount()const;
private:
char*str;
};
voidwriteToFile(char。,int);
//main.cpp
#include
#include”mystring.h”
intMyString::wordCount()const
{
//********333********
//********666********
}
intmain()
{
charinname[128],pathname[80];
strcpy(pathname,"");
sprintf(inname,"in.dat",pathname);
cout<<"读取输入文件…\n\n";
ifstreaminfile(inname);
if(infile.fail()){
cerr<<"打开输入文件失败!";
exit(1);
}
charbuf[4096];
infile.getline(buf,4096);
MyStringstr1("dog"),str2("thequickbrownfoxjumpsoverthelazydog"),str3(buf);
str1.wordCount();
cout<<"STR1="<
cout<<"STR2="<
writeToFile(pathname,str3.wordCount());
return0;
}
选项
答案
if (str == NULL) return 0; int counter = 1; int length = strlen(str); for (int i = 0; i < length; i++) if (isspace(str[i])) counter++; return counter;
解析
主要考查考生对动态数组的掌握情况,计算单词个数通过计算空格数目来完成。
转载请注明原文地址:https://jikaoti.com/ti/RnE0FFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
假定MyClass为—类,执行MyClassa[3],*p[2];语句时会自动调用该类构造函数()次。
下列说法中错误的是()。
Staff类含有血型数据成员ID,两个Staff对象相等是指它们的ID相同。下面的函数重载了运算符“==”,它用来判断两个Staff对象是否相等,相等时返回true,否则返回false。请将空格处缺失部分补充完整。booStaffi::S
在下列概念中,不属于面向对象方法的是______。
operator是运算符重载时必须使用的关键字,它和被重载的运算符连在一起,作为运算符函数的专用函数名,务必把该函数说明为【】的。
汇编程序的功能是将汇编语言所编写的源程序翻译成由【】组成的目标程序。
关于C++主函数特性,下列叙述正确的是()。
下列叙述中,不属于数据库系统的是()。
关于在调用模板函数时实参的使用,下列表述中正确的是
算法的时间复杂度是指
随机试题
亚里士多德文艺理论的代表作是
A.中性粒细胞B.嗜酸性粒细胞C.嗜碱性粒细胞D.血小板E.红细胞有助于维持血管壁完整性的是
根据我国有关法律、法规和司法解释,如果保险合同的当事人不具有行为能力,则此保险合同属于()。
赵凯1966年1月参加工作,其所在公司于1996年1月参加社会养老保险,个人账户积累额为12000元,过渡性养老金发放标准为每工作一年每月发放10元。2006年,2007年当地政府核定的社会月平均工资是1000元。如果赵凯在2008年1月退休,计发月数为1
只要项目运营期前若干年的净现金流量相等,就可以使用简化公式计算项目的静态投资回收期。( )
()的主要目的是激励提高各个利益主体参加培训的积极性。
以“忠诚、为民、公正、廉洁”为主要内容的人民警察核心价值观,鲜明地体现了人民警察的:
行政法规应当在公布后的三十日内报全国人民代表大会备案。()
要求为当前表建立一个唯一索引,索引表达式为姓名,索引名为xm。则下列各语句中正确的是______。
Sidney:Wouldyouliketohavesomeice-cream?Martha:Thankyou,______,becauseI’monadiettoloseweight.
最新回复
(
0
)