C++ MFC 将部分字符串数组转换为一个整数
C++ MFC Converting part of string array into one integer
问题来了。我正在尝试编写一个类似于所有计算机中的计算器。它应该从一个 EditBox 中获取值,进行所有需要的计算,然后显示在另一个 EditBox 中。例如:3*6/2;结果:9;
我成功做到了:
double rezultatas = 0;
double temp = 0;
// TODO: Add your control notification handler code here
UpdateData(TRUE);
for (int i = 0; i < seka_d.GetLength(); i++)
{
if (seka_d[i] == '/' || seka_d[i] == '*')
{
if (seka_d[i] == '*')
{
temp = (seka_d[i - 1] - '0') * (seka_d[i + 1] - '0');
}
if (seka_d[i] == '/')
{
temp = (seka_d[i - 1] - '0') / (seka_d[i + 1] - '0');
}
//temp = (seka_d[i - 1] - '0') / (seka_d[i + 1] - '0');
}
if (seka_d[i] == '+' || seka_d[i] == '-')
{
if (seka_d[i] == '-')
{
temp = (seka_d[i - 1] - '0') - (seka_d[i + 1] - '0');
}
if (seka_d[i] == '+')
{
temp = (seka_d[i - 1] - '0') + (seka_d[i + 1] - '0');
}
//temp = (seka_d[i - 1] - '0') + (seka_d[i + 1] - '0');
}
if (seka_d[i] == '-')
{
temp = (seka_d[i - 1] - '0') - (seka_d[i + 1] - '0');
}
//rezultatas++;
}
result_d = temp;
UpdateData(FALSE);
它检查字符串 seka_d 中是否存在任何符号,例如“*”、“-”、“/”、“+”,然后对两个相邻符号进行运算(例如 1+2,总和 1和 2)(我知道它还不能在多个操作中正常工作),但现在我还必须使用双精度操作,所以我想知道是否可以将字符串的一部分转换为整数或双精度(例如 0.555+ 1.766)。想法是从开始到符号(从开始到'+')和从符号到字符串结尾或另一个符号(例如,如果字符串是 0.555+1.766-3.445,它将从'+' 直到 '-')。这样可以吗?
您可以使用 CString::Tokenize
https://msdn.microsoft.com/en-us/library/k4ftfkd2.aspx
或转换为std::string
std::string s = seka_d;
这里是MFC
例子:
void foo()
{
CStringA str = "1.2*5+3/4.1-1";
CStringA token = "/+-*";
double result = 0;
char operation = '+'; //this initialization is important
int pos = 0;
CStringA part = str.Tokenize(token, pos);
while (part != "")
{
TRACE("%s\n", part);
double number = atof(part);
if (operation == '+') result += number;
if (operation == '-') result -= number;
if (operation == '*') result *= number;
if (operation == '/') result /= number;
operation = -1;
if (pos > 0 && pos < str.GetLength())
{
operation = str[pos - 1];
TRACE("[%c]\n", operation);
}
part = str.Tokenize(token, pos);
}
TRACE("result = %f\n", result);
}
注意,这不处理括号。例如a*b+c*d
就是((a*b)+c)*d
Window的计算器做同样的事情。
问题来了。我正在尝试编写一个类似于所有计算机中的计算器。它应该从一个 EditBox 中获取值,进行所有需要的计算,然后显示在另一个 EditBox 中。例如:3*6/2;结果:9; 我成功做到了:
double rezultatas = 0;
double temp = 0;
// TODO: Add your control notification handler code here
UpdateData(TRUE);
for (int i = 0; i < seka_d.GetLength(); i++)
{
if (seka_d[i] == '/' || seka_d[i] == '*')
{
if (seka_d[i] == '*')
{
temp = (seka_d[i - 1] - '0') * (seka_d[i + 1] - '0');
}
if (seka_d[i] == '/')
{
temp = (seka_d[i - 1] - '0') / (seka_d[i + 1] - '0');
}
//temp = (seka_d[i - 1] - '0') / (seka_d[i + 1] - '0');
}
if (seka_d[i] == '+' || seka_d[i] == '-')
{
if (seka_d[i] == '-')
{
temp = (seka_d[i - 1] - '0') - (seka_d[i + 1] - '0');
}
if (seka_d[i] == '+')
{
temp = (seka_d[i - 1] - '0') + (seka_d[i + 1] - '0');
}
//temp = (seka_d[i - 1] - '0') + (seka_d[i + 1] - '0');
}
if (seka_d[i] == '-')
{
temp = (seka_d[i - 1] - '0') - (seka_d[i + 1] - '0');
}
//rezultatas++;
}
result_d = temp;
UpdateData(FALSE);
它检查字符串 seka_d 中是否存在任何符号,例如“*”、“-”、“/”、“+”,然后对两个相邻符号进行运算(例如 1+2,总和 1和 2)(我知道它还不能在多个操作中正常工作),但现在我还必须使用双精度操作,所以我想知道是否可以将字符串的一部分转换为整数或双精度(例如 0.555+ 1.766)。想法是从开始到符号(从开始到'+')和从符号到字符串结尾或另一个符号(例如,如果字符串是 0.555+1.766-3.445,它将从'+' 直到 '-')。这样可以吗?
您可以使用 CString::Tokenize
https://msdn.microsoft.com/en-us/library/k4ftfkd2.aspx
或转换为std::string
std::string s = seka_d;
这里是MFC
例子:
void foo()
{
CStringA str = "1.2*5+3/4.1-1";
CStringA token = "/+-*";
double result = 0;
char operation = '+'; //this initialization is important
int pos = 0;
CStringA part = str.Tokenize(token, pos);
while (part != "")
{
TRACE("%s\n", part);
double number = atof(part);
if (operation == '+') result += number;
if (operation == '-') result -= number;
if (operation == '*') result *= number;
if (operation == '/') result /= number;
operation = -1;
if (pos > 0 && pos < str.GetLength())
{
operation = str[pos - 1];
TRACE("[%c]\n", operation);
}
part = str.Tokenize(token, pos);
}
TRACE("result = %f\n", result);
}
注意,这不处理括号。例如a*b+c*d
就是((a*b)+c)*d
Window的计算器做同样的事情。