嵌入式軟件筆試(轉載)

時間:2005-06-25
地點:深圳南山區
方式:閉卷,寫程序

1、將一個字符串逆序
2、將一個鏈表逆序
3、計算一個字節裏(byte)裏面有多少bit被置1
4、搜索給定的字節(byte)
5、在一個字符串中找到可能的最長的子字符串
6、字符串轉換爲整數
7、整數轉換爲字符串
 
 
1、將一個字符串逆序
2、將一個鏈表逆序
3、計算一個字節裏(byte)裏面有多少bit被置1
4、搜索給定的字節(byte)
5、在一個字符串中找到可能的最長的子字符串
6、字符串轉換爲整數
7、整數轉換爲字符串
/*
* 題目:將一個字符串逆序
* 完成時間:2006.9.30深圳極訊網吧
* 版權歸劉志強所有
* 描述:寫本程序的目的是希望練一下手,希望下午去面試能成功,不希望國慶節之後再去找工作拉!
*/
#include <iostream>
using namespace std;
//#define NULL ((void *)0)
char * mystrrev(char * const dest,const char * const src)
{
if (dest==NULL && src==NULL)
   return NULL;
char *addr = dest;
int val_len = strlen(src);
dest[val_len] = '\0';
int i;
for (i=0; i<val_len; i++)
{
   *(dest+i) = *(src+val_len-i-1); 
}
return addr;

}
main()
{
char *str="asdfa";
char *str1=NULL;
str1 = (char *)malloc(20);
if (str1 == NULL)
   cout<<"malloc failed";
cout<<mystrrev(str1,str);
free(str1);
str1=NULL;//杜絕野指針
}

p=head;
q=p->next;
while(q!=NULL)
{
temp=q->next;
q->next=p;
p=q;
q=temp;
}
這樣增加個輔助的指針就行樂。

ok 通過編譯的代碼:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
typedef struct List{
int data;
struct List *next;
}List;
List *list_create(void)
{
struct List *head,*tail,*p;
int e;
head=(List *)malloc(sizeof(List));
tail=head;
printf("\nList Create,input numbers(end of 0):");
scanf("%d",&e);
while(e){
p=(List *)malloc(sizeof(List));
p->data=e;
tail->next=p;
tail=p;
scanf("%d",&e);}
tail->next=NULL;
return head;
}
List *list_reverse(List *head)
{
List *p,*q,*r;
p=head;
q=p->next;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=NULL;
head=p;
return head;
}
void main(void)
{
struct List *head,*p;
int d;
head=list_create();
printf("\n");
for(p=head->next;p;p=p->next)
printf("--%d--",p->data);
head=list_reverse(head);
printf("\n");
for(p=head;p->next;p=p->next)
printf("--%d--",p->data);
}
 
       編寫函數數N個BYTE的數據中有多少位是1。
解:此題按步驟解:先定位到某一個BYTE數據;再計算其中有多少個1。疊加得解。
#incluede<iostream>
#define N 10
//定義BYTE類型別名
#ifndef BYTE
typedef unsigned char BYTE;
#endif
int comb(BYTE b[],int n)
{
int count=0;
int bi,bj;
BYTE cc=1,tt;
//歷遍到第bi個BYTE數據
for(bi=0;bi<n;bi++)
{
//計算該BYTE的8個bit中有多少個1
tt=b[bi];
       for(bj=0;bj<8;bj++)
{
         //與1相與或模2結果是否是1?測試當前bit是否爲1
         //if(tt%2==1)
       if((tt&cc)==1)
{
            count++;
         }
         //右移一位或除以2,效果相同
//tt=tt>>1;
tt=tt/2;
}
}
return count;
}
//測試
int main()
{
BYTE b[10]={3,3,3,11,1,1,1,1,1,1};
cout<<comb(b,N)<<endl;
return 0;
}

1。編寫一個 C 函數,該函數在一個字符串中找到可能的最長的子字符串,且該字符串是由同一字符組成的。
char * search(char *cpSource, char ch)
{
char *cpTemp=NULL, *cpDest=NULL;
int iTemp, iCount=0;
while(*cpSource)
{
if(*cpSource == ch)
{
iTemp = 0;
cpTemp = cpSource;
while(*cpSource == ch)
++iTemp, ++cpSource;
if(iTemp > iCount)
iCount = iTemp, cpDest = cpTemp;
if(!*cpSource)
break;
}
++cpSource;
}
return cpDest;
}

#include <stdio.h>
#include <string.h>
//
// 自定義函數MyAtoI
// 實現整數字符串轉換爲證書輸出
// 程序不檢查字符串的正確性,請用戶在調用前檢查
//
int MyAtoI(char str[])
{
int i;
int weight = 1; // 權重
int rtn = 0; // 用作返回
for(i = strlen(str) - 1; i >= 0; i--)
{
   rtn += (str[i] - '0')* weight; //
   weight *= 10; // 增重
}
return rtn;
}
void main()
{
char str[32];
printf("Input a string :");
gets(str);
printf("%d\n", MyAtoI(str));
}
 
#include<stdio.h>
#include<string.h>
void reverse(char s[])
{   //字符串反轉
    int c, i=0, j;
    for(j=strlen(s)-1;i<j;j--)
    {    c=s[i];
        s[i]=s[j];
        s[j]=c;
        i++;
    }
}
void IntegerToString(char s[],int n)
{    int i=0,sign;
    if((sign=n)<0)//如果是負數,先轉成正數  
        n=-n;
    do //從個位開始變成字符,直到最高位,最後應該反轉
    {    s[i++]=n%10+'0';
    }while((n=n/10)>0);
    //如果是負數,補上負號
    if(sign<0)
        s[i++]='-';
    s[i]='\0';//字符串結束
    reverse(s);
}
void main()
{    int m;
    char c[100];
    printf("請輸入整數m: ");
    scanf("%d",&m);
    IntegerToString(c,m);
    printf("integer = %d string = %s\n", m, c);
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
閱讀(61) | 評論(0) | 轉發(0) |
給主人留下些什麼吧!~~
評論熱議
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章