I am new at C and trying to make a little program.
我是C的新手,我想做一个小程序。
Basically it's a program that takes elements name, group, period (Not scientifically correct) number from a text file. Then insert elements names to 2d string array (elmName in code), element numbers to 2d int array (elmNumber) and then print them as the periodic table.
基本上,它是一个程序,从文本文件中获取元素名称、组、周期(不是科学上正确的)数字。然后将元素名插入到2d字符串数组(代码中的elmName)中,将元素号插入到2d int数组(elmNumber)中,然后将它们打印为元素周期表。
But when i try to run it, program takes the last element name it read and assigns it every element in 2d string array (elmName).
但是当我尝试运行它时,程序会获取它所读取的最后一个元素名,并将它分配给2d字符串数组中的每个元素(elmName)。
Here is the code :
下面是代码:
char* elmName[18][5];
int elmNumber[18][5];
//AtomNumber[Group][Period]
int iGroupCount = 18;
int iPeriodCount = 5;
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
elmNumber[j][i] = 0;
}
}
printf("\n");
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
printf("%d ", elmNumber[j][i]);
}
printf("\n");
}
int g,p,num;
//g = Group
//p = Period
//num = Atom no.
int tempNum;
char tempName[2];
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
//if there is not an element at this group and period name it '*'
elmName[j][i] = "*";
}
}
FILE *fs = fopen("element.txt" , "r");
while(!feof(fs)){
fscanf(fs ,"%d\t%d\t%d\t%s\n" , &g , &p , &tempNum , tempName);
printf("%d\t%d\t%d\t%s\n" , g , p , tempNum , tempName);
if( elmName[g][p] == "*"){
elmNumber[g][p] = tempNum;
elmName[g][p] = tempName;
}
//g = group No
//p = period No
}
fclose(fs);
And here is the text file it reads :
这是它的文本文件:
0 0 1 H
17 0 2 He
0 1 3 Li
1 1 4 Be
12 1 5 B
13 1 6 C
14 1 7 N
15 1 8 O
16 1 9 F
17 1 10 Ne
0 2 11 Na
1 2 12 Mg
12 2 13 Al
13 2 14 Si
14 2 15 P
15 2 16 S
16 2 17 Cl
If you have a solution please help. :)
如果你有办法,请帮忙。:)
2 个解决方案
#1
0
Change this line char* elmName[18][5];
to char elmName[18][5][4]; assuming your strings won't have more than 3 characters(3 characters + 1 for NULL). This line creates a 3 dimensional character array.
改变行char* elmName[18][5];char elmName[18][5][4];假设你的字符串不超过3个字符(3个字符+ 1为空)。这一行创建了一个三维字符数组。
The [i,j]th element of this will be a character string which is what you would require in this case: Also change this line : if( elmName[g][p] == "*")
to if( strcmp(elmName[g][p], "*")==0)
.This is because you cannot compare values of arrays (string which is a character array) using equal operator .
(i,j)th元素,这将是一个字符串,你需要在这种情况下:这条线也发生了变化:如果(elmName[g][p]= =“*”)如果(strcmp(elmName[g][p],“*”)= = 0)。这是因为你无法比较的数组值(字符串是一个字符数组)使用相等操作符。
Also change elmName[g][p] = tempName;
to strcpy(elmName[g][p],tempName). Don't forget to include string.h
. You have to copy each character to the character array which is done by strcpy
也可以更改elmName[g][p] = tempName;拷贝字符串(elmName[g][p],tempName)。不要忘记包含字符串。h。您必须将每个字符复制到由strcpy完成的字符数组中
Here is the corrected code:
以下是更正后的代码:
#include<stdio.h>
#include<string.h>
int main()
{
char elmName[18][5][4];
int elmNumber[18][5];
//AtomNumber[Group][Period]
int iGroupCount = 18;
int iPeriodCount = 5;
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
elmNumber[j][i] = 0;
}
}
printf("\n");
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
printf("%d ", elmNumber[j][i]);
}
printf("\n");
}
int g,p,num;
//g = Group
//p = Period
//num = Atom no.
int tempNum;
char tempName[2];
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
//if there is not an element at this group and period name it '*'
strcpy(elmName[j][i] , "*");//This line has been changed
}
}
FILE *fs = fopen("element.txt" , "r");
while(!feof(fs)){
fscanf(fs ,"%d\t%d\t%d\t%s\n" , &g , &p , &tempNum , tempName);
printf("%d\t%d\t%d\t%s\n" , g , p , tempNum , tempName);
if( strcmp(elmName[g][p], "*")==0)//This line has been changed
{
elmNumber[g][p] = tempNum;
strcpy(elmName[g][p], tempName);//This line has been changed
}
//g = group No
//p = period No
}
fclose(fs);
//For viewing results
for(int i=0;i<18;++i)
for(int j=0;j<5;++j)
printf("%d %d %s\n",i,j,elmName[i][j]);
return 0;
}
This is the ouput that I got when using your sample file
这是我在使用示例文件时得到的输出
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 H
17 0 2 He
0 1 3 Li
1 1 4 Be
12 1 5 B
13 1 6 C
14 1 7 N
15 1 8 O
16 1 9 F
17 1 10 Ne
0 2 11 Na
1 2 12 Mg
12 2 13 Al
13 2 14 Si
14 2 15 P
15 2 16 S
16 2 17 Cl
0 0 H
0 1 Li
0 2 Na
0 3 *
0 4 *
1 0 *
1 1 Be
1 2 Mg
1 3 *
1 4 *
2 0 *
2 1 *
2 2 *
2 3 *
2 4 *
3 0 *
3 1 *
3 2 *
3 3 *
3 4 *
4 0 *
4 1 *
4 2 *
4 3 *
4 4 *
5 0 *
5 1 *
5 2 *
5 3 *
5 4 *
6 0 *
6 1 *
6 2 *
6 3 *
6 4 *
7 0 *
7 1 *
7 2 *
7 3 *
7 4 *
8 0 *
8 1 *
8 2 *
8 3 *
8 4 *
9 0 *
9 1 *
9 2 *
9 3 *
9 4 *
10 0 *
10 1 *
10 2 *
10 3 *
10 4 *
11 0 *
11 1 *
11 2 *
11 3 *
11 4 *
12 0 *
12 1 B
12 2 Al
12 3 *
12 4 *
13 0 *
13 1 C
13 2 Si
13 3 *
13 4 *
14 0 *
14 1 N
14 2 P
14 3 *
14 4 *
15 0 *
15 1 O
15 2 S
15 3 *
15 4 *
16 0 *
16 1 F
16 2 Cl
16 3 *
16 4 *
17 0 He
17 1 Ne
17 2 *
17 3 *
17 4 *
You are getting the last added element as output because you are assigning address of string tempName to the 2d character array pointer elmName. In my suggested solution I have used static 3 dimensional character array if you wanted to use 2 dimensional array you have to assign dynamically allocated character array (string) to it instead of a statically allocated variable in your case tempName
您将获得最后一个添加的元素作为输出,因为您将字符串tempName的地址分配给2d字符数组指针elmName。在我建议的解决方案中,我使用了静态的三维字符数组,如果您想使用二维数组,您必须为它分配动态分配的字符数组(string),而不是在您的示例tempName中分配的静态变量
#2
0
You should use Array of pointers to take input as line in C
. 2-D
arrays are not suitable for storing strings.
您应该使用指针数组来将输入作为C. 2-D数组中的行来存储字符串。
char *lineptr[MAXLINE]; //Array of pointers to store lines.
int readline(char* lineptr[])
{
char line[1000]; //it takes line as input
for(i=0;i<MAXLINE;i++){
if(fscanf(stdin,"%s",line)){
char *temp=malloc((strlen(line)+1)*sizeof(char)); //allocates memory for temp to store new line in every loop
strcpy(temp,line); //copies line to temp
lineptr[i]=temp; //lineptr[i] will point to new line i.e temp
}
}
return i; //returns the number of lines
}
After taking input in lineptr[]
you can manipulate your input lines as required.
在输入lineptr[]之后,您可以根据需要操作输入行。
#1
0
Change this line char* elmName[18][5];
to char elmName[18][5][4]; assuming your strings won't have more than 3 characters(3 characters + 1 for NULL). This line creates a 3 dimensional character array.
改变行char* elmName[18][5];char elmName[18][5][4];假设你的字符串不超过3个字符(3个字符+ 1为空)。这一行创建了一个三维字符数组。
The [i,j]th element of this will be a character string which is what you would require in this case: Also change this line : if( elmName[g][p] == "*")
to if( strcmp(elmName[g][p], "*")==0)
.This is because you cannot compare values of arrays (string which is a character array) using equal operator .
(i,j)th元素,这将是一个字符串,你需要在这种情况下:这条线也发生了变化:如果(elmName[g][p]= =“*”)如果(strcmp(elmName[g][p],“*”)= = 0)。这是因为你无法比较的数组值(字符串是一个字符数组)使用相等操作符。
Also change elmName[g][p] = tempName;
to strcpy(elmName[g][p],tempName). Don't forget to include string.h
. You have to copy each character to the character array which is done by strcpy
也可以更改elmName[g][p] = tempName;拷贝字符串(elmName[g][p],tempName)。不要忘记包含字符串。h。您必须将每个字符复制到由strcpy完成的字符数组中
Here is the corrected code:
以下是更正后的代码:
#include<stdio.h>
#include<string.h>
int main()
{
char elmName[18][5][4];
int elmNumber[18][5];
//AtomNumber[Group][Period]
int iGroupCount = 18;
int iPeriodCount = 5;
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
elmNumber[j][i] = 0;
}
}
printf("\n");
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
printf("%d ", elmNumber[j][i]);
}
printf("\n");
}
int g,p,num;
//g = Group
//p = Period
//num = Atom no.
int tempNum;
char tempName[2];
for(int i=0;i<iPeriodCount;i++){
for(int j=0;j<iGroupCount;j++){
//if there is not an element at this group and period name it '*'
strcpy(elmName[j][i] , "*");//This line has been changed
}
}
FILE *fs = fopen("element.txt" , "r");
while(!feof(fs)){
fscanf(fs ,"%d\t%d\t%d\t%s\n" , &g , &p , &tempNum , tempName);
printf("%d\t%d\t%d\t%s\n" , g , p , tempNum , tempName);
if( strcmp(elmName[g][p], "*")==0)//This line has been changed
{
elmNumber[g][p] = tempNum;
strcpy(elmName[g][p], tempName);//This line has been changed
}
//g = group No
//p = period No
}
fclose(fs);
//For viewing results
for(int i=0;i<18;++i)
for(int j=0;j<5;++j)
printf("%d %d %s\n",i,j,elmName[i][j]);
return 0;
}
This is the ouput that I got when using your sample file
这是我在使用示例文件时得到的输出
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 H
17 0 2 He
0 1 3 Li
1 1 4 Be
12 1 5 B
13 1 6 C
14 1 7 N
15 1 8 O
16 1 9 F
17 1 10 Ne
0 2 11 Na
1 2 12 Mg
12 2 13 Al
13 2 14 Si
14 2 15 P
15 2 16 S
16 2 17 Cl
0 0 H
0 1 Li
0 2 Na
0 3 *
0 4 *
1 0 *
1 1 Be
1 2 Mg
1 3 *
1 4 *
2 0 *
2 1 *
2 2 *
2 3 *
2 4 *
3 0 *
3 1 *
3 2 *
3 3 *
3 4 *
4 0 *
4 1 *
4 2 *
4 3 *
4 4 *
5 0 *
5 1 *
5 2 *
5 3 *
5 4 *
6 0 *
6 1 *
6 2 *
6 3 *
6 4 *
7 0 *
7 1 *
7 2 *
7 3 *
7 4 *
8 0 *
8 1 *
8 2 *
8 3 *
8 4 *
9 0 *
9 1 *
9 2 *
9 3 *
9 4 *
10 0 *
10 1 *
10 2 *
10 3 *
10 4 *
11 0 *
11 1 *
11 2 *
11 3 *
11 4 *
12 0 *
12 1 B
12 2 Al
12 3 *
12 4 *
13 0 *
13 1 C
13 2 Si
13 3 *
13 4 *
14 0 *
14 1 N
14 2 P
14 3 *
14 4 *
15 0 *
15 1 O
15 2 S
15 3 *
15 4 *
16 0 *
16 1 F
16 2 Cl
16 3 *
16 4 *
17 0 He
17 1 Ne
17 2 *
17 3 *
17 4 *
You are getting the last added element as output because you are assigning address of string tempName to the 2d character array pointer elmName. In my suggested solution I have used static 3 dimensional character array if you wanted to use 2 dimensional array you have to assign dynamically allocated character array (string) to it instead of a statically allocated variable in your case tempName
您将获得最后一个添加的元素作为输出,因为您将字符串tempName的地址分配给2d字符数组指针elmName。在我建议的解决方案中,我使用了静态的三维字符数组,如果您想使用二维数组,您必须为它分配动态分配的字符数组(string),而不是在您的示例tempName中分配的静态变量
#2
0
You should use Array of pointers to take input as line in C
. 2-D
arrays are not suitable for storing strings.
您应该使用指针数组来将输入作为C. 2-D数组中的行来存储字符串。
char *lineptr[MAXLINE]; //Array of pointers to store lines.
int readline(char* lineptr[])
{
char line[1000]; //it takes line as input
for(i=0;i<MAXLINE;i++){
if(fscanf(stdin,"%s",line)){
char *temp=malloc((strlen(line)+1)*sizeof(char)); //allocates memory for temp to store new line in every loop
strcpy(temp,line); //copies line to temp
lineptr[i]=temp; //lineptr[i] will point to new line i.e temp
}
}
return i; //returns the number of lines
}
After taking input in lineptr[]
you can manipulate your input lines as required.
在输入lineptr[]之后,您可以根据需要操作输入行。