1.static有什么用途?(请至少说明两种)
1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变。
2) 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所有函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。
3)在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是,这个函数被限制在声明它的模块的本地范围内使用
2.引用与指针有什么区别?
1) 引用必须被初始化,指针不必。
2) 引用初始化以后不能被改变,指针可以改变所指的对象。
3) 不存在指向空值的引用,但是存在指向空值的指针。
3.描述实时系统的基本特性
在特定时间内完成特定的任务,实时性与可靠性。
4.全局变量和局部变量在内存中是否有区别?如果有,是什么区别?
全局变量储存在静态数据库(全局区),局部变量在堆栈。
5.什么是平衡二叉树?
左右子树都是平衡二叉树 且左右子树的深度差值的绝对值不大于1。
6.堆栈溢出一般是由什么原因导致的?
没有回收垃圾资源。
7.什么函数不能声明为虚函数?
constructor函数不能声明为虚函数。
8.冒泡排序算法的时间复杂度是什么?
时间复杂度是O(n^2)。
9.写出float x 与“零值”比较的if语句。
if(x>0.000001&&x<-0.000001)
10.Internet采用哪种网络协议?该协议的主要层次结构?
Tcp/Ip协议
主要层次结构为: 应用层/传输层/网络层/数据链路层/物理层。
11.Internet物理地址和IP地址转换采用什么协议?
ARP (Address Resolution Protocol)(地址解析协议)
12.IP地址的编码分为哪俩部分?
IP地址由两部分组成,网络号和主机号。不过是要和“子网掩码”按位与上之后才能区分哪些是网络位哪些是主机位。
13.用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出。写出C程序。有些,M,N值可能导致不能全部输出
循环链表,用取余操作做
我的答案:
- #include "iostream"
- #include "stdio.h"
- using namespace std;
- struct Node
- {
- int data;//存储1-N的值
- bool flag;//标记是否已被输出
- Node *next;
- };
- //建立链表,循环链表,尾节点指向首节点,表长度为N,节点的数据值为1---N
- Node* init(int N)
- {
- Node * head=NULL;
- Node *q=NULL;
- for (int i=1;i<=N;i++)
- {
- if (i==1)
- {
- Node *p=(Node *)malloc(sizeof(Node));
- p->data=i;
- p->flag=false;
- head=p;
- q=p;
- q->next=head;
- }
- else
- {
- Node *p=(Node *)malloc(sizeof(Node));
- p->data=i;p->flag=false;
- q->next=p;
- q=p;
- q->next=head;
- }
- }
- return head;
- }
- //打印所建立的循环链表,测试建表成功
- void printLink(Node *head)
- {
- Node *p=head;
- do{
- cout<<p->data<<", ";
- p=p->next;
- }while (p!=head);
- cout<<endl;
- }
- //判断是否还有没有打印出来的节点
- int isallflag(Node * head)
- {
- Node *p=head;
- int k=0;
- do{
- if (p->flag==false)
- {
- k=1;
- }
- p=p->next;
- }while (p!=head);
- return k;
- }
- //功能函数,约瑟夫环,打印出一个标记一个点,直至都打印出来
- void fun(Node * head,int m)
- {
- Node *p=head;
- int ct=1;
- while (isallflag(head)==1)
- {
- if (ct==m)
- {
- if (p->flag==false)
- {
- p->flag=true;
- cout<<p->data<<endl;
- }
- ct=1;
- p=p->next;
- }
- else
- {
- ct++;
- p=p->next;
- }
- }
- }
- int main(void)
- {
- int m=0,n=0;
- cout<<"input m,n\n";
- cin>>m;
- cin>>n;
- Node *head=init(n);
- fun(head,m);
- return 0;
- }
#include "iostream"
#include "stdio.h"
using namespace std;
struct Node
{
int data;//存储1-N的值
bool flag;//标记是否已被输出
Node *next;
};
//建立链表,循环链表,尾节点指向首节点,表长度为N,节点的数据值为1---N
Node* init(int N)
{
Node * head=NULL;
Node *q=NULL;
for (int i=1;i<=N;i++)
{
if (i==1)
{
Node *p=(Node *)malloc(sizeof(Node));
p->data=i;
p->flag=false;
head=p;
q=p;
q->next=head;
}
else
{
Node *p=(Node *)malloc(sizeof(Node));
p->data=i;p->flag=false;
q->next=p;
q=p;
q->next=head;
}
}
return head;
}
//打印所建立的循环链表,测试建表成功
void printLink(Node *head)
{
Node *p=head;
do{
cout<<p->data<<", ";
p=p->next;
}while (p!=head);
cout<<endl;
}
//判断是否还有没有打印出来的节点
int isallflag(Node * head)
{
Node *p=head;
int k=0;
do{
if (p->flag==false)
{
k=1;
}
p=p->next;
}while (p!=head);
return k;
}
//功能函数,约瑟夫环,打印出一个标记一个点,直至都打印出来
void fun(Node * head,int m)
{
Node *p=head;
int ct=1;
while (isallflag(head)==1)
{
if (ct==m)
{
if (p->flag==false)
{
p->flag=true;
cout<<p->data<<endl;
}
ct=1;
p=p->next;
}
else
{
ct++;
p=p->next;
}
}
}
int main(void)
{
int m=0,n=0;
cout<<"input m,n\n";
cin>>m;
cin>>n;
Node *head=init(n);
fun(head,m);
return 0;
}
14.不能做switch()的参数类型是:
switch的参数不能为实型。只能是整型和字符型
######################################################
1.printf的输出问题
- <SPAN style="FONT-SIZE: 14px">printf("%d",total);正确
- printf(total);错误
- printf("hello");正确</SPAN>
printf("%d",total);正确
printf(total);错误
printf("hello");正确
2.整数类型的长度
char 1个子节,8位
unsigned short[int]
[signed] short int
short 2个字节,16位
[signed] int
unsigned int
int 型在vc里是4个子节,32位,也可能是16位,2个字节
long [int]
unsigned long[int]
long型都是32位,4个字节
float 32 ,4
double 64,8
long double 128,16
char 8,一个字节,存放的实际上是字符的ascii码
3、找出错误并改正
- <SPAN style="FONT-SIZE: 14px">char *my_cpy(char*src, int len){
- char dest[1024];
- memcpy(dest, src,len);
- return dest;
- }</SPAN>
char *my_cpy(char*src, int len){
char dest[1024];
memcpy(dest, src,len);
return dest;
}
上面的函数是否有问题,如果有指出其所在,如果没有,给出函数功能描述。
答案:
1。数组应该初始化
2。memcpy不判断是否越界,所以调用前应该判断是否越界
3。不应该返回rest,因为这个数组是在函数内部申请的,所以函数结束之后就会消失,指针也会变成“野指针”,所以指向非法地址
最后一个比较隐蔽
- <SPAN style="FONT-SIZE: 14px">char *memcpy( char*dest, const char *src,int len )
- {
- char* pDest =(char*)dest;
- char* pSrc =(char*)src;
- int pos;
- for(pos=0;pos<len;pos++)
- {
- pDest[pos] =pSrc[pos];
- }
- return(char*)pDest;
- }</SPAN>
char *memcpy( char*dest, const char *src,int len )
{
char* pDest =(char*)dest;
char* pSrc =(char*)src;
int pos;
for(pos=0;pos<len;pos++)
{
pDest[pos] =pSrc[pos];
}
return(char*)pDest;
}
存在地问题就是没有判断指针是否非法assert(dest !=NULL || src != NULL); 条件为 FLASE 显示 如果assert的参数为假,则会先打印出错信息,然后调用abort()来终止程序
不调用其他函数,写一个memcpy的函数,函数原型为
- void *memcpy(void*dest, void *src, size_t length);
- -----------利用好断言---------
- /* memcpy ───拷贝不重叠的内存块 */
- void memcpy(void*pvTo, void* pvFrom, size_t size)
- {
- void* pbTo =(byte*)pvTo;
- void* pbFrom =(byte*)pvFrom;
- ASSERT(pvTo !=NULL && pvFrom != NULL);
- /* 内存块重叠吗?如果重叠,就使用memmove */
- ASSERT(pbTo>=pbFrom+size|| pbFrom>=pbTo+size);
- while(size-->0)
- *pbTo++ ==*pbFrom++;
- return(pvTo);
- }
void *memcpy(void*dest, void *src, size_t length);
-----------利用好断言---------
/* memcpy ───拷贝不重叠的内存块 */
void memcpy(void*pvTo, void* pvFrom, size_t size)
{
void* pbTo =(byte*)pvTo;
void* pbFrom =(byte*)pvFrom;
ASSERT(pvTo !=NULL && pvFrom != NULL);
/* 内存块重叠吗?如果重叠,就使用memmove */
ASSERT(pbTo>=pbFrom+size|| pbFrom>=pbTo+size);
while(size-->0)
*pbTo++ ==*pbFrom++;
return(pvTo);
}
-----------------------
常见函数编程:
- <SPAN style="FONT-SIZE: 14px">char *strcpy(char*strDest, const char *strSrc)
- {
- ASSERT(strDest !=NULL && strSrc != NULL);
- char *addr =strDest;
- while(*strDest++=*strSrc++)
- NULL; //NULL可以省略,但更有利于编译器发现错误
- }
- return addr;
- }
- void *memcpy(void*dest, const void *src, int count)
- {
- ASSERT(dest!= NULL&& src!= NULL);
- for(int i=0; i<cout; i++)
- {
- dest[i] = src[i];
- }
- }
- int strcmp(constchar*str1, const char *str2)
- {
- while (str1 !=NULL && str2 != NULL)
- {
- if(*str1 <*str2) return -1;
- else if(*str1 >*str2) return 1;
- else { str1++;str2++;}
- }
- if(str1 == NULL&& str2 != NULL)
- return -1;
- else if(str1 !=NULL && str2 == NULL)
- return 1;
- else return 0;
- }
- //way2: more compact
- int strcmp(constchar*str1, const char *str2)
- {
- int i = strlen(str1 );
- int j;
- for(j=0; j<=i;j++)
- {
- if(str1[j] >str2[j]) return 1; //if str2 terminates, then str2[j]=0, str1[j]>str2[j],return 1;
- else if(str1[j]< str2[j]) return -1;
- else if(str1[j] =='') return 0;
- }
- }
- //way3: optimizeagain.
- int strcmp(constchar * str1, const char * str2 )
- {
- while(1)
- {
- if(*str1 >*str2) return 1;
- else if(*str1 <*str2) return -1;
- else if(*str1 =='') return 0;
- str1++;str2++;
- }
- }</SPAN>