c - 我应该如何制作一个在元素周期表中添加新元素的函数?
问题描述
我在这个程序中有一个主菜单,其中包含用于搜索元素和添加元素的选项。请检查此添加功能并让我知道我应该对此进行哪些更正,因为它无法正常工作。我还必须为用户在添加元素后返回主菜单做一个选项,我该怎么做。
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;
}
}
}
解决方案
有一些错误
首先使用这个循环:
for (int i = 1; i <= n; i++)
{
an[i] = i;
}
每个输入元素都将存在,因此您不能有更多元素。您必须t
与e[i].atno
.
您应该使用&
inscanf
进行浮动。scanf("%lf", e[t - 1].bp);
这应该是scanf("%lf", &e[t - 1].bp);
(它不像字符串)
最后,由于您将double vlaue
主要分配给您的bp
我将浮点数转换为双精度。
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
}
}
推荐阅读
- junit4 - 如何使用 MockMvc 访问 DispatcherServlet?
- telegram-bot - 电报机器人 sendMessage 问题。Parse_mode='HTML' 不工作
- vue.js - 在继续显示组件之前等待 API 调用
- python - (pytest)为什么属性模拟在夹具中不起作用?
- javascript - 在 React-Native 上监控服务和特性
- python - 如何正确使用 Gophish python API 来避免这种 Json 混乱?
- visual-studio-2015 - Visual Studio 2015 中的 SSIS 部署非常慢
- ansible - AWX 无法从私人仓库安装要求
- python-3.x - 通过使用共享访问策略的 python 的 REST 调用为 iothub 内置端点创建 consumerGroup
- python - 检查类 __init__ 中 *args 的内容