研究生的一份试题的几道题节选

作者: 知识学者 | 来源:发表于2017-12-23 12:43 被阅读94次

    首先祝朋友考研成功,勇往直前,我是不考研的,所以完全以提高能力,使用为主,不在意细节。小伙伴让我帮忙看了一下试卷,故截取了几道题目。

    c我是真的应了那句话,从入门到放弃,没有字符串,没有API,没有好类库。。。

    1.0 下列语句可以正确进行字符串赋值 ( D )

    A char *s = "good";  *s = "good";       B   char s1[10]; s1 = "good";
    C char s3[10]; *s3 = "good";              D   char *s = "good";
    

    c语言没有字符串类型,只能通过数组,或者指针实现字符串。数组名是字符串的首地址,字符串复制二种形式。
    char *ch="sddf"; //指针。
    char ch[]="dfffg"; //数组的初始化。

    2.0 下列函数功能是( )

    int f(char *a, char *b)
    {
        while (*a++ == *b++)
        {
    
        }
        return 0;
    }
    

    编译器跑一下,其实根据返回值int可以推测一下内容

    #include<stdio.h>
    int f(char *, char *b);
    int main()
    {
    
        char *p1="abc", *p2 = "def";
        int num1=f(p1, p2);
        int num2 = f(p2, p1);
        printf("%s\n", p2);
        printf("num1=%d\n", num1);
        printf("num2=%d\n", num2);
        return 0;
    }
    
    int f(char *a, char *b)
    {
        while (*a++ == *b++)
        {
    
        }
        return 0;
    }
    
    def
    num1=0
    num2=0
    请按任意键继续. . .
    

    所以这东西,好像什么都没有干

    3.0定义 char *aa="123456"; 则执行语句 printf("%c",*aa++); 后,正确的输出是:

    printf("%c",*aa++); *的优先级高于++,所以就是数组aa的首元素,(数组名是首元素地址。)

    #include <stdio.h>
    
    int main() {
    char *aa="123456";
        printf("%c\n",*aa++);
        return 0;
    }
    
    

    让编译器跑一下,就知道了,很不喜欢这种题目,考优先级,和printf函数知识点。

    D:\untitled2\cmake-build-debug\untitled2.exe
    1
    
    Process finished with exit code 0
    
    

    4.0有二个磁盘文件A和B,各存放一行字母,要求使用c语言工具把这二个文件中的信息合并(按照字母顺序排列),输出到一个新文件C中。

    思路是读取二个文件的字符串,把它们放入字符数组中,在排序,在写入文件

    题目思路不难,但是细节太多,我是c从入门到放弃,对c中好多函数不熟悉,对c的文件操作也不怎么熟悉,一边查询资料,一边写。其中主要FILE 的指针,会移动的,在进行相关操作时候,一定要用 rewind()函数,使文件的位置指针回到开头,我在这里花了不少时间,隐形的不过,只要一步一步测试,才发现问题出在这里。

    #include <stdio.h>
    #include <stdlib.h>
    #include <mem.h>
    
    int longFife(FILE *fp);
    void sort(char ch[] ,int n);
    void arry(FILE *fp1,FILE *fp2,char *ch);
    void display(FILE *fp);
    void pr(char *ch ,int n);
    void sort(char *ch ,int n);
    int main() {
        FILE *fp,*fp1,*fp2;
        int len;
        char *p;
    
        if((fp1=fopen("d:\\a.txt","r+"))==NULL)
        {
            printf("open file a error");
            exit(0);
        }
    
        if((fp2=fopen("d:\\b.txt","r+"))==NULL)
        {
            printf("open file b error");
            exit(0);
        }
    
    len=longFife(fp1)+longFife(fp2);
    
      printf("file1:");
        rewind(fp1);
        display(fp1);
        printf("\n");
    
        printf("file2:");
        rewind(fp2);
        display(fp2);
    
        printf("\n");
        printf("the arry length=%d\n",len);
        p=(char *)malloc(sizeof(char)*(len+1));
         if(p!=NULL)
             printf("malloc arry  is succeed\n");
    
           arry(fp1,fp2,p);
         printf("\n");
         printf("before sort array:p=%s",p);
         printf("\n");
    
        if((fp=fopen("d:\\d.txt","w+"))==NULL)
        {
            printf("open file c error");
            exit(0);
        }
    
        sort(p,len);
        printf("after sort arry:p=%s",p);
        printf("\n");
    
         if(fputs(p,fp)!=EOF)
             printf("write file is succeed");
    
        printf("read this file:");
        rewind(fp);
        display(fp);
    
        free(p);
        fclose(fp1);
        fclose(fp2);
        fflush(fp);
        fclose(fp);
        free(p);
        return 0;
    }
    
    void  arry(FILE *fp1,FILE *fp2,char *ch)
    {
        rewind(fp1);
        rewind(fp2);
        int n1=longFife(fp1);
        int n2=longFife(fp2)+n1;
    
        int i,j;
        rewind(fp1);
       for(i=0; i<n1; i++)
       {
           ch[i]=fgetc(fp1);
       }
        rewind(fp2);
        for(j=n1;j<n2; j++)
        {
            ch[j]=fgetc(fp2);
        }
         ch[n2]='\0';
    
       printf("char array is succeed");
    }
    
    int longFife(FILE *fp)
    {
        int  len=0;
        char ch;
        ch=fgetc(fp);
        while(!feof(fp))
        {
            ch=fgetc(fp);
            len++;
        }
    
       return len;
    }
    
    void display(FILE *fp)
    {
        char ch;
        while(!feof(fp))
        {
          ch=fgetc(fp);
          putchar(ch);
        }
    
    }
    // 好久没有写排序了,选择了不熟悉的选择,写出了bug。
    void sort(char *ch ,int n)
    {
        int i,j;
        char min,temp;
        for(i=0; i<n-1; i++)
        {
            min=i;  //查找在第二个for循环
            for(j=i+1; j<n; j++)
            if(ch[j]<ch[min])
            {
               min=j;
            }
            if(min!=i)
            {
                temp=ch[i];//交换的元素在第一个for循环
                ch[i]=ch[min];
                ch[min]=temp;
            }
    
    
    
        }
    
    }
    
    void pr(char *ch ,int n)
    {
        int i;
        for(i=0; i<n; i++)
        {
           printf("%c",ch[i]) ;
    
        }
    printf("\n");
    }
    
    

    结果:

    D:\Clion\cmake-build-debug\Clion.exe
    file1:awouyt
    file2:rrtyyopmn
    the arry length=15
    malloc arry  is succeed
    char array is succeed
    before sort array:p=awouytrrtyyopmn
    after sort arry:p=amnooprrttuwyyy
    write file is succeedread this file:amnooprrttuwyyy
    Process finished with exit code 0
    
    

    c我是从入门到放弃了,结果还是要用到它

    相关知识点

    FILE是一个文件结构体,用它定义文件指针来实现对文件的操作。

    fopen函数打开文件
    格式 fopen(文件名,文件使用方式);
    打开发生错误,fopen函数将还回NULL。

    fclose(文件指针) 文件关闭,对文件进行读写操作后,必须将它关闭。

    fputc(char ch,FILE *fp), ch是要输出的字符,作用输出一个字符文件到磁盘文件fp中,如果输出失败则返回文件结束符EOF(值为-1)。

    fgetc(fp);从指定的一个文件读入一个字符,如果遇到文件结束符则返回符EOF(值为-1)。

    如果处理二进制文件,读入一个字节的二进制数据可能是-1,则这恰是EOF的值,为解决这个bug,用feof(FILE *fp)来解决,当遇到文件结尾feof(fp)值为0

    int fseek(FILE *stream, long offset, int whence);
    fseek 设置当前读写点到 offset 处, whence 可以是 SEEK_SET,SEEK_CUR,SEEK_END 这些值决定是从文件头、当前点和文件尾计算偏移量 offset。
    可以定义一个文件指针 FILE *fp,当你打开一个文件时,文件指针指向开头,你要指到多少个字节,只要控制偏移量就好,例如, 相对当前位置往后移动一个字节:fseek(fp,1,SEEK_CUR); 中间的值就是偏移量。 如果你要往前移动一个字节,直接改为负值就可以:fseek(fp,-1,SEEK_CUR)。

    咽喉发炎,还没有好,17年真是不顺,希望一切都能好起来。

    文章参考:
    C语言中字符串赋值处理方式
    C++ 抽象类
    文件操作,合并两个二进制文件为单独一个二进制文件;
    C 文件读写
    C语言实现两个文件合并

    相关文章

      网友评论

        本文标题:研究生的一份试题的几道题节选

        本文链接:https://www.haomeiwen.com/subject/koqtgxtx.html