嵌入式软件常见的笔试题(c)

找工作的事情告一段落,现在把一些公司常见的笔试题型整理一下,本人主要是找嵌入式软件方面的工作,笔试的也主要是C语言、数据结构,大体上都比较基础,但是得早作准备,才会占得先机。

 

1:整型数求反

2:字符串求反,字符串加密,越界问题

3:字符串逆序,两端对调;字符串逆序,指针法

4:递归求n

5:不用库函数,比较两个字符串的大小

6:求0-3000中含有92的全部数之和

7:质因数分解

8:判断一个数(x)是否是2n次方

9:判断CPU的大小端

10: 字符串转成成整型数

11: 整型数转换成字符串

12: //插入法//选择法//冒泡法

13: 二分收索法

14: //编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004//1231235959秒,则输出200511000

15: //1、滤除给定字符串首尾的空格,和字符串中的TAB符和换行符

//2、滤除空格

//3、判断质数

16:文件的读写操作

fopen(“name”,”rw”),fputc(ch,fp),fgetc(fp),fclose(fp),feof(fp)

 //1从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个“#”为止

   //2将一个磁盘文件中的信息复制到另一个磁盘文件中

   //3/*fread(buffer,size,count,fp) ,fwrite(buffer,size,count,fp)*/

   //4//rewind(fp1);//使位置指针重新返回文件的开头,此函数没有返回值

   //5 seek(fp,位移量(long),起始点)

 

17: 字符串常量指针问题

18:union struct sizeof()

 

 

 

 

 

//-----------------------------------------------------------------

/*整型数求反*/

//-----------------------------------------------------------------

#include<stdio.h>

 

int main()

{

         int num1 = 8765439;

         int num2 = 0;

         while(num1 > 0)

         {

                   num2 = num2 * 10 + (num1 % 10);

                   num1 = num1/10;

 

         }

 

         printf("%d", num2);

 

         return 0;

}

//-------------------------------------------------------------------

 

 

//-----------------------------------------------------------------

/*字符串求反,字符串加密,越界问题*/

//-----------------------------------------------------------------

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

 

 

void fun1(unsigned char *p);

void fun2(unsigned char *q);

 

void main()

{

    unsigned char str[]="abcdefg";

        

         printf("%s\n",str);

 

         fun1(str);

         printf("%s\n",str);

 

         fun2(str);

         printf("%s\n",str);

 

 

 

}

 

void fun1(unsigned char *p)

{

         while(*p !='\0')

         {

                   if((*p=(*p * 2+10) ) >=128)

                   {

                            *p= *p / 3;

                   }

                   p++;

         }

        

 

}

 

//字符串逆序,两端对调

void fun2(unsigned char *q)

{

         char temp,i,len;

         len=strlen((char * )q);

//      len=7;

         for(i=0;i<(len-1)/2;i++)

         {

                   temp=q[len-1-i];

             q[len-1-i]=q[i];

             q[i]=temp;

         }

}

//字符串逆序,指针法

void pointer_str(void)

{

                   char *src="hello,world";

                   char *dest=NULL;

                   int len=strlen(src);

                   dest=(char *)malloc((len+1)* sizeof(char));

                   char *d=dest;

                   char *s=&src[len-1];

 

                   while(len-- != 0)

                   {

                            *(d++) = *(s--);

                   }

                  

                   *d='\0';

                   printf("%s\n\n",src);

                   printf("%s\n\n",dest);

                   free(dest);

}

 

 

//递归求n

int n_jiecheng(unsigned char n)

{

         if(n==1)

         {

                   return 1;

         }

         else if(n>1)

         {

                   n=n*n_jiecheng(n-1);

                  

         }

         printf("n=%d\n",n);

         return n;

}

 

//不用库函数,比较两个字符串的大小

int strcmp_z( char *src, char *dest)

{

/*     while((*src != '\0') && (*src == *dest) )

         {

                   src++;

                   dest++;

         }

         return ((*src) - (*dest) ) ? -1 : 0 ;

*/

         int i;

         for(i=0;src[i]==dest[i];i++)

         {

                   if(src[i] == '\0' && dest[i] == '\0')

                   {

                            return 0;

                   }

                  

         }

          return -1;

}

 

//-----------------------------------------------------

//0-3000中含有92的全部数之和

//-----------------------------------------------------

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

int fun(char *);

 

int fun(char *p)

{

         char flag_9=0,flag_2=0;

         while(*p != '\0')

         {

                   if('9'== *p)

                   {

                            flag_9=1;

                   }

                   else if ('2'== *p)

                  {

                            flag_2=1;

                   }

                   p++;

         }

 

         if(2==(flag_9+flag_2))

         {

                   return 1;

         }

 

         return 0;

}

 

int main()

{

         int i,j=0;

         unsigned int  sum = 0

         char table[]="0123";

         char *str=table;

         for(i=0;i<3000;i++)

         {

                   itoa(i,str,10);

                   if(fun(str))

                   {

                            sum+=i;

                            printf("%u,%u,%s,%u\n",j++,i,str,sum);

                   }

         }

         return 0;

}

//---------------------------------------------------------------

 

 

//-------------------------------------------------------------

//质因数分解

//--------------------------------------------------------------

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

int j=0;

int IsPrime(int Num)

{

    int i;

    for(i=3; i<=sqrt(Num); i=i+2)

    {

        if (Num%i==0)

        {

            return 0;

        }   

    }

    return 1;

}

int resolve(int Num)

{

 int i,n;

   

    for(i=2; i<Num/2;i++)

    {

        if(IsPrime(i)==1 && Num%i==0)

        {

            if(j==0)

                //------------第一次进入时输出   

                printf("%d",i);

            else

                //-------------第二次以后输出

                printf("*%d",i);

            j++;

        }

        else

            continue;

     n = Num/i;//------------- 保存另一个数;

        if(IsPrime(n)==1)

        {

            printf("*%d",n);

            system("pause");//--------系统暂停

            exit(0);//------------发现

        }

        else

            //--------如果不是素数,调用自身

            resolve(n); //-------------利用递归

    }

    return n;

}

int main()

{

    int num;

    printf("请输入一个合数:");

    scanf("%d",&num);

    if(IsPrime(num)==1)

    {

    printf("这不是一个合数!");

    system("pause");

    return 0;

    }

    printf("%d=",num);

    resolve(num);

    system("pause");

    return 0;

}

 

 

 

//-------------------------------------------------------------

//判断一个数(x)是否是2n次方

-------------------------------------

#include <stdio.h>

 

int func(int x)

{

    if( (x&(x-1)) == 0 )

        return 1;

    else

        return 0;

}

 

int main()

{

    int x = 8;

    printf("%d\n", func(x));

}

 

 

:

(1) 如果一个数是2n次方,那么这个数用二进制表示时其最高位为1,其余位为0

 

(2) == 优先级高于 &

 

//----------------------------------------------------------------

 

 

//------------------------------------------

//求下面函数的返回值(微软) -- 统计1的个数

//------------------------------------------

int func(int x)

{

    int countx = 0;

    while(x)

    {

        countx++;

        x = x&(x-1);

    }

    return countx;

}

 

假定x = 9999

10011100001111

答案: 8

 

思路x转化为2进制,看含有的1的个数。

每执行一次x = x&(x-1),会将x用二进制表示时最右边的一个1变为0,因为x-1将会将该位(x用二

进制表示时最右边的一个1)变为0

 

 

//----------------------------------------------------------------

//判断CPU的大小端

//----------------------------------------------------------------

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

int checkCPUendian()

{

         union

         {

                   unsigned int a;

                   unsigned char b;

         }c;

         c.a=1;

         return(c.b==1);

}

 int main()

 {

 

          if(checkCPUendian())

          {

                    printf("little-endian\n");

          }

          else

          {

                    printf("big-endian\n");

          }

 }

 

//---------------------------------------------------------------

typedef int(*CallBack)(char *p);//后面';'

变量名:CallBack

1.*CallBack:CallBack是一个指针

2.(*CallBack)():指向一个函数

3.(*CallBack)(char *p):这个函数有一个参数,类型为char*

4.int(*CallBack)(char *p):这个函数有一个返回值,类型为int

最终:CallBack是一个指针,指向具有一个char*类型参数,且返回一个int型数据的函数前面有个

typedef,所以现在CallBack是这种类型的别名

int func(char *p)

{

...

}

...

CallBack my_callback1 = func;//OK

CallBack my_callback2 = &func;//OK以上二者均可

 

 

 

 

 

//----------------------------------------------------------------------------

【转】指向函数的指针与返回指针值的函数

1、指向函数的指针

定然形式为:   数据类型 (*指针变量名)(); 例如:int (*p)();  定义P是一个指向函数的指针变量,此函数带回整型的返回值。若写成 int *p(); 它就声明了一个函数,函数的返回值是指向整型变量的指针。在int (*p)();  的定义中,(*p) 和函数名是等价的。

函数指针变量常用的用途之一是把指针作为参数传递到其他函数。

2、返回指针值的函数

定义形式为:类型名      *函数名(参数表列)例如:int *a(int x, int y); a是函数名,调用它以后能得到一个指向整型数据的指针。

3、有关指针的数据类型的小结

    int   i;       定义整型变更;

   int   *P      p为指向整型数据的指针变量

   int   a[n]    定义一个数组a,它有n 个变量

  int    *p[n]   定义一个指针数组,即数组里存放的都是指向整型元素的指针

 int    (*p) [n]  定义一个指向数组的指针变量

  int   f()    定义一个返回值为整型的函数

 int  *p()   定义一个函数,该函数返回一个指向整型元素的指针变量

 int  (*p)()  指向函数的指针,该函数返回一个整型值

int **p   p是一个指针变量,它指向一个指向整型数据的指针变量

返回指向函数的指针 例:

在C++ primer 238页中出现的int(*ff(int))(int*,int) ,解释为:

ff(int)是一个函数,带有一个int型的形参,所以,这个首先是一个函数。如下面例子中的int (*funOne(int number))(int a, int b)的funOne(int number).

其返回的使int (*)(int*,int)的函数指针,这个函数指针如下文中的int funtwo(int a,int b),funtwo本身就是一个指针。

#include <iostream>

using namespace std;

int funTwo(int a, int b)
{
    return a * b;
}

// funOne是一个函数,带有一个int型参数,它返回一个指向函数的指针
// 这个指向函数的指针指向一个返回int型,并带有两个int型的形参的函数
int (*funOne(int number))(int a, int b)
{
    return funTwo;
}

int main()
{
    cout << funOne(1)(3, 10) << endl;
    return 0;
}

----------

OutPut:

 

对于上文的funcOne(int number)(int int)我们可以如下方式引用:

typedef int (*PF)(int, int); //申明函数指针
PF ff(int);

// ff returns a pointer to function

 

//--------------------------------------------------------------

//字符串转成成整型数

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

char str[6]="12345";

int string_to_int(char s[])

{

         int i;

         int sum=0;

         for(i=0;s[i]!='\0';i++)

         {

                   sum=sum*10+s[i]-'0';

         }

         return sum;

 

}

 

int main(void)

{

                   printf("%d\n",string_to_int(str));

                   return 0;

}

 

 

//-----------------------------------------------------------------------------

//使用数组和指针的时候,注意指针越界的问题

//char str[6]="12345";

int a=1234;

 

int main(void)

{

         char b[10]={'0','0','0','0'};

         char *str;

         str=b;

         printf("%d\n",a);

         sprintf(str,"%d",123456789);

//      itoa(a,str,10);

         printf("%s\n",str);

                   return 0;

}

 

 

//----------------------------------------------------------------------

//整型数转换成字符串

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

//char str[6]="12345";

 

 

void releas(char str[])

{

         int i=0,temp=0,len=0;

         len=strlen(str);

         printf("len==%d\n",len);

         for(i=0;i< (len-1)/2;i++)

         {

                   temp=str[len-1-i];

                   str[len-1-i]=str[i];

                   str[i]=temp;

         }

}

void itoa_zhang(int n ,char s[])

{

         int i=0,sign=0;

         if((sign=n) < 0)

         {

                   sign=-1;

                   n=-n;

         }

 

         do{

                   s[i++]=n%10+'0';

         }while(n=n/10);

                  

                   if(sign==-1)

                   {

                            s[i]='-';

                   }

 

         s[i+1]='\0';

 

}

 

int main(void)

{

         char s[10]="0000";//注意,要保证定义的数组长度足够

         char *p;

         p=s;

         printf("%s\n",p);

         itoa_zhang(-1235,p);

         printf("%s\n",p);

         releas(p);

         printf("%s\n",p);

                   return 0;

}

 

 

//=========================================

//插入法排序

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

void inser_sort(char s[])

{

         int i,j,key=0,len;

         len=strlen(s);

         printf("%s\n",s);

/*    

         for(j=1;j<len;j++)

         {

                   i=j-1;

                   key=s[j];

                   while((i>=0)&&(s[i]>key))

                   {

                            s[i+1]=s[i];

                            i--;

                   }

 

                   s[i+1]=key;

         }

*/

         for(j=1;j<len-1;j++)

         {

                   key=s[j];

                   for(i=j-1;i>=0;i--)

                   {

                            if(s[i]>key)

                            {

                                     s[i+1]=s[i];

                            }

                            else

                            {

                                     break;

                            }

                   }

                   s[i+1]=key;

         }

 }

 

int main(void)

{

         char s[5]={'4','3','7','1'};

 

         inser_sort(s);

         printf("%s\n",s);

 

 

                   return 0;

}

 

 

//========================================

//插入法

//选择法

//冒泡法

//=========================================

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

//char str[6]="12345";

 

//插入法

void inser_sort(char s[])

{

         int i,j,key=0,len;

         len=strlen(s);

         printf("%s\n",s);

/*    

         for(j=1;j<len;j++)

         {

                   i=j-1;

                   key=s[j];

                   while((i>=0)&&(s[i]>key))

                   {

                            s[i+1]=s[i];

                            i--;

                   }

 

                   s[i+1]=key;

         }

*/

         for(j=1;j<len;j++)

         {

                   key=s[j];

                   for(i=j-1;i>=0;i--)

                   {

                            if(s[i]>key)

                            {

                                     s[i+1]=s[i];

                            }

                            else

                            {

                                     break;

                            }

                   }

                   s[i+1]=key;

         }

 }

 

 

//选择法

void choose_sort(char s[])

{

         int i,j,temp,key=0,len;

         len=strlen(s);

         printf("%s\n",s);

 

         for(i=0;i<(len-1);i++)

         {

                   for(j=i+1;j<len;j++)

                   {

                            if(s[i]>s[j])

                            {

                                     temp=s[j];

                                     s[j]=s[i];

                                     s[i]=temp;

                            }

                   }

         }

}

 

//简单选择法,当找到最小的值时,再进行交换

void choose_sort_simple(char s[])

{

         int i,j,min,temp,key=0,len;

         len=strlen(s);

         printf("%s\n",s);

 

         for(i=0;i<(len-1);i++)

         {

                   min=i;

                   for(j=i+1;j<len;j++)

                   {

                            if(s[min]>s[j])

                            {

                                     min=j;

                            }

                   }

                    

                   if(i!=min)

                   {

                            temp=s[i];

                            s[i]=s[min];

                            s[min]=temp;

                            printf("%s\n",s);

                   }

                  

                  

         }

}

 

//冒泡法,每执行一次,最大的值沉到下面,小的上升

void maopao_sort(char s[])

{

         int i,j,temp,key=0,len;

         len=strlen(s);

         printf("%s\n",s);

 

         for(i=0;i<(len-1);i++)

         {

                   printf("%d: %s\n",i,s);

                   for(j=0;j<(len-1-i);j++)

                   {

                            if(s[j+1]<s[j])

                            {

                                     temp=s[j+1];

                                     s[j+1]=s[j];

                                     s[j]=temp;

                            }

                   }

         }

}

 

//冒泡法,每执行一次,最小的值冒到前面

void maopao_sort_1(char s[])

{

         int i,j,temp,key=0,len;

         bool flag=true;

         len=strlen(s);

         printf("%s\n",s);

 

         for(i=0;i<(len-1) && flag;i++)

         {

                   flag=false;

                   for(j=len-1;j>=i;j--)

                   {

                            if(s[j]<s[j-1])

                            {

                                     temp=s[j];

                                     s[j]=s[j-1];

                                     s[j-1]=temp;

                                     flag=true;

                            }

                   }

                            printf("%d: %s\n",i,s);

         }

}

int main(void)

{

         char s[5]={'9','3','7','1'};

 

//      inser_sort(s);

// choose_sort(s);

         maopao_sort(s);

         printf("%s\n",s);

 

 

                   return 0;

}

 

//======================================

//二分收索法

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

#define size 10

 

int g[size]={0,1,2,3,4,5,6,7,8,9};

 

int search(int x)

{

         int high,low=0,mid;

         high=size-1;

        

         while(low<=high)

         {

                   mid=(high+low)/2;

                   if(g[mid] == x)

                   {

                            return mid;

                   }

                   else if(g[mid]>x)

                   {

                            high=mid-1;

                   }

                   else if(g[mid]<x)

                   {

                            low=mid+1;

                   }

 

         }

         return -1;

}

 

int main(void)

{

 

         printf("%d\n",search(7));

                   return 0;

}

 

 

//编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004//1231235959秒,则输出200511000秒。答:

//

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

 

/*输入年月日时分秒,输出年月日时分秒的下一秒,输出仍然在原内存空间*/

void NextMinute(int *nYear,int *nMonth,int *nDate,int *nHour,int *nMinute,int *nSecond)

         int nDays; 

         (*nSecond)++;  // 秒加

         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\n",nYear,nMonth,nDate,nHour,nMinute,nSecond);

 }   

 

 

 

//===========================

//1、滤除给定字符串首尾的空格,和字符串中的TAB符和换行符

//2、滤除空格

//3、判断质数

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<math.h>

 

bool charFiter(const char *pSource,char *pTarget)

{

         char i;

         char *src=pTarget; //栈指针在函数退出时,自动消亡

         while(*pSource != '\0')

         {

                   if(*pSource == ' ')

                   {

                            pSource++;

                   }

                   else

                            break;

         }

 

         while(*pSource != '\0')

         {

                   if((*pSource == '\t') || (*pSource == '\n') )

                   {

                            pSource++;

                            continue;//注意continue

                   }

 

                   *(pTarget++) = *(pSource++);

 

                            printf("pTarget_addr:%u,pTarget:%c,pSource:%c\n",pTarget, *(pTarget-1),*(pSource-1));//

         }

        

 

         for(i=strlen(src)-1;i>=0;i--)

         {

                   if(src[i] != ' ')

                   {

                            break;

                   }

                   else

                   {

                            src[i]='\0';

                   }

         }

 

/*

         pTarget--;

         if(*pTarget == ' ')

         {

                  

                   *pTarget='\0';

        

         }

         else

                   *(++pTarget)='\0';

*/

         return 1;

}

 

 

 

 

//#include <stdio.h>

//#include <ctype.h>

 

void deleteSpace(char str[])

{

         int i = 0;

         int j = 0;

         while(str[i] != '\0')

         {

                   //if(!isspace(str[i]))

                   if(str[i] != ' ')

                   str[j++] = str[i];

                   i++;

         }

         str[j] = '\0';

}

//判断素数(质数)

void zhishu(char m)

{

         int i,k;

         k=sqrt(m);

         for(i=2;i<=k;i++)

         {

                   if(m%i ==0)

                   {

                            break;

                   }

         }

         if(i>k){

                   printf("%d is a zhishu\n",m);

         }

         else

                   printf("%d is not a zhishu\n",m);

}

 

 

int main(void)

{

                   const char *pSource1="   1  23              4 5\n67 89     ";

                   char a[] = "abdcd gdad gg";

                   char table[20]="0";

                   char *pTarget1;

                   pTarget1=table;

                           

                   charFiter(pSource1,pTarget1);

 

                   printf("pSource1:%s,   pTarget1:%s,\n",pSource1,pTarget1);

                  

                   //滤除空格

                   deleteSpace(a);

                   printf("%s\n",a);

                   //判断质数

                   zhishu(12);

                   return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

/******************************************************************************

****************************文件的读写操作*************************************

******************************************************************************/

/*从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个“#”为止*/

/*

int main(void)

{

         FILE *fp;

         char filename[10];

         char ch;

         printf("please intput a filename\n");

         scanf("%s",filename);

 

         if((fp = fopen(filename,"w")) == NULL)

         {

                   printf("can not open!\n");

                   exit(0);

         }

        

         ch=getchar();

         ch=getchar();

         while(ch != '#')

         {

                   fputc(ch,fp);

                   putchar(ch);

                   ch=getchar();

        

         }

 

  putchar(10);

         fclose(fp);

}

*/

 

/*将一个磁盘文件中的信息复制到另一个磁盘文件中*/

 

/*

int main(void)

{

         FILE *in,*out;

         char filename1[10];

         char filename2[10];

         char ch;

         printf("please intput a filename\n");

         scanf("%s",filename1);

 

         printf("please intput a filename\n");

         scanf("%s",filename2);

 

         if((in = fopen(filename1,"r")) == NULL)

         {

                   printf("can not open!\n");

                   exit(0);

         }

 

         if((out = fopen(filename2,"w")) == NULL)

         {

                   printf("can not open!\n");

                   exit(0);

         }

        

         while(! feof(in) )

         {

                   fputc(fgetc(in),out);

         }

         fclose(in);

         fclose(out);

 

}

*/

 

/*fread(buffer,size,count,fp) ,fwrite(buffer,size,count,fp)*/

 

#define SIZE 4

struct student_type

{

         char name[10];

         int num;

         int age;

         char addr[15];

}stud[SIZE];

 

void save()

{

         FILE *fp;

         int i;

         if((fp=fopen("stu_dat","wb")) ==NULL)

         {

                   printf("can not open file \n");

                   return ;

         }

         for(i=0;i<SIZE;i++)

         {

                   if(fwrite(&stud[i],sizeof(struct student_type),1,fp) != 1 )

                   {

                            printf("file write error\n");

                   }

                  

         }

         fclose(fp);

}

void load()

{

         FILE *fp;

         int i;

         if((fp=fopen("stu_dat","rb")) ==NULL)

         {

                   printf("can not open file \n");

                   return ;

         }

         for(i=0;i<SIZE;i++)

         {

                   if(fread(&stud[i],sizeof(struct student_type),1,fp) != 1 )

                   {

                            if(feof(fp)) {fclose(fp);return;}

                            printf("file read error\n");

                   }

                  

         }

         fclose(fp);

 

}

 

int main(void)

{

         load();

         save();

         return;

 

}

 

//rewind(fp1);//使位置指针重新返回文件的开头,此函数没有返回值

 

void main()

{

         FILE *fp1,*fp2;

         int i;

         if((fp=fopen("file1","r")) ==NULL)

         {

                   printf("can not open file \n");

                   return ;

         }

 

         if((fp=fopen("file2","w")) ==NULL)

         {

                   printf("can not open file \n");

                   return ;

         }

 

         while(!feof(fp1))

         {

                   putchar(fgetc(fp1));

         }

 

         rewind(fp1);//使位置指针重新返回文件的开头,此函数没有返回值

 

         while(!feof(fp1))

         {

                   fputc(fgetc(fp1),fp2);

         }

 

         fclose(fp1);

         fclose(fp2);

 

}

//fseek(fp,位移量(long),起始点

fseek(fp,100L,0) //将位置指针移到离文件头100个字节处

fseek(fp,50L,1) //将位置指针移到离当前位置50个字节处

fseek(fp,-10L,2)//将位置指针从文件末尾处向后退10个字节

ftell(fp) //返回当前位置

 

 

 

 

 

*

1分2分5分的硬币,组成1角,共有多少种组合。

*/

 

#include <iostream>

using namespace std;

 

int solve(int total);

 

int main()

{

    int total = 10;

    int count;

    count = solve(total);

    cout << "=========================" << endl;

    cout << "共有" << total << "种解法!"<< endl;

    system("PAUSE");

    return 0;

}

 

int solve(int total)

{

    int max1 = total / 5 + 1;

    int i, j, k, max2, count;

    count = 0;

 

    for (i = 0; i < max1; i++)

    {

        max2 = (total - i * 5) / 2 + 1;

        for (j = 0; j < max2; j++)

        {

            k = (total - i * 5 - j * 2) / 1;

            if (i || j || k)

            {

                count++;

                cout << " 有 " << i << "个5,"

                     << " 有 " << j << "个2,"

                     << " 有 " << k << "个1."

                     << endl;

            }

        }

    }

    return count;  

}

 

/*

输出结果:

 有 0个5, 有 0个2, 有 10个1.

 有 0个5, 有 1个2, 有 8个1.

 有 0个5, 有 2个2, 有 6个1.

 有 0个5, 有 3个2, 有 4个1.

 有 0个5, 有 4个2, 有 2个1.

 有 0个5, 有 5个2, 有 0个1.

 有 1个5, 有 0个2, 有 5个1.

 有 1个5, 有 1个2, 有 3个1.

 有 1个5, 有 2个2, 有 1个1.

 有 2个5, 有 0个2, 有 0个1.

=========================

共有10种解法!

*/

        采用指针存储字符串,其实质就是把字符串的首地值附给基类型为char的指针变量,从而可以从字符串首元素开始对字符串进行操作,这里面也存在一点问题.

 

        用这个类子给大家解释解释.

    

        int main()

         {

            char *p="hello world";

            p[0]='H';

            printf("%s\n",p);

            return 0;

          }

     运行结果会出现断错误,原因在于,*p="hello world" 这句仅仅声明了一个指针变量,指向字符串"hello world",而"hello world"这个字符串程序没有给它分配空间,编译器把它分配到常量区.而常量字符串的值是不允许被修改的 ,所以会出现断错误.

 

 

       程序改为如下就正确了

        int main()

        {

           char p[12]="hello world";

           char *p1=p;

           p1[0]='H';

           printf("%s\n",p1);

           return 0;

        }   

     

      原因在于,p[12]="hello world"是你自己定义的一个长度为12 的字符数组,所以字符串"hello world"编译器会给它分配空间(在栈中),所以你能修改它的值.

 

 

 

/*这个结构体的结果是16,4*/

union T {

   int i;

   struct N {

      int    j;

      float  k;

      double m;

   };

};

 printf("%d\n",sizeof(T.N));

printf("%d\n",sizeof(T));

/*这个结构体的结果是16,16*/

union T {

   int i;

   struct N {

      int    j;

      float  k;

      double m;

   }A;

};

 printf("%d\n",sizeof(T.N));

printf("%d\n",sizeof(T));

你可能感兴趣的:(嵌入式软件常见的笔试题(c))