随笔-4  评论-0  文章-0  trackbacks-0
http://web.nuist.edu.cn/courses/jsj/GD_jsj_020b/text/chapter09/section3/part3/exer_l.htm
http://www.diybl.com/course/3_program/c/c_js/20071119/86868.html
1. 找错
void test1()
{
char string[10];
char* str1="0123456789";
strcpy(string, str1);
}
答:表面上并且编译都不会错误。但如果string数组原意表示的是字符串的话,那这个赋值就没有达到意图。最好定义为char string[11],这样最后一个元素可以存储字符串结尾符'\0';

void test2()
{
char string[10], str1[10];
for(int I=0; I<10;I++)
{
str1[I] ='a';
}
strcpy(string, str1);
}
答:strcpy使用错误,strcpy只有遇到字符串末尾的'\0'才会结束,而str1并没有结尾标志,导致strcpy函数越界访问,不妨让str1[9]='\0',这样就正常了。

void test3(char* str1)
{
char string[10];
if(strlen(str1)<=10)
{
strcpy(string, str1);
}
}
答:这又会出现第一道改错题的错误了。strlen(str1)算出来的值是不包含结尾符'\0'的,如果str1刚好为10个字符+1结尾符,string就得不到结尾符了。可将strlen(str1)<=10改为strlen(str1)<10。

2. 找错
#define MAX_SRM 256

DSN get_SRM_no()
{
static int SRM_no;
int I;
for(I=0;I {
SRM_no %= MAX_SRM;
if(MY_SRM.state==IDLE)
{
break;
}
}
if(I>=MAX_SRM)
return (NULL_SRM);
else
return SRM_no;
}
答:我不知道这段代码的具体功能,但明显有两个错误
1,SRM_no没有赋初值
2,由于static的声明,使该函数成为不可重入(即不可预测结果)函数,因为SRM_no变量放在程序的全局存储区中,每次调用的时候还可以保持原来的赋值。这里应该去掉static声明。

3. 写出程序运行结果
int sum(int a)
{
auto int c=0;
static int b=3;
c+=1;
b+=2;
return(a+b+c);
}
void main()
{
int I;
int a=2;
for(I=0;I<5;I++)
{
printf("%d,", sum(a));
}
}
答:8,10,12,14,16
该题比较简单。只要注意b声明为static静态全局变量,其值在下次调用时是可以保持住原来的赋值的就可以。

4.
int func(int a)
{
int b;
switch(a)
{
case 1: b=30;
case 2: b=20;
case 3: b=16;
default: b=0;
}
return b;
}
则func(1)=?
答:func(1)=0,因为没有break语句,switch中会一直计算到b=0。这是提醒我们不要忘了break。呵呵。

5:
int a[3];
a[0]=0; a[1]=1; a[2]=2;
int *p, *q;
p=a;
q=&a[2];
则a[q-p]=?
答:a[q-p]=a[2]=2;这题是要告诉我们指针的运算特点

6.
定义 int **a[3][4], 则变量占有的内存空间为:_____
答:此处定义的是指向指针的指针数组,对于32位系统,指针占内存空间4字节,因此总空间为3×4×4=48。

7.
编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。
答:
/*输入年月日时分秒,输出年月日时分秒的下一秒,输出仍然在原内存空间*/
void NextMinute(int *nYear,int *nMonth,int *nDate,int *nHour,int *nMinute,int *nSecond)
{
int nDays;
(*nSecond)++; // 秒加1
if(*nSecond>=60) // 秒满60,做出特殊处理,下面时,日,月等类同
{
*nSecond=0;
(*nMinute)++;
if(*nMinute>=60)
{
*nMinute=0;
(*nHour)++;
if(*nHour>=24)
{
*nHour=0;
(*nDate)++;
switch(*nMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
nDays=31;
break;
case 2:// 判断闰年
if(*nYear%400==0||*nYear%100!=0&&*nYear%4==0)
{
nDays=29;
}
else
{
nDays=28;
}
break;
default:
nDays=30;
break;
}
if(*nDate>nDays)
{
*nDate=1;
(*nMonth)++;
if(*nMonth>12)
{
*nMonth=1;
(*nYear)++;
}
}
}
}
}
}
/*示例可运行代码*/
void main()
{
int nYear=2004,nMonth=12,nDate=31,nHour=59,nMinute=59,nSecond=59;
NextMinute(&nYear,&nMonth,&nDate,&nHour,&nMinute,&nSecond);
printf("The result:%d-%d-%d %d:%d:%d",nYear,nMonth,nDate,nHour,nMinute,nSecond);
}

加拿大著名电信设备制造商北电网络公司始建于一个世纪以前,在通讯发展进步历程中始终处于领袖地位,广东北电通信设备有限公司成立于1995年3月,是北电在华投资的核心公司之一。公司网址是http://www.gdnt.com.cn/
  下面是广东北电的笔试题(中英文题),这套题早已在网络上流传数年,从来只见题目,不见解答,lol那就让我做做吧。英文搞得不对的地方那就没办法了。希望大家转贴的时候声明出处。

一:英文题。
1. Tranlation (Mandatory)

  CDMA venders have worked hard to give CDMA roaming capabilities via the development of RUIM-essentially, a SIM card for CDMA handsets currently being deployed in China for new CDMA operator China Unicom. Korean cellco KTF demonstrated earlier this year the ability to roam between GSM and CDMA using such cards.However,only the card containing the user’s service data can roam-not the CDMA handset or the user’s number (except via call forwarding).
翻译:CDMA开发商一直致力于RUIM卡的开发,以此赋予CDMA漫游的能力。RUIM卡类似于SIM卡,事实上目前它已经被中国的CDMA运营商中国联通广泛使用。韩国手机制造企业KTF今年早些时候展示了使用此种卡在GSM和CDMA网络中漫游的功能,但是,只有该卡包含的用户服务数据能够漫游,CDMA手机本身及用户号码则不能(除了呼叫前转业务)。
呵呵。上文可能翻译的不太精准,欢迎批评。  

2. Programming (Mandatory)
  Linked list
  a. Implement a linked list for integers,which supports the insertafter (insert a node after a specified node) and removeafter (remove the node after a specified node) methods;
  b. Implement a method to sort the linked list to descending order.
答:题目的意思是实现一个整型链表,支持插入,删除操作(有特殊要求,都是在指定节点后进行操作),并写一个对链表数据进行降序排序的方法。
那我们不妨以一个线性链表进行编程。
// 单链表结构体为
typedef struct LNode
{
int data;
struct LNode *next;
}LNode, *pLinkList;

// 单链表类
class LinkList
{
private:
pLinkList m_pList;
int m_listLength;
public:
LinkList();
~LinkList();
bool InsertAfter(int afternode, int data);//插入
bool RemoveAfter(int removenode);//删除
void sort();//排序
};

实现方法
//insert a node after a specified node
bool LinkList::InsertAfter(int afternode, int data)
{
LNode *pTemp = m_pList;
int curPos = -1;
if (afternode > m_listLength ) // 插入点超过总长度
{
return false;
}
while (pTemp != NULL) // 找到指定的节点
{
curPos++;
if (curPos == afternode)
break;
pTemp = pTemp->next;
}
if (curPos != afternode) // 节点未寻到,错误退出
{
return false;
}
LNode *newNode = new LNode; // 将新节点插入指定节点后
newNode->data = data;
newNode->next = pTemp->next;
pTemp->next = newNode;
m_listLength++;
return true;
}

//remove the node after a specified node
bool LinkList::RemoveAfter(int removenode)
{
LNode *pTemp = m_pList;
int curPos=-1;
if (removenode > m_listLength) // 删除点超过总长度
{
return false;
}

// 找到指定的节点后一个节点,因为删除的是后一个节点
while (pTemp != NULL)
{
curPos++;
if (curPos == removenode+1)
break;
pTemp = pTemp->next;
}
if (curPos != removenode) // 节点未寻到,错误退出
{
return false;
}
LNode *pDel = NULL; // 删除节点
pDel = pTemp->next;
pTemp->next = pDel->next;
delete pDel;
m_listLength--;
return true;
}

//sort the linked list to descending order.
void LinkList::sort()
{
if (m_listLength<=1)
{
return;
}
LNode *pTemp = m_pList;
int temp;
// 选择法排序
for(int i=0;i for(int j=i+1;j if (pTemp[i].data {
temp=pTemp[i].data;
pTemp[i].data=pTemp[j].data;
pTemp[j].data=temp;
}
} 
前两个函数实现了要求a,后一个函数sort()实现了要求b

3. Debugging (Mandatory)
a. For each of the following recursive methods, enter Y in the answer box if the method terminaters (assume i=5), Otherwise enter N.
(题目意思:判断下面的递归函数是否可以结束)
static int f(int i){
return f(i-1)*f(i-1);
}
Ansewr: N,明显没有返回条件语句,无限递归了

static int f(int i){
if(i==0){return 1;}
else {return f(i-1)*f(i-1);}
}  
Ansewr:Y,当i=0时可结束递归

static int f(int i){
if(i==0){return 1;}
else {return f(i-1)*f(i-2);}
}
Ansewr:N,因为i=1时,f(i-2)=f(-1),进入一个无限递归中

b. There are two errors in the following JAVA program:
static void g(int i){
if(i==1){return;}
if(i%2==0){g(i/2);return;}
else {g(3*i);return;}
}
please correct them to make sure we can get the printed-out result as below:
3 10 5 16 8 4 2 1
答:在第一个if语句前加 System.out.print(i+" ");
  else 里面的g(3*i)改为g(3*i+1)
该题由网友alvin补上,我不熟java。谢谢他。  

中文笔试题
1.汉译英
  北电网络的开发者计划使来自于不同组织的开发者,能够在北电网络的平台上开发圆满的补充业务。北电网络符合工业标准的开放接口,为补充业务的开展引入了无数商机,开发者计划为不同层面的开发者提供不同等级的资格,资格的划分还考虑到以下因素:补充业务与北电网络平台的集合程度,开发者团体与北电网络的合作关系,等等。
答:呵呵。这个这个基本上还是不现丑了吧。cry

2.编程
  将整数转换成字符串:void itoa(int,char);
例如itoa(-123,s[])则s=“-123”;
答:
char* itoa(int value, char* string)
{
char tmp[33];
char* tp = tmp;
int i;
unsigned v;
char* sp;
// 将值转为正值
if (value < 0)
v = -value;
else
v = (unsigned)value;
// 将数转换为字符放在数组tmp中
while (v)
{
i = v % 10;
v = v / 10;
*tp++ = i+'0';
}
// 将tmp里的字符填入string指针里,并加上负号(如果有)
sp = string;
if (value < 0)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
posted on 2008-11-10 15:40 Rockin 阅读(177) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。