我正在做C++作业,但无法弄清楚算法。
我必须编写一个使用字符串操作的程序。
字符串和运算符(+和*)应以空格('')区分
乘法先于加法
+)使用atoi将字符串更改为整数
例如 :
输入:abc + b * 4 + xy * 2 + z
输出: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"? }
}
最佳答案
算法:
代码:
#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++ - C++中字符串的加法和乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61027763/