zoukankan      html  css  js  c++  java
  • c primer plus 习题答案(6)

    p376.7

    A方案

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define LEN 40
     4 void rank(FILE *, FILE *);
     5 int main(void)
     6 {
     7     FILE *fp, *fc;
     8     int ch, bp;
     9     char file1[LEN], file2[LEN];
    10 
    11     puts("enter file1 name");
    12     if((fp=fopen(gets(file1), "r"))==NULL){
    13         fputs("can't open", stdout);
    14         exit(1);
    15     }
    16 
    17     puts("enter file2 name");
    18     if((fc=fopen(gets(file2), "r"))==NULL){
    19         fputs("can't open", stdout);
    20         exit(2);
    21     }
    22 
    23     rank(fp, fc);
    24     if((fclose(fp)!=0)||(fclose(fc)!=0))
    25         puts("error in closing files");
    26 
    27     system("pause");
    28     return 0;
    29 }
    30 
    31 void rank(FILE *fp, FILE *fc)
    32 {
    33     int ch, bp;
    34     while(1){
    35         while(((ch=getc(fp))!='
    ')&&ch!=EOF)
    36             putc(ch, stdout);
    37         if(ch=='
    ')
    38             putc(ch, stdout);
    39         while(((bp=getc(fc))!='
    ')&&bp!=EOF)
    40             putc(bp, stdout);
    41         if(ch=='
    ')
    42             putc(bp, stdout);
    43         if((ch==EOF)&&(bp==EOF))
    44             break;
    45     }
    46 }

    B方案

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define LEN 40
     4 void rank(FILE *, FILE *);
     5 int main(void)
     6 {
     7     FILE *fp, *fc;
     8     int ch, bp;
     9     char file1[LEN], file2[LEN];
    10 
    11     puts("enter file1 name");
    12     if((fp=fopen(gets(file1), "r"))==NULL){
    13         fputs("can't open", stdout);
    14         exit(1);
    15     }
    16 
    17     puts("enter file2 name");
    18     if((fc=fopen(gets(file2), "r"))==NULL){
    19         fputs("can't open", stdout);
    20         exit(2);
    21     }
    22 
    23     rank(fp, fc);
    24     if((fclose(fp)!=0)||(fclose(fc)!=0))
    25         puts("error in closing files");
    26 
    27     system("pause");
    28     return 0;
    29 }
    30 
    31 void rank(FILE *fp, FILE *fc)
    32 {
    33     int ch, bp;
    34     while(1){
    35         while(((ch=getc(fp))!='
    ')&&ch!=EOF)
    36             putc(ch, stdout);
    37         while(((bp=getc(fc))!='
    ')&&bp!=EOF)
    38             putc(bp, stdout);
    39         if((bp=='
    ')||(ch=='
    '))
    40             printf("
    ");
    41         if((ch==EOF)&&(bp==EOF))
    42             break;
    43     }
    44 }

    p376.8

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define LEN 40
     4 int main(int argc, char *argv[])
     5 {
     6     FILE *fp, *fc, *fd[LEN];
     7     char name1[LEN], name2[LEN];
     8     int ch, bp, count[2]={0}, i, num[LEN]={0};
     9     if(argc==2){
    10         puts("enter file1 name");
    11         if((fp=fopen(gets(name1), "r"))==NULL)
    12             fputs("can't open", stderr);
    13         puts("enter file1 name");
    14         if((fc=fopen(gets(name2), "r"))==NULL)
    15             fputs("can't open", stderr);
    16 
    17         while((ch=getc(fp))!=EOF)
    18             if(ch==argv[1][0])
    19                 count[0]++;
    20         while((bp=getc(fc))!=EOF)
    21             if(bp==argv[1][0])
    22                 count[1]++;
    23         
    24         printf("%s has %d %c
    ", name1, count[0], *argv[1]);
    25         printf("%s has %d %c
    ", name2, count[1], *argv[1]);
    26     }
    27 
    28     for(i=2; i<argc; i++){
    29         if((fd[i]=fopen(argv[i], "r"))==NULL){
    30             fputs("can't open
    ", stderr);
    31             continue;
    32         }
    33         while((ch=getc(fd[i]))!=EOF)
    34             if(ch==*argv[1])
    35                 num[i]++;
    36         printf("%s has %d %c
    ", argv[i], num[i], *argv[1]);
    37     }
    38 
    39     system("pause");
    40     return 0;
    41 }

    p376.10

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #define LEN 40
     4 int main(void)
     5 {
     6     FILE *fp;
     7     char name[LEN];
     8     long offset;
     9     int ch;
    10 
    11     puts("enter the file name");
    12     if((fp=fopen(gets(name), "r"))==NULL){
    13         fprintf(stderr, "can't open
    ");
    14         exit(1);
    15     }
    16     puts("enter an offset of file(q to quit)");
    17     while(scanf("%ld", &offset)==1){
    18         fseek(fp, offset, SEEK_SET);
    19         while(((ch=getc(fp))!='
    ')&&ch!=EOF&&ch!='
    ')
    20             fprintf(stdout, "%c", ch);
    21         if(ch==EOF)
    22             exit;
    23         printf("
    ");
    24         puts("enter an offset of file(q to quit)");
    25     }
    26 
    27     fclose(fp);
    28     system("pause");
    29     return 0;
    30 }
  • 相关阅读:
    windows下wchar_t* 转char*
    VS2010的调试参数/Zi /DEBUG
    fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
    使用opencv传中文文件崩溃
    【20160924】GOCVHelper综述
    编译ITK
    几款开源图像处理软件评测研究
    新注册域名greenopen.site,向专业道路进军
    openmp在图像处理上面的运用
    实现multbandblend
  • 原文地址:https://www.cnblogs.com/coding-time/p/4526661.html
Copyright © 2011-2022 走看看