【问题标题】:Organizing inputs using a struct - C programming使用结构组织输入 - C 编程
【发布时间】:2016-07-31 20:31:09
【问题描述】:

我正在制作一个程序,该程序将使用 argv 接收输入参数“N”。值 N 将允许用户输入关于化学元素的行的 N 值。例如,一行看起来像

1 Hydrogen H other_nonmetals 1.008 1 0 0 0 0 0 0

并使用结构,我将扫描并打印有组织的输入。

我目前有两个主要问题。第一个问题是扫描电子值。在上面的 Hydrogen 示例中,我需要扫描 1 0 0 0 0 0 0 并在我的函数 print_element 中重新打印出来。当我这样做时,只存储值 1。我希望省略所有的零,但如果电子值是1 0 0 0 0 0 1,那么我的函数中只会打印 1。

我遇到的下一个问题是我的 for 循环。在循环函数print_element 时,将打印一个没有值的额外元素。例如,如果用户输入 Hydrogen 和 Barium 的值,它将打印 Hydrogen,然后是一个全为零的完全空白元素,然后是 Barium。我不知道如何摆脱空白元素。

#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 20

typedef struct{
  int num;
  char name[MAX_ELEMENTS];
  char symbol[MAX_ELEMENTS];
  char class[MAX_ELEMENTS];
  double weight;
  char electrons[MAX_ELEMENTS];
} element_t;

void scan_element(element_t *uno){
 scanf("%d %s %s %s %lf %20s", &uno->num, uno->name, uno->symbol, uno->class, &uno->weight, uno->electrons);
}

void print_element(element_t uno){
 printf("---------------\n| %d\t%.4f\n| %s\t%s\n| %s\n---------------\n", uno.num, uno.weight, uno.symbol, uno.name, uno.electrons);
}

int main (int argc, char *argv[]){
 int i;
 if (argc != 2){
     printf("ERROR: You must provide exactly one argument to this program.\n");
     return 0; }
 int N = (int)strtol(argv[1], NULL, 10);
 if(N <= 0){
     printf("ERROR: Your must provide an integer greater than 0 and less than or equal to 20.\n");
     return 0; }
 element_t uno[MAX_ELEMENTS];
 for(i=0; i<=argc; i++){
 scan_element(&uno[i]); }
 printf("%d total elements.\n", N);
 printf(" had the smallest atomic number.\n");
 printf(" had the largest atomic number.\n");
 for(i=0; i<=argc; i++){
     print_element(uno[i]); }
 return 0;
}

【问题讨论】:

  • 您使用了不一致的缩进 Pico-style 缩进。 C 不是 Pico;请不要使用那种风格。使用 Allman(我用那个)或 1TBS(很多人用那个)。如果您使用众所周知的样式并且在处理缩进时完全严格(您的代码不是),它会使您的代码更易于阅读。
  • 您的扫描循环是for(i=0; i&lt;=argc; i++){ scan_element(&amp;uno[i]); },但应该是for (i = 0; i &lt; N; i++) { scan_element(&amp;uno[i]); }。请注意不同的限制 — N&lt;(而不是 argc&lt;=)。您的打印循环也有同样的问题。这可以解释为什么会有额外的大量输入。您似乎在使用 MAX_ELEMENTS 做太多事情。这些天元素多达118个,但名称长度不同,电子能级数不同等等。
  • 您的测试和错误消息不匹配:if(N &lt;= 0){ printf("ERROR: Your must provide an integer greater than 0 and less than or equal to 20.\n"); return 0; } — 您可能应该使用:if (N &lt;= 0 || N &gt; MAX_ELEMENTS){ fprintf(stderr, "ERROR: Your must provide an integer greater than 0 and less than or equal to %d.\n", MAX_ELEMENTS); return 1; }return 1 在大多数系统上表示失败;使用EXIT_FAILURE 而不是 1在所有系统上都是安全的,stderr 用于报告错误)。

标签: c loops struct scanf


【解决方案1】:

for(i=0; i&lt;=argc; i++) 替换为for(i=0;i&lt;N;i++)

要省略electrons 中的第一个 0 及其后的所有内容,请添加

char*tmp;
if(tmp=strstr(uno->electrons," 0"))
    *tmp=0;

scan_element

如果只传递print_element中的指针会更快,因为会复制4或8个字节而不是92个。

使用当前的方式从输入中获取字符串并不好。见How to prevent scanf causing a buffer overflow in C?

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2021-08-10
    • 1970-01-01
    • 2016-09-19
    • 2016-06-15
    相关资源
    最近更新 更多