小编Jes*_*Ros的帖子

解析缺少条目的 csv 文件

我正在尝试用 C 解析 csv 文件,其中分隔符|使用strtok. 问题是某些字段为空,因此两个分隔符彼此相邻放置。看起来strtok只是跳过所有空字段并仅输出下一个非空字段。

问题是我需要知道正在读取的令牌属于哪个位置。

这是一个非常小的例子来说明。

文件

node|171933|||traffic_signals|||||40.4200658|-3.7016652
Run Code Online (Sandbox Code Playgroud)

例如,这一行有 10 个字段,但只有字段 1、2、9 和 10 具有一些值。

代码

#include <stdio.h>
#include <stdlib.h> 
#include <string.h>

void main()
{
    FILE *fp;
    char lineBuf[128];
    char *token;
    int i=0;

    if((fp = fopen("test.txt", "r"))==NULL){
      fprintf (stderr, "\nError when opening file\n");
      return ;
    }

    fgets (lineBuf, sizeof(lineBuf), fp);

    token=strtok(lineBuf, "|\n");
    while(token!=NULL){
      printf("Element %d: %s\n",i,token); i++;
      token=strtok(NULL, "|\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出

Element 0: node
Element 1: 171933
Element 2: traffic_signals
Element …
Run Code Online (Sandbox Code Playgroud)

c csv parsing

5
推荐指数
1
解决办法
4296
查看次数

第1行的Gnuplot坏数据

好吧,我收到了错误

    gnuplot> plot "plot_error.p"
                  ^
             Bad data on line 1
Run Code Online (Sandbox Code Playgroud)

当试图plot_error.p在以下数据文件上执行绘图脚本时(tesc_error.dat)

    2       0.02373300      0.00187922
    3       0.12182900      0.01080161
    4       0.30066000      0.02936487
    5       0.88415600      0.07882007
    6       1.70864800      0.14576794
    7       4.11814900      0.44127670
    8       8.79967900      0.84273207
    9       22.09179700     2.25049799
    10      54.13644000     5.28557289
    11      164.75478299    20.67593118
    12      376.32501997    39.98897077
    13      807.50995700    82.47956624
Run Code Online (Sandbox Code Playgroud)

脚本是

    set encoding iso_8859_1
    set terminal postscript eps enhanced color solid
    set xrange[1:14] 
    set yrange[0:900] 
    set title "1D MFPT" 
    set xlabel "{/Symbol G}" 
    set ylabel "t_{esc}" 
    unset key

    set …
Run Code Online (Sandbox Code Playgroud)

plot gnuplot

4
推荐指数
1
解决办法
1万
查看次数

acos()结果不好,C中有两个关闭点

我正在创建一个函数,使用它们的纬度/经度(以度为单位而不是弧度)和余弦的球面律来计算两点的距离.我acos()遇到的问题是,由于函数中的舍入误差,当两点非常接近时,我得到的结果远远不够好.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct{
 double lat;
 double lon;
} point;

#define DEG_TO_RAD 0.017453292519943295769236907684886
#define EARTH_RADIUS_IN_METERS 6372797.560856

double distance(point a, point b) {
  double arg=sin(a.lat * DEG_TO_RAD) * sin(b.lat * DEG_TO_RAD) + cos(a.lat * DEG_TO_RAD) * cos(b.lat * DEG_TO_RAD) * cos((a.lon-b.lon) * DEG_TO_RAD);
  if(arg>1) arg=1;
  else if (arg<-1) arg=-1;
  printf("arg=%.12f acos(arg)=%.12f\n",arg, acos(arg));    //to see the problem
  return acos(arg) * EARTH_RADIUS_IN_METERS;
}

int main(){
  point p1,p2;

  p1.lat=63.0;
  p1.lon=27.0;
  p2.lat=p1.lat;
  p2.lon=p1.lon;

  printf("dist=%.8f\n",distance(p1,p2));

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是 …

c rounding-error math.h

3
推荐指数
1
解决办法
1083
查看次数

返回一个已评估参数的函数

我是C++的新手,我想做以下但无法弄清楚如何.

我想基于带有一些参数的模板定义一个函数.比方说,我们有f(x)=a*x+b,但参数ab将作为参数传递.

我想定义一个函数create_function(double a, double b),返回a*x+bab评估,无论参数传递,这样我可以用另一个函数func( double x )来评价它.

目标是使用给定的参数创建一次函数,然后可以由另一个例程重复调用它f(x).真正的功能比这复杂得多,但我能够适应它.

我一直在看这篇文章,它会与此类似,但我不知道如何使它适应返回仍然依赖的函数x.

c++

2
推荐指数
1
解决办法
178
查看次数

标签 统计

c ×2

c++ ×1

csv ×1

gnuplot ×1

math.h ×1

parsing ×1

plot ×1

rounding-error ×1