C 我想是斯特克的问题
没办法,有什么帮助吗?我不认为这是strtok,我很确定这是我的代码。我想不出我是怎么想的。Get和Set正在导致sigsevg。如果我将printf()放在num=strtof等后面,num是正确的,但其他命令的解释不正确C 我想是斯特克的问题,c,string,strtok,C,String,Strtok,没办法,有什么帮助吗?我不认为这是strtok,我很确定这是我的代码。我想不出我是怎么想的。Get和Set正在导致sigsevg。如果我将printf()放在num=strtof等后面,num是正确的,但其他命令的解释不正确 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { float height; float width;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
float height;
float width;
float length;
}Box;
void usage(void)
{
printf("\nUsage: [command] [parameter] [amount]\n");
printf("commands:\n\tSet\n\tGet\n");
printf("parameters:\n\theight\n\twidth\n\tlength\n");
}
int main()
{
usage();
Box box1 = {0,0,0};
int loop = 1;
float num;
char cp[65], *delims = " !@#$%^&*():;/><.,\\?\"";
char *tok1, *tok2, *tok3, *temp;
beginning:
while(loop)
{
//Read the command from standard input
char str[65];
fgets(str, 64, stdin);
str[64] = 0;
//Tokenize the string
strncpy(cp, str, 64);
tok1 = strtok(cp, delims);
tok2 = strtok(NULL, delims);
tok3 = strtok(NULL, delims);
//Check if tok3 is float
num = strtof(tok3, &temp);
if(num != 0)
{
}
else
{
usage();
goto beginning;
}
if(tok1 == 'Get' && tok2 == 'height')
{
printf("%f", box1.height);
}
else if(tok1 == 'Get' && tok2 == 'width')
{
printf("%f", box1.width);
}
else if(tok1 == 'Get' && tok2 == 'length')
{
printf("%f", box1.length);
}
else if(tok1 == 'Get')
{
usage();
goto beginning;
}
if(tok1 == 'Set' && tok2 == 'height')
{
box1.height = num;
printf("%f", box1.height);
}
else if(tok1 == 'Set' && tok2 == 'width')
{
box1.width = num;
}
else if(tok1 == 'Set' && tok2 == 'length')
{
box1.length = num;
}
else if(tok1 == 'Set')
{
usage();
goto beginning;
}
}
return 0;
}
#包括
#包括
#包括
类型定义结构
{
浮动高度;
浮动宽度;
浮子长度;
}盒子;
无效用法(void)
{
printf(“\n用法:[command][parameter][amount]\n”);
printf(“命令:\n\tSet\n\tGet\n”);
printf(“参数:\n\theight\n\twidth\n\t长度\n”);
}
int main()
{
用法();
框box1={0,0,0};
int循环=1;
浮点数;
char cp[65],*delims=“!@$%^&*():;/>
C字符串必须使用双引号,您不能使用==
测试它们是否相等,您应该使用:
关于strof
:
num = strtof(tok3, &temp);
如果不必使用temp
,请使用空指针:
num = strtof(tok3, NULL);
以及使用goto
的代码段:
if(num != 0)
{
}
else
{
usage();
goto beginning;
}
goto
很难看,请改用continue
:
if(num == 0)
{
usage();
continue;
}
C字符串必须使用双引号,您不能使用==
测试它们是否相等,您应该使用:
关于strof:
num = strtof(tok3, &temp);
如果不必使用temp
,请使用空指针:
num = strtof(tok3, NULL);
以及使用goto
的代码段:
if(num != 0)
{
}
else
{
usage();
goto beginning;
}
goto
很难看,请改用continue
:
if(num == 0)
{
usage();
continue;
}
错误的标签,我不认为strtok是问题所在如果你不打算使用
temp
,你可能应该把NULL
传递给strtof
:num=strtof(tok3,NULL);
字符cp[65],*delims=“!@$^&*():;/>错误的标签,我不认为strtok是问题所在如果你不打算使用temp
,你应该把NULL
传递给strtof
:num=strtof(tok3,NULL);
字符cp[65],*delims=“!@$^&*):;/>你可以将浮点数与!=
进行比较;你只需要意识到不精确和舍入会发生,并且它们可能会导致不直观的结果。在这种情况下,没有执行任何算术运算,零是精确的,所以这个问题不会在这里发生。你们的快速回答令人难以置信。我仍然在捕捉sigsevg在num=strtof时(tok3和temp)temp
已分配,它是一个char*
。注意strof
的第二个参数是一个char**
。很抱歉,我不明白。我尝试的都是错误的this@Toxor与之相比,我尝试合并所有建议的更改。注意,我在循环末尾添加了一个loop=0;
,但这只是对于在线编译器。您可以将浮点数与!=
进行比较;您只需意识到不精确和舍入会发生,并且它们可能会导致不直观的结果。在这种情况下,没有执行任何算术运算,零是精确的,所以问题不会在这里发生。你们的快速答案令人难以置信。我仍然很担心在num=strtof(tok3和temp)时捕捉sigsevgtemp
已分配,它是一个char*
。注意strof
的第二个参数是一个char**
。很抱歉,我不明白。我尝试的都是错误的this@Toxor与之相比,我尝试合并所有建议的更改。注意,我在循环末尾添加了一个loop=0;
,但这只是用于联机编译器。