试题一
一、问答题
1. 请简述static的作用?……………………………………………………………………… 3’
2. 请简述volatile的作用?…………………………………………………………………… 3’
3 头文件中ifndef / define / endif 是做什么用的?………………………………………… 3’
4. #include <filename.h> 和 #include “filename.h” 有什么区别?…………………… 3’
5. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题)。… 3’
6. 写一个"标准"宏MIN ,这个宏输入两个参数并返回较小的一个。……………………… 4’
7. 下面的代码输出是什么,为什么?………………………………………………………… 5’
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) ? puts("> 6") : puts("<= 6");
}
8. 请问以下程序的运行结果?是否能满足真实要求呢?如果不能请修改?………………5’
swap(int a ,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
main()
{
int a=1,b=2;
swap(a,b);
printf(“%d,%d”,a,b);
}
9. 假设机器是WIN32的系统。……………………………………………………………………5’
1)
char str[] = “Hello” ; char *p = str ; int n = 10;
请计算
sizeof (str ) = sizeof ( p ) = sizeof ( n ) =
2)
void Func ( char str[100])
{
请计算
sizeof( str ) =
}
3)
void *p = malloc( 100 );请计算sizeof ( p ) =
10. 请仔细分析以下程序并回答问题。………………………………………………………16’
1)
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行Test函数会有什么样的结果?
2)
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
请问运行Test函数会有什么样的结果?
3)
Void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
请问运行Test函数会有什么样的结果?
4)
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
请问运行Test函数会有什么样的结果?
二、编程题
第一组:请在以下三到题中任意选一道做………………………………………………10’
1. 请编写程序,把文件a.txt和文件b.txt的内容都拷贝到文件c.txt中。
2. 请编写一个程序,在一个升序的整形数组中是否有一个需要找的数,用折半查找法?
3. 请编写一个程序,输出一个short类型数的镜像。(既:假如一个unsigned short类型的数的二进制码是1111 1111 0000 0000 那输出应为 0000 0000 1111 1111)。
第二组:请在以下三到题中任意选一道做(如果选题3做,加5分)………………10’
1. 请编写程序,将一个有数字组成的字符串转化为整数。(不允许使用库函数atoi())
2. 已知strcpy函数的原型是char *strcpy(char *strDest, const char *strSrc);其中strDest是目的字符串,strSrc是源字符串。不调用C++/C的字符串库函数,请编写函数 strcpy。
3. 请编写一个程序,用递归的算法,来实现10!?
第三组:请在以下三到题中任意选一道做(如果选题3做,加5分)………………15’
1. 请编写一个程序,输出显示如下图形。
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
2. 设编号为1,2,… n的17个人围坐一圈,从1开始报数,数到3 的那个人出列,它的下一位又从1开始报数,数到3的那个人又出列,依次类推,请编写一个程序,来得到最后留下来的那个人的编号。
3. 请编写一个程序,实现一个二叉树中序的遍历。
第四组 该组是必须做题 15’
1.请编写一个程序,实现以下功能。
1)创建一棵链表,该链表的每个节点有来存放一个整形的数。(提示:在创建链表时可以用输入0来结束创建)
2)查找一个数是否是该链表的一个节点。
3)如果这个数是在这个链表中,那么就删除该节点。
请自己定义相关数据结构。
试题一参考答案
一、问答题
1. 答:在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不。
在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。
在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是这个函数被限制在声明它的模块的本地范围内使用。(考生可以自己的话总结,只要符合要求,就给分)
2. 答:不会在两个操作之间把volatile变量缓存在寄存器中。在多任务、中断、甚至setjmp环境下,变量可能被其他的程序改变,编译器自己无法知道,volatile就是告诉编译器这种情况。
不做常量合并、常量传播等优化。
对volatile变量的读写不会被优化掉。如果你对一个变量赋值但后面没用到,编译器常常可以省略那个赋值操作,然而对Memory Mapped IO的处理是不能这样优化的。(考生可以自己的话总结,只要符合要求,就给分)
3. 答:防止该头文件被重复引用。
4. 答:对于#include <filename.h> ,编译器从标准库路径开始搜索 filename.h,对于#include “filename.h” ,编译器从用户的工作路径开始搜索 filename.h。
5. 答:#define SECONDS_PER_YEAR (365*24*60*60)UL //无符号整形
6. 答:#define MIN(A,B) ((A)<=(B)?(A):(B))
7. 答:输出是 ">6"。原因是当表达式中存在有符号类型和无符号类型时所有的操作数都自动转换为无符号类型。因此-20变成了一个非常大的正整数,所以该表达式计算出的结果大于6。
8. 答:输出是 1,2 。不能满足真实的要求。
程序应该为
swap(int *p1 ,int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
main()
{
int a=1,b=2;
swap(&a,&b);
printf(“%d,%d”,a,b);
}
9. 答:6 4 4 4 4
10.
1)答:程序崩溃。因为GetMemory并不能传递动态内存,Test函数中的 str一直都是 NULL。strcpy(str, "hello world");将使程序崩溃。
2)答:可能是乱码。因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。
3)答:能够输出hello。会有内存泄漏的情况。
4)答:篡改动态内存区的内容,后果难以预料,非常危险。因为free(str);之后,str成为野指针,if(str != NULL)语句不起作用。
二、编程题(注:编程题主要考察考生的思路,考生的回答不一定会和参考答案一样)
第一组:
1. 答:
#include <stdio.h>
void mian(void)
{
FILE *fp1,*fp2,*fp3;
char ch;
fp1=fopen("a.txt","r");
fp2=fopen("b.txt","r");
fp3=fopen("c.txt","w");
ch=fgetc(fp1);
while(ch!=EOF)
{
fwrite(&ch,1,1,fp3);
ch=fgetc(fp1);
}
ch=fgetc(fp2);
while(ch!=EOF)
{
fwrite(&ch,1,1,fp3);
ch=fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
2. 答:
#include<stdio.h>
int find_number(int num[],int need_num,int size)
{
int low,high,middle;
low=0;
high=size-1;
while(high>=low)
{
middle=(low+high)/2;
if(need_num>num[middle])
low=middle+1;
else if(need_num<num[middle])
high=middle-1;
else if(need_num==num[middle])
return 1;
}
return 0;
}
/**************用来测试的*****************/
void main(void)
{
int a[]={1,2,3,4,5,6,7,8,9,10};
int flag=0;
flag=find_number(a,3,10);
if(flag)
printf("exist\n");
else
printf("no exist\n");
}
/****************************************/
3. 答:
void image(unsigned short src)
{
int i;
unsigned short dest=0;
printf("%x\n",src);
for(i=0;i<16;i++)
{
dest=((((src>>i)&1)<<(15-i))|dest);
}
printf("%x\n",dest);
}
第二组:
1. 答:
int my_atoi(char *pnum)
{
char *ptemp=pnum;
int i=0;
int data=0;
while(*ptemp++!='\0')
i++;
ptemp=pnum;
while(i)
{
data=data*10+*ptemp++-'0';
i--;
}
return data;
}
2. 答:
char *strcpy(char *strDest, const char *strSrc);
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != ‘\0’ )
return address ;
}
3. 答:
int mul(long n)
{
int result;
if(n==1||n==0)
result=1;
else
result=n*mul(n-1);
return result;
}
第三组:
1. 答:
#include<stdio.h>
#include<math.h>
void main(void)
{
int x,y,result;
for(y=-2;y<3;y++)
{
for(x=-2;x<3;x++)
{
if(fabs(x)>fabs(y))
{
result=3-fabs(x);
printf("%d",result);
}
else
{
result=3-fabs(y);
printf("%d",result);
}
}
printf("\n");
}
}
2. 答:
#include <stdio.h>
#include <stdlib.h>
#define null 0
struct node
{
int data;
struct node *next;
};
void main(void)
{
int i;
struct node *p,*tmp,*head;
p=head=(struct node *)malloc(sizeof(struct node));
head->data=1;
head->next=null;
for(i=2;i<=17;i++)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=i;
tmp->next=null;
head->next=tmp;
head=head->next;
}
head->next=p;
while(p!=p->next)
{
p->next->next=p->next->next->next;
p=p->next->next;
}
printf("%d\n",p->data);
}
3. 答:
typedef struct tree /*定义树结点结构*/
{
struct tree *child;
struct tree *rchild;
int data;
}treenode,*bintree;
bintree create_tree(int data[],int len) /*利用插入算法创建二叉树*/
{
bintree root;
int i;
root=NULL;
for(i=0;i<len;i++)
root=insert_node(root,data[i]);
return root;
}
void inorder(bintree root) /*中序遍历二叉树的递归算法*/
{
if(root!=NULL)
{
inorder(root->lchild);
printf("%d",root->data);
inorder(root->rchild);
}
}
第四组:
1.答:
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct data)
struct data
{
int num;
struct data *next;
};
int n=0;
struct data *creat(void)
{
struct data *head;
struct data *p1,*p2;
n=0;
p1=p2=(struct data *)malloc(LEN);
scanf("%d",&p1->num);
head=NULL;
if(p1->num==0)
free(p1);
while(p1->num!=0)
{
n=n+1;
if(n= =1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct data *)malloc(LEN);
scanf("%d",&p1->num);
}
p2->next=NULL;
return head;
}
int find(struct data *head,int findnum)
{
struct data *p1;
p1=head;
while(p1!=NULL)
{
if(p1->num!=findnum)
p1=p1->next;
else
return findnum;
}
if(p1==NULL)
return 0;
}
struct data *del(struct data *head,int neednum)
{
struct data *p1,*p2;
if(head==NULL)
return head;
p1=head;
while(neednum!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(neednum==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
free(p1);
return head;
}
void print(struct data *head)
{
struct data *p1;
p1=head;
while(p1!=NULL)
{
printf("%d\n",p1->num);
p1=p1->next;
}
}
void main(void)
{
struct data *head;
int findnum=5;
int flag;
head=creat();
print(head);
flag=find(head,findnum);
if(flag)
head=del(head,flag);
print(head);
}