如何使用字符串进行计算! [英] How to do calculations using string!

查看:113
本文介绍了如何使用字符串进行计算!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起来很有趣.但是因为我正在开发以前的程序
写的,我必须做一个简单的计算.

有一个具有CString返回类型的函数,它通常用于
将值写入报告中.但是现在我必须从该CString值中减去一个简单的值.

Well I know it sounds funny. But because I''m working on a program that was previously
written and I have to make a simple calculation.

There is function that has a return type of CString and its is generally used to
write the value in a report. But now I have to make a simple subtraction from that CString value. Is that possible ?

推荐答案

您必须先将其转换为数字!
如果您知道字符串始终是数字并且是有效的,则只需使用 atoi [ ^ ]
如果不确定是否总是如此,请使用sscanf:
You will have to convert it to a number first!
If you know that the string is always a number, and valid, then just use atoi[^]
If you aren''t sure that it always will be, then use sscanf:
int value;
int convCount;
convCount = sscanf(strInt, "%d", &value );

然后检查convCount:如果为1,那么它就起作用了.

You can then check convCount: if it is one, then it worked.


正如已经建议的那样,您可以在执行减法之前将CString变量转换为数字1(然后转换结果)返回到CString以显示它).但是,您还应该能够找到CString的起源并修改程序(即您应该将数字值存储在数字变量中,并将其仅转换为字符串)
As already suggested you may convert the CString variable into a numeric one, before performing the subtraction (and then convert the result back to a CString in order to display it). However you should be also able to track down where the CString originates and modify the program (that is you should store numeric values in numeric variables and convert them to strings only when you need to represent them).


sscanf的替代方法是使用std :: stringstream.但是,您需要将CString转换为std :: string,反之亦然

这是来自 Codeguru 的代码片段如何执行此操作,然后我将告诉您如何使用StringStream

CString cs("Hello");

//将TCHAR字符串转换为LPCSTR
CT2CA pszConvertedAnsiString(cs);
//使用LPCSTR输入构造一个std :: string
std :: string strStd(pszConvertedAnsiString);

现在是stringstream部分.这就是我的做法.
//这是std :: string
std :: string ss("10");

//使用字符串作为输入流
istringstream astream(ss,ios :: in);

int val;

//从输入流中提取值
astream>> val;
val-= 1;

//现在将字符串用作输出流
ostringstream os(ios :: out);

//提取值
os<< val;
//从中创建一个字符串
字符串ass = os.str();
//将您的std :: string转换为CString
CString final(ass.c_str());
An alternative to sscanf is to use std::stringstream. However you need to convert CString to std::string and vice versa

Here a snippet from Codeguru how to do just that, and then I''ll tell you how to use StringStream

CString cs ("Hello");

// Convert a TCHAR string to a LPCSTR
CT2CA pszConvertedAnsiString (cs);
// construct a std::string using the LPCSTR input
std::string strStd (pszConvertedAnsiString);

And now comes the stringstream part. Here''s how I would do it.
//here''s the std::string
std::string ss("10");

//use the string as an input stream
istringstream astream(ss,ios::in);

int val;

//extract the value from the input stream
astream>>val;
val -= 1;

//now use the string as an output stream
ostringstream os(ios::out);

//extract the value
os<<val;
//create a string out of it
string ass = os.str();
//convert your std::string to a CString
CString final(ass.c_str());


这篇关于如何使用字符串进行计算!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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