文本框输入的string变成char
时间: 2024-05-10 08:16:04 浏览: 4
可以通过以下方法将文本框输入的string转换为char:
```c++
string input = "Hello World";
const char* output = input.c_str();
```
这里使用了string类的c_str()方法,将string类型的变量input转换为const char*类型的变量output。注意,output是一个指向字符数组的指针,指向的字符数组包含了字符串的内容。
相关问题
文本框输入的string变成char.winform
可以使用 `ToCharArray()` 方法将 `string` 类型的变量转换为 `char` 类型的数组。
示例代码:
```csharp
string input = textBox1.Text;
char[] charArray = input.ToCharArray();
```
其中,`textBox1` 是 WinForm 中的文本框控件。
easyx表达式求值文本框输入
你可以使用EasyX图形库提供的Edit类创建一个文本框,然后在用户输入数学表达式后,使用数学库计算表达式的值,并将结果输出到窗口中。
以下是一个示例代码:
```cpp
#include <graphics.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <stack>
#include <cmath>
using namespace std;
double compute(string exp) {
stack<double> nums;
stack<char> ops;
for(int i = 0; i < exp.size(); i++) {
if(exp[i] == ' ')
continue;
if(exp[i] >= '0' && exp[i] <= '9') {
double num = 0;
while(i < exp.size() && ((exp[i] >= '0' && exp[i] <= '9') || exp[i] == '.')) {
if(exp[i] == '.') {
i++;
double frac = 1;
while(i < exp.size() && exp[i] >= '0' && exp[i] <= '9') {
frac /= 10;
num += (exp[i] - '0') * frac;
i++;
}
}
else {
num = num * 10 + (exp[i] - '0');
i++;
}
}
nums.push(num);
i--;
}
else if(exp[i] == '(')
ops.push(exp[i]);
else if(exp[i] == ')') {
while(ops.top() != '(') {
double num2 = nums.top();
nums.pop();
double num1 = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if(op == '+')
nums.push(num1 + num2);
else if(op == '-')
nums.push(num1 - num2);
else if(op == '*')
nums.push(num1 * num2);
else if(op == '/')
nums.push(num1 / num2);
}
ops.pop();
}
else {
while(!ops.empty() && ((exp[i] == '*' || exp[i] == '/') || (exp[i] == '+' || exp[i] == '-') && (ops.top() == '*' || ops.top() == '/'))) {
double num2 = nums.top();
nums.pop();
double num1 = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if(op == '+')
nums.push(num1 + num2);
else if(op == '-')
nums.push(num1 - num2);
else if(op == '*')
nums.push(num1 * num2);
else if(op == '/')
nums.push(num1 / num2);
}
ops.push(exp[i]);
}
}
while(!ops.empty()) {
double num2 = nums.top();
nums.pop();
double num1 = nums.top();
nums.pop();
char op = ops.top();
ops.pop();
if(op == '+')
nums.push(num1 + num2);
else if(op == '-')
nums.push(num1 - num2);
else if(op == '*')
nums.push(num1 * num2);
else if(op == '/')
nums.push(num1 / num2);
}
return nums.top();
}
int main() {
initgraph(640, 480);
setbkcolor(WHITE);
cleardevice();
EditBox expBox(20, 20, 400, 30, true);
expBox.SetDefaultText("请输入数学表达式");
while(1) {
if(expBox.IsEnter()) {
string exp = expBox.GetText();
double result = compute(exp);
expBox.Clear();
expBox.SetDefaultText("请输入数学表达式");
char resultStr[50];
sprintf_s(resultStr, "%.2f", result);
outtextxy(20, 60, resultStr);
}
Sleep(10);
}
getch();
closegraph();
return 0;
}
```
这个示例中使用了一个compute函数,该函数可以计算一个字符串形式的数学表达式的值。在主函数中,创建了一个EditBox对象,用于接收用户输入的数学表达式,然后调用compute函数计算表达式的值,并将结果输出到窗口中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)