C语言中如何查找数组中的元素

发布于 2024-09-03 19:17:00 字数 2318 浏览 5 评论 0原文

我试图找到数组中元素的位置。 我尝试使用我生成的这段代码

for(i=0;i<10;i++)
    {
    if (strcmp(temp[0],varptr[i])==0) j=i;
    }

varptr 是一个指向数组 var[11][10] 的指针,它是由 *varptr[11][10] 定义的。我已将字符串分配给 var[i],并且我想获取元素的“i”号而不是地址。

感谢您的任何评论。

编辑: temp 也是一个指针,指向我要检查的字符串。我还使用二维数组来保存变量名称及其地址。所以是的,我想将它保存在二维数组中。问题是这段代码根本不起作用,它没有将 i 分配给 j,所以我想知道这个想法的问题出在哪里?编写“中断”不会改变代码是否有效,它只是稍微优化了代码。

完整代码:

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

double atof(char*);
int main(void)
{
    char in[100], *temp[10],var[11][10],*varptr[11][10];
    int i,j, n = 0,fullval=0;
    double val[11];
    strcpy(var[11], "ans");
    for(i=0;i<11;i++)
    {
        for(j=0;j<10;j++) varptr[i][j]=&var[i][j];
    }
START:
    printf("Enter the expression: ");
    fflush(stdout);
    for(i=0;i<10;i++) temp[i]=NULL;

    if (fgets(in, sizeof in, stdin) != NULL)
    {
        temp[0] = strtok(in, " ");

        if (temp[0] != NULL)
        {
            for (n = 1; n < 10 && (temp[n] = strtok(NULL," ")) != NULL; n++)
                ;
        }
        if (*temp[0]=="quit")
        {
            goto FINISH;}

        if (isdigit(*temp[0]))
        {

            if (*temp[1]=='+') val[0] = atof(temp[0])+atof(temp[2]);
            else if (*temp[1]=='-') val[0] = atof(temp[0])-atof(temp[2]);
            else if (*temp[1]=='*') val[0] = atof(temp[0])*atof(temp[2]);
            else if (*temp[1]=='/') val[0] = atof(temp[0])/atof(temp[2]);
            printf("%s = %f\n",var[11],val[0]);
            goto START;

        }
        else
            if (temp[1]==NULL) //asking the value of a variable
            {
            for(i=0;i<10;i++)
            {
            if (strcmp(temp[0],varptr[i])==0) j=i;
            }
            printf("%s = %d\n",var[j],val[j]);
            goto START;
            }
            if (*temp[1]==61)
            {
            strcpy(var[fullval], temp[0]);
            if ((temp[3])!=NULL)
            {
            }
            val[fullval]=atof(temp[2]);
            printf("%s = %f\n",var[fullval],val[fullval]);
            fullval++;
            goto START;
            }
            if (*temp[1]!=61)
            {


            }

    }
    getch();
FINISH:
    return 0;


}

I am trying to find the location of an element in the array.
I have tried to use this code i generated

for(i=0;i<10;i++)
    {
    if (strcmp(temp[0],varptr[i])==0) j=i;
    }

varptr is a pointer which points to array var[11][10] and it is by the definition *varptr[11][10]. I have assigned strings to var[i] and i want to get the "i" number of my element NOT THE ADRESS.

Thanks for any comment.

EDit: temp is also a pointer which points to the string that i want to check. Also i am using the 2D array for keeping variable names and their address. So yes i want to keep it inside a 2D array. The question is this code is not working at all, it does not assigns i to j, so i wonder where is the problem with this idea? writing a "break" does not change if the code works or not, it just optimizes the code a little.

Full Code:

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

double atof(char*);
int main(void)
{
    char in[100], *temp[10],var[11][10],*varptr[11][10];
    int i,j, n = 0,fullval=0;
    double val[11];
    strcpy(var[11], "ans");
    for(i=0;i<11;i++)
    {
        for(j=0;j<10;j++) varptr[i][j]=&var[i][j];
    }
START:
    printf("Enter the expression: ");
    fflush(stdout);
    for(i=0;i<10;i++) temp[i]=NULL;

    if (fgets(in, sizeof in, stdin) != NULL)
    {
        temp[0] = strtok(in, " ");

        if (temp[0] != NULL)
        {
            for (n = 1; n < 10 && (temp[n] = strtok(NULL," ")) != NULL; n++)
                ;
        }
        if (*temp[0]=="quit")
        {
            goto FINISH;}

        if (isdigit(*temp[0]))
        {

            if (*temp[1]=='+') val[0] = atof(temp[0])+atof(temp[2]);
            else if (*temp[1]=='-') val[0] = atof(temp[0])-atof(temp[2]);
            else if (*temp[1]=='*') val[0] = atof(temp[0])*atof(temp[2]);
            else if (*temp[1]=='/') val[0] = atof(temp[0])/atof(temp[2]);
            printf("%s = %f\n",var[11],val[0]);
            goto START;

        }
        else
            if (temp[1]==NULL) //asking the value of a variable
            {
            for(i=0;i<10;i++)
            {
            if (strcmp(temp[0],varptr[i])==0) j=i;
            }
            printf("%s = %d\n",var[j],val[j]);
            goto START;
            }
            if (*temp[1]==61)
            {
            strcpy(var[fullval], temp[0]);
            if ((temp[3])!=NULL)
            {
            }
            val[fullval]=atof(temp[2]);
            printf("%s = %f\n",var[fullval],val[fullval]);
            fullval++;
            goto START;
            }
            if (*temp[1]!=61)
            {


            }

    }
    getch();
FINISH:
    return 0;


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似最初 2024-09-10 19:17:00

一条评论:一旦找到字符串,您就可以退出循环。

#define NOT_FOUND (-1)

int j = NOT_FOUND;
int i;
for (i = 0 ; i < 11 && j == NOT_FOUND; i++)
{
    if (strncmp(temp,var[i], 10) == 0) // Nick D's comment
    {
        j = i;
    }
}

另一条评论:

我无法理解 varptr 和 var 之间的关系(请显示定义)。我在上面使用了 var ,假设它已定义:

char var[11][10];

char temp[10];

One comment: you can exit the loop as soon as you find your string.

#define NOT_FOUND (-1)

int j = NOT_FOUND;
int i;
for (i = 0 ; i < 11 && j == NOT_FOUND; i++)
{
    if (strncmp(temp,var[i], 10) == 0) // Nick D's comment
    {
        j = i;
    }
}

Another comment:

I was unable to understand how varptr and var relate to each other (please show definitions). I have used var in the above on the assumption it is defined:

char var[11][10];

also

char temp[10];
瑕疵 2024-09-10 19:17:00
j=-1;
for(i=0;i<10;i++) 
{ 
  if (strcmp(temp[0],varptr[i])==0) {j=i;break;} 
}//strcmp is not safe, try use strncmp
j=-1;
for(i=0;i<10;i++) 
{ 
  if (strcmp(temp[0],varptr[i])==0) {j=i;break;} 
}//strcmp is not safe, try use strncmp
诗笺 2024-09-10 19:17:00
int i;
int found = 0;
for (i = 0 ; i < 11 ; i++)
{
    if (strcmp(temp,var[i]) == 0) 
    {
           found = 1; 
           break;
    }
}
int i;
int found = 0;
for (i = 0 ; i < 11 ; i++)
{
    if (strcmp(temp,var[i]) == 0) 
    {
           found = 1; 
           break;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文