C語言程序設計第五版譚浩強課後答案 第十章習題答案

C語言程序設計第五版譚浩強課後答案

最下方有所有答案的總目錄
1.什麼是文件型指針?通過文件指針訪問文件有什麼好處?

  • 答:緩衝文件系統中,關鍵的概念是“文件類型指針”,簡稱“文件指針”。每個被使用的文件都在內存中開闢一個相應的文件信息區,用來存放文件的有關信息(如文件的名字、文件狀態及文件當前位置等)。這些信息是保存在一個結構體變量中的。該結構體類型是由系統聲明的,取名爲FILE。

  • 通過文件指針訪問文件的好處是:可以隨機訪問文件,有效表示數據結構,動態分配內存,方便使用字符串,有效使用數組。

2.對文件的打開與關閉的含義是什麼?爲什麼要打開和關閉文件?

  • 答:”打開“是指爲文件建立相應的信息區(用來存放有關文件的信息)和文件緩衝區(用來暫時存放輸人輸出的數據)。

  • ”關閉“是指撤銷文件信息區和文件緩衝區,使文件指針變量不再指向該文件,顯然就無法進行對文件的讀寫了。

3.從鍵盤輸入一個字符串,將其中的小寫字母全部轉換成大寫字母,然後輸出到一個磁盤文件test中保存,輸入的字符串以“!”結束。

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
	FILE *fp = NULL;
	char c;
	int i;
	
	if ( (fp=fopen("test", "w")) == NULL ) {
		printf("open file test error!\n");
		exit(EXIT_FAILURE);
	}

	while ( (c=getchar()) != EOF && c != '!' ) {
		if ( c>='a' && c<='z' )
			c = c-'a' + 'A';
		fputc(c, fp);
	}

	fclose(fp);
}

結果:

輸入 : 123我的AbcABC!
test文件的內容 : 123我的ABCABC

4.有兩個磁盤文件A和B,各存放一行字母,今要求把這兩個文件中的信息合併(按字母順序排列),輸出到一個新文件C中去。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void swap(char *s, int i, int j) {
    char t = s[i];
    s[i] = s[j];
    s[j] = t;
}

void select_sort(char *str) {
    int i, j;
    int len = strlen(str);
    for (i=0; i<len; i++) {
        int min = i;
        for (j=i+1; j<len; j++) {
            if ( str[j] < str[min] )
                min = j;
        }   
        swap(str, min, i); 
    }   
}

int main( void ) { 
    FILE *fa, *fb, *fc;
    char buf[1024] = {0};

    fa = fopen("A", "r");
    fb = fopen("B", "r");
    fc = fopen("C", "w");

    fgets(buf, 1024, fa);
    int len = strlen(buf);
    fgets(buf+len, 1024-len, fb);
    select_sort(buf);
    fputs(buf, fc);

    fclose(fa);
    fclose(fb);
    fclose(fc);
}

5.有5個學生,每個學生有3門課程的成績,從鍵盤輸人學生數據(包括學號,姓名,3門課程成績),計算出平均成績,將原有數據和計算出的平均分數存放在磁盤文件stud中。

#include <stdio.h>
#include <stdlib.h>

struct student {
    int num;
    char name[32];
    int score[3];
    float avg;
};

int main( viod ) { 
    int i;
    struct student stu[5];
    FILE *fp = NULL;
        
    for (i=0; i<5; i++) {
        printf("num name score1 score2 score3:\n");
        scanf("%d %s %d %d %d", &stu[i].num, &stu[i].name, 
            &stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
        stu[i].avg = (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
    }   

    if ( (fp=fopen("stud", "wb")) == NULL ) { 
        printf("open file stud for write error\n");
        return 1;
    }

    if ( fwrite(stu, sizeof(stu), 1, fp) != 1 ) {
        printf("write error\n");
        return 1;
    }
    fclose(fp);
}

測試程序查看輸入文件的容:

1 zhagnsan 10 20 30 20.000000
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000
4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000

6.將第5題stud文件中的學生數據,按平均分進行排序處理,將已排序的學生數據存入一個新文件stu_ sort 中。

#include <stdio.h>
#include <stdlib.h>

struct student {
    int num;
    char name[32];
    int score[3];
    float avg;
};

void sort(struct student stu[], int len) {
    int i, j;
    struct student tmp;
    for (i=0; i<len; i++) {
        int min = i;
        for (j=i+1; j<len; j++) {
            if ( stu[j].avg > stu[min].avg )
                min = j;
        }   
        tmp = stu[min];
        stu[min] = stu[i];
        stu[i] = tmp;
    }   
}

int main( viod ) {
    int i;
    struct student stu[5];
    FILE *fp = NULL;
    if ( (fp=fopen("stud", "rb")) == NULL ) {
        printf("open file stud for read error\n");
        return 1;
    }

    if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
        printf("write error\n");
        return 1;
    }
    fclose(fp);

    sort(stu, 5);

    FILE *fw = fopen("stu_sort", "wb");
    fwrite(stu, sizeof(stu), 1, fw);
    fclose(fw);
}

測試程序,查看文件內容,確實排過序:

4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000
1 zhagnsan 10 20 30 20.000000
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000

7.將第6題已排序的學生成績文件進行插人處理。插人一個學生的3門課程成績,程序先計算新插人學生的平均成績,然後將它按成績高低順序插入,插入後建立一個新文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
    int num;
    char name[32];
    int score[3];
    float avg;
};

void sort(struct student stu[], int len) {
    int i, j;
    struct student tmp;
    for (i=0; i<len; i++) {
        int min = i;
        for (j=i+1; j<len; j++) {
            if ( stu[j].avg > stu[min].avg )
                min = j;
        }   
        if ( min != i ) { 
            tmp = stu[min];
            stu[min] = stu[i];
            stu[i] = tmp;
        }   
    }   
}
int main( viod ) { 
    int i;
    struct student stu[5];
    FILE *fp = NULL;
    if ( (fp=fopen("stu_sort", "rb")) == NULL ) {
        printf("open file stud for read error\n");
        return 1;
    }

    if ( fread(stu, sizeof(stu), 1, fp) != 1 ) {
        printf("write error\n");
        return 1;
    }
    fclose(fp);

    struct student new_stu[6];
    memcpy(new_stu, stu, sizeof(stu));
    printf("num name score0 score1 score2:\n");
    scanf("%d %s %d %d %d", &new_stu[5].num, &new_stu[5].name, &new_stu[5].score[0],
            &new_stu[5].score[1], &new_stu[5].score[2]);
    new_stu[5].avg = (new_stu[5].score[0]+new_stu[5].score[1]+new_stu[5].score[2])/3.0;
    sort(new_stu, 6);

    FILE *fw = fopen("tmp_sort", "wb");
    fwrite(new_stu, sizeof(new_stu), 1, fw);
    fclose(fw);
}

查看tmp_sort文件,確實插入和排序了:

4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000
1 zhagnsan 10 20 30 20.000000
8 hehe 12 3 4 6.333333
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000

8.將第7題結果仍存人原有的stu_sort 文件而不另建立新文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
    int num;
    char name[32];
    int score[3];
    float avg;
};

int main( viod ) { 
    int i;
    struct student stu[6];
    FILE *fp = NULL;
    if ( (fp=fopen("tmp_sort", "rb")) == NULL ) { 
        printf("open file stud for read error\n");
        return 1;
    }   

    if ( fread(stu, sizeof(stu), 1, fp) != 1 ) { 
        printf("write error\n");
        return 1;
    }   
    fclose(fp);

    FILE *fw = fopen("stu_sort", "wb");
    fwrite(stu, sizeof(stu), 1, fw);
    fclose(fw);
}

查看原本的stu_sort文件:

4 zhaoliu 90 80 10 60.000000
5 sunqi 90 1 1 30.000000
1 zhagnsan 10 20 30 20.000000
8 hehe 12 3 4 6.333333
2 lisi 3 6 9 6.000000
3 wangwu 1 2 3 2.000000

9.有一磁盤文件employee,內存放職工的數據。每個職工的數據包括職工姓名、職工號、性別、年齡、住址、工資、健康狀況、文化程度。今要求將職工名、工資的信息單獨抽出來另建一個簡明的職工工資文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct employee {
    int  num;      // 編號
    char name[32];
    char sex[4]; 
    int  age;
    char addr[60];
    int  salary;   
    char health[10]; // 健康狀況
    char class[10];  // 文化程度
};

struct emp {
    char name[32];
    int salary;
};

int main( void ) { 
    int i;
    FILE *fp1, *fp2; 
    struct emp emp_arr[5];
    struct employee employee_arr[5];

    fp1=fopen("employee", "rb");
    fread(employee_arr, sizeof(employee_arr), 1, fp1);
    fclose(fp1);

    for (i=0; i<5; i++) {
        strcpy(emp_arr[i].name, employee_arr[i].name);
        emp_arr[i].salary = employee_arr[i].salary;
    }

    fp2=fopen("emp", "wb");
    fwrite(emp_arr, sizeof(emp_arr), 1, fp2);
    fclose(fp2);
}

查看emp文件的內容如下:

abc 1800 
def 2000 
hehe 3000 
haha 2800 
ggg 2500 

10.從第9題的“職工工資文件”中刪去一個職工的數據,再存回原文件。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct emp {
    char name[32];
    int salary;
};

int main( void ) { 
    int i;
    FILE *fp;
    char name[32]; 
    struct emp emp_arr[5];

    fp=fopen("emp", "rb");
    fread(emp_arr, sizeof(emp_arr), 1, fp);
    fclose(fp);

    printf("name:");
    scanf("%s", &name);
    fp=fopen("emp", "wb");
    for (i=0; i<5; i++) {
        if ( strcmp(emp_arr[i].name, name) == 0 ) { 
            continue;
        }   
        fwrite(&emp_arr[i], sizeof(emp_arr[i]), 1, fp);
    }   
    fclose(fp);
}

刪除ggg後的源文件內容:

abc 1800 
def 2000 
hehe 3000 
haha 2800 

11.從鍵盤輸人若干行字符(每行長度不等),輸人後把它們存儲到一磁盤文件中。再從該文件中讀入這些數據,將其中小寫字母轉換成大寫字母后在顯示屏上輸出。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void ) { 
    int i;
    FILE *fp = fopen("tmp.txt", "w");
    char buf[1024] = {}; 
    
    while ( fgets(buf, 1024, stdin) != NULL ) { 
        fputs(buf, fp);
        memset(buf, 0x00, sizeof(buf));
    }   
    fclose(fp);

    fp = fopen("tmp.txt", "r");
    while ( !feof(fp) ) { 
        memset(buf, 0x00, sizeof(buf));
        fgets(buf, 1024, fp);
        for (i=0; buf[i] != '\0'; i++) {
            if ( buf[i]>='a' && buf[i]<='z' )
                printf("%c", buf[i]-32);
            else
                printf("%c", buf[i]);
        }   
    }   
    fclose(fp);
}

執行結果:

輸入:
this is maomaochong
litao love IDFD
1243
輸出:
THIS IS MAOMAOCHONG
LITAO LOVE IDFD
1243

C語言程序設計第五版譚浩強課後習題答案 第一章
C語言程序設計第五版譚浩強課後習題答案 第二章
C語言程序設計第五版譚浩強課後習題答案 第三章
C語言程序設計第五版譚浩強課後習題答案 第四章
C語言程序設計第五版譚浩強課後習題答案 第五章
C語言程序設計第五版譚浩強課後習題答案 第六章
C語言程序設計第五版譚浩強課後習題答案 第七章
C語言程序設計第五版譚浩強課後習題答案 第八章
C語言程序設計第五版譚浩強課後習題答案 第九章
C語言程序設計第五版譚浩強課後習題答案 第十章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章