http://topic.csdn.net/u/20090603/13/05ea579e-ba1d-416f-9305-f0bf9aa56e53.html
http://kinghuimail.blog.163.com/blog/static/95820408201011139931364/
http://fishlife.iteye.com/blog/641330
1.用C++写个程序,如何判断一个操作系统是16位还是32位的?不能用sizeof()函数
}
2.在不用第三方参数的情况下,交换两个参数的值
#include <stdio.h>
void main()
{
int i=60;
int j=50;
i=i+j;
j=i-j;
i=i-j;
printf("i=%d\n",i);
printf("j=%d\n",j);
}
方法二:
i^=j;
j^=i;
i^=j;
方法三:
// 用加减实现,而且不会溢出
a = a+b-(b=a)
3.struct A
{
char t:4;
char k:4;
unsigned short i:8;
unsigned long m;
}
sizeof(A)=?(不考虑边界对齐)
这题各种答案都有,有的说是6,有的说是7,我自己在VC++6.0上跑了一下是8;
4.
union a {
int a_int1;
double a_double;
int a_int2;
};
typedef struct
{
a a1;
char y;
} b;
class c
{
double c_double;
b b1;
a a2;
};
输出cout<<sizeof(c)<<endl;的结果?
注意结构体的边界对齐。
答:
VC6环境下得出的结果是32
5
.程序改错
class mml
{
private:
static unsigned int x;
public:
mml(){ x++; }
mml(static unsigned int &) {x++;}
~mml{x--;}
pulic:
virtual mon() {} = 0;
static unsigned int mmc(){return x;}
......
};
class nnl:public mml
{
private:
static unsigned int y;
public:
nnl(){ x++; }
nnl(static unsigned int &) {x++;}
~nnl{x--;}
public:
virtual mon() {};
static unsigned int nnc(){return y;}
......
};
代码片断:
mml* pp = new nnl;
..........
delete pp;
A:
基类的析构函数应该为虚函数
virtual ~mml{x--;}
6
strncpy()和memcpy()的区别:
strncpy()字符串,memcpy()拷贝内存内容.
strncpy
原型:extern char *strncpy(char *dest, char *src, int n);
用法:#include <string.h>
功能:把src所指由NULL结束的字符串的前n个字节复制到dest所指的数组中。
说明:
如果src的前n个字节不含NULL字符,则结果不会以NULL字符结束。
如果src的长度小于n个字节,则以NULL填充dest直到复制完n个字节。
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指内存区域复制count个字节到dest所指内存区域。
说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。
7
#include <iostream>
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
Node* ReverseList(Node* head)
{
if (!head || !head->next)
{
return head;
}
Node* p1 = head;
Node* p2 = p1->next;
head->next = NULL;
while (p2)
{
p1 = p2;
p2 = p2->next;
p1->next = head;
head = p1;
}
return head;
}
Node* RecReverseList(Node* head) //递归方法
{
if (!head || !head->next)
{
return head;
}
Node *newhead = RecReverseList(head->next);
head->next->next = head;
head->next = NULL;
return newhead;
}
void main()
{
Node a, b, c;
a.data = 1, a.next = &b;
b.data = 2, b.next = &c;
c.data = 3, c.next = NULL;
Node* tmp = &a;
while(tmp)
{
cout<<tmp->data<<" ";
tmp = tmp->next;
}
cout<<endl;
tmp = RecReverseList(&a);
while(tmp)
{
cout<<tmp->data<<" ";
tmp = tmp->next;
}
cout<<endl;
}
1. 链表中的倒数第m个元素
给定一个单向链表,请设计一个既节省时间又节省空间的算法来找出该链表中的倒数第m个元素。实现这个算法。
“倒数第m个元素”是这样规定的:当m=0时,链表的最后一个元素(尾元素)将被返回。
前进m步后启动一个“后指针”
2. 空链表和循环链表
给定一个链表,它可能是一个以"NULL"结尾的非循环链表,也可能是一个循环结构结尾的循环链表。写一个函数来判断该链表是一个循环链表还是一个非循环 链表,该函数不得对链表本身做任何修改。
用两个指针,一快一慢,如果是循环链表,快的指针迟早会 超过慢指针,时间复杂度:O(n),最多也是3n .
int DetermineTermination(node *head)
{
node *fast, *slow;
fast = slow = head;
while(1){
if(!fast || !fast->next){
return 0;
}
else if(fast == slow || fast->next == slow){
return 1;
}
else {
slow = slow->next;
fast = fast->next->next;
}
}
}
二、其他
1. 字节的升序存储和降序存储方式
请编写一个函数还判断某计算机的字节存储顺序是升序(little-endian)还是降序(big-endian)。
“字节的存储顺序”指的是多字节数据的各个字节在计算机里的存储顺序。比如说用来表示整数的那4个字节的顺序,其包括两种,一是按从最低位字节 (LSB)到最高位字节(MSB)的顺序进行存储;另一种是从MSB到LSB的顺序进行存储。这里说的“高位字节”指的是字(word)中的高位字节。如 果字节表示的是字的低值部分,我们就说它是这个字的LSB。比如说,数值“5A6C”中的LSB就是"6C"。反过来说,“5A6C”中的MSB是 “5A”。
在一台采用字节降序存储方案的计算机里,MSB将被保存在最低位的地址里;在一台采用字节升序存储方案的计算机里,LSB将被保存在最低位的地址里。比如 说,采用字节降序存储方案的计算机把十六进制值“A45C”中的"A4"保存在第一个字节里,把"5C"保存在第二个字节里;而一台采用字节升序存储方案 的计算机将把“5C”保存在第一个字节里,把"A4"保存在第二个字节里。
具体到本题中,可以使用字符指针来获取某个字节中的内容。
/* Return 1 if the machine is little-endian, 0 if the
* machine is big-endian
*/
int Endianness(void)
{
int testNum;
char *ptr;
testNum = 1;
ptr = (char *)&testNum;
return (*ptr);
}
有一种更精妙的解法,利用了“union”类型:这种类型与 “struct”类似,
只是它的所有数据成员都是从内存中的同一个位置开始存储的。
这样使你能够把同样的数据当做不同的变量类型来使用。
/* Return 1 if the machine is little-endian, 0 if the
* machine is big-endian
*/
int Endianness(void)
{
union{
int theInteger;
char singleByte;
} endianTest;
endianTest.theInteger = 1;
return endianTest.singleByte;
}