高階語言程式設計課程第十次個人作業

102400226石华波發表於2024-12-02

2024高階語言程式設計:https://edu.cnblogs.com/campus/fzu/2024C
高階語言程式設計課程第十次個人作業:https://edu.cnblogs.com/campus/fzu/2024C/homework/13314
學號:102400226
姓名:石華波

本次作業所用到的"Source.txt"文字檔案均為下圖檔案:

//1202.1
#include<stdio.h>

void CopyFile(const char *Source,const char *Destination);

int main(){
    char *Source="Source.txt";
    char *Destination="Destination.txt";
    CopyFile(Source,Destination);
    return 0;
}

void CopyFile(const char *Source,const char *Destination){
    FILE *Src=fopen(Source,"r");
    FILE *Dest=fopen(Destination,"w");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return;
    }
    else if(Dest==NULL) {
        printf("Failed to open file \"%s\".\n",Destination);
        return;
    }
    char ch;
    while((ch=fgetc(Src))!=EOF) fputc(ch,Dest);
    fclose(Src);
    fclose(Dest);
    printf("Done!");
}

//1202.2
#include<stdio.h>

int CountCharacters(const char *Source);

int main(){
    char *Source="Source.txt";
    int Amount=CountCharacters(Source);
    printf("%d characters totally.",Amount);
    return 0;
} 

int CountCharacters(const char *Source){
    FILE *Src=fopen(Source,"r");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return -1;
    }
    int Count=0;
    while(fgetc(Src)!=EOF) Count++;
    fclose(Src);
    return Count;
}

//1202.3
#include<stdio.h>

void DisplayFile(const char *Source);

int main(){
    char *Source="Source.txt";
    DisplayFile(Source);
    return 0;
} 

void DisplayFile(const char *Source){
    FILE *Src=fopen(Source,"r");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return;
    }
    char ch;
    while((ch=fgetc(Src))!=EOF) putchar(ch);
    fclose(Src);
}

//1202.4
#include<stdio.h>

void AppendFile(const char *Source,const char *Text);

int main(){
    char *Source="Source.txt";
    AppendFile(Source,"\nThe extra line.");
    return 0;
} 

void AppendFile(const char *Source,const char *Text){
    FILE *Src=fopen(Source,"a");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return;
    }
    fprintf(Src,Text);
    fclose(Src);
    printf("Done!");
}

//1202.5
#include<stdio.h>
#include<string.h>

void DeletLine(const char *Source,const char *Text);

int main(){
    char *Source="Source.txt";
    DeletLine(Source,"third");
    return 0;
} 

void DeletLine(const char *Source,const char *Text){
    FILE *Src=fopen(Source,"r");
    FILE *Dest=fopen("NewFile.txt","w");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return;
    }
    char Line[32767];
    while(fgets(Line,sizeof(Line),Src)!=NULL){
        if(strstr(Line,Text)==NULL){
            fputs(Line,Dest);
        }
    }
    fclose(Src);
    fclose(Dest);
    printf("Done!");
}

//1202.6
#include<stdio.h>
#include<string.h>

int FileBytes(const char *Source);

int main(){
    char *Source="Source.txt";
    int Size=FileBytes(Source);
    printf("The file is %d Bytes long.",Size);
    return 0;
} 

int FileBytes(const char *Source){
    FILE *Src=fopen(Source,"r");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return -1;
    }
    fseek(Src,0,SEEK_END);
    int Size=ftell(Src);
    fclose(Src);
    return Size;
}

//1202.7
#include<stdio.h>

typedef struct{
    char ID[10];
    char Name[20];
    float Grade[3];
    float Average;
} Student;

void SaveInfo(const char *Source,Student Students[]);

int main(){
    char *Source="students.txt";
    Student Students[5];
    for(int i=0;i<5;i++){
        printf("Now Enter Student%d's Infos.\n",i+1);
        printf("ID: \n");
        scanf(" %s",Students[i].ID);
        printf("Name: \n");
        scanf(" %s",Students[i].Name);
        printf("Grades(Three): \n");
        scanf(" %f %f %f",&Students[i].Grade[0],&Students[i].Grade[1],&Students[i].Grade[2]);
        Students[i].Average=(Students[i].Grade[0]+Students[i].Grade[1]+Students[i].Grade[2])/3;
    }
    SaveInfo(Source,Students);
    printf("Done!");
    return 0;
} 

void SaveInfo(const char *Source,Student Students[]){
    FILE *Src=fopen(Source,"w");
    if(Src==NULL) {
        printf("Failed to open file \"%s\".\n",Source);
        return;
    }
    for(int i=0;i<5;i++){
        fprintf(Src,"Student %d\n",i+1);
        fprintf(Src,"ID: %s,Name: %s,Grades: %.2f %.2f %.2f,Average: %.2f\n",Students[i].ID,Students[i].Name,Students[i].Grade[0],Students[i].Grade[1],Students[i].Grade[2],Students[i].Average);
    }
    fclose(Src);
}

相關文章