C 将字符串分隔为多个整数
请告诉我如何将其转换为:C 将字符串分隔为多个整数,c,C,请告诉我如何将其转换为: char infix[] = "123+354*87/156=" (can be variable) 如何将数字从该字符串(到整数,如123 354 87 156,no到1 2 3 3 3 5 4…)和字符(chars+*/)中分离出来。您可以按照 #include<stdio.h> int main() { char infix[] = "123+354*87/156="; char *p = infix; while(1)
char infix[] = "123+354*87/156=" (can be variable)
如何将数字从该字符串(到整数,如123 354 87 156,no到1 2 3 3 3 5 4…)和字符(chars+*/)中分离出来。您可以按照
#include<stdio.h>
int main()
{
char infix[] = "123+354*87/156=";
char *p = infix;
while(1)
{
if(*p == '\0')
break;
if(*p == '+' || *p == '*' ||*p == '/' ||*p == '=' || *p == '-')
{
printf(" ");
}
else
printf("%c", *p);
p++;
}
printf("\n");
return 0;
}
当你使用C++时,每个运算符都有一个字符长度,字符串有“数字运算符数运算符…数字运算符”(这意味着从数字开始,结束运算符和开关总是在两者之间),然后使用ISTIGSTROWS:
#include <sstream>
using namespace std;
void main()
{
char infix[] = "123+345*87/156=";
istringstream is(infix);
double nums[999]; // maybe you need more than 999
char chars[999];
int nums_pos = 0;
int chars_pos = 0;
bool number = true; // begin with number
while (!is.eof())
{
if (number)
{
is >> nums[nums_pos];
nums_pos++;
number = false;
}
else
{
is >> chars[chars_pos];
chars_pos++;
number = true;
}
}
// you got nums_pos-1 numbers and chars_pos chars
}
#包括
使用名称空间std;
void main()
{
字符中缀[]=“123+345*87/156=”;
istringstream是(中缀);
双nums[999];//也许你需要更多的999
char-chars[999];
int nums_pos=0;
int chars_pos=0;
bool number=true;//以number开头
而(!is.eof())
{
如果(数字)
{
is>>nums[nums_pos];
nums_pos++;
数字=假;
}
其他的
{
是>>字符[chars_pos];
chars_pos++;
数字=真;
}
}
//你有nums\u pos-1数字和chars\u pos chars
}
我想您需要构建一个简单的计算器。。。如果你打算从头开始做这件事,你需要一些背景知识,并使用有限状态机、解析等概念
但是有很多工具可以使这项任务变得更容易:lex/yacc(C)、flex/bison(C++)或COCO/R(多种语言)
这是C语言中的一个简单示例,它将字符串拆分为数字(state=NUM)和符号(state=SYM):
#包括
#包括
#定义无0
#定义NUM 1
#定义SYM 2
int _tmain(int argc,char*argv[])
{
字符中缀[]=“123+354*87/156=”;
字符缓冲区[10];
int i,j;
int state=无;
字符c;
i=0;
j=0;
while(i
这里还有另一种可能的方法,但按照您的要求使用C
#include <stdio.h>
main()
{
char infix[] = "123+354*87/156=";
int curVal = 0;
// For each character in infix
for(char *p = infix; *p != '\0'; ++p)
{
// If it is not a ascii numeral
if(*p > '9' || *p < '0')
{
// Output value
printf("%d\n", curVal/10);
// Output char
printf("%c\n", *p);
curVal = 0;
}
else
{
// Accumulate the individual digits
curVal += (*p) - '0';
curVal *= 10;
}
}
}
这被称为“词法分析”或“标记化”,是计算机科学中研究得很好的问题。您希望如何格式化数据?你尝试过什么?看看strtok:我把它保存到令牌(struct-int和char)中,但是在转换int(strtol)然后获取chart时仍然有问题。谢谢,是的,我正在搜索这个。。。我最终使用了与此类似的代码
#include <string.h>
#include <ctype.h>
#define NONE 0
#define NUM 1
#define SYM 2
int _tmain(int argc, char* argv[])
{
char infix[] = "123+354*87/156=";
char buffer[10];
int i, j;
int state = NONE;
char c;
i = 0;
j = 0;
while(i < strlen(infix)) {
c = infix[i];
switch(state) {
case NUM:
if ( isdigit(c) ) {
buffer[j++] = c;
buffer[j] = 0;
i++;
}
else {
printf("%s\n", buffer);
j = 0;
state = NONE;
}
break;
case SYM:
i++;
printf("%c\n", c);
state = NONE;
break;
case NONE:
if ( isdigit(c) ) state = NUM;
else state = SYM;
break;
}
}
getchar();
return 0;
}
#include <stdio.h>
main()
{
char infix[] = "123+354*87/156=";
int curVal = 0;
// For each character in infix
for(char *p = infix; *p != '\0'; ++p)
{
// If it is not a ascii numeral
if(*p > '9' || *p < '0')
{
// Output value
printf("%d\n", curVal/10);
// Output char
printf("%c\n", *p);
curVal = 0;
}
else
{
// Accumulate the individual digits
curVal += (*p) - '0';
curVal *= 10;
}
}
}
123
+
354
*
87
/
156
=