0
点赞
收藏
分享

微信扫一扫

2013-7-22学习C面试题

陌岛 2023-05-05 阅读 58


1.   编程:计算班级学生平均成绩和不及格人数。

#include <stdio.h>
 int main(int argc, constchar * argv[])
 {
 
     int stu[10]={99,98,55,96,95,94,93,92,91,90};
     int sum=0;
     int count=0;
     float avr=0;
     for (int i=0; i<10; i++) {
         sum+=stu[i];
         if (60>stu[i]) {
             count++;
         }
     }
     avr = (float)sum / 10;
     printf("学生平均成绩是:%f,不及格人数是:%d",avr,count);
     return0;
 }

2.   编程,用条件表达式实现三个整数从大到小排序。

#include <stdio.h>
 int main(int argc, constchar * argv[])
 {
 
     int a=10,b=20,c=30;
     int max,mid,min;
     max=a>b?(a>c?a:c):(b>c?b:c);
     mid=a>b?(a<c?a:c>b?c:b):(b<c?b:c);
     min=a<b?(a<c?a:c):(b<c?b:c);
     printf("max=%d,mid=%d,min=%d",max,mid,min);
     return0;
 }

3.   请说明三种循环语句的异同点。

     三种循环分别是while;do while 和 for

     相同点都是实现了一段逻辑循环操作,都具有以下三条内容:

     (1)循环体的设计

     (2)循环条件的设计

     (3)循环入口的初始化工作

     不同点:while是先进行判断,然后执行循环体;do while是先进行循环体然后再进行判断;for也是先进行判断然后进入循环体,结束后,进行++操作

     for循环一般用在循环次数固定的情况下

4.   请举例说明 break、continue 语句的区别。

      break//终止当前循环       contine//跳过此次循环

#include <stdio.h>
 int main(int argc, constchar * argv[])
 {
 
     for (int i=0; i<4; i++) {
         for (int j=0; j<4; j++) {
             if (i==1) {
                 continue;
             }
             if (j==2) {
                 break;
             }
             printf("%d   %d\n",i,j);
         }
     }
     return0;
 }

结果:

0   0

0   1

2   0

2   1

3   0

3   1

5.   用三种循环语句编写程序实现:计算 100 以内的整数之和 。

     

for循环:

#include <stdio.h>
 int main(int argc, constchar * argv[])
 {
 
     int sum=0;
     for (int i=0; i<=100; i++) {
         sum+=i;
     }
     printf("100以内的整数的和:%d",sum);
     return0;
 }

while循环:

#include <stdio.h>
 int main(int argc, constchar * argv[])
 {
 
     int sum=0;
     int i=0;
     while (i<=100) {
         sum+=i;
         i++;
     }
     printf("100以内的整数的和:%d",sum);
     return0;
 }

do while循环:

#include <stdio.h>
 int main(int argc, constchar * argv[])
 {
 
     int sum=0;
     int i=0;
     do {
         sum+=i;
         i++;
     }while (i<=100);
     printf("100以内的整数的和:%d",sum);
     return0;
 }

一、请填写BOOL , float, 指针变量 与“零值”比较的 if 语句。(10分)

提示:这里“零值”可以是0, 0.0 , FALSE或者“空指针”。例如 int 变量 n 与“零值”比较的 if 语句为:

    if ( n == 0 )

    if ( n != 0 )

以此类推。

 

请写出 BOOL  flag 与“零值”比较的 if 语句:

 if(FALSE == flag)[错误]

 if(flag)或者if(!flag)

请写出 float  x 与“零值”比较的 if 语句:

 

const double EPSILON = 1.00e-07;
 
 if (x<abs(EPSILON))
 {
 
 }

 

请写出 char  *p 与“零值”比较的 if 语句:

 

 if(NULL == p)或者if(NULL != p)

 

二、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)

 

char  str[] = “Hello” ;
        char   *p = str ;
 int     n = 10;
 请计算
 sizeof (str ) = 5[错]     6[对]          char str[8] = "Hello"   sizeof(str) =8
         
 sizeof ( p ) =  8    
          
 sizeof ( n ) = 4
 void Func ( char str[100])
 {
 请计算
  sizeof( str ) = 8  //相当于指针
 }
  
 void *p = malloc( 100 );
 请计算
 sizeof ( p ) =8

 

三、简答题(25分)

 

1、头文件中的 ifndef/define/endif 干什么用?

 

 防止引用该头文件的时候重复定义

 

2、#include  <filename.h>   和  #include  “filename.h” 有什么区别?

 

 第一种是指从系统文件中去找filename.h文件;第二种是从当前文件中去找filename.h的文件

 

3、const 有什么用途?(请至少说明两种)

 1.const修饰常量,该变量不可修改,例如const double PI=3.14

 2.常指针,const放在不同的地方有不同的效果,例如:

const int *p 和 int const *p 效果是一样的,指针所指向的内容是不可以变的,指针指向可以变

const int *p 和 int const *p指针指向不可变,但指针指向的内容可以变

int const * const p  指针指向和内容都不可以变

4、在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?

 

 首先,作为extern是C/C++语言中表明函数和全局变量作用范围(可见性)的关键字,该关键字告诉编译器,其声明的函数和变量可以在本模块或其它模块中使用。

被extern "C"修饰的变量和函数是按照C语言方式编译和连接的

 

 

5、请简述以下两个for循环的优缺点

 

// 第一个
 for (i=0; i<N; i++)
 {
 if (condition)
     DoSomething();
 else
     DoOtherthing();
 }
 // 第二个
 if (condition)
 {
 for (i=0; i<N; i++)
     DoSomething();
 }
 else
 {
     for (i=0; i<N; i++)
     DoOtherthing();
 }

优点:

 如果N比较小,第一种方法代码量少

 

缺点:

 

 耗时间

 

优点:

 节省时间

 

缺点:

 受客观条件的制约,有可能condition在DoSomething()中被修改

 

 

四、有关内存的思考题(20分)

 

void GetMemory(char *p)
 {
 p = (char *)malloc(100);
 }
 void Test(void)
 {
 char *str = NULL;
 GetMemory(str); 
 strcpy(str, "hello world");
 printf(str);
 }

请问运行Test函数会有什么样的结果?

答:程序有错误!

修改:

方法一:
 void GetMemory(char **p)
 {
      p=(char *)malloc(100);
 }
 void Test(void)
 {
      char *str = NULL;
      GetMemory(&str);
      strcpy(str,"hello world");
      printf(str);
 }
 方法二:
 char * GetMemory(char *p)
 {
      p=(char *)malloc(100);
      return p;
 }
 void Test(void)
 {
      char *str = NULL;
      str = GetMemory(str);
      strcpy(str,"hello world");
      printf(str);
 }
  
  
  
 char *GetMemory(void)
 { 
 char p[] = "hello world";
 return p;
 }
 void Test(void)
 {
 char *str = NULL;
 str = GetMemory();  
 printf(str);
 }

请问运行Test函数会有什么样的结果?

答:有错误

虽然地址传过来了,但是“hello world”值没有传过来           为什么? 地址和strlen大小都一样

Void GetMemory2(char **p, int num)
 {
 *p = (char *)malloc(num);
 }
 void Test(void)
 {
 char *str = NULL;
 GetMemory(&str, 100);
 strcpy(str, "hello"); 
 printf(str);  
 }

请问运行Test函数会有什么样的结果?

答:

 能够输出hello

void Test(void)
 {
 char *str = (char *) malloc(100);
     strcpy(str, “hello”);
     free(str);    
     if(str != NULL)
     {
       strcpy(str, “world”);
 printf(str);
 }
 }

请问运行Test函数会有什么样的结果?

答:

 free了空间以后,str不知道指向哪儿了,可能为空,也可能不为空,所以这样写不稳定

 

 

 

 

五、编写strcpy函数(10分)

已知strcpy函数的原型是

       char *strcpy(char *strDest, const char *strSrc);

       其中strDest是目的字符串,strSrc是源字符串。

 

(1)不调用C++/C的字符串库函数,请编写函数 strcpy

 

char *strcpy(char *strDest,constchar *strSrc)
 {
     
     if ((strDest == NULL)||(strSrc==NULL)) {
         returnNULL;
     }
     char *strDestCopy = strDest;
     while ((*strDest++=*strSrc++)!='\0');
     return strDestCopy;
 }

 

(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

 

 返回赋值完成后的字符串,以便在外部函数进行赋值或者打印出来

 实现链式表达式

 

六、编写类String的构造函数、析构函数和赋值函数(25分)

已知类String的原型为:

class String
     {
       public:
         String(const char *str = NULL);    // 普通构造函数
         String(const String &other);        // 拷贝构造函数
         ~ String(void);                        // 析构函数
         String & operate =(const String &other);    // 赋值函数
       private:
         char      *m_data;                // 用于保存字符串
     };
        请编写String的上述4个函数。
 
 #include <iostream>
 usingnamespacestd;
 #include <string.h>
 
 class String1
 {
 public:
     String1(constchar *str = NULL) //普通构造函数
     {
         if (str==NULL) {
             m_data = newchar[1];
             m_data = '\0';
         }
         //delete []m_data;
         else
         {
             m_data = newchar[strlen(str)+1];
             strcpy(m_data, str);
         }
     }
     String1(constString1 &other) //拷贝构造函数
     {
/*
         if (&other == this) {
             cout<<"不能为复制本身";
         }*/不需要写这个  因为不可能自己作为参数传递给自己,应为自己还没创建好
         if (this->m_data == other.m_data) {
             cout<<"不要赋值相等的值";
         }
         else
         {
             if (m_data) {
                 delete []m_data;
             }
             m_data = newchar [strlen(other.m_data)+1];
             strcpy(m_data, other.m_data);
         }
     }
     ~String1(void)
     {
         if (m_data) {
             delete []m_data;
         }
     }
     String1 &operate = (const String1 &other)
     {
         if(this == &other)
         {
             return *this;
         }
         delete []m_data;
         m_data = newchar[strlen(other.m_data)+1];
         strcpy(m_data,other.m_data);
     };
 private:
     char * m_data;//用于保存字符串
 };
 
 int main(int argc, constchar * argv[])
 {
 
     String1 s="ding";
     String1 ss= s;
     cout<<ss;
     return0;
 }


栈和堆的区别:

栈是自动释放,堆是手动释放

局部变量都是存放在栈上面的,new,malloc等新建出来的变量是存在堆上的

栈容量比较小,堆容量比较大


举报

相关推荐

0 条评论