如何从文件中读取值,将它们存储在结构中并找到它们的平均值、总和、乘积等...使用 C

How Can Read Values from File, store them in a structure and find their average, sum, product etc... using C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

#define SIZE 1024

typedef struct rational
{
    int  numerator;
    int denominator;
    struct rational *next;
}rational;

double add_rationals(rational*);
void multiply_rationals();
void subtract_rationals();
void divide_rationals();
void read_data(FILE*, int[], int*);
void display_values(rational*);
rational * createList(int []);

int main()
{
    system("cls");


    FILE* ifp;
    ifp = fopen("file.txt", "r");
    int data[SIZE] ={0};
    int sz = SIZE;
    
    read_data(ifp, data, &sz);
    
    printf("\nRead Integer Count:%d\n", sz);

    rational* HEAD = createList(data);
    display_values(HEAD);
 
    /*
        want to start after the 1st number because 1st number is size of arr
        data[0] is the 1st number we just read from the text
    */

    //display_values(HEAD, data[0]); // passing data[0] as size cuz the 1st number is size


}



// double add_rationals(rational* r)
// {
//     rational* c;
//     c = r;

//     static double x = 0 , y = 0, sum;
//     x = r->numerator;
//     y = r->denominator;

//     while(c->next != NULL)
//     {
//         sum = x/y + add_rationals(r->next);
//     }

//     return sum;
    
// }

rational * createList(int data[])
{
    rational *head = NULL;
    rational *temp = NULL;
    rational *p = NULL;
    rational *r = NULL;
    int count = 0;
    
    for (int i = 1; i < 5; i++)
    {
        temp = (rational*)malloc(sizeof(double));
        temp->numerator = data[i];
        temp->denominator = data[++i];
        
        temp->next = NULL;

        if(head == NULL) // Checking if the List has started yet
        {
            count++;
            head = temp;
            printf("\nHEAD NUMERATOR: %d, HEAD DENOMINATOR: %d\n\n", head->numerator, head->denominator);
        }
        else
        {
            p = head;
            while (p->next != NULL)
            {
                count++;
                p = p->next;

            }
            p->next = temp;
            
        }
        
    }
}




void display_values(rational* HEAD)
{
    rational* o = NULL;
    o = HEAD;
    while(o->next != NULL)
    {
        printf("\n\nNUM: %d, DEN: %d", o->numerator, o->denominator);
        o = o->next;
    }
    
}

void read_data(FILE* fptr, int d[], int*size)
{
    *size = 0;
    while ((fscanf(fptr, "%d", &d[*size])) == 1)
        (*size)++;
    
}

问题>>>

  1. 我无法在函数 void display_values() 函数
  2. 中显示链表中的所有元素
  3. 先决条件是这样的..从文本文件中取出第一个数字并创建一个该大小的数组... 让我用例子告诉你: 2 5 2 6 7 是文本文件中的整数

我想将第 2 项和第 4 项作为分子读取到结构中,将第 3 项和第 5 项作为分母读取 我想要 5/2 和 6/7 作为有理数并用它们做所有这些操作 比如 Add, Sub, Mul , Div...etc

您的代码中存在多个问题,我在下面的评论中更正了它们:

rational* createList(int data[])
{
    rational *head = NULL;
    rational *temp = NULL;
    rational *p = NULL;
    rational *r = NULL; // this variable is not used
    int count = 0; // I didn't see the purpose of this variable
    
    for (int i = 1; i < 5; i++) // the 5 is a magic number, 
    {                          //if the input file changes, the code will not run as intended
        temp = (rational*)malloc(sizeof(double)); // space should be allocated for rational type not double type
        temp->numerator = data[i];
        temp->denominator = data[++i];
        
        temp->next = NULL;

        if(head == NULL) 
        {
            count++;
            head = temp;
            printf("\nHEAD NUMERATOR: %d, HEAD DENOMINATOR: %d\n\n", head->numerator, head->denominator);
        }
        else
        {
            p = head;
            while (p->next != NULL)
            {
                count++;
                p = p->next;

            }
            p->next = temp;            
        }       
    }
    // the return value for this function is missing.
}

void display_values(rational* HEAD)
{
    rational* o = NULL;
    o = HEAD;
    while(o->next != NULL) // this condition will miss the last node of the linked list
    {
        printf("\n\nNUM: %d, DEN: %d", o->numerator, o->denominator);
        o = o->next;
    }
    
}

你把它搞得太复杂了。

你应该:

  1. 从输入文件中读取一个整数
  2. 分配一个有理数结构的数组
  3. 循环读取一个结构并使其指向数组中的下一个结构的次数
  4. 为数组的最后一个元素设置 next=NULL

这样一来,您就可以使用一个 malloc,并避免使用 data 整数数组。

可能的代码:

...
rational* read_data(FILE*, int*);
...
    rational* HEAD = read_data(ifp, &sz);

    printf("\nRead Integer Count:%d\n", sz);

    display_values(HEAD);
...
rational* read_data(FILE* fptr, int* size)
{
     *size = 0;
    if (1 != fscanf(fptr, "%d", size)) return NULL;
    rational* ls = malloc(*size * sizeof(rational)); // never cast return value of malloc!

    for (int i = 0; i < *size; i++) {
        if (2 != fscanf(fptr, "%d%d", &ls[i].numerator, &ls[i].denominator)) {
            free(ls);
            return NULL;
        }
        ls[i].next = ls + i + 1;  // by definition same as &(ls[i+1])
    }
    ls[*size - 1].next = NULL;
    return ls;
}

但是你应该为fopen返回NULL或read_data返回NULL(文件读取错误)添加一些错误处理...