有没有一个变量,可以保存字母和小数

Is there a variable, that can hold letters and decimals c++?

本文关键字:保存 小数 有一个 变量      更新时间:2023-10-16

我正在制作一个程序,如果给出一些信息,可以计算三角形,但如果我尝试使用"字母"或","然后,程序出现故障,程序需要重新启动。我目前使用双精度来保持数字和小数,但双精度的规则之一是你需要使用"。"小数,但我已经击中了","所以我想知道是否有办法解决它。我还想知道这个人是否可以选择在同一个盒子里同时写数字和字母。

#include <iostream>
#include <windows.h>
#include <math.h>
#define PI 3.14159265359
using namespace std;
void SetColor(int ForgC);
int failure = 0;
int wait;
double a, b, c, A, B, C = 90;
int main()
{
    SetColor(10);
    {
info:
        while (GetAsyncKeyState(VK_RETURN))
        {
        }
        system("cls");
        cout << "nttttFind triangle infon" << endl;
        SetColor(11);
        cout << "ttttNote: Use 0 if you don't have the number!" << endl;
        SetColor(10);
        cout << "What information do you have?";
        SetColor(11);
        cout << "tIf you use decimal numbers then use "." not ","!";
        SetColor(10);
        cout << "a = ";
        cin >> a;
        cout << "b = ";
        cin >> b;
        cout << "c = ";
        cin >> c;
        cout << "A = ";
        cin >> A;
        cout << "B = ";
        cin >> B;
        while (GetAsyncKeyState(VK_RETURN))
        {
        }
    }
    {
calculate:
        if (a == 0 && b == 0 && c == 0 && A == 0 && B == 0)
        {
            cout << "There need to be at least two numbers!n" << endl;
            system("pause");
            failure = 1;
        } // Failsafe
        else if (c != 0)
        {
            if (a >= c || b >= c)
            {
                cout << "a or b cannot be greater than c!n" << endl;
                system("pause");
                failure = 1;
            }
        } // Failsafe 2
        else if (A >= 90 || B >= 90)
        {
            cout << "A or B cannot be equal to, or greater than 90!n" << endl;
            system("pause");
            failure = 1;
            ;
        } // Failsafe 3
        if (a != 0 && c != 0)
        {
            b = sqrt(c * c - a * a);
            A = asin(a / c) * 180 / PI;
            B = 90 - A;
        }
        else if (b != 0 && c != 0)
        {
            a = sqrt(c * c - b * b);
            A = acos(b / c) * 180 / PI;
            B = 90 - A;
        }
        else if (a != 0 && b != 0)
        {
            c = sqrt(a * a + b * b);
            A = atan(a / b) * 180 / PI;
            B = 90 - A;
        }
        else if (c != 0 && A != 0)
        {
            a = c * sin(A);
            b = c * cos(A);
            B = 90 - A;
        }
        else if (c != 0 && B != 0)
        {
            A = 90 - B;
            a = c * sin(A);
            b = c * cos(A);
        }
        else if (a != 0 && B != 0)
        {
            b = a * tan(B);
            c = sqrt(a * a + b * b);
            A = 90 - B;
        }
        else if (b != 0 && A != 0)
        {
            a = b * tan(A);
            c = sqrt(a * a + b * b);
            B = 90 - A;
        }
        else if (a != 0 && A != 0)
        {
            b = a / tan(A);
            c = sqrt(a * a + b * b);
            B = 90 - A;
        }
        else if (b != 0 && B != 0)
        {
            a = b / tan(B);
            c = sqrt(a * a + b * b);
            A = 90 - B;
        }
        if (failure == 1)
        {
            goto failsafe;
        }
        goto answer;
    }
    {
failsafe:
        failure = 0;
        system("cls");
        cout << "nttttFind triangle infon" << endl;
        SetColor(11);
        cout << "ttttNote: Use 0 if you don't have the number!" << endl;
        SetColor(10);
        cout << "What information do you have?";
        SetColor(11);
        cout << "tIf you use decimal numbers then use "." not ","!";
        SetColor(10);
        cout << "a = ";
        cin >> a;
        cout << "b = ";
        cin >> b;
        cout << "c = ";
        cin >> c;
        cout << "A = ";
        cin >> A;
        cout << "B = ";
        cin >> B;
        while (GetAsyncKeyState(VK_RETURN))
        {
        }
        goto calculate;
    }
answer:
    while (1)
    {
        system("cls");
        wait = 1;
        cout << "nttttFind triangle infon" << endl;
        SetColor(11);
        cout << "ttttNote: Use 0 if you don't have the number!" << endl;
        SetColor(10);
        cout << "What information do you have?";
        SetColor(11);
        cout << "tIf you use decimal numbers then use "." not ","!";
        SetColor(10);
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
        cout << "c = " << c << endl;
        cout << "A = " << A << endl;
        cout << "B = " << B << endl;
        cout << "C = " << C << endl;
        cout << "nDo you want to write new information? (";
        SetColor(9);
        cout << "Y";
        SetColor(10);
        cout << "/N)";
        while (wait == 1)
        {
            if (GetAsyncKeyState(VK_RIGHT))
            {
                wait = 0;
            }
            else if (GetAsyncKeyState(VK_RETURN))
            {
                goto info;
            }
        }
        system("cls");
        wait = 1;
        cout << "nttttFind triangle infon" << endl;
        SetColor(11);
        cout << "ttttNote: Use 0 if you don't have the number!" << endl;
        SetColor(10);
        cout << "What information do you have?";
        SetColor(11);
        cout << "tIf you use decimal numbers then use "." not ","!";
        SetColor(10);
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
        cout << "c = " << c << endl;
        cout << "A = " << A << endl;
        cout << "B = " << B << endl;
        cout << "C = " << C << endl;
        cout << "nDo you want to write new information? (Y/";
        SetColor(9);
        cout << "N";
        SetColor(10);
        cout << ")";
        while (wait == 1)
        {
            if (GetAsyncKeyState(VK_LEFT))
            {
                wait = 0;
            }
            else if (GetAsyncKeyState(VK_RETURN))
            {
                return 0;
            }
        }
    }
}
void SetColor(int ForgC)
{
    WORD wColor;
    // We will need this handle to get the current background attribute
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    // We use csbi for the wAttributes word.
    if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
    {
        // Mask out all but the background attribute, and add in the forgournd color
        wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
        SetConsoleTextAttribute(hStdOut, wColor);
    }
    return;
}

您需要添加错误处理。

如果输入失败,您需要决定要做什么。例如,你可以打印错误信息,清除流状态并重新开始:

std::cin.exceptions(std::ios::failbit);
try
{
    std::cout << "a = ";
    std::cin >> a;
    std::cout << "b = ";
    std::cin >> b;
    std::cout << "c = ";
    std::cin >> c;
    std::cout << "A = ";
    std::cin >> A;
    std::cout << "B = ";
    std::cin >> B;
} catch(std::exception const& e)
{
    std::cerr << "Error in input.n";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max());
    goto info;
}

我衷心建议使用goto程序流(使用循环)。你也可以使用非异常错误检查:

std::cout << "a b c A B = ";
if (!(std::cin >> a >> b >> c >> A >> B))
{
    if (std::cin.eof())
    {
        std::cerr << "Goodbyen";
        exit(1);
    }
    std::cerr << "Error in input.n";
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max());
    goto info;
}

p。对于numeric_limits特征,包括

#include <limits>

除了文本输入通常棘手的错误处理,特别是在控制台上,以及有趣的gotos:为了更改数字格式(以及其他格式),您可能需要在cin(以及为了一致性,在cout)上"灌输一个区域设置"。我从来没有这样做过,但这个网站有看起来合理和简单的例子:http://www.linuxtopia.org/online_books/programming_books/c++_practical_programming/c++_practical_programming_101.html