软通动力笔试(带参考答案)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
答案仅供参考
一、改错题
1.找出以下两处逻辑错误(非编程规范)并改正
char* getErrorString(int errcode)
{
char errorString[]= ”not exist";
char errorString2[]= "not available";
ﻩswitch(errcode){
case 1:
return errorString;
case2:
returnerrorString2;
default:
return NULL;
ﻩ}
}
voidprintErrorString(interrcode)
{
ﻩchar*errorString = getErrorString(errcode);
ﻩprintf("errcode:%d, errorString:%s\n",errcode,errorString); }
case1与case2返回了两个局部变量指针,而局部变量在函数结束后将自动释放。
改正:将字符数组改为指针
char *errorString="not exist”;
char *errorString2 = "notavailable";
2. 以下就是输出“wele home"得程序,找出逻辑错误(非编程规范)并改正
const int MAX_STR_SIZE =12;
intmain(int argc, char*argv[])
{
char str[MAX_STR_SIZE];
strcpy(str,”welehome”);
ﻩprintf("%s",str);
return 0;
}
“wele home”字符串应该就是13个字节,而str字符数组只能存储12个字符改正:const int MAX_STR_SIZE =13;
二、程序填空题
1. 写出String类得构造函数,析构函数与赋值函数
class String
{
public:
String(const char*str = NULL);
ﻩString(const String &other);
~String();
ﻩString& operator=(constString &other);
private:
char*m_data;
};
String::~String()
{
(1)delete [] m_data;
}
String::String(const char *str)
{
if( (2)str==NULL ) {
ﻩﻩm_data =(3)new char;
*m_data =0;
}
else{
ﻩint length = (4)strlen(str);
ﻩm_data = new char[length+1];
strcpy(m_data, str);
ﻩ}
}
String& String::operator=(constString &other)
{
ﻩif( (5) this == other) {
ﻩreturn (6)*this;
}
ﻩ(7) delete []m_data;
int length = strlen( (8)other );
m_data = newchar[length+1];
ﻩ(9)strcpy(m_data,other、m_data)
ﻩreturn(10)*this;
}
2.一个计算到指定年份有多少个“黑色星期五”(当月得13号并且就是星期五)得程序,具体忘了.
三、编程题
1.实现以下函数,把给定string中得‘[’与‘]’替换成‘(’与‘)'
std::string replace(std::string&str)
{
for(int i= 0; i 〈 str、size();i++)
{
if(str[i]==’[’)
{
str[i]='(’;
}
if(str[i]==’]’)
{
str[i]=')';
}
}
return str;
}
2.实现函数,使数字num按B进制输出
int main(int argc, char *argv[])
{
long num = 134;
int B = 8;
func(num, B);
return0;
}
例如134按2进制输出10000110,按8进制输出206,按十六进制输出86
void func(long num,int B)
{
int a[100] ={0};
int count=0;
while(num>0)
{
a[count++]=num%B; //保存余数
num=num/B;
}
for(int i = count — 1;i >=0; i—-)//逆序输出{
if(a[i]〉=10)//十六进制要特殊处理
{
printf(”%c",’A’+a[i]-10);
}
else
{
printf("%d”,a[i]);
}
}
printf(”\n”);
}