嵌入式工程师笔试题一

时间:2022-04-10 08:36:20

1、将一个字符串逆序 

 

#include<stdio.h>
#include<string.h>
#include<malloc.h>


char *mystrrev(char *const dest,const char *const src)
{
    if( dest==NULL && src==NULL )
        return NULL;

    int val_len = strlen(src);

    dest[val_len] = '\0';
    int i;
    for( i=0; i<val_len; i++ ){
    *(dest+i) = *(src+val_len-1-i);
    }
    return dest;

}
int main( )
{
    char *str="asdfa";
    char *str1=NULL;
    str1 = (char *)malloc(20);
    if( str1 == NULL )
    {
        printf("molloc failed!\n");
    }
    printf("%s\n", mystrrev(str1,str));
    free(str1);
    str1=NULL;
    return 0;
}

 

2、将一个链表逆序 

 

嵌入式工程师笔试题一嵌入式工程师笔试题一
 1 #include<stdio.h>
 2 #include<malloc.h>
 3 
 4 typedef struct List{
 5     int data;
 6     struct List *next;
 7 }List;
 8 
 9 List *list_create( void )
10 {
11     List *head,*tail,*p;
12     int e;
13     head = (List *)malloc(sizeof(List));
14     tail = head;
15     printf("\nList Create,input number(end of 0):");
16     scanf("%d",&e);
17     while( e )
18     {
19         p = (List *)malloc(sizeof(List));
20         p->data = e;
21         tail->next = p;
22         tail = p;
23         scanf("%d",&e);
24     }
25     tail->next=NULL;
26     return head;
27 }
28 
29 List *list_reverse(List *head)
30 {
31     List *p,*q,*r;
32     p = head;
33     q = p->next;
34     while(q!=NULL)
35     {
36         r=q->next;
37         q->next=p;
38         p=q;
39         q=r;
40     }
41     head->next = NULL;
42     head = p;
43 
44     return head;
45 }
46 
47 void main( )
48 {
49     List *head,*p;
50     int d;
51     head = list_create( );
52     printf("\n");
53     for( p=head->next; p; p=p->next )
54     {
55         printf("--%d--",p->data);
56     }
57     head = list_reverse(head);
58     printf("\n");
59     for( p=head; p->next; p=p->next )
60         printf("--%d--",p->data);
61 
62 }
View Code

 

3、计算一个字节里(byte)里面有多少bit被置

 

嵌入式工程师笔试题一嵌入式工程师笔试题一
 1 #include<stdio.h>
 2 
 3 #define N 10
 4 #ifndef BYTE
 5 typedef unsigned char BYTE;
 6 #endif
 7 
 8 int comb(BYTE b[ ],int n)
 9 {
10     int count=0;
11     int bi,bj;
12     BYTE cc=1,tt;
13 
14     for( bi=0; bi<n; bi++ )
15     {
16         tt=b[bi];
17         for( bj=0; bj<8; bj++ )
18         {
19             if( (tt&cc) == 1 )
20             {
21                 count++;
22             }
23             tt = tt/2;
24         }
25     }
26     return count;
27 }
28 
29 int main( )
30 {
31     BYTE b[10] = {3,3,3,11,1,1,1,1,1,1};
32     printf("%d\n",comb(b,N));
33     return 0;
34 }
View Code

 

4、在一个字符串中找到可能的最长的子字符串,

该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。

 

嵌入式工程师笔试题一嵌入式工程师笔试题一
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 
 5 char * search( char *cpsource, char ch )
 6 {
 7     char *cpTemp=NULL,*cpDest=NULL;
 8     int iTemp,iCount=0;
 9     while( *cpsource )
10     {
11         if( *cpsource == ch )
12         {
13             iTemp = 0;
14             cpTemp = cpsource;
15             while( *cpsource == ch )
16             {
17                 ++iTemp;
18                 ++cpsource;
19             }
20             if( iTemp > iCount )
21             {
22                 iCount = iTemp;
23                 cpDest = cpTemp;
24             }
25             if( !*cpsource )
26                 break;
27         }
28         ++cpsource;
29     }
30     return cpDest;
31 }
32 
33 int main( )
34 {
35     char s[200];
36     char ch;
37     printf("请输入您要处理的字符串:\n");
38     while( gets(s) )
39     {
40           printf("请输入匹配字符:\n");
41 
42           scanf("%c",&ch);
43           getchar( );
44           printf("%c\n",ch);
45           printf("%s\n",search(s,ch));
46           printf("请输入您要处理的字符串:\n");
47     }
48     return 0;
49 }
View Code

 

5、整数转换为字符串

 

嵌入式工程师笔试题一嵌入式工程师笔试题一
 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 void reverse( char s[ ] )
 5 {
 6     char c;
 7     int i=0;
 8     int j;
 9     for( j=strlen(s)-1; i<j; j-- )
10     {
11         c = s[i];
12         s[i] = s[j];
13         s[j] = c;
14         i++;
15     }
16 }
17 
18 void IntegerToString( char s[ ],int n )
19 {
20     int i=0;
21     int sign;
22     if( (sign=n)<0 )
23         n=-n;
24     do
25     {
26         s[i++] = n%10+'0';
27     }while( (n=n/10)>0 );
28     if( sign < 0 )
29         s[i++] = '-';
30     s[i] = '\0';
31     reverse(s);
32 }
33 int main( )
34 {
35     int m;
36     char c[100];
37     printf("请输入整数m:");
38     scanf("%d",&m);
39     IntegerToString(c,m);
40     printf("integer = %d string = %s\n",m,c);
41 }
View Code

 

 6、字符串转换为整数 

嵌入式工程师笔试题一嵌入式工程师笔试题一
 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int MyAtoI( char str[] )
 5 {
 6     int i;
 7     int weight = 1;
 8     int rtn = 0;
 9 
10     for( i=strlen(str)-1; i>=0; i-- )
11     {
12         rtn += (str[i]-'0')*weight;
13         weight *= 10;
14     }
15     return rtn;
16 }
17 
18 void main( )
19 {
20     char str[32];
21 
22     printf("Input a string:");
23     gets(str);
24     printf("%d\n",MyAtoI(str));
25 }
View Code