我应该如何制作一个功能以在元素周期表中添加新元素?

问题描述 投票:-1回答:1

我在该程序中有一个主菜单,其中包含用于搜索元素和添加元素的选项。请检查此添加功能,并让我知道我应该对此进行哪些更正,因为它无法正常工作。添加元素后,我还必须为用户提供一个选项以返回主菜单,我该怎么做。

struct element {
  int atno;
  char name[20];
  char symbol[20];
  char mn[20];
  float bp;
  char state[20];
};

int n = 118, i = 0;
void add(struct element e[], int n);

int main() {
  struct element e[n];
  e[0].atno = 1;
  strcpy(e[0].name, "Hydrogen");
  strcpy(e[0].symbol, "H");
  strcpy(e[0].mn, "Metal");
  e[0].bp = -252.879;
  strcpy(e[0].state, "Gas");

  e[1].atno = 2;
  strcpy(e[1].name, "Helium");
  strcpy(e[1].symbol, "He");
  strcpy(e[1].mn, "Non-Metal");
  e[1].bp = -268.28;
  strcpy(e[1].state, "Gas");

  add(e, n);
}

void add(struct element e[], int n) {
  int t;
  int an[150];
  for (int i = 1; i <= n; i++) {
    an[i] = i;
  }
  printf("Enter the atomic number of the element you want to add: ");
  scanf("%d", &t);
  for (int i = 1; i <= n; i++) {
    if (an[i] == t) {
      printf("The element already exists");
    } else {
      e[t - 1].atno = t;
      printf("Enter the Name:");
      scanf("%s", (e[t - 1].name));
      printf("Enter the Symbol:");
      scanf("%s", (e[t - 1].symbol));
      printf("Enter the Boiling Point:");
      scanf("%f", e[t - 1].bp);
      printf("Enter whether it is metal/non metal:");
      scanf("%s", (e[t - 1].mn));
      printf("Enter the state at room temperature:");
      scanf("%s", (e[t - 1].state));

      an[t] = t;
    }
  }
}

c structure
1个回答
0
投票

有一些错误

首先使用此循环:

 for (int i = 1; i <= n; i++)
    {
        an[i] = i;
    }

每个输入元素都会存在,因此您不能再有其他元素。您必须将te[i].atno进行比较。

您应将&中的scanf用于浮点。 scanf("%lf", e[t - 1].bp);应该为scanf("%lf", &e[t - 1].bp);(不像字符串)

最后,因为您将主double vlaue分配给了bp,所以我将float转换为double。

struct element {
    int atno;
    char name[20];
    char symbol[20];
    char mn[20];
    double bp;//warning since you have assigned double type to it in main
    char state[20];
};

int n = 118, i = 0;
void add(struct element e[], int n);

int main() {
    struct element e[n];
    e[0].atno = 1;
    strcpy(e[0].name, "Hydrogen");
    strcpy(e[0].symbol, "H");
    strcpy(e[0].mn, "Metal");
    e[0].bp = -252.879;
    strcpy(e[0].state, "Gas");

    e[1].atno = 2;
    strcpy(e[1].name, "Helium");
    strcpy(e[1].symbol, "He");
    strcpy(e[1].mn, "Non-Metal");
    e[1].bp = -268.28;
    strcpy(e[1].state, "Gas");

    add(e, n);
}

void add(struct element e[], int n)
{
    int t;
    int an[150];
  /*  for (int i = 1; i <= n; i++)
    {
        an[i] = i;
    }*///every element will exits with your method
    printf("Enter the atomic number of the element you want to add: ");
    scanf("%d", &t);
    int check = 0;
    for (int i = 0; i < n; i++)
    {
        if (e[i].atno == t)
        {
            printf("The element already exists");
            check = 1;
            break;
        }
    }
    if (check == 0)
    {
        e[t - 1].atno = t;
        printf("Enter the Name:");
        scanf("%s", (e[t - 1].name));
        printf("Enter the Symbol:");
        scanf("%s", (e[t - 1].symbol));
        printf("Enter the Boiling Point:");
        scanf("%lf", &e[t - 1].bp);
        printf("Enter whether it is metal/non metal:");
        scanf("%s", (e[t - 1].mn));
        printf("Enter the state at room temperature:");
        scanf("%s", (e[t - 1].state));
 //call menu function
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.