Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何解释用户输入的字符串?_C_String_Embedded_Ascii_Type Conversion - Fatal编程技术网

C 如何解释用户输入的字符串?

C 如何解释用户输入的字符串?,c,string,embedded,ascii,type-conversion,C,String,Embedded,Ascii,Type Conversion,在本例中,用户输入字符串值(例如:char buffer[7]={'A'、'+'、'2'、'5'、'.'、'\r'})如何将此字符串解释为A有设备A或B有设备B、+25.5有温度值(我想将此温度值转换为整数)和回车。在解释并转换成整数之后,我需要通过接口发送(我知道如何通过接口发送)。给我一些解释方面的想法。使用==运算符来比较值 int main() { printf("Welcome to the temperature control program:\n"); printf("e

在本例中,用户输入字符串值(例如:char buffer[7]={'A'、'+'、'2'、'5'、'.'、'\r'})如何将此字符串解释为A有设备A或B有设备B、+25.5有温度值(我想将此温度值转换为整数)和回车。在解释并转换成整数之后,我需要通过接口发送(我知道如何通过接口发送)。给我一些解释方面的想法。

使用==运算符来比较值

int main()
{
printf("Welcome to the temperature control program:\n");
    printf("enter the ascii temperature characters");
    printf("enter the ascii characters of an euipment name with + or - and followed by a integer value");
    printf("type character terminated by carriage return");

    char tbuffer [tbuffer_length];
    printf("enter the temperature value");
    scanf("%7c", tbuffer);
    char *t;
    t = fgets (tbuffer, tbuffer_length, stdin );
    if(tbuffer[0] = 'A')
    {

    }
    if (tbuffer[0] = 'B')
    {

    }
    if (tbuffer[0] = 'C')
    {

    }

return 0;
}
在代码
scanf(“%7c”,tbuffer)中应该是
scanf(“%7s”,tbuffer)

另外
if(tbuffer[0]='A')
应该是
if(tbuffer[0]='A')
。其他
if
语句也是如此

要将字符串转换为整数,请尝试以下代码:

int main()
{
printf("Welcome to the temperature control program:\n");
    printf("enter the ascii temperature characters");
    printf("enter the ascii characters of an euipment name with + or - and followed by a integer value");
    printf("type character terminated by carriage return");

    char tbuffer [tbuffer_length];
    printf("enter the temperature value");
    scanf("%7c", tbuffer);
    char *t;
    t = fgets (tbuffer, tbuffer_length, stdin );
    if(tbuffer[0] == 'A')
    {

    }
    if (tbuffer[0] == 'B')
    {

    }
    if (tbuffer[0] == 'C')
    {

    }

return 0;
}
现在变量
dec
包含小数部分,
frac
包含小数部分


例如,如果
buffer[7]={'A'、'+'、'2'、'5'、'.'、'5'、'\0'}然后
dec=25
frac=5

使用
==
进行等效性测试您能告诉我如何比较温度值吗?您可以使用我的代码从字符串中提取数据。但我不明白A、B和C的目的。它们是什么?请使用更多数据和示例更新您的问题。另外,请说明A、B和C的用途。有三种设备或任务(A、B、C),因此我想解释并调用另一侧的设备(除了硬件{Aeroflex gaisler hardware})。
char temp[10];
int dec,frac; 



if(tbuffer[0] = 'A')
{
   i=2;
   j=0;
   while(tbuffer[i] != '.')
   {
      temp[j]=tbuffer[i];
      j++;
      i++;
   }
   temp[j]='\0';
   dec=atoi(temp);
   j=0;
   i++;
   while(tbuffer[i] != '\0')
   {
      temp[j]=tbuffer[i];
      j++;
      i++;
   }   
   temp[j]='\0';
   frac=atoi(temp);
}