首页 > 解决方案 > 从链表计算数据并将结果输出到 C 中的 CSV 文件

问题描述

我在 C 语言中创建了一个程序,其中包含坐标和位置 ID 的 CSV 文件被插入到链接列表中。我然后:

现在我必须将 4 个计算、相应的位置 ID 和 x 坐标打印到 CSV 文件中。

我查看了有关如何执行此操作的各种教程,但它们仅显示有关如何将链接列表中的未修改值打印到输出文件中的方法。

对不起,如果这真的令人困惑。如果您想让我澄清任何事情,请发表评论。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <string.h>

#define MAX 200

struct wake {
    double x, y, u, v, id;
    struct wake *next;
}*head;                                                         

typedef struct wake data;

void read_csv();
void minNode();
void maxNode();

int main(int argc, char *argv[]) {
    read_csv();
}

void read_csv() {
    // Opens the CSV datafile
    FILE *fp = fopen("data2.csv", "r");
    
    char buffer[MAX];
    struct wake** tail = &head;
    
    while (fgets(buffer, MAX, fp)) {
        data *node = malloc(sizeof(data));
        node->x = atof(strtok(buffer, ","));
        node->y = atof(strtok(NULL, ","));
        node->u = atof(strtok(NULL, ","));
        node->v = atof(strtok(NULL, ","));
        node->id = atof(strtok(NULL, ","));
        node->next = NULL;
        *tail = node;
        tail = &node->next;
  
    }
    minNode();
    maxNode();
}

void minNode() {  
    struct wake *current = head;  
    
    double x_id, y_id;
    //double min;
      
    if(head == NULL) {  
        printf("List is empty \n");  
    }  
    else {  
        //Initializing min with head node data  
        x_id = head->id*head->x;
        y_id = head->id*head->y;
          
        while(current != NULL){  
             //If current node's data is smaller than min  
             //Then, replace value of min with current node's data  
             if(x_id > current->id*current->x) {  
                 x_id = current->id*current->x;
             }
             
             if(y_id > current->id*current->y) {  
                 y_id = current->id*current->y;
             }
             current= current->next;  
        }          
        printf("Min x X id: %lf\n",x_id);  
        printf("Min y X id: %lf\n",y_id); 
    }  
}

void maxNode() {
    struct wake *current = head;  
    
    double x_id, y_id;
    //double min;
      
    if(head == NULL) {  
        printf("List is empty \n");  
    }  
    else {  
        //Initializing min with head node data  
        x_id = head->id*head->x;
        y_id = head->id*head->y;
          
        while(current != NULL){  
             //If current node's data is smaller than min  
             //Then, replace value of min with current node's data  
             if(x_id < current->id*current->x) {  
                 x_id = current->id*current->x;
             }  
             if(y_id < current->id*current->y) {  
                 y_id = current->id*current->y;
             }
             current= current->next;  
        }          
        printf("Max x X id: %lf\n",x_id);  
        printf("Max y X id: %lf\n",y_id); 
    }  
}

这是输入文件:

19.743748,-11.838155,1.083537,-0.003929,0.947989
19.810734,-11.838155,1.083533,-0.004183,0.947972
19.877850,-11.838155,1.083525,-0.004426,0.947953
19.945097,-11.838155,1.083514,-0.004656,0.947930
20.012476,-11.838155,1.083501,-0.004868,0.947904
20.079988,-11.838155,1.083488,-0.005062,0.947875
20.147631,-11.838155,1.083478,-0.005237,0.947844
20.215405,-11.838155,1.083475,-0.005394,0.947812

标签: cprintinglinked-listoutput

解决方案


推荐阅读