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))!=‘\n‘)&&ch!=EOF)
36             putc(ch, stdout);
37         if(ch==‘\n‘)
38             putc(ch, stdout);
39         while(((bp=getc(fc))!=‘\n‘)&&bp!=EOF)
40             putc(bp, stdout);
41         if(ch==‘\n‘)
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))!=‘\n‘)&&ch!=EOF)
36             putc(ch, stdout);
37         while(((bp=getc(fc))!=‘\n‘)&&bp!=EOF)
38             putc(bp, stdout);
39         if((bp==‘\n‘)||(ch==‘\n‘))
40             printf("\n");
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\n", name1, count[0], *argv[1]);
25         printf("%s has %d %c\n", 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\n", 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\n", 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\n");
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))!=‘\n‘)&&ch!=EOF&&ch!=‘\r‘)
20             fprintf(stdout, "%c", ch);
21         if(ch==EOF)
22             exit;
23         printf("\n");
24         puts("enter an offset of file(q to quit)");
25     }
26
27     fclose(fp);
28     system("pause");
29     return 0;
30 }
时间: 2024-10-29 10:46:48

c primer plus 习题答案(6)的相关文章

c++ primer plus 习题答案(1)

第五版c++ primer plus,IDE是visual studio2013. p180.2 1 #include<iostream> 2 #include<ctime> 3 #include<cstdlib> 4 5 int main(void){ 6 using namespace std; 7 int i, j, count=0, sum = 0, pt[10]; 8 double mean; 9 for (i = 0; i < 10; i++){ 10

c primer plus 习题答案(4)

p319.3 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<ctype.h> 4 #include<string.h> 5 void deliver(char *a1, char *a2, int n); 6 7 int main(void) 8 { 9 int n; 10 char str1[81], *ptr, ar[81]; 11 ptr=gets(str1); 12 n=strlen(ptr)

c++ primer plus 习题答案(2)

p259.4 1 #include<iostream> 2 #include<cstdlib> 3 #include<cctype> 4 using namespace std; 5 struct stringy{ 6 char* str; 7 int ct; 8 }; 9 void set(stringy &, const char*); 10 void show(const stringy &, int times = 1); 11 void sho

c primer plus 习题答案(3)

p281.2 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define SIZE 5 4 void copy_arr(double ar[], double pr[],int n); 5 void copy_ptr(double ar[], double pr[], int n); 6 7 int main(void) 8 { 9 double source[5]={1.1, 2.2, 3.3, 4.4, 5.5}; 10 double

c primer plus 习题答案(5)

p352.6 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main(void) 6 { 7 int i, j, count[10]={0}, seed[10]; 8 9 for(i=0; i<10; i++){ 10 printf("please enter a seed.\n"); 11 scanf("%d", &see

c primer plus 习题答案(7)

p421.5 1 #include<stdio.h> 2 #include<stdlib.h> 3 #define CSIZE 4 4 #define SCORE 3 5 6 void get_info(struct students *p); 7 void get_average(struct students *p); 8 void print_info(struct students *p); 9 void print_class_average(struct student

c++ primer plus 习题答案(8)

p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; public: Cd(char *s1, char *s2, int n, double x); Cd(const Cd & st); Cd(); virtual ~Cd(); virtual void Report()const; Cd & operator = (const Cd & s

《C++primer》v5 第5章 语句 读书笔记 习题答案

5.1 空语句只有一个";".如果什么也不想做可以使用空语句. 5.2 用花括号{}括起来的叫块,也叫复合语句.有多条语句作用在同一个作用域时,需要用花括号括起来. 5.3 降低了. 5.4 (a)每次迭代时候会初始化iter,但是iter缺少初值,所以这段代码根本不会通过编译.另外这里的括号需要一个bool类型的,而定义迭代器根本不会返回一个bool类型.假如上面那些问题都可以通过,每次迭代都会初始化这个iter,会导致死循环. (b)我试了一下编译未通过是因为没找到适合的find函

《C++primer》v5 第3章 字符串、向量和数组 读书笔记 习题答案

3.1略 3.2 string str; //读行 while(getline(cin,str)) cout<<str<<endl; //读单个词 while(cin>>str) cout<<str<<endl; 3.3 输入运算符读到空白符结束 getline读到换行符结束,并丢弃换行符 3.4 比较大小. 比较大小是比较的第一个不相同的字符的大小. int main() { string a,b; cin>>a>>b;