實驗七及實驗結論


part——1

驗證性實驗1:要把文件添加到所創建的項目中,否則在當前路徑下找不到文件。更改代碼后,d盤中出現file3.txt文件,結果已轉換成大寫。

 

 驗證性實驗2:

 

 程序更改后運行結果正確。

 

 驗證性實驗三:

 

 屏幕上運行結果輸出正確,在當前路徑下生成了文本文件file3.dat,用記事本打開文件,數據信息正確,直觀可讀。

 

驗證性實驗4:

 

 屏幕正確輸出,咋當前路徑下生成了二進制文件file.dat,用記事本打開該文件,里面數據信息不能直觀可讀。

二進制文件和文本文件的區別:並無本質的區別,他們的區別在與被打開對應所需要的程序不同。

 

#include<stdio.h>
#include<stdlib.h>
int main(){
    FILE *fin;
    char ch;
    fin=fopen("file1.txt","r");
    if(!fin)
    {
        printf("fail to open file1.txt\n");
        exit(0);
    }
    ch=fgetc(fin);
    while(!feof(fin))
    {
      putchar(ch);
        ch=fgetc(fin);
    }
    fclose(fin);
    getchar();
}

 

 

 

part——2編程練習

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int N = 10;

// 定義結構體類型struct student,並定義其別名為STU 
typedef struct student {
    long int id;
    char name[20];
    float objective;    /*客觀題得分*/
    float subjective;    /*操作題得分*/
    float sum;
    char level[10];    
}STU; 

// 函數聲明
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);

int main() {
    STU stu[N];
    
    printf("錄入%d個考生信息: 准考證號,姓名,客觀題得分(<=40),操作題得分(<=60)\n", N); 
    input(stu, N);
    
    printf("\n對考生信息進行處理: 計算總分,確定等級\n");
    process(stu, N);
    
    printf("\n打印考生完整信息: 准考證號,姓名,客觀題得分,操作題得分,總分,等級\n");
    output(stu, N); 
    
    system("pause");
    return 0;
} 

// 從文本文件examinee.txt讀入考生信息:准考證號,姓名,客觀題得分,操作題得分
void input(STU s[], int n) {
    FILE *fin;
    int i;
    fin=fopen("examinee.txt","r");
    if(!fin){//如果文件為空
        printf("fail to open examinee.txt\n");
        exit(0);
    }
    for(i=0;i<n;i++)
        fscanf(fin,"%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective);

    fclose(fin);
}

// 輸出考生完整信息: 准考證號,姓名,客觀題得分,操作題得分,總分,等級
// 不僅輸出到屏幕上,還寫到文本文件result.txt中 
void output(STU s[], int n) {
    FILE *fout;
    int i;
    fout=fopen("result.txt","w");
    if(!fout){//如果文件為空或者創建失敗
        printf("fail to open result.txt\n");
        exit(0);
    }
    for(i=0;i<n;i++){
         printf("%ld %10s %2.2f %10.2f %10.2f %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
fprintf(fout,
"%ld %10s %2.2f %10.2f %10.2f %5s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); } fclose(fout); } // 對考生信息進行處理:計算總分,排序,確定等級 void process(STU s[], int n) { int i,j; STU temp; for(i=0;i<n;i++)s[i].sum=s[i].subjective*0.6+s[i].objective*0.4; for(i=0;i<n-1;i++) for(j=0;j<n-i-1;j++) if(s[j].sum<s[j+1].sum) { temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } int a=n*0.1-1; int b=n*0.5-1; for(i=0;i<n;i++) { if(i<=a) strcpy(s[i].level,"優秀"); else if(i>a&&i<=b) strcpy(s[i].level,"合格"); else strcpy(s[i].level,"不合格"); } }

 

 

實驗總結和體會:

1、printf函數用的時候少寫了f檢查半天都沒看出來,還是點錯誤行點出來的。

2、對於循環的語句,有時候就會把語句放錯位置,還需要加強。

3、文件不難,定義,打開,規定使用范圍,最后關閉就ok。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM