c - 从链表计算数据并将结果输出到 C 中的 CSV 文件
问题描述
我在 C 语言中创建了一个程序,其中包含坐标和位置 ID 的 CSV 文件被插入到链接列表中。我然后:
- 找到x 坐标乘以 ID的最小值和最大值
- 找到y 坐标乘以 ID的最小值和最大值
现在我必须将 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
解决方案
推荐阅读
- c# - C# 调用 web 服务导致 401
- c# - 如何创建符合 MVVM 的 UWP 可重用内容对话框
- javascript - React 组件之间如何传递参数?
- python - 为具有多个输入和输出的函数并行化 for 循环
- microsoft-graph-api - 如何将返回的 ODATA 解析为数组?
- angular - 404 使用 Angular inMemoryWebApi
- javascript - 我可以将 src img 属性指向 blob 文件或 base64 编码图像吗?
- php - Woocommerce:短代码/ PHP 来显示可变产品的当前价格?
- javascript - 无法使用猫鼬按日期获取集合
- android - 无法通过改造协程 API 调用获得下载进度