C++程序员面试指南第6章

**面试题1:编码实现数字转换为字符串
编码实现函数itoa(),设计一个程序,把一个数字转换为字符串存储到一个缓冲区。例如数字:
5486321,转化成字符串:“5486321”。
答案:用数字100做做测试
#include<stdio.h>
#include<math.h>
#include<string.h>


int myItoa(int num,char *p,int n)
{
if(p == NULL)  //校验指针的有效性
{
return -1;
}
if(num < 0)  //判断数值是否为负值
{
*p++ = '-';
num = 0 - num; //将负值转换为正值
}
int temp = 0;
*p = 0;
if(n == 16)   //按十六进制转换
{
for(int i = 0; i < 8; i++)
{
temp = int( num / pow(16,7-i));  //pow()是计算平方的16^(7-i)
if(temp != 0)
{
*p = 1;
}
if(*p != 0)
{
if(temp >= 0 && temp <= 9)
{
*p++ = temp + '0';
}
else if(temp >=10 && temp <= 15)
{
*p++ = temp - 10 + 'A';
}
num = num % int(pow(16,7-i));
}
}
}
else if(n == 10)   //按十进制转换
{
for(int i = 0; i < 10; i++)
{
temp = int(num / pow(10,9 - i));
if(temp != 0)
{
*p = 1;
}
if(*p != 0)
{
*p++ = temp + '0';
num = num %int( pow(10,9 - i));

}
}
else if(n == 2)  //按二进制转换
{
for(int i = 0; i < 32; i++)
{
temp = int( num / pow(2,31 - i));
if(temp != 0)
{
*p = 1;
}
if (*p != 0)
{
*p++ = temp + '0';
num = num %int(pow(2,31 - i));
}
}
}
else if(n == 8)   //按八进制转换
{
for(int i = 0; i < 16; i++)
{
temp = int( num / pow(8, 15 - i));
if(temp != 0)
{
*p = 1;
}
if (*p != 0)
{
*p++ = temp + '0';
num = num %int(pow(8, 15 - i));
}
}
}
*p += '\0';
return 0;
}


void main(void)
{
int num = 100;
char str[15];
myItoa(num, str, 2);
printf("The number 'num' is %d and the binary string  'str' is %s \n",num, str);


myItoa(num, str, 8);
printf("The number 'num' is %d and the string octal  'str' is %s. \n",num, str);


myItoa(num, str, 10);
printf("The number 'num' is %d and the string algorism  'str' is %s. \n",num, str);


myItoa(num, str, 16);
printf("The number 'num' is %d and the string hex  'str' is %s. \n",num, str);
}


运行结果:(有乱码)
The number 'num' is 100 and the binary string  'str' is 1100100烫烫烫烫蘢
The number 'num' is 100 and the string octal  'str' is 1440100烫烫烫烫蘢.
The number 'num' is 100 and the string algorism  'str' is 1000100烫烫烫烫蘢.
The number 'num' is 100 and the string hex  'str' is 6400100烫烫烫烫蘢.


**面试题2:编码实现字符串转换为数字  编码实现函数atoi(),设计一个程序,把一个字符串转换
为一个整型数值。例如数字“5486321”,转换成字符:5486321.
答案:
#include <stdio.h>
#include <math.h>


int myAtoi(const char *str)
{
int num = 0; //保存转换后的数值
int isNegative = 0; //记录字符串中是否有负号
int n = 0;
char *p = (char*)str;
if (p == NULL)
{
return -1;
}
while(*p++ != '\0')  //计算数字字符串长度
{
n++;
}
p = (char*)str;
if(p[0] == '-')
{
isNegative = 1;
}


char temp = '0';
for(int i = 0; i < n; i++)
{
char temp = *p++;
if(temp > '9' || temp < '0')  //滤除非数字字符
{
continue;
}
if(num != 0 || temp != '0')  //滤除字符串开始的0字符
{
temp -= 0x30; //将数字字符转换为数值
num += temp * int(pow(10, n - 1 -i));
}
}
if(isNegative)  //如果字符串中有负号
{
return (0 - num);
}
else
{
return num;  //返回转换后的数值
}
}


void main(void)
{
char * str = "-012450";
int num = 0;
num = myAtoi(str);
printf(" The number 'num' is %d and the string 'str' is %s. \n",num, str);
return ;
}




运行结果:
 The number 'num' is -12450 and the string 'str' is -012450.


面试题3:编写一个标准strcpy函数    试编码实现标准库函数strcpy函数


# include <iostream.h>
# include <assert.h>
char *strcpy(char *str1, const char *str2)
{
assert((str1 != NULL) && (str2 != NULL));  //判断指针的合法性
char *address = str1;                      //记录目标指针所指向的地址
while((*str1++ = *str2++) != '\0');        //拷贝直到str2结束   
return address;                //返回目标地址
}


void main(void)
{
char str1[13] = "12345";
char str2[13] = "Hello world!";
char *str = str1;
str = strcpy(str1,str2);
cout<<"str :"<<str<<endl;
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
return ;
}


运行结果:
str :Hello world!
str1:Hello world!
str2:Hello world!


面试题4:简述strcpy、sprintf、memcpy的区别  函数strcpy、sprintf和memcpy都可以完成从
“源字符串”到“目的字符串”的拷贝功能,简述这三个函数的异同。
答案:三者主要有以下不同之处:
(1)操作对象不同,strcpy的两个操作对象均为字符串,sprintf的操作源对象可以是多种数据
类型,目的操作对象是字符串,memcpy的两个对象就是两个任意可操作的内存地址,并不限于何
种数据类型。
(2)执行效率不同,memcpy最高,strcpy次之,sprintf的效率最低。
(3)实现功能不同,strcpy主要实现字符串变量间的拷贝,sprintf主要实现其他数据类型格式
到字符串的转化,memcpy主要是内存块间的拷贝。


面试题5:找出程序的错误之处 指出以下三段程序中的错误,并改正
void test1()
{
char string[10];
char *str = "0123456789";
strcpy(string, str1);
}
void test2()
{
char string[10], str1[10];
int i;
for(i = 0; i < 10; i++)
{
str1[i] = 'a';
}
strcpy(string, str1);
}
void test3(*str1)
{
char string[10];
if(strlen(str1) <= 10)
{
strcpy(string, str1);
}
}
答案:(1)test1中,数组str1越界,应将:
char string[10];
改为:
char string[11];
(2)test2中,strcpy不会结束,直到内存错误,应改为:
void test2()
{
char string[11], str1[11];
int i;
for(i = 0; i < 10; i++)
{
str1[i] = 'a';
}
str1[i] = '\0';
strcpy(string, str1);
cout<<string<<endl;
}
(3)test3中,str1数组下标越界。应将:
strlen(str1) <= 10;
改为:
strlen(str1) < 10;
测试:
# include <iostream.h>
# include <string.h>


void test1()
{
char string[10];
char *str1 = "0123456789";
strcpy(string, str1);
cout<<string<<endl;
}
void test2()
{
char string[11], str1[11];
int i;
for(i = 0; i < 10; i++)
{
str1[i] = 'a';
}
str1[i] = '\0';
strcpy(string, str1);
cout<<string<<endl;
}
void test3(char *str1)
{
char string[10];
if(strlen(str1) < 10)
{
strcpy(string, str1);
}
cout<<string<<endl;
}


void main(void)
{
test1();
test2();
char *str1 = "012345678";
test3(str1);
return ;
}


运行结果:
0123456789
aaaaaaaaaa
012345678


面试题6:判断程序会出现什么问题 判断以下程序有什么问题,运行后有什么结果?
# include <stdio.h>
# include <string.h>


void main(void)
{
char s[] = "123456789";
char d[] = "123";
strcpy(d, s);
printf("d: %s\ns: %s\n",d,s);
}
答案:在进行字符串拷贝时导致数组d越界,输出结果如下:
d: 123456789
s: 56789

猜你喜欢

转载自blog.csdn.net/juluwangriyue/article/details/53862716