设为首页 加入收藏

TOP

华为2011第一次笔试题目总结(二)
2014-11-23 23:10:32 来源: 作者: 【 】 浏览:8
Tags:华为 2011 第一次 笔试 题目 总结
,它的结果是字符串在内存中的所占字节大小,它要把\0算进去的。


2、如下程序用于输出“Welcome to Huawei Test”,请指出其中的两处错误,并给出错误原因。


char * GetWelcome(void){


char * pcWelcome;


char * pcNewWelcome;


pcWelcome=”Welcome to Huawei Test”;


pcNewWelcome=(char *)malloc(strlen(pcWelcome));


if(NULL==pcNewWelcome){


return NULL;


free(pcNewWelcome);


}


strcpy(pcNewWelcome, pcWelcome);


return pcNewWelcome;


}


错误1 :pcNewWelcome=(char *)malloc(strlen(pcWelcome));修改为 pcNewWelcome=(char *)malloc(strlen(pcWelcome)+1);


错误2: return NULL; free(pcNewWelcome);顺序错了,先free再return


测试程序如下:


#include


#include


#include


char * GetWelcome(void){


char * pcWelcome;


char * pcNewWelcome;


pcWelcome=”Welcome to Huawei Test”;


pcNewWelcome=(char *)malloc(strlen(pcWelcome));


if(NULL==pcNewWelcome){


return NULL;


free(pcNewWelcome);


}


strcpy(pcNewWelcome, pcWelcome);


return pcNewWelcome;


}


void main(){


char * str;


str=GetWelcome();


printf(str);


}


3、请指出下面程序的两处错误,并给出错误原因:


unsigned long FUNC_B(unsigned char str)


{


unsigned long sum;


while(0<=str)


{


sum+=str;


str–;


}


return sum;


}


错误1:sum赋初值=0;


错误2:0<=str修改为0

测试程序:


#include


#include


#include


unsigned long FUNC_B(unsigned char str)


{


unsigned long sum = 0;


while(0

{


sum+=str;


str–;


}


return sum;


}


//see it clearly!


//this button;右键添加断点或者(F9)


//this menu; 或者菜单栏中组件—>开始调试(F5)


//shift+F9—quick watch


//找到原因了吧?嗯,sum 初始化问题,


int main()


{


//now,initial this sum;let us get the issue;


//declare a num first;


//add a breakpoint here


//clearly


unsigned long summer;


summer = FUNC_B(5);


return 0;


}


应用题


注意:p是数组的首地址,相当于指针常量,是不能赋值。p++ 相当于 p = p + 1; 隐含的要改变p的值,这肯定不成, 也就是说p不能做左值(l-value) ,应该换个指针变量来指向p。


1、对于一个任意输入的少于200字节的字符串,把它按‘-’分成若干子串(子串最多20个),输出原始字符串和每个子串,要求能依次处理用户输入的多个字符串,直到用户输入字符串“0”时程序退出。


#include


using namespace std;


#include



void main(){


char str_s[200];


char *ptr=str_s;


char *pt=str_s;


int lenth;


cout<<”please input the source string you want to deal with:”<

while(true){


gets(str_s);


lenth=strlen(str_s);


char *ptr=str_s;


char *pt=str_s;


if (str_s[0] == ’0′)


{


break;


}


int flag=0,length=0;


if(lenth<200){


while(*ptr!=’\0′&&length<=lenth){


cout<<*ptr;


ptr++;


length++;


}


cout<

while(*pt!=’\0′&&flag<=20){


if(*pt!=’-'){


cout<<*pt;


pt++;


}


else{


flag++;


pt++;


cout<

}


}


cout<

}


cout<<”please input the source string you want to deal with:”<

}


}


小笨解析:


#include “stdafx.h”


#include


using namespace std;



int _tmain(int argc, _TCHAR* argv[])


{


char getword[200];


cout<<”Please enter your word:”<

while (true)


{


cin>>getword;


if (getword[0] == ’0′)


{break;}


}


return 0;


}


这么写就能处理多个了


小笨的程序:引入临时数组


#include


using namespace std;


int main()


{


cout<<”Please enter your word:”<

//char outdata[20][200];//用于保存输出的二维数组。


while (true)


{


char getword[200]={‘\0′};//保存用户输入的数组


cin>>getword; //用户输入,碰到回车键的时候进行判断


cout<

if (getword[0] == ’0′)//如果输入为0,退出执行程序


{


break;


}


int num = 0;//源数组的计数


int tempnum = 0;//临时数组的计数


int length = strlen(getword);


if (getword[length - 1] != ‘-’)//如果最后一个字符不是“-”,则自动添加一个“-”


{


getword[length] = ‘-’;


getword[length + 1] = ‘\0′;


}


char temparray[200]={‘\0′};//数组初始化操作


while (getword[num] != ‘\0′)


{


if (getword[num] == ‘-’)//如果碰到了“-”数组计数依然向后移动,


//而临时数组归零,为下一个数组。


{


temparray[tempnum] = ‘\0′;


cout<

tempnum = 0;


}


else


{


temparray[tempnum] = getword[num];//将子串赋给临时数组


tempnum++;


}


num++;


}


cout<<”Please enter your word:”<

}


return 0;


}


小笨讲解:


指向字符串的指针就不用重新申请空间. char a[111]; char *b = a; 申请,比如malloc(sizeof(int) * num);是指向一片内存区域。


2、将parabuf[]中的字符串,如“123”,“-301”等转化成数字123,-301并输出,不能用atoi等函数。


#include


using namespace std;


int change(char* str)


{


int base = 0;


while (*str)


{


base = base * 10 + (*str) – ’0′;


str++;


}


return base;


}



void main()


{


char str[100];


cin>>str;



int value1;


if (*str == ‘-’)


{


value1 = -1 * change(str + 1);


}


else{


v

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇百度笔试一面归来。。。 下一篇面试常见话题的问与答 About Your..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: