简体   繁体   English

在 C 中添加和打印数组元素

[英]Adding and Printing elements of an array in C

This is a function that is trying to perform the first step of converting a CFG to Chromsky Normal From by adding 0->S as a rule to the end of the list of rules.这是一个 function,它试图通过将 0->S 作为规则添加到规则列表的末尾来执行将 CFG 转换为 Chromsky Normal From 的第一步。 My rules are an array of characters.我的规则是一个字符数组。 For some reason it will only print the array the first time and not any time after that.出于某种原因,它只会第一次打印数组,之后不会打印。 I want it to print the input and then result once the array has been edited.我希望它打印输入,然后在编辑数组后生成结果。 I cannot figure out why it won't print.我无法弄清楚为什么它不会打印。 The output is just the original input and then blank spaces. output 只是原始输入,然后是空格。

UPDATE: It will now print both times, but the output of the second print is the same as the first.更新:现在将打印两次,但第二次打印的 output 与第一次相同。 It is not registering that I added ',','0','>','S' as elements to the array.它没有注册我将 ',','0','>','S' 作为元素添加到数组中。 Am I adding the elements wrong?我是否添加了错误的元素?

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

//global variables
char stringCFG[200];

//a program to convert CFG to Chromsky Normal Form
int main(int argc, char *argv[]){
  //intialize instance variables
  FILE *CFG;
  FILE *output;
  char ch;
  int RHS= 0;
  int isThereS= 0;
  int howManyItems= 0;

  CFG= fopen(argv[1], "r");
  output= fopen(argv[2], "w");
  
  for (int i=0; i<200; i++){
      fscanf(CFG, "%c", &stringCFG[i]);
      howManyItems++;
      fprintf(output,"%c", stringCFG[i]);
  }
  printf("\n");

  for(int i=0; i<200; i++){
    if(stringCFG[i] == '>'){
      RHS= 1;
    }
    if(stringCFG[i] == ','){
      RHS= 0;
    }
    if(RHS== 1){
      if(stringCFG[i] == 'S'){
        isThereS=1;
      }
    }
  }

  if(isThereS==1){
    stringCFG[howManyItems]= ',';
    howManyItems++;
    stringCFG[howManyItems]='0';
    howManyItems++;
    stringCFG[howManyItems]='>';
    howManyItems++;
    stringCFG[howManyItems]='S';
    howManyItems++;
  }
 for(int i=0; i<200; i++){
   fprintf(output,"%c",stringCFG[i]);
  }
  fclose(CFG);
  fclose(output);
}

The problem seems to be here:问题似乎在这里:

  for (int i=0; i<200; i++){
      fscanf(CFG, "%c", &stringCFG[i]);
      howManyItems++;
      fprintf(output,"%c", stringCFG[i]);
  }

This loop always executes 200 times regardless of what is in the file.不管文件中有什么,这个循环总是执行 200 次。 In other words - the value of howManyItems will be 200 when the loop is done.换句话说 - 循环完成后howManyItems的值将是 200。

You can check that simply by printing howManyItems after the loop, ie您可以通过在循环后打印howManyItems来检查,即

printf("After loop, howManyItems=%d\n", howManyItems);

Since you have:既然你有:

char stringCFG[200];

then然后

stringCFG[howManyItems]= ',';  // bad.. writing to stringCFG[200]
howManyItems++;
stringCFG[howManyItems]= '0';  // bad.. writing to stringCFG[201]
howManyItems++;
...

will write outside of the array.将写入数组之外。 That is undefined behavior.那是未定义的行为。

You need to stop the first loop once the whole file has been read.读取整个文件后,您需要停止第一个循环。 Something like:就像是:

  for (int i=0; i<200; i++){
      if (fscanf(CFG, "%c", &stringCFG[i]) != 1)
      {
          // No more data
          break;
      }
      howManyItems++;
      fprintf(output,"%c", stringCFG[i]);
  }

and all the following loops must then use howManyItems as the upper limit.然后所有以下循环都必须使用howManyItems作为上限。

Like喜欢

  for(int i=0; i<howManyItems; i++){
    if(stringCFG[i] == '>'){
      RHS= 1;
    }
    ...
  }

BTW: Since you want to be able to add 4 extra chars, you probably should do:顺便说一句:由于您希望能够添加 4 个额外的字符,您可能应该这样做:

char stringCFG[200]; --> char stringCFG[200 + 4];

BTW: Hard-coding the value 200 over and over again is bad practice.顺便说一句:一遍又一遍地硬编码值 200 是不好的做法。 Instead use a define like:而是使用如下定义:

#define MAX_CHARS 200

and replace all the hard-coded 200 with MAX_CHARS .并将所有硬编码的200替换为MAX_CHARS Then you can adjust the maximum simply by editing one line instead of multiple lines.然后您可以简单地通过编辑一行而不是多行来调整最大值。

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

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