请编写一个函数int compare(char *s,char *t)), 该函数的功能是对两个字符串进行比较。当s所指字符串和t所指字符串相等时,返回值为0;当s所指字符串大于是t指字符串时,返回值大于0;当s所指字符串小于t所指字符串时,返回值小于0。
注意:部分源程序已存在文件PROC8.cpp中。
文件PROC8.cpp的内容如下:
//PROC8.cpp
include <iostream>
include <string>
using namespace std;
int compare(char *s,char *t)
{
//* * * * * * * * *
}
int main ()
{
char str1[100],str2[100];
int result;
cout<<"Input the first string\n";
cin>>str1;
cout<<"Input the second string\n";
cin>>str2;
result=compare(str1,str2);
if (result==0)
cout<<"string1=string2 ! \n";
else if (result>0)
cout<<"string1>string2 ! \n";
else
cout<<"string1<string2 ! \n";
return 0;
}
第3题
指出下面程序段中的错误的是 ______。
class X{//A
int a,b;
public:
X(int aa,int bb) {a=aa; b=bb;}//B
}:
X x(2,3);//C
X y(4); //D
第4题
下面程序的预设功能是:将文本文件readme 中的内容读出,显示在屏幕上。
include <fstream>
using namespace std;
int main ()
{
char buf[ 80 ];
ifstream file( "readme");
while (_______________)
{
file.getline( buf, 80 );
cout<<buf<<end1;
}
me. close();
}
则程序中空白处应填入的语句为【 】。
第5题
有如下程序:
include <iostream>
using namespaee std;
class ONE{
public:
virtual void f( ){cout<<"1";}
};
class TWO:public ONE{
public:
TWO( )1 cout<<"2";}
{;
class THREE:public TWO{
public:
virtual void f( )}TWO::f( );cout<<"3";}
};
int main( ){
ONE aa,*P;
TWO bb;
THREE cc;
P=&cc;
p->f( );
return 0;
}
程序的输出结果是______。
第6题
n(n≥2)位整数,函数求出s的n位数之和作为函数的返回值。
例如,s值为4315,则函数返回13。s值为13函数,则返回4。
注意:部分源程序已存在文件PROC15.cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数fun()的花括号中填写若干语句。
文件PROC15.cpp的内容如下:
//PROC15.cpp
include<iostream>
using namespace std;
unsigned short fun(unsigned short s);
int main()
{
unsigned short a;
cout<<"Enter a unsigned short integer number:";
cin>>a;
if(a<10)
cout<<"Data error!";
else
cout<<"The result: "<<fun(a)<<end1;
return 0;
}
unsigned short fun(unsigned short s)
{
//* * * * * *
}
第8题
问题,请改正程序中的错误,使该程序的输出结果为:
5
5
源程序文件test28_1.cpp清单如下:
include <iostream.h>
class A{
public:
/***************** found *****************
static int n=1;
A(){n++;};
~A() (n--;};
};
/***************** found *****************
int n=0;
int main() {
A a;
A b[3];
A *c=new A;
c=&a;
/***************** found *****************
cout<<c.n<<end1;
cout<<A::n<<end1;
return 0;
}
第9题
有以下面程序:
include <iostream>
using namespace std;
long fib(int n)
{
if (n>2)
return (fib(n-1)+fib(n-2));
else
return 2;
}
int main()
{
cout<<fib(3)<<endl;
return 0;
}
则该程序的输出结果应该是【 】。
第10题
operator=(const TestClass& rhs)
{
if(this==&rhs)return *this;
value=rhs.value;
return *this;
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!