使用strtok解析浮点数字母数字字符串 [英] parsing floats out of alphanumeric strings using strtok

查看:73
本文介绍了使用strtok解析浮点数字母数字字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devc ++ 4992开发WIN32 API应用程序,该应用程序将接受Dow

Jones / NASDAQ /等。股票价格作为输入,解析它们,并用

来做事。用户只需将价格剪切并粘贴到窗口中,然后点击按钮即可处理它。


信息因此以char数组的形式进入程序。价格可以在1美元到100美元之间,包括美分。所以我们可以有价格,比如

3.01,1.56,11.57等等。字符数组是一个字母数字字符串,所以

一切都不是x.xx或者xx.xx必须被解析出来。


一旦数字被解析出来,它就会被存储在一个数组中以供以后使用。
使用。最后,我们将在稍后显示数字,并且它们必须是相同的两位小数格式的




我实际上很难得到实际数字API工作。当它b / b
读取数据时,它可能永远陷入循环,似乎永远不会是b $ b击中NULL。我把它剥了下来,写了这个小型的控制台应用程序给了b $ b试图找出它出错的地方。令人惊讶的是,这个应用似乎没有被困在一个循环中工作。


这里有一个问题我似乎无法解决。缓冲区应该是
将数字显示为两位小数,但它没有这样做。我已经花了六个小时左右的时间试图解决这个问题。我想我会在这里问一下b $ b。运行此程序时,当前缓冲区应显示

到小数点后两位...提前致谢。


#include< cstdlib>

#include< iostream>

#include< math.h>

#include< tchar.h>


使用命名空间std;


int main(int argc,char * argv [])

{

char rawtxt [] ="解析此3.50now 4.00 5.67",outtxt [] ="",

temptxt [] ="",buffer [] ="" ;;

int i = 0;

float USERX [5000];

const char delimiters [] ="

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZ - + = _,!?" ;; //解析

tchar字符串在找到这些字符时分割。

char * token;


token = strtok(rawtxt ,分隔符); //首先退回价格

of rawtxt

cout<< 这个程序应该将一个字符串解析为数字,其中包含

十进制数。 << endl<< endl;

cout<< 要解析的字符串: << rawtxt<< endl<< endl;


while(令牌!= NULL)

{

cout<< 当前令牌: <<令牌<< endl;

USERX [i] = atof(令牌);

_sntprintf(buffer,sizeof(buffer)/ sizeof(buffer [0]),

("%4.2f"),USERX [i]);

token = strtok(NULL,delimiters);

cout<< "用户X [" << i<< "]: << USERX [i]<< endl;

i ++;

cout<< 当前缓冲区: <<缓冲区<<结束;

}

系统(暂停);

返回EXIT_SUCCESS;

}

I am working on a WIN32 API app using devc++4992 that will accept Dow
Jones/NASDAQ/etc. stock prices as input, parse them, and do things with
it. The user can just cut and paste back prices into a window and hit
a button to process it.

The information thus enters the program as a char array. Prices can be
between $1 and $100, including cents. So we can have prices such as
3.01, 1.56, 11.57, etc. The char array is an alphanumeric string, so
everything that isn''t x.xx or xx.xx has to be parsed out.

Once a number gets parsed out, it gets stored in an array for later
use. Eventually, we will display the numbers later, and they must be
in the same two decimal places format.

I am having a terrible time getting the actual API to work. When it
reads data it can get stuck in the loop forever, seemingly never
hitting a NULL. I stripped it down and wrote this small console app to
try to figure out where it is going wrong. Surprise, this app seems to
work without being stuck in a loop.

There is one problem here I can''t seem to fix. The buffer should
display the number to two decimal places, but its not doing so. I''ve
been trying to figure this out for six hours or so and I thought I''d
ask here. When this program is run, the current buffer should display
to two decimal places... Thanks in advance.

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <tchar.h>

using namespace std;

int main(int argc, char *argv[])
{
char rawtxt[] = "parse this 3.50now 4.00 5.67", outtxt[] = "",
temptxt[] = "", buffer[] ="";
int i = 0;
float USERX[5000];
const char delimiters[] = "
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZ-+=_,!?"; // parse
tchar string by splitting when these chars are found.
char* token;

token = strtok (rawtxt, delimiters); // cut first back price out
of rawtxt

cout << "This program should parse a string into numbers with
decimals." << endl << endl;
cout << "string to parse: " << rawtxt << endl << endl;

while (token != NULL)
{
cout << "current token: " << token << endl;
USERX[i] = atof(token);
_sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]),
("%4.2f"), USERX[i]);
token = strtok (NULL, delimiters);
cout << "USERX[" << i << "]: " << USERX[i] << endl;
i++;
cout << "current buffer: " << buffer << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

推荐答案

1和


100,包括美分。所以我们可以有价格,比如

3.01,1.56,11.57等等。字符数组是一个字母数字字符串,所以

一切都不是x.xx或者xx.xx必须被解析出来。


一旦数字被解析出来,它就会被存储在一个数组中以供以后使用。
使用。最后,我们将在稍后显示数字,并且它们必须是相同的两位小数格式的




我实际上很难得到实际数字API工作。当它b / b
读取数据时,它可能永远陷入循环,似乎永远不会是b $ b击中NULL。我把它剥了下来,写了这个小型的控制台应用程序给了b $ b试图找出它出错的地方。令人惊讶的是,这个应用似乎没有被困在一个循环中工作。


这里有一个问题我似乎无法解决。缓冲区应该是
将数字显示为两位小数,但它没有这样做。我已经花了六个小时左右的时间试图解决这个问题。我想我会在这里问一下b $ b。运行此程序时,当前缓冲区应显示

到小数点后两位...提前致谢。


#include< cstdlib>

#include< iostream>

#include< math.h>

#include< tchar.h>


使用命名空间std;


int main(int argc,char * argv [])

{

char rawtxt [] ="解析此3.50now 4.00 5.67",outtxt [] ="",

temptxt [] ="",buffer [] ="" ;;

int i = 0;

float USERX [5000];

const char delimiters [] ="

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZ - + = _,!?" ;; //解析

tchar字符串在找到这些字符时分割。

char * token;


token = strtok(rawtxt ,分隔符); //首先退回价格

of rawtxt

cout<< 这个程序应该将一个字符串解析为数字,其中包含

十进制数。 << endl<< endl;

cout<< 要解析的字符串: << rawtxt<< endl<< endl;


while(令牌!= NULL)

{

cout<< 当前令牌: <<令牌<< endl;

USERX [i] = atof(令牌);

_sntprintf(buffer,sizeof(buffer)/ sizeof(buffer [0]),

("%4.2f"),USERX [i]);

token = strtok(NULL,delimiters);

cout<< "用户X [" << i<< "]: << USERX [i]<< endl;

i ++;

cout<< 当前缓冲区: <<缓冲区<<结束;

}

系统(暂停);

返回EXIT_SUCCESS;

}

100, including cents. So we can have prices such as
3.01, 1.56, 11.57, etc. The char array is an alphanumeric string, so
everything that isn''t x.xx or xx.xx has to be parsed out.

Once a number gets parsed out, it gets stored in an array for later
use. Eventually, we will display the numbers later, and they must be
in the same two decimal places format.

I am having a terrible time getting the actual API to work. When it
reads data it can get stuck in the loop forever, seemingly never
hitting a NULL. I stripped it down and wrote this small console app to
try to figure out where it is going wrong. Surprise, this app seems to
work without being stuck in a loop.

There is one problem here I can''t seem to fix. The buffer should
display the number to two decimal places, but its not doing so. I''ve
been trying to figure this out for six hours or so and I thought I''d
ask here. When this program is run, the current buffer should display
to two decimal places... Thanks in advance.

#include <cstdlib>
#include <iostream>
#include <math.h>
#include <tchar.h>

using namespace std;

int main(int argc, char *argv[])
{
char rawtxt[] = "parse this 3.50now 4.00 5.67", outtxt[] = "",
temptxt[] = "", buffer[] ="";
int i = 0;
float USERX[5000];
const char delimiters[] = "
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX YZ-+=_,!?"; // parse
tchar string by splitting when these chars are found.
char* token;

token = strtok (rawtxt, delimiters); // cut first back price out
of rawtxt

cout << "This program should parse a string into numbers with
decimals." << endl << endl;
cout << "string to parse: " << rawtxt << endl << endl;

while (token != NULL)
{
cout << "current token: " << token << endl;
USERX[i] = atof(token);
_sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]),
("%4.2f"), USERX[i]);
token = strtok (NULL, delimiters);
cout << "USERX[" << i << "]: " << USERX[i] << endl;
i++;
cout << "current buffer: " << buffer << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}


BGP写道:
我正在使用devc ++ 4992开发一个WIN32 API应用程序,它将接受Dow
Jones /纳斯达克/等。股票价格作为输入,解析它们,并用它来做事。用户可以将价格剪切并粘贴到窗口中并点击按钮来处理它。

信息因此作为char数组进入程序。价格可以在
I am working on a WIN32 API app using devc++4992 that will accept Dow
Jones/NASDAQ/etc. stock prices as input, parse them, and do things with
it. The user can just cut and paste back prices into a window and hit
a button to process it.

The information thus enters the program as a char array. Prices can be
between


这篇关于使用strtok解析浮点数字母数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆