现在的位置: 首页 > 综合 > 正文

c++试题(16)

2013年10月10日 ⁄ 综合 ⁄ 共 4071字 ⁄ 字号 评论关闭

编写一个函数,作用是把一个char组成的字符串循环右移n个。
比如原来是     abcdefghi
如果n=2,移位后应该是    hiabcdefg 
函数头是这样的: 
void LoopMove(char * pStr,int steps)
{
//请填充...
//pStr是指向以'/0'结尾的字符串的指针,
//steps是要求移动的n。
}

****************************************************************************
#i nclude<iostream>
using namespace std;

void LoopMove(char* pstr, int steps)
{
   char ch;
   int len = strlen(pstr);//求传入数组的长度
   steps% = len; //考虑steps大于字符串长度的情况
   len = len - 1;//求数组最后一个下标的值
  
   for(int i=1; i <= steps; i++)//循环steps次
   {
      ch = pstr[len];//把数组最后一个元素赋给ch
     
      for(int k=len; k>=1;k--)
      {
         pstr[k] = pstr[k-1];//从最后一个元素开始把前一个数组元素赋给自己
      }
      pstr[0] = ch;//最后一元素到了第一个元素的位置
   }
 
}
  
main()
{
   char yes[] = "abcdefghi";
    LoopMove(yes, 12);//
   cout<<yes<<endl;
   system("pause");
}

将字符串逆序:
*  Reverse strings  */
/*  li58 2005-10-27  */

#i nclude <stdio.h>

main()
{
  int len,i;
  char ch;
  char str[100];
  clrscr();
  printf("input the string:");
  gets(str);
  len = strlen(str);
  for (i=0; i<len/2; i++)
  {
    ch = str[i];
    str[i] = str[len-i-1];
    str[len-i-1] = ch;
  }
  puts(str);
  getch();
}

*********************************************************************
1) Please write the code required to count the number of bits set in a byte.?The function should have the
prototype: int Count(unsigned char Byte); 

2) Please write the code to implement the following C function:
void ReverseString(char* pString);
The argument should be a null terminated string which should be
reversed.i.e. if Hello/0 is passed in it comes out as olleH/0.
*****************************
void reverseString(char* pString)
{
   int i, len;
   char temp;
   for (int i = 0; i < len / 2; i++)
   {
      temp = pString[i];
      pString[i] = pString[len - i - 1];
      pString[len - i - 1] = temp;
   }
}
void ReverseString(char* pString)
{
int i,j;
char temp;

j = strlen(pString) - 1;
for(i = 0;i < j;i++,j--)
{
temp = pString[i];
pString[i] = pString[j];
pString[j] = pString[i];
}
}

3) Given a singly forward linked list i.e.
A->B->C->D
describe the algorithm that could efficiently reverse the order
of the list:
D->C->B->A

****************************************
1。请实现一个类型,该类型只能在栈上分配,不能从堆上分配
2。请实现一个类型,该类型只能在堆上分配,不能从栈上分配
1。重载一个private的operator new

说明:new操作符变为私有,不能这样构造对象了:
CMyClass* pmyObj = new CMyClass(); // 堆上
只能这样:
CMyClass myObj;// 栈上

2。将ctor作为private,写一个public的static的CreateObject方法,在其中用new
创建object。

说明:ctor变为私有,不能这样构造对象了
CMyClass myObj;// 栈上
只能这样:
CMyClass* pmyObj = new CMyClass(); // 堆上

******************************************
1。在C++中有没有纯虚构造函数?
2。在c++的一个类中声明一个static成员变量有没有用?
3。在C++的一个类中声明一个静态成员函数有没有用?
4。如何实现一个非阻塞的socket?
5。setsockopt, ioctl都可以对socket的属性进行设置,他们有什么不同?
6。解释一下进程和线程的区别?
7。解释一下多播(组播)和广播的含义?
8。多播采用的协议是什么?
9。在c++中纯虚析构函数的作用是什么?请举例说明。
10。编程,请实现一个c语言中类似atoi的函数功能(输入可能包含非数字和空格)

1:没有,但有虚构造函数
2:有用,简单的说,你可以在你的类声明一个static int i;用i来记录在整个程序中你创建这
个类对象的个数!
3:有。
比如:effive C++上的一个例子
class Month {
public:
static const Month Jan() { return 1; }
static const Month Feb() { return 2; }
...
static const Month Dec() { return 12; }
int asInt() const // 为了方便,使Month
{ return monthNumber; } // 可以被转换为int
private:
4 异步选择,
6:一个程序,进程只用一个,线程可以有多个。
7.多播是一对多点的通信,数据报交付到一组计算机中的每一个.而广播就是整个局域网中的每一个计算机了.
多播是一种将报文发往多个接收者的通信方式。在许多应用中,它比广播更好,因为多播降低了不参与通信的主机的负担。简单的主机成员报告协议( IGMP )是多播的基本模块。
  广播通常局限在单个局域网中,对目前许多使用广播的应用来说,可采用多播来替代广播

9:用在基类,可以防止派生类对象被正确调用其析构函数
class Base{
virtual ~Base()
}

class Derind:public Base()
{
}
10:这个就非常简单了
首先,如果字符串中存在字符或者空格,就返回0,
剩下的也就非常简单了!以下代码经过测试
需要包含#i nclude "math.h"
int myatoi(const char *p)
{
// assert(p);
 int iRe(0);
 const char *pT = p;
 while (*pT == '0')// 首字符为零
 {
  ++pT;
 }
 int iNum = strlen(pT);
 while (*pT != '/0')
 {
  if(47 < *pT && *p < 59) // asil48是0,57是9
  {
   int iTemp = *pT - 48;
   iTemp = iTemp*(pow(10,--iNum));
   iRe += iTemp;
  }
  else
  {
   iRe = 0;
   break;
  }
  ++pT;
 }
 return iRe;
}

**************************************************************
new动态分配失败会抛出什么异常,C++中提供了那两个标准函数来设定异常处理HANLDER?
会抛出 std::bad_alloc,

函数是
#i nclude <new>
typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();
***********************************************************
不用任何局部和全局变量实现int strlen(char *a)

int strlen(char *a) {
    if('/0' == *a)
        return 0;
    else
        return 1 + strlen(a + 1);
}
*****************************
该题目的大概意思是求一个递增排列的数列中相同数字的最大长度,并返回。
比如{1,1,1,1,2,2,2,2,2,2,3,4}返回6,因为有6个2;
    {2,2,3,5,5,5,6,8,8,8,8,8}返回5,因为有5个8。
计算函数已经给出来一部分,补全函数的空白处
int Max_length(int x[],int n)
{
   int length=1;
   for(int i=1;i<n;i++)
     {
       if( 空 )//if (x[i] == x[i - length])就在这儿
{
            length++;
}
      }
     return length;
}

 

抱歉!评论已关闭.