问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程prog3,其中声明的MyString类是一个用于表示字符串的类。成员函数endsWith的功能是判断此字符串是否以指定的后缀结束,其参数s用于指定后缀字符串。如果参数S表示的字符串是MyString对象表示的字符串的后缀,则返回true;否则返回false。注意,如果参数s是空字符串或等于MyString对象表示的字符串,则结果为true。 例如,字符串“cde”是字符串“abcde”的后缀,而字符串“bde”不是字符串“abcde”的后缀。请编写成员函数endsWith。在main函数中给出了一组测试数据,此种情况下程序的输出应为: s1=abcde s2=cde s3=bde s4= s5=abcde s6=abcdef s1 endsWith s2:true s1 endsWith s3:false s1 endsWith s4:true s1 endsWith s5:true s1 endsWith s6:false 要求: 补充编制的内容写在“//********333********”与“//********666********”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中,输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 //Mystring.h #include <iostream> #include <string.h> using namespace std; class MyString { public: MyString(const char* s) { size = strlen(s); str = new char[size + 1]; strcpy(str, s); } ~MyString() { delete [] str;} bool endsWith(const char* s) const; private: char* str; int size; }; void writeToFile(const char * ); //main.cpp #include "MyString.h" bool MyString::endsWith (const char* s) const { //******** 333******** //******** 666******** } int main () { char s1[] = "abcde"; char s2[] = "cde"; char s3[] = "bde"; 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 endsWith s2 :" << str.endsWith(s2) << endl << "s1 endsWith s3 :" << str.endsWith(s3) << endl << "s1 endsWith s4 :" << str.endsWith(s4) << endl << "s1 endsWith s5 :" << str.endsWith(sS) << endl << "s1 endsWith s6 :" << str.endsWith(s6) << endl; writeToFile (""); return 0; }

【参考答案】

int s_size=strlen(s); //把字符串s的长度赋值给s_size for(int i=0; i<s_......

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

问答题
请使用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:Boek(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,否则返回falseint numOfPages()const{ return num_pages;} 返回书的页数int currentPage()const{ return cur_page;} 返回打开页面的页码void openAtPage(int page no){ 把书翻到指定页cout << endl;if(page_no <1 ||page_no >num_pages){cout << 无法翻到第 <<cur_page<< 页。 ;close();}else{cur_page = page_no;cout << 已把“ << title << ”翻到第 << 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 << 已把书合上。 ;}cout << endl;}};int main () {Book book ( C++语言程序设计 , 299);book.openAtPage (50);book.openAtNextPage ();book.close ();book.close ();book.openAtNextPage ();return 0;}
问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有类AutoMobile(“汽车”)及其派生类Car(“小轿车”)、Truck(“卡车”)的定义,以及主函数main的定义。请在横线处填写适当的代码,然后删除横线,以实现上述类定义。此程序的正确输出结果应为:车牌号:冀ABC1234 品牌:ForLand 类别:卡车 当前档位:0 最大载重量:12车牌号:冀ABC1234 品牌:ForLand 类别:卡车 当前档位:2 最大载重量:12车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:0 座位数:5车牌号:沪XYZ5678 品牌:QQ 类别:小轿车 当前档位:-1 座位数:5注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“ ****found****”。#include <iostream>#include <iomanip>#include <cmath>using namespace std;class AutoMobile{ 汽车 类char * brand; 汽车品牌char * number; 车牌号int speed; 档位:1、2、3、4、5,空档:0,倒档:-1public:AutoMobile(const char * the_brand, const char * the_number): speed(0){ ********** found**********______;strcpy(brand, the_brand);number = new char[strlen (the_number) +1]; ********** found**********______;}~AutoMobile () { delete[] brand; delete[] number; }const char * theBrand() const{ return brand; } 返回品牌名称const char * theNumber()const{ return number; } 返回车牌号int currentSpeed () const { return speed; } 返回当前档位void changeGearTo (int the_speed) { 换到指定档位if (speed>= -1 && speed<=5)speed=the_speed;}virtual const char * category () const =0; 类别:卡车、小轿车乖virtual void show()const{cout << 车牌号: << theNumber()<< 品牌: <<theBrand() ********** found**********<< 类别: <<<< 当前档位: << currentSpeed();}};class Car: public AutoMobile{ 小汽车 类int seats; 座位数public:Car (const char * the_brand, const char * the_number, int the_seats): AutoMobile(the_brand, the_number), seats(the_seats){}int numberOfSeat () const { return seats; } 返回座位数const char * category()const{ return 小轿车 ; } 返回汽车类别void show () const {AutoMobile::show ();cout << 座位数: << numberOfSeat () << endl;}};class Truck: public AutoMobile{ “卡车”类int max load; 最大载重量public:Truck (const char * the _brand, const char * the_number, int the_max_load): AutoMobile (the_brand, the_number), maxload (the_maxload) {}int maxLoad () const{ return max load; } 返回最大载重量const char * category()const{ return 卡车 ; } 返回汽车类别void show () const { 调用基类的show()函数 ********** found**********______cout << 最大载重量: << maxmoad () << endl;}};int main () {Truck truck ( ForLand , 冀ABC1234 , 12);truck.show ();truck.changeGearTo (2);truck.show ();Car car ( QQ , 沪XYZ5678 , 5);car.show ();car.changeGearTo (-1);car.show ();cout << endl;return 0;}