c Language file input/output question. It's a function to find the width of the circumference by printing coordinates, but the width of the circumference doesn't come out

Asked 2 years ago, Updated 2 years ago, 64 views

//You receive a bundle of two coordinate values from a file to construct a rectangle //Create a program that calculates the width and circumference of each and outputs it to the screen

//1. Receive the name of the file containing the square information from the user //2. Initial input of the number of squares in the first line of the file //3. Create a RECTANGLE array that corresponds to the number of squares using dynamic allocation techniques //4. Read the two coordinate values (x1, y1, x2, y2) from the file and put them in the array (step 2) //Read coordinate values from a file as many as the number of squares determined) //5. Implement the function of calculating the width and circumference as a function, respectively

// Use functions as much as possible when implementing a program, and avoid using global variables // Make a structure that means coordinates and squares as shown earlier and use it

typedef struct { int x; int y; } } POINT; typedef struct { POINT P1; POINT P2; } } RECTANGLE; typedef struct { int width; int height; int area; int permeter; }ALL; FILE* getFileName() { printf ("Please enter a name for the file: "); char list[20]; scanf_s("%s", list, 20); FILE* fp = fopen(list, "rt"); If (fp == NULL) {// If there is no file, output that there is no file printf("File not found"\n"); exit(0); } return fp; } int getRecNum(FILE* fp) {

int recNum = 0;
fscanf(fp, "%d", &recNum);

return recNum;

} RECTANGLE* createRecNum(int recNum) { return (RECTANGLE*)malloc(sizeof(RECTANGLE)recNum); } void createPoint(FILE fp, RECTANGLE* point, int recNum) { for (int i = 0; i < recNum; i++) { fscanf(fp, "%d %d %d %d", &point[i].P1.x, &point[i].P2.x, &point[i].P1.y, &point[i].P2.y); } } ALL* createAll(intrecNum) {//Allocate data storage space return (ALL*)malloc(sizeof(ALL)recNum); } void makeAll(int m, ALL all, RECTANGLE* point) { for (int i = 0; i < m; i++) { all[i].width = abs(point[i].P1.x - point[i].P2.x); all[i].height = abs(point[i].P1.y - point[i].P2.y); all[i].area = abs(all[i].width)*abs(all[i].height); all[i].permeter = 2 * (abs(all[i].width) + abs(all[i].height));

    printf("P1 = [%d %d]\n", point[i].P1.x, point[i].P1.y);
    printf("P2 = [%d %d]\n", point[i].P2.x, point[i].P2.y);
    printf("width : %d\n", all[i].width);
    printf("height : %d\n", all[i].height);
    printf("area : %d\n", all[i].area);
    printf("permeter = %d\n", all[i].permeter);
}

} int main(void) { int recNum = 0; //1. Receive the name of the file containing the square information from the user FILE* fp = getFileName();

//2. Receive the number of squares in the first line of the file at first
recNum = getRecNum(fp);
printf("%d\n", recNum);


//3. Create a RECTANGLE array that corresponds to the number of squares using dynamic allocation techniques
RECTANGLE* point = NULL;
point = createRecNum(recNum);

//4. Read the two coordinate values (x1, y1, x2, y2) from the file and put them in the array (step 2) 
//Read coordinate values from a file as many as the number of squares determined)
createPoint(fp, point, recNum);

//5. The function of calculating the width and circumference is implemented as a function, respectively
//width,height,area,permeter
ALL* last = NULL;
last = createAll(recNum);
makeAll(recNum, last, point);

free(point);
free(last);

return 0;

}

//The contents of the text file are

3

10 10 20 20

20 20 70 70

30 30 50 50

It's like this

c file-io struct

2022-09-21 17:36

1 Answers

The order of x, y coordinates is wrong. You have to receive it in the order of x1, y1, x2, y2, and y2, but it's because you received it in the order of x1, x2, y1, and y2.

void createPoint(FILE* fp, RECTANGLE* point, int recNum) {
    for (int i = 0; i < recNum; i++) {
        fscanf(fp, "%d %d %d %d", &point[i].P1.x, &point[i].P1.y, &point[i].P2.x, &point[i].P2.y);
    }
}


2022-09-21 17:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.