簡體   English   中英

C ++輸入僅接受運算符或僅數字

[英]C++ Input to only accept operator symbols or only numbers

我一直試圖將其編碼為C ++的初學者程序,以幫助我更多地了解該語言,但我真的不知道該如何解決。

基本上,程序只需要接受“ +”,“ *”,“ /”,“%”或數字,否則它將表示輸入無效。

到目前為止,這是我所做的

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string x;

    cout<<"Enter expression: ";
    getline(cin,x);



    if(x.find('*') != string::npos){

        cout<<"Multiplication";
    }else if(x.find('/') != string::npos){

        cout<<"Division";
    }else if(x.find('%') != string::npos){

        cout<<"Modulo";
    }else if(x.find('+') != string::npos){

        cout<<"Addition";
    }else{

        cout<<"Invalid!";
}
    return 0;
}

有效輸入的定義

在這里,我假設有效輸入是由以下自然語句給出的。 首先,正如您提到的,

  • 每個輸入必須由*/%+以及一個整數構成

  • 只能執行零或一次操作。 (因此1+1是有效的。)

對於整數輸入,我還假設

  • 輸入字符串的左側和右側均允許使用空格字符

  • 不允許在非空格字符之間使用空格字符。

  • 第一非空白字符必須是01 ,..., 9-負整數)。

  • 第二和隨后的非空白字符必須是01 ,..., 89

注意,在我的假設中,正號字符+ ,小數點字符. 不允許用於整數輸入。 例如,在此定義中,

  • "123"" 123""123 "" -123 "都是有效的整數輸入。
  • "abc""123a"" 1 23""+123""1.0"都是無效的。

整數的有效性檢查功能

首先,要檢查整數輸入的有效性,我們使用以下修整函數對輸入進行修整並除去左右空格:(如果可以使用C ++ 17,則從性能上更std::string_view觀點。)

#include <string>

std::string trimLR(const std::string& str)
{
    const auto strBegin = str.find_first_not_of(" \f\n\r\t\v");
    if (strBegin == std::string::npos){
        return "";
    }

    const auto strEnd = str.find_last_not_of(" \f\n\r\t\v");
    const auto strRange = strEnd - strBegin + 1;

    return str.substr(strBegin, strRange);
}

接下來,我們定義以下簡單的有效性檢查函數isInteger ,它檢查傳遞的字符串是否為整數。 在這里, std::isdigit可用於檢查每個字符是否為數字。 請注意, 過去的 帖子中提出了各種有趣的方法。

#include <string>
#include <algorithm>

bool isInteger(const std::string& s)
{
    const auto ts = trimLR(s);

    if(ts.empty()){
        return false;
    }

    const std::size_t offset = (ts[0] == '-') ? 1 : 0;
    const auto begin = ts.cbegin() + offset;

    return (begin != ts.cend()) // false if s is just a negative sign "-"
              && std::all_of(begin, ts.cend(), [](unsigned char c){
                     return std::isdigit(c);
                 });
}

主功能

現在,輕松實現主要功能。 以下代碼將檢查輸入並正常工作。 接下來的考慮因素是編寫測試和性能調整:

演示(乘法)

演示(部門)

演示(模數)

演示(附加)

演示(無效1)

演示(無效2)

#include <iostream>

int main()
{
    std::string x;

    std::cout << "Enter expression: ";
    std::getline(std::cin, x);

    const auto optPos = x.find_first_of("*/%+");

    if (optPos == std::string::npos)
    {
        if(isInteger(x)){
            std::cout << "Valid input, " << x;
        }
        else{
            std::cout << "Invalid input, " << x;
        }

        return 0;
    }

    const auto left  = x.substr(0, optPos);
    const auto opt   = x.substr(optPos, 1);
    const auto right = x.substr(std::min(optPos+1, x.length()-1));

    if (!isInteger(left) || !isInteger(right))
    {
        std::cout
            << "Either `" << left << "`, `" << right 
            << "` or both are invalid inputs." << std::endl;

        return 0;
    }

    const auto leftVal  = std::stod(left);
    const auto rightVal = std::stod(right);

    if(opt == "*")
    {
        std::cout 
            << "Multiplication: " 
            << x << " = " << (leftVal * rightVal);
    }
    else if(opt == "/")
    {
        std::cout 
            << "Division: " 
            << x << " = " << (leftVal / rightVal);
    }
    else if(opt == "%")
    {
        std::cout 
            << "Modulo: " 
            << x << " = " << (std::stoi(left) % std::stoi(right));
    }
    else if(opt == "+")
    {
        std::cout 
            << "Addition: " 
            << x << " = " << (leftVal + rightVal);
    }

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM