簡體   English   中英

C++中字符串的加法和乘法

[英]addition and multiplication of string in C++

我正在做我的 C++ 作業,但我無法弄清楚算法。

我必須制作一個使用字符串操作的程序。

字符串和運算符(+ 和 *)應該用空格('')來區分

乘法先於加法運算

+) 使用 atoi 將字符串更改為 integer

例如:

輸入:abc + b * 4 + xy * 2 + z

OUTPUT:abcbbbbxyxyz

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

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include<sstream>
using namespace std;

enum classify {NUMBER, STRING, OPERATION, SPACE};

int char_type(string c)
{
        if (c >= "0" && c <= "9") return NUMBER;
        else if (c == "*" || c == "+") return OPERATION;
        else if (c == " ") return SPACE;
        else return STRING;
}

int main(void)
{
        string input;
        getline(cin, input);
        istringstream token(input);
        string buffer;

        while (getline(token, buffer, ' '))
                { after I classify them using enum, how can I
                  let the computer to know "multiplication first"? }
}

我無法弄清楚算法。

一種規范的方法是將您的中綴數學表達式轉換為逆波蘭表示法,然后對其進行評估。 有關完整詳細信息,請參閱https://en.wikipedia.org/wiki/Shunting-yard_algorithm

算法:

  • 將輸入字符串轉換為標記。 標記由空格分隔。 例如,“abc + b * 4 + xy * 2 + z”轉換為標記“abc”、“b”、“ ”、“4”、“+”、“xy”、“ ”、“2”、 “+”和“z”。
  • 執行乘法運算。
    • Go 通過令牌一一對應。
    • 如果標記是乘號(“*”):
      • 確定它之前或之后的標記是數字。
      • 如果前面的token是數字:
        • 創建一個臨時字符串,將之后的標記(字母)重復添加到臨時字符串之前的標記(數字)指定的次數。
      • 如果后面的token是數字:
        • 創建一個臨時字符串,將之前的標記(字母)重復添加到臨時字符串中,達到標記之后的標記(數字)指定的次數。
      • 將之前的標記設置為臨時字符串。
      • 使當前標記(乘號)和下一個標記為空標記。
    • 刪除空標記。
  • 執行加法操作。
    • Go 通過令牌一一對應。
    • 如果標記是加號(“*”):
      • 將 after 標記連接到之前標記的末尾。
      • 使當前標記(乘號)和下一個標記為空標記。
    • 刪除空標記。
  • 將標記轉換回字符串。

代碼:

#include <iostream>
#include <string>
#include <vector>

typedef std::string Token;
typedef std::vector<Token> Tokens;

Tokens tokenise(std::string str)
{
    Tokens tokens;

    int startOfToken = 0;

    // Move through until you find a space (" ") and then add the token (the start of the token is the previous space) and the end is the current space
    for (int i = 0; i < str.size(); i++)
    {
        if (str.substr(i, 1) == " ")
        {
            tokens.push_back(str.substr(startOfToken, i - startOfToken));

            startOfToken = i + 1;
        }
    }

    // Add last token (there is no space after it to mark it)
    tokens.push_back(str.substr(startOfToken));

    return tokens;
}

bool containsNumber(Token token)
{
    if (token.find("0") != std::string::npos)
    {
        return true;
    }
    else if (token.find("1") != std::string::npos)
    {
        return true;
    }
    else if (token.find("2") != std::string::npos)
    {
        return true;
    }
    else if (token.find("3") != std::string::npos)
    {
        return true;
    }
    else if (token.find("4") != std::string::npos)
    {
        return true;
    }
    else if (token.find("5") != std::string::npos)
    {
        return true;
    }
    else if (token.find("6") != std::string::npos)
    {
        return true;
    }
    else if (token.find("7") != std::string::npos)
    {
        return true;
    }
    else if (token.find("8") != std::string::npos)
    {
        return true;
    }
    else if (token.find("9") != std::string::npos)
    {
        return true;
    }

    return false;
}

Tokens removeEmptyTokens(Tokens tokens)
{
    Tokens newTokens;

    for (int i = 0; i < tokens.size(); i++)
    {
        // Only add token to new tokens if it isn't empty
        if (tokens[i] != "")
        {
            newTokens.push_back(tokens[i]);
        }
    }

    return newTokens;
}

Tokens performMultiplicationOperations(Tokens tokens)
{
    // Iterate through the tokens
    for (int i = 0; i < tokens.size(); i++)
    {
        // If a token is a multiplication sign ("*")
        if (tokens[i] == "*")
        {
            // Store token before sign and after sign
            std::string* previousToken = &tokens[i - 1];
            std::string* nextToken = &tokens[i + 1];

            // Find out whether the previous token or the next token is a number
            bool tokenThatIsNumber = containsNumber(*nextToken); // False if previous token is a number, true if next token is a number

            // If previous token is a number:
            if (tokenThatIsNumber == false)
            {
                // Convert previous token to a number
                int previousTokenAsInteger = std::stoi(*previousToken);

                // Temp variable to store final string
                std::string tempString;

                // Add next token to temp string as many times as previous token specifies
                for (int j = 0; j < previousTokenAsInteger; j++)
                {
                    tempString += *nextToken;
                }

                // Set previous token to temp string
                *previousToken = tempString;

                // Get rid of current token ("*") and next token
                tokens[i] = "";
                *nextToken = "";
            }
            // If next token is a number:
            else
            {
                // Convert next token to a number
                int nextTokenAsInteger = std::stoi(*nextToken);

                // Temp variable to store final string
                std::string tempString;

                // Add previous token to temp string as many times as next token specifies
                for (int j = 0; j < nextTokenAsInteger; j++)
                {
                    tempString += *previousToken;
                }

                // Set previous token to temp string
                *previousToken = tempString;

                // Make current token ("*") and next token empty
                tokens[i] = "";
                *nextToken = "";
            }
        }
    }

    // Remove empty tokens
    tokens = removeEmptyTokens(tokens);

    return tokens;
}

Tokens performAdditionOperations(Tokens tokens)
{
    // Iterate through the tokens
    for (int i = 0; i < tokens.size(); i++)
    {
        // If a token is an addition sign ("+")
        if (tokens[i] == "+")
        {
            // Store token before sign and after sign
            std::string* previousToken = &tokens[i - 1];
            std::string* nextToken = &tokens[i + 1];

            // Concatenate next token onto end of previous token
            *previousToken += *nextToken;

            // Make current token ("+") and next token empty
            tokens[i] = "";
            *nextToken = "";
        }
    }

    // Remove empty tokens
    tokens = removeEmptyTokens(tokens);

    return tokens;
}

std::string tokensToString(Tokens tokens)
{
    std::string tempString;

    for (int i = 0; i < tokens.size(); i++)
    {
        tempString += tokens[i];
    }

    return tempString;
}

int main()
{
    // Get user input
    std::string input = "";

    std::cout << "Please enter something: ";

    std::getline(std::cin, input);

    // Tokenise
    Tokens tokens = tokenise(input);

    // Perform multiplication operations
    tokens = performMultiplicationOperations(tokens);

    // Perform addition operations
    tokens = performAdditionOperations(tokens);

    // Convert tokens to strings
    std::string finalString = tokensToString(tokens);

    std::cout << finalString;
}

我實際上是 C++ 的新手,所以我很難理解 Toggy Smith 的代碼

但我能夠讓它更短!

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

enum classify { NUMBER, STRING };
typedef vector<string> Tokens;

Tokens tokenize(string input, string token)
{
    istringstream list(input);
    Tokens v;
    while (getline(list, token, ' '))
        v.push_back(token);
    return v;
}

int char_type(string c)
{
    if (c >= "0" && c <= "9") return NUMBER;
    else return STRING;
}

Tokens remove_empty(Tokens tokens)
{
    Tokens newTokens;
    for (int i = 0; i < tokens.size(); i++)
    {
        if (tokens[i] != "")
        {
            newTokens.push_back(tokens[i]);
        }
    }
    return newTokens;
}

Tokens multiply(Tokens tokens)
{
    for (int i = 0; i < tokens.size(); i++)
    {
        if (tokens[i] == "*")
        {
            string* left = &tokens[i - 1];
            string* right = &tokens[i + 1];
            int left_type = char_type(*left);
            int right_type = char_type(*right);

            if (left_type == NUMBER)
            {
                int number = stoi(*left);
                string tempString;
                for (int j = 0; j < number; j++)
                    tempString += *right;
                *left = tempString;
                tokens[i] = "";
                *right = "";
            }
            else if (right_type == NUMBER)
            {
                int number = stoi(*right);
                string tempString;
                for (int j = 0; j < number; j++)
                    tempString += *left;
                *left = tempString;
                tokens[i] = "";
                *right = "";
            }
        }
    }
    tokens = remove_empty(tokens);
    return tokens;
}

Tokens add(Tokens tokens)
{
    for (int i = 0; i < tokens.size(); i++)
    {
        if (tokens[i] == "+")
        {
            string* left = &tokens[i - 1];
            string* right = &tokens[i + 1];
            *left += *right;
            tokens[i] = "";
            *right = "";
        }
    }
    tokens = remove_empty(tokens);
    return tokens;
}

string tok_to_str(Tokens tokens)
{
    string tempString;
    for (int i = 0; i < tokens.size(); i++)
        tempString += tokens[i];
    return tempString;
}

int main(void)
{
    string input;
    cout << "입력 >> ";
    getline(cin, input);

    istringstream list(input);
    string token;

    // Tokenize
    Tokens tokens = tokenize(input, token);

    // Multiplication
    tokens = multiply(tokens);

    // Addition
    tokens = add(tokens);

    // Tokens to Strings
    string output = tok_to_str(tokens);

    cout << output;
}

暫無
暫無

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

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