如何在字符串中分隔数字
How to seperate numbers in a string
#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;
struct Calculations //structure to hold the numbers and operators from 'equation'
{
double num1;
char operators;
double num2;
double answer;
};
Calculations myCalculationArray[SIZE]; // the array of calculations
int main()
{
while (i = 0; i <= 5; i++;)
{
cout << "Enter equation: n";
getline(cin, equation);
cout << equation;
}
}
好吧,所以我试图建立一个计算器,用户在其中输入一个方程;22/2";,然后将第一个数字分配给num1='22',将运算符='/'分配给运算符等。
由于这似乎是学生的家庭作业,我将为您提供另一个计算器解决方案,您可以根据自己的喜好进行修改;(
#include<iostream> #include<cmath> using namespace std; int main() { char op; float num1, num2; cout << "Enter two operands: "; cin >> num1 >> op >> num2; switch(op) { case '+': cout << num1+num2; break; case '-': cout << num1-num2; break; case '*': cout << num1*num2; break; case '/': cout << num1/num2; break; default: // If the operator is other than +, -, * or /, error message is shown cout << "Error! operator is not correct"; break; } }
您可以借助strtok、strchr来解析字符串(分离出运算符号和两个数字操作数(。为了将诸如";34〃;到34,你可以使用atoi和atod等功能。
相关文章:
- 如何在 C++ 中将文件中的逗号分隔数字读取到数组中?
- 分隔整数 (C++) 的数字
- 如何从文件中读取用空格分隔的数字
- C ++正则表达式允许用点分隔的数字
- 读取以逗号分隔的数字,并将其写入以制表符分隔的文件
- C++ 从文件中读取 int,每个数字用逗号分隔
- 我如何使用CIN输入整数,每个数字都被逗号分隔
- 如何从逗号分隔的数字对中填充 2 个向量 C++.
- 为什么我可以在 C++ 中用撇号分隔数字,但不能用 C 分隔数字
- 用字符分隔数字
- 逗号分隔的数字在模拟器上正确显示,但在iPad上没有显示
- 在 cpp 中使用 getline 输入数组中的空格分隔数字
- 给定分隔成数组元素的数字,如何在 int 中重新组合数字
- 如果字符串中没有空格,如何解析带有数字和字符的字符串并分隔数字和字符
- 扫描以空格分隔的数字
- 解析逗号分隔的数字(C++)
- C++ - 如何将空格分隔的数字输入数组
- 递增由不同分隔符分隔的字符数组中的数字
- 解析来自 getline 的空格分隔数字
- 如何在C或c++代码中分隔数字常量(例如10,000)中的数字