分类: C/C++
2009-06-27 13:35:46
普天C++笔试题解答
1.实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数。
编写了一个完整的可运行程序,其中del_node和insert两个函数分别为题目中要求的两个函数,
其余部分是用来测试这两个函数的.
#include
#include
#include
struct tag_Node {
struct tag_Node *prev;
struct tag_Node *next;
int value;
};
typedef struct tag_Node Node;
typedef Node DList;
DList *head = NULL;
DList *old_head = NULL; /* 保存头结点的指针 */
Node *append_node(int n)
{
Node *p = (Node *)malloc(sizeof(Node));
if (!p) {
printf("memory alloc failure!\n");
exit(1);
}
p->value = n;
if (!head) {
/* now q contain the address of head */
old_head = head = p;
head->prev = 0;
head->next = 0;
}
head->next = p;
p->prev = head;
p->next = NULL;
head = p;
return p;
}
/* delete a node from double linked list */
void del_node(Node *p)
{
p->prev->next = p->next;
p->next->prev = p->prev;
printf("Node %d has been deleted!\n", p->value);
free(p);
}
/* insert a new node after Node p */
Node* insert(Node *p, int n)
{
Node *new_node = (Node*)malloc(sizeof(Node));
if (!new_node) {
printf("memory alloc failure!\n");
exit(1);
}
new_node->value = n;
new_node->next = p->next;
p->next->prev = new_node;
new_node->prev = p;
p->next = new_node;
printf("Node %d has been inserted\n", new_node->value);
return new_node;
}
void print()
{
Node *p = old_head;
printf("( ");
while (p)
{
printf("%d, ", p->value);
p = p->next;
}
printf(")\n");
}
void free_all()
{
Node *p = old_head;
Node *q;
while (p)
{
q = p;
p = p->next;
free(q);
}
printf("memory has been freed!\n");
}
int main()
{
Node *p, *q;
append_node(3);
append_node(4);
p = append_node(5);
append_node(0);
q = append_node(1);
append_node(2);
print(old_head);
insert(p, 6);
print(old_head);
del_node(q);
print(old_head);
free_all();
return 0;
}
2.写一个函数,将其中的\t都转换成4个空格。
/* 此程序将源程序文件中所有的'\t'转换成4个空格 ,并另存为expanded.c*/
#include
#include
int expand_tab()
{
FILE *fp, *fp2;
int c;
char *r_name = "expanded.c";
if ((fp = fopen(__FILE__, "r")) == NULL)
{
perror("file open error");
exit(1);
}
if ((fp2 = fopen(r_name, "w")) == NULL)
{
perror("output file open error");
exit(2);
}
while ((c = fgetc(fp)) != EOF)
{
if (c == '\t')
{
/* '\t' is replaced with 4 spaces */
fputs(" ", fp2);
continue;
}
fputc(c, fp2);
}
fclose(fp);
fclose(fp2);
return 0;
}
int main()
{
int rval = expand_tab();
if (!rval)
printf("expand successfully!\n");
else if (rval == 1)
printf("error occured in %s!\n", __FILE__);
else if (rval == 2)
printf("error occured in tempfile\n");
else
printf("unknown error!\n");
return 0;
}
3.Windows程序的入口是哪里?写出Windows消息机制的流程。
入口点是WinMain函数.
4.如何定义和实现一个类的成员函数为回调函数?
将成员函数定义为static
5.C++里面是不是所有的动作都是main()引起的?如果不是,请举例。
全局对象和全局对象的构造函数是在main函数之前执行的。
6.C++里面如何声明const void f(void)函数为C程序中的库函数?
使用extern "C"连接指令, extern "C" const void f(void);
7.下列哪两个是等同的
int b;
A const int* a = &b;
B const* int a = &b;
C const int* const a = &b;
D int const* const a = &b;
C和D是等同的,将b的地址赋给一个指向const int对象的常指针.
A的=号左边为一个指向const int对象的指针. B的=号左边为一个指向int型对象的常指针.
8.内联函数在编译时是否做参数类型检查?
会做类型检查的,只不过内联函数是在调用点处展开代码,省去了函数调用时候的开销,
但是也可能会带来代码体积变大的问题.
void g(base & b){
b.play;
}
void main(){
son s;
g(s);
return;
}