如何将数字带入一个字符串?我正在制作计算器,按钮工作正常。 C ++

问题描述 投票:-2回答:1

我需要将数字收集到一个字符串中,例如:用户单击“ 1”,然后单击“ 2”,这加起来(直到使用数学符号)才变成一个字符串“ 12”。请我真的需要我的计算器项目的帮助(计算器只是Windows计算器的简化版本)。我正在做代码块的opengl项目。

void MouseFunc(int key, int state, int x, int y)
{
if(key==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
    cout<<"X="<<x<<"\n";
    cout<<"Y="<<y<<"\n";
}

if(x>=0 && x<150)
{
    if(y>500 && y<=600)
    cout << "C";

}

if(x>150 && x<300)
{
    if(y>500 && y<=600)
    cout << "0";
}

if(x>300 && x<450)
{
    if(y>500 && y<=600)
    cout << ".";
}

if(x>450 && x<=600)
{
    if(y>500 && y<=600)
    cout << "=";
}

if(x>=0 && x<150)
{
    if(y>400 && y<500)
    cout << "1";
}

if(x>150 && x<300)
{
    if(y>400 && y<500)
    cout << "2";
}

if(x>300 && x<450)
{
    if(y>400 && y<500)
    cout << "3";
}

if(x>450 && x<600)
{
    if(y>400 && y<500)
    cout << "+";
}

if(x>=0 && x<150)
{
    if(y>300 && y<400)
    cout << "4";
}

if(x>150 && x<300)
{
    if(y>300 && y<400)
    cout << "5";
}

if(x>300 && x<450)
{
    if(y>300 && y<400)
    cout << "6";
}

if(x>450 && x<=600)
{
    if(y>300 && y<400)
    cout << "-";
}

if(x>=0 && x<150)
{
    if(y>200 && y<300)
    cout << "7";
}

if(x>150 && x<300)
{
    if(y>200 && y<300)
    cout << "8";
}

if(x>300 && x<450)
{
    if(y>200 && y<300)
    cout << "9";
}

if(x>450 && x<600)
{
    if(y>200 && y<300)
    cout << "*";
}

if(x>=0 && x<150)
{
    if(y>100 && y<200)
    cout << "\\sqrt";
}

if(x>150 && x<300)
{
    if(y>100 && y<200)
    cout << "sqrt";
}

if(x>300 && x<450)
{
    if(y>100 && y<200)
    cout << "del";
}

if(x>450 && x<600)
{
    if(y>100 && y<200)
    cout << "/";
}

感谢所有尝试提供帮助的人。 :)

c++ string opengl
1个回答
0
投票

只需将char '0'48添加到所需的数字;

std::string str = "";
    str += 6 + '0';
    str += 8 + '0';
std::cout << str << std::endl; // Output 68

也,

我需要到星期二,所以请尽快提供帮助。

请在您落后于进度之前进行计划,并尝试避免这种表现形式。


0
投票

只需将char '0'48添加到所需的数字;

std::string str = "";
    str += 6 + '0';
    str += 8 + '0';
std::cout << str << std::endl; // Output 68
© www.soinside.com 2019 - 2024. All rights reserved.