问答题

请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中声明了MyString类。MyString是一个用于表示字符串的类。成员函数startsWith的功能是判断此字符串是否以指定的前缀开始,其参数s用于指定前缀字符串。如果参数s表示的字符串是MyString对象表示的字符串的前缀,则返回true;否则返回false。注意,如果参数s是空字符串或等于MyString对象表示的字符串,则结果为true。
例如:字符串"abc"是字符串"abcde"的前缀,而字符串"abd"不是字符串"abcde"的前缀。请编写成员函数startsWith。在main函数中给出了一组测试数据,此情况下程序的输出应该是:
s1=abcde
s2=abc
s3=abd
s4=
s5=abcde
s6=abcdef
s1 startsWith s2:true
s1 startsWith s3 false
s1 startsWith s4 true
s1 startsWith s5 f true
s1 startsWith s6 false
要求:补充编制的内容写在//********333********//********666********两行之间,不得修改程序的其他部分。
注意:程序最后已经将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//源程序
#include"MyString.h"
bool MyString::startsWith(const char*s)const
//********333********
//********666********

int main()
char s1[]="abcde";
char s2[]="abc";
char s3[]="abd";
char s4[]=" ";
char s5[]="abcde";
char s6[]="abcdef";
MyString str(s1);
cout<<"s1="<<s1<<endl<<"s2="<<s2<<endl<<"s3="<<s3<<endl
<<"s4="<<s4<<endl<<"s5="<<s5<<endl<<"s6="<<s6<<endl:
cout<<boolalpha<<"s1 startsWith s2:"<<str.startsWith(s2)<<endl
<<"s1 startsWith s3:"<<str.startsWith(s3)<<endl
<<"s1 startsWith s4:"<<str.startsWith(s4)<<endl
<<"s1 startsWith s5:"<<str.startsWith(s5)<<endl
<<"s1 startsWith s6:"<<str.startsWith(s6)<<endl;
//writeToFile("K:\\b10\\61000101\\");
return 0:

【参考答案】

//********333********
if(s==NULL) return true;
els......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用“答题”菜单或使用VC6打开考生文件夹proj1下的工程proj1,此工程包含程序文件 main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个 ERROR************found************下的语句行有错,请加以改正。改正后程序的输出应该是: 书名:C++语言程序设计 总页数:299 已把 C++语言程序设计 翻页到第50页 已把“C++语言程序设计 翻页到第51页 已把书合上。 书是合上的。 已把 C++语言程序设计 翻页到第1页 注意:只能修改每个 ERROR************found************下的那一行,不要改动程序中的其他内容。 源程序 #include<iostream> using namespace std; class Book char*title; int num_pages; 页数 int cur_page; 当前打开页面的页码,0表示书未打开 public: Book(const char*theTitle,int pages):num_pages(pages) ERROR************found************ title=new char[strlen(theTitle)]; strcpy(title,theTitle); cout<<endl<< 书名: <<title<< 总页数: <<num_pages; ~Book()delete[]title; ERROR************found************ bool isOpen() const return num_pages!=0; 书打开时返回true,否则返回false int numOfPages() const return num_pages; 返回书的页数 int currentPage() const return cur_page; 返回打开页面的页码 void openAtPage(int page_no) 把书翻到指定页 cout<<endl; if(page_no<1 1|| page_no>num_pages) cout<< 无法翻到第 <<cur_page<< 页。 ; close(); else cur_page=page_no; cout<< 已把 <<ticle<< 翻到第 <<cur_page<< 页 ; void openAtPrevPage()openAtPage(cur_page-1); 把书翻到上一页 void openAtNextPage()openAtPage(cur_page+1); 把书翻到下一页 void close() 把书合上 cout<<endl; if(! isOpen())cout<< 书是合上的。 ; else ERROR************found************ num_pages=0; cout<< 已把书合上。 ; couf<<endl; ; int main() Book book( C++语言程序设计 ,299); book.openAtPage(50); book.openAtNextPage(); book.close(); book.close(); book.openAtNextPage(); return 0: