cpp_string_cc - 8BitsCoding/RobotMentor GitHub Wiki

문제

Have the function StringCalculate(str) take the str parameter being passed and evaluate the mathematical expression within in. The double asterisks (**) represent exponentiation.

For example, if str were "(2+(3-1)*3)**3" the output should be 512. Another example: if str is "(2-0)(6/2)" the output should be 6. There can be parenthesis within the string so you must evaluate it properly according to the rules of arithmetic. The string will contain the operators: +, -, /, , (, ), and **. If you have a string like this: #/## or #+#(#)/#, then evaluate from left to right. So divide then multiply, and for the second one multiply, divide, then add. The evaluations will be such that there will not be any decimal operations, so you do not need to account for rounding.


풀이

string Find

parFerm=str.find(')'))!=string::npos

string replace

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

int main() {
	// your code goes here
	string s = "abcde";
	cout << s.substr(0, 2);
    // ab
	return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
	// your code goes here
	string s = "abcde";
	cout << s.replace(1, 2, "zz");
    // azzde
	return 0;
}
// 괄호 처리
int parFerm;
while((parFerm=str.find(')'))!=string::npos)
{
    int parOuvr;
    for(parOuvr=parFerm-2; str[parOuvr]!='('; parOuvr--);
    stringstream ss;
    // parOuvr = '(' 위치
    // parFerm = ')' 위치
    // (9*9) -> parOuvr = 0 / parFerm = 4
    ss << eval(str.substr(parOuvr+1,parFerm-parOuvr-1));
    str.replace(parOuvr,parFerm-parOuvr+1,ss.str());
}

전체코드

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <cmath>
using namespace std;

double eval(string str)
{
  if(str.find('+')==string::npos&&str.find('-',1)==string::npos&&
     str.find('*')==string::npos&&str.find('/')==string::npos)
  {
        // 사칙연산이 없다면 리턴
        return strtol(str.c_str(),NULL,10);
  }

    // 괄호 처리
  int parFerm;
  while((parFerm=str.find(')'))!=string::npos)
  {
    int parOuvr;
    for(parOuvr=parFerm-2;str[parOuvr]!='(';parOuvr--);
    stringstream ss;
    ss<<eval(str.substr(parOuvr+1,parFerm-parOuvr-1));
    str.replace(parOuvr,parFerm-parOuvr+1,ss.str());
  }

    // 9-3 -> 9+-3 변경
  for(int i=1;i<str.length();i++)
  {
    if(str[i]=='-'&&'0'<=str[i-1]&&str[i-1]<='9')
    {
      str.insert(i++,"+");
    }
  }

  // 사칙연산 처리
  int plusPos;
  if((plusPos=str.find('+'))!=string::npos)
  {
    return eval(str.substr(0,plusPos))+eval(str.substr(plusPos+1));
  }
  int multPos;
  if((multPos=str.find('*'))!=string::npos&&str[multPos+1]!='*')
  {
    return eval(str.substr(0,multPos))*eval(str.substr(multPos+1));
  }
  int divPos;
  if((divPos=str.find('/'))!=string::npos)
  {
    for(divPos=str.length()-2;str[divPos]!='/';divPos--);
    return eval(str.substr(0,divPos))/eval(str.substr(divPos+1));
  }
  int powPos=str.find("**");
  return pow(eval(str.substr(0,powPos)),eval(str.substr(powPos+2)));
}


int StringCalculate(string str) { 

  // code goes here   
  for(int i=1;i<str.length();i++)
  {
    // 9(3*3) -> 9*(3*3) 으로 변경
    if(str[i]=='('&&('0'<=str[i-1]&&str[i-1]<='9'||str[i-1]==')'))
    {
        str.insert(i++,"*");
    }
  }
  return (int)eval(str); 
            
}


int main() { 
  
  // keep this function call here
  cout << StringCalculate(gets(stdin));
  return 0;
    
}
⚠️ **GitHub.com Fallback** ⚠️