Experiment 12_ 1_ First understanding ASCII code file

Posted by NFD on Mon, 07 Mar 2022 23:51:19 +0100

Title Description

An ASCII code file is known. The file name is dict.dic. The file only contains the characters in the ASCII code, which can correspond to the integer 0-127. We divide the characters in ASCII code into four categories. The first category is uppercase letter "A-Z", the second category is lowercase letter "A-Z", the third category is numeric character "0-9", the fourth category is other characters "! @#$% ^ & *" (characters that do not belong to the first three categories are the fourth category).
requirement:
1. Count the number of these four types of characters in the file.
2. Count the number of lines, maximum length and minimum length of the file. It should be noted here that although the line feed character of the file is indeed one character and the ASCII code is 10, the line feed character in the file is not counted when counting the line length.
3. Output specific statistical information of upper and lower case letters, one letter per line.
4. Close the file after processing.
Content tips: in the operation of files in this topic, three new C language file operation functions will be used, a new way to open files, as follows:
1.FILE *fp=fopen(“file.txt”,“r”);// FP is the file pointer, "file. TXT" is the file name to be opened. At this time, it should be in a directory with the program, "R" is to open the ASCII file in read-only mode.
2.int ch=fgetc(fp);// Read a byte (character) from the file pointed to by FP and store it in the variable ch.
3.fclose(fp);// Close the file pointed to by FP.

input

It can only be one of the three integers 1, 2 and 3, and its output corresponds to Task1, Task2 and Task3 in the output respectively.

output

If the contents of dict.dic file are as follows: (there is a newline character on each line of the file, and there is no space in the front)
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
~!@#$%^&*()_+`1234567890- =][';/.,<>?":{}|
The outputs of the three tasks are as follows: (Note: Tasks 1, 2 and 3 also need to be output)
Task1:
capital: 26
lowercase: 26
digit: 10
others: 36

Task2:
line: 3
43 characters in max line.
26 characters in min line.

Task3:
CAPITAL:
A:1
B:1
C:1
D:1
E:1
F:1
G:1
H:1
I:1
J:1
K:1
L:1
M:1
N:1
O:1
P:1
Q:1
R:1
S:1
T:1
U:1
V:1
W:1
X:1
Y:1
Z:1
LOWERCASE:
a:1
b:1
c:1
d:1
e:1
f:1
g:1
h:1
i:1
j:1
k:1
l:1
m:1
n:1
o:1
p:1
q:1
r:1
s:1
t:1
u:1
v:1
w:1
x:1
y:1
z:1

source code

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    int count;
    struct node * nextPtr;
}LISTNODE, * LISTNODEPTR;
void createListHead(LISTNODEPTR * ,LISTNODEPTR *);
void insertEnd(int,LISTNODEPTR *);
void countPlus(int,LISTNODEPTR);
void printList(LISTNODEPTR,int);
void destroyList(LISTNODEPTR headPtr);
int main(){
    FILE * fp = fopen("dict.dic","r");
    LISTNODEPTR capitalHeadPtr,capitalLastPtr,lowercaseHeadPtr,lowercaseLastPtr;
    createListHead(&capitalHeadPtr,&capitalLastPtr);
    createListHead(&lowercaseHeadPtr,&lowercaseLastPtr);
    int letter = 26;
    while(letter--){
        insertEnd(0,&capitalLastPtr);
        insertEnd(0,&lowercaseLastPtr);
    }
    int mode;
    scanf("%d",&mode);
    int ch = fgetc(fp);
    int capitalCount = 0,lowercaseCount = 0,digitCount = 0,othersCount = 0,lineCount = 0,lineMax,lineMin,flag = 0,temp;
    while(ch != EOF){
        if(ch != '\n'){
            lineCount++;
            if(ch >= 'A' && ch <= 'Z') {
                capitalCount++;
                countPlus(ch-'A'+1,capitalHeadPtr);
            }
            else if(ch >= 'a' && ch <= 'z') {
                lowercaseCount++;
                countPlus(ch-'a'+1,lowercaseHeadPtr);
            }
            else if(ch >= '0' && ch <= '9') digitCount++;
            else othersCount++;
        }
        else{
            flag++;
            othersCount++;
            if(flag == 1){
                lineMax = lineCount;
                lineMin = lineCount;
            }
            else if(lineCount > lineMax) lineMax = lineCount;
            else if(lineCount < lineMin) lineMin = lineCount;
            lineCount = 0;
        }
        temp = ch;
        ch = fgetc(fp);
    }
    if(temp != '\n'){
        flag++;
        othersCount++;
        if(flag == 1){
            lineMax = lineCount;
            lineMin = lineCount;
        }
        else if(lineCount > lineMax) lineMax = lineCount;
        else if(lineCount < lineMin) lineMin = lineCount;
        lineCount = 0;
        ch = fgetc(fp);
    }
    switch (mode)
    {
    case 1:
        printf("Task1:\ncapital: %d\nlowercase: %d\ndigit: %d\nothers: %d\n",capitalCount,lowercaseCount,digitCount,othersCount);
        break;
    
    case 2:
        printf("Task2:\nline: %d\n%d characters in max line.\n%d characters in min line.",flag,lineMax,lineMin);
        break;
    
    case 3:
        printf("Task3:\nCAPITAL:\n");
        printList(capitalHeadPtr,1);
        printf("LOWERCASE:\n");
        printList(lowercaseHeadPtr,0);
        break;

    default:
        printf("??");
        break;
    }
    destroyList(capitalHeadPtr);
    destroyList(lowercaseHeadPtr);
    fclose(fp);
    return 0;
}
void createListHead(LISTNODEPTR * headPtrPtr,LISTNODEPTR * lastPtrPtr){
    (*headPtrPtr) = malloc(sizeof(LISTNODE));
    if((*headPtrPtr) != NULL){
        (*headPtrPtr)->nextPtr = NULL;
        (*lastPtrPtr) = (*headPtrPtr);
    }
    else{
        printf("Error! -101");
    }
}
void insertEnd(int data,LISTNODEPTR * lastPtrPtr){
    LISTNODEPTR newPtr = malloc(sizeof(LISTNODE));
    if(newPtr != NULL){
        newPtr->count = data;
        newPtr->nextPtr = NULL;
        (*lastPtrPtr)->nextPtr = newPtr;
        (*lastPtrPtr) = newPtr;
    }
    else{
        printf("Error! -102");
    }
}
void countPlus(int num,LISTNODEPTR headPtr){
    while(num--){
        headPtr = headPtr->nextPtr;
    }
    (headPtr->count)++;
}
void printList(LISTNODEPTR headPtr,int mode){
    int num = 0;
    while (headPtr->nextPtr != NULL)
    {
        headPtr = headPtr->nextPtr;
        if(mode){
            printf("%c:%d\n",'A'+num,headPtr->count);    
        }
        else{
            printf("%c:%d\n",'a'+num,headPtr->count); 
        }
        num++;
    }
    
}
void destroyList(LISTNODEPTR headPtr){
    LISTNODEPTR tempPtr = NULL;
    while(headPtr != NULL){
        tempPtr = headPtr;
        headPtr = headPtr->nextPtr;
        free(tempPtr);
    }
}

Overall thinking

In fact, it's quite brainless. You can do whatever the topic asks you to do... The linked list used to store upper and lower case data seems to be easier to use array...

matters needing attention

When the last line has only the end of file (EOF), it cannot be regarded as a line!! Special treatment is required

Topics: C