简体   繁体   English

从文本文件中读取多个类型为double的数字

[英]reading a number of type double from a text file

I am still stuck on this problem, I have tried a lot of things to get it and still no success... All I am trying to do is read the number into one array and the letters into another. 我仍然停留在这个问题上,我已经尝试了很多方法来解决这个问题,但仍然没有成功……我要做的就是将数字读入一个数组,将字母读入另一个数组。 That way, I can call both at the same time to take a statistical analysis of the letter frequency of two books. 这样,我可以同时调用两个书,对两本书的字母频率进行统计分析。 I know how to do this for a string, but I want to only copy the input of the numbers, which are two spaces in front of the letter they represent. 我知道如何对字符串执行此操作,但是我只想复制数字的输入,数字代表的是字母前面的两个空格。 The code reads two files through the command prompt(argv[1] and argv[2]) and compares the two together take the root mean square(RMS) of the two as the output. 该代码通过命令提示符(argv [1]和argv [2])读取两个文件,并将两个文件的均方根(RMS)作为输出进行比较。 Here is the code I have so far: 这是我到目前为止的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#define NUM_LETTERS 26
int main( int argc, char *argv[] )
{
    FILE *fp, *fp2;
    int ch, i, counter;
    double frequencysquared[NUM_LETTERS], freqone[NUM_LETTERS], freqtwo[NUM_LETTERS], average;
    fp = fopen(argv[1], "r");
    fp2 = fopen(argv[2], "r");
    while((ch=fgetc(fp)) != EOF)
    {
        freqone[ch] = fgets(/*unsure*/, 22, fp); 
    }
    while((ch=fgetc(fp2)) != EOF)
    {
        freqtwo[ch] = fgets(/*unsure*/, 22, fp2);
    }
    while(i=0; i<NUM_LETTERS; i++)
    {
        average += pow(( freqone[i]-freqtwo[i] ), 2);
    }
    average/=NUM_LETTERS;
    average = sqrt(average);
    printf("RMS Frequency: %lf", average);
    fclose(fp);
    fclose(fp2);
    return 0;
}

Text file: 文本文件:

A 0.08030130328545595500
B 0.01404566680148545800
C 0.02309245337888202900
D 0.04680329046987134100
E 0.12475974957130967000
F 0.02262448732647651800
G 0.02092142515718645500
H 0.06495870199587520900
I 0.06832638626586488900
J 0.00118328558965393900
K 0.00796545073487383260
L 0.03678714564106335500
M 0.02553256208071077300
N 0.07071134182580297900
O 0.07759211410349403600
P 0.01653257210855475400
Q 0.00111309068179311220
R 0.06200383063640040700
S 0.06269909448568859700
T 0.09005003894146078300
U 0.02792921679195900500
V 0.00865068674018190480
W 0.02356209073861756000
X 0.00115988728703366340
Y 0.02033479628434954300
Z 0.00035933107595423293

The rough outline of the program is on the right track, but you have many details wrong. 该程序的大致轮廓是正确的,但是您有许多错误的细节。 For one, scanf can take care of getting the input. 一方面, scanf可以照顾到获得输入。 No need to fiddle with strings. 无需摆弄琴弦。

Also, you need much better error checking. 另外,您需要更好的错误检查。

Here's a start: 这是一个开始:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE ('Z' - 'A' + 1)

void die(int err) {
  fprintf(stderr, "error %d\n", err);
  exit(err);
}

int main(int argc, char *argv[]) {
  char ch;
  double f, a[SIZE], b[SIZE];
  FILE *fa, *fb;

  if (argc != 3) die(1);
  fa = fopen(argv[1], "r");
  if (!fa) die(2);
  fb = fopen(argv[2], "r");
  if (!fb) die(3);
  for (int i = 0; i < SIZE; ++i)
    a[i] = b[i] = -1.0;
  while (fscanf(fa, " %c%lf", &ch, &f) == 2)
    if ('A' <= ch && ch <= 'Z') a[ch - 'A'] = f; else die(4);
  fclose(fa);
  while (fscanf(fb, " %c%lf", &ch, &f) == 2)
    if ('A' <= ch && ch <= 'Z') b[ch - 'A'] = f; else die(5);
  fclose(fb);
  double sum_d2 = 0;
  for (int i = 0; i < SIZE; ++i) {
    if (a[i] < 0 || b[i] < 0) die(100 + i);
    double d = a[i] - b[i];
    sum_d2 += d * d;
  }
  double rms = sqrt(sum_d2 / SIZE);
  printf("RMS Frequency: %f\n", rms);
  return 0;
}

Just do step at a time. 一次执行一个步骤。 Make the code as simple as possible. 使代码尽可能简单。 It is better to use a separate function to calculate the RMS , just because having everything in the main function can get overwhelming. 最好使用一个单独的函数来计算RMS ,因为主函数中的所有内容都会变得不知所措。

Here is some example code that will help you achieve your task: 这是一些示例代码,可以帮助您完成任务:

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

#define NUMLETTERS 26

#define RMS_VALID 1
#define RMS_INVALID 0

double calc_rms(double X[], double Y[], int n, double *rms);
void read_file(double numbers[], char letters[], FILE *stream);

int 
main(int argc, char const *argv[]) {
    FILE *fp1, *fp2;
    double numbers1[NUMLETTERS], numbers2[NUMLETTERS];
    char letters1[NUMLETTERS], letters2[NUMLETTERS];
    double rms;

    fp1 = fopen(argv[1], "r");
    fp2 = fopen(argv[2], "r");
    if (fp1 == NULL || fp2 == NULL) {
        fprintf(stderr, "%s\n", "Error Reading a File");
        exit(EXIT_FAILURE);
    }

    read_file(numbers1, letters1, fp1);
    read_file(numbers2, letters2, fp2);

    if (calc_rms(numbers1, numbers2, NUMLETTERS, &rms) != RMS_INVALID) {
        printf("RMS Frequency: %f\n", rms);
    }

    return 0;
}

void
read_file(double numbers[], char letters[], FILE *stream) {
    double onedouble;
    char oneletter;
    int count = 0;

    while (fscanf(stream, " %c %lf", &oneletter, &onedouble) == 2) {
        numbers[count] = onedouble;
        letters[count] = oneletter;
        count++;
    }
}

double
calc_rms(double X[], double Y[], int n, double *rms) {
    int i;
    double sum = 0.0, diff;

    if (n <= 0) {
        return RMS_INVALID; // just for precaution
    }

    for (i = 0; i < n; i++) {
        diff = (X[i] - Y[i]);
        sum += diff * diff;
    }
    *rms = sqrt(sum/n);
    return RMS_VALID;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM