LAB05 Structures and file processing Solution

 Fundamentals of C Programming 2018

(LAB 05 Structures and file processing)

SOLUTION ARCHIVE

复习资料备份

BEFORE THE LAB…

You are expected to have done the following before coming to the lab:

1. Go through F05 material and the preparation tasks therein.

2. Be familiar with syntax for structure definition and file open/processing.

 

IN THE LAB…

Discuss

What are structures in C and why do we use them?

How are structures passed to functions – by value/reference? How about an array of structures? Can a function return a structure?

What is the difference between binary and text files? What are the pros and cons of using binary and text files?

 

Task 1 [struct]

Write a program that use a structure type player_t to store information about a Cricket player. You should store the following fields (no more, no less).

  • letter – a char representing the first letter of the player’s name
  • age – an int field to store the age of the player
  • avg – a double field to store the batting average of the player

At this stage the program should only define the structure type player_t and declare a player_t type variable called player. Understand the distinct differences/similarities between  struct player , player_t and player.

 

NOTE: Be careful, pay attention to where in the program you should define player_t and player. You should avoid using global variables.

 

Task 2 [struct]

Write function that reads the corresponding fields from the user and return a the player’s information in structure form. Use the prototype:

player_t scanPlayer (void);

 

Sample output when running this function would be:

Enter the first letter of the player name> B
Enter the age> 40
Enter the batting average> 99.941908

 

Also write a function that prints the content of a structure type player_t. Use the prototype:

void printPlayer (player_t p);

 

Sample output when running this function would be (notice the rounding of batting average when printing):

B 40 99.94

 

Use these two functions in your main program to scan, store and print the variable player.

 

Task 3 [struct]

Write a function that compares two players to decide if player A is better than player B. Use the following function prototype:

int compAB (player_t playerA, player_t playerB);

 

If the batting average of player A is at least 10 runs more than player B, the player A is better (and vice versa). If the difference in batting average is 10 or less, and player A is at least 15 years younger than player B, the player A is better (and vice versa). If the difference in batting average is 10 or less and the difference in age is 15 or less, both players are indifferent.

 

The function should return an integer depending on the outcome of the comparison as follows:

1 : player A is better than player B

0 : both players are indifferent

-1: player B is better than player A

 

Task 4 [file]

Write function that prints the first letter of the player name to a text file. Use the prototype:

int savePlayerName (player_t p, char filename []);

The function should return

0 – if the letter was written successfully to the filename specified

1 – otherwise

 

Open the file you created, manually using Gedit. Do you see that the first letter of the player name has been saved in that file?

 

Task 5 [file]

Write function that reads the first letter of the player name from a text file. Use the prototype:

char readPlayerName (char filename []);

The function should return

the letter (a-z, A-Z) read from the file – if the letter was read successfully from the filename specified

1 – otherwise

 

Test if your function works – you might want to think about how you could test the function.

 

NOTE: Assume the file always has only one valid character – no need to read until the end of file (EOF).

 

Task 6 [file]

Change the functions in Task 4 and 5 to print/read the entire structure, not only the first letter of the name.

Solution

#include <stdio.h>

typedef struct player {
    char letter;
    int age;
    double avg;
} player_t;

player_t scanPlayer();
void printPlayer (player_t p);
int compAB (player_t playerA, player_t playerB);
int savePlayerName (player_t p, char filename []);
char readPlayerName (char filename []);
int savePlayer(player_t p, char filename []);
player_t readPlayer(char filename []);
int savePlayerBin(player_t p, char filename[]);
player_t loadPlayerBin(char *); 
int main() {
    player_t p = scanPlayer();
    printPlayer(p);
    savePlayerName(p,"playerName.txt");
    char name = readPlayerName("playerName.txt");
    printf("Readed name = %c\n",name);
    savePlayer(p,"player.txt");
    player_t p2 = readPlayer("player.txt");
    printf("read whole player is \n");
    printPlayer(p2);
    savePlayerBin(p2,"playerBin.txt");
    player_t p3 = loadPlayerBin("playerBin.txt");
    printf("read whole player Bin is \n");
    printPlayer(p3);
}

player_t scanPlayer() {
    player_t player;
    printf("Name >\n");
    scanf("%c",&player.letter);
    printf("age >\n");
    scanf("%d",&player.age);
    printf("avg >\n");
    scanf("%lf",&player.avg);
    return player;
}
void printPlayer (player_t p) {
    printf("%c %d %.2lf\n",p.letter,p.age,p.avg);
}

int compAB (player_t playerA, player_t playerB) {
    if(playerA.avg+10 >= playerB.avg ) 
        return 1;
    else if(playerA.avg < 10 && playerB.avg < 10 && playerA.age <=15 && playerB.age <= 15) 
        return 0;
    else 
        return -1;
}

int savePlayerName (player_t p, char filename []) {
    FILE *fp = NULL;
    fp = fopen(filename,"w");
    if(fp == NULL) return 1;
    fprintf(fp,"%c",p.letter);
    fclose(fp);
    return 0;
}

char readPlayerName (char filename []) {
    FILE *fp = NULL;
    fp = fopen(filename,"r");
    if(fp == NULL) return 1;
    char name;
    fscanf(fp,"%c",&name);
    if( (name > 'z' && name < 'a') || (name > 'Z' && name < 'A'))  
        return 1;
    return name;
}
int savePlayer(player_t p ,char filename[]) {
    FILE *fp = NULL;
    fp = fopen(filename,"w");
    if(fp == NULL) return 1;
    fprintf(fp,"%c %d %lf",p.letter,p.age,p.avg);
    fclose(fp);
    return 0;
}
player_t readPlayer(char filename[]) {
    FILE *fp = NULL;
    fp = fopen(filename,"r");
    player_t p;
    fscanf(fp,"%c %d %lf",&p.letter,&p.age,&p.avg);
    fclose(fp);
    return p;
}
int savePlayerBin(player_t p, char filename[]) {
    FILE *fp = NULL;
    fp = fopen(filename,"w");
    if(fp == NULL) return 1;
    fwrite(&p,sizeof(p),1,fp);
    fclose(fp);
    return 0;
}
player_t loadPlayerBin(char filename[]) {
    FILE *fp = NULL;
    fp = fopen(filename,"r");
    player_t p;
    fread(&p,sizeof(p),1,fp);
    fclose(fp);
    return p;
}

 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください