# CC++面试题（一）

xiaoxiao2021-02-28  5

#### 1.求下面函数的返回值(微软) ———统计1的个数

int func(int x) { int countx = 0; while(x) { countx++; x = x&(x-1); } return countx; } 假定x = 9999 10011100001111 答案: 8

１：将最低的为1的位变成0，这个解释理解起来不难。

#### 2.判断一个数(x)是否是2的n次方

int func(int x) { if( (x&(x-1)) == 0 ) { return 1; } else { return 0; } } int main() { int x = 8; printf("%d\n", func(x)); return 0; }

##### 字符串逆序
char * reverse(char *str) { if (str == NULL ) { return NULL; } char *end = str; while (*end) { end++; } end--; char *ptr = (char*)malloc(sizeof(char)*(strlen(str)+1)); if (ptr == NULL) { printf("malloc err!\n"); exit(1); } char *begin = ptr; while(str <= end) { *ptr++ = *end--; } *ptr = '\0'; printf("%s\n",begin); return begin; }
##### 3.字符串逆序（不借助额外分配的内存）
void swap(char *ch1,char *ch2) { char tmp = *ch1; *ch1 = *ch2; *ch2 =tmp; } char* Reverse1(char* s) { // p指向字符串头部 char* begin = s; // q指向字符串尾部 char* end = s; while (*end) ++end; end--; #if 0 // 交换并移动指针，直到beginend交叉 while(begin < end) { char tmp = *begin ; *begin++ = *end; *end-- = tmp ; //swap(begin,end); 用指针操作的时候用char *参数 //begin++; //end--; } #else //不借助临时变量 这种只适合于纯字符的字符串（没有空格符号等） while(begin < end) { *end = *end ^ *begin; *begin = *end ^ *begin; *end = *end-- ^ *begin++; } #endif return s; }

//递归调用 void swap(char &ch1,char &ch2) { char tmp = ch1; ch1 = ch2; ch2 =tmp; } void reverse(char s[], int left, int right) { if (left >= right) { return; } char ch = s[left]; s[left] = s[right]; s[right] = ch; //swap(s[left],s[right]); 用元素的值操作的时候可以用引用 reverse(s, left + 1, right - 1); } //调用方法：reverse(s,0,strlen(s)-1); //非递归调用 void reverse(char s[], int left, int right) { while (left <= right) { char ch; ch = s[right]; s[right--] = s[left]; s[left++] = ch; } }

#### 4按单词逆序

void reverse_word(char *begin, char *end) { while (begin <= end) { char ch = *begin; *begin++ = *end; *end-- = ch; } } char *reverse_sentence(char *s) { char *begin = s; char *end = s; while (*end != '\0') { if (*end == ' ') { reverse_word(begin, end - 1); end++; begin = end; } else { end++; } } reverse_word(begin, end - 1);//对最后一个单词逆序 reverse_word(s, end - 1);//将整个字符串逆序 return s; }
##### 5.编写一个类string 的构造，析构和赋值函数
class string() { public: string(c6nst char *str= NULL); string(const string &other); string& operator=(const string &other); ~string(void); private: char * m_data; } //有参构造 string::string(const char *str) { if( str == NULL) { //对于NULL字符串只需要一个‘\0’标志 m_data = new char[1]; if(m_data ==NULL) { cout<<"new err!\n"<<endl; exit(1); } m_data[0]='\0'; } else { int length = strlen(str); m_data = new char[length +1]; if(m_data == NULL) { cout<<"new char err!"<<endl; exit(1); } strcpy(m_data,str); } } //拷贝构造 string ::string(const string &other) { cout<<"copy !"<<endl; m_data = new char[strlen(other.m_data) + 1]; if(m_data == NULL) { cout<<"new char err!"<<endl; exit(1); } strcpy(m_data,other.m_data); } //重载= string::string & operator=(const string &other) { //输入参数const 型 if(this == &other)//如果对象与other是同一个对象，this 指向对象的地址&other 也是指向对象的地址 { return *this;//直接返回本身 } delete[] m_data;// 释放原来的内存资源 int length = strlen(other.m_data); m_data = new char[length +1]; if(m_data == NULL) { cout<<"new char err!"<<endl; exit(1); } strcpy(m_data,other.m_data); return *this;//返回对象的引用 } //析构 string::~string() { //如果m_data不为空，则释放内存 if(m_data != NULL) { delete [] m_data; m_data = NULL; } } int main() { string a("hello");//调用有参构造 string b(a);//调用拷贝构造 strng c("world"); c=b;//调用赋值 = string d; system("pause"); return 0; }

#### 6.改错题

void foo(int age,char *b) { b= (char *)malloc(64); sprintf(b,"your age is %d",age); } int main() { char*f; foo(24,f); printf("%s\n",f); return 0; } /*修改为：------------------------------*/ void foo(int age,char **b) { *b= (char **)malloc(64); sprintf(b,"your age is %d",age); } int main() { char *f; foo(24,&f); printf("%s\n",f); return 0; }
##### 7.多态实现,举例分析
class shape { public: virtual double area() = 0; }; class circle:public shape { public: circle(double r) { this->r =r; } virtual double area() { return r*r*3.14; } private: double r; }; class triangle:public shape { public: triangle(double x,double y) { this->x = x; this->y =y; } virtual double area() { return x*y/2; } private: double x,y; }; class rectangle:public shape { public: rectangle(double x,double y) { this->x = x; this->y = y; } virtual double area() { return x*y; } private: double x,y; }; int main() { circle c(0.5); rectangle r(2.5,3.8); triangle t(2.4,4.2); shape *base; base = &c; cout<<base->area()<<endl; base= &r; cout<<base->area()<<endl; base = &c; cout<<base->area()<<endl; system("pause"); return 0; }