stl vector를 활요한 수식 연산 관련해서 질문입니다.

balgarac1의 이미지

코드만 보시면 이해하기 힘들 것 같아서 문제에 관련된 문서와 소스는 첨부시켜 놓겠습니다.
3 + 5 * 6 이런 수식을 연산하는 계산기를 만드는 프로그래밍 입니다.

NumberElement* elem1 = EvaluateExpression(); <- 문제가 되는 함수
입니다.

class AssessSciCal : public SciCal
{
public:
	AssessSciCal():SciCal("")
	{
 
	}
 
	void create_assessment(std::string name, std::string results)
	{
		std::stringstream sstream;
		sstream << name << "_" << results << ".assess";
 
		std::ofstream fout;
		fout.open(sstream.str());
		fout.close();
	}
 
	void Assess01_02()
	{
		m_expressionList.clear();
		m_ElementList.clear();
		m_expressionList = std::string("3 * ( 2 + 1 ) =");
		ParseExpression(m_expressionList);
		NumberElement* elem1 = EvaluateExpression(); <- 문제가 되는 함수
		NumberElement* elem2 = new IntegerNumber(9);
 
		if (elem1->get_val().integer == elem2->get_val().integer)
			create_assessment("pa02_02", "PASS");
		else
			create_assessment("pa02_02", "FAIL");
	}
};

EvaluateExpression(); <- 문제가 되는 함수가 있는 클래스 부분입니다.
디버깅 해본 결과 문제가 되는 부분은..

class SciCal
{
public:
	SciCal(std::string expression);//뒤에서 만들어주면 되겠구만
 
public:
	// You don't have to implement the void ParseExpression(unsigned int idx) function
	// void ParseExpression(unsigned int idx);
 
	// You should implement the  ParseExpression function. [Mandatory]
	void ParseExpression(std::string expr);
 
	// You may use PushNumber function. [Not Mandatory]
	void PushNumber(std::string number);
 
	// This function prints each element in m_ElementList [Mandatory]
	void PrintElements();
	// This function prints the elements from the given list [Mandatory]
	void PrintElements(std::vector<Element*>& list);
 
	// This function prints the elements from the given list [Mandatory]
	std::vector<Element*> TransformPostfix(std::vector<Element*> element_list);
 
	// This function prints the elements from the given list [Mandatory]
	NumberElement* EvaluateExpression();
 
 
 
	OperationElement* GetOperationElement(char op);
	std::vector<Element*> GetElements() { return m_ElementList; }
 
protected:
	// You don't have to store the expression in the m_expressionList
	// std::vector<std::string> m_expressionList;
 
	// m_expressionList stores the given string expression using m_expressionList variable.
	// You must use it. [Mandatory]
	std::string m_expressionList;
	// m_ElementList stores elements of the expression.
	// You must use it. [Mandatory]
	std::vector<Element*> m_ElementList;
 
	std::vector<Element*> m_Postfixelements;
	TheStack* m_stack;
};

// This function prints the elements from the given list [Mandatory]
NumberElement* SciCal::EvaluateExpression()
{
	std::vector<NumberElement*> stack;
 
	for (std::vector<Element*>::iterator iter = m_Postfixelements.begin(); <- 이 부분에서 시작주소를 받아오지 못하고 바로 for문을 탈출합니다. 
	iter != m_Postfixelements.end(); ++iter)
	{
		if ((*iter)->get_type() != Element::OP)
			stack.push_back((NumberElement*)*iter);
		else {
			NumberElement* elem1 = stack.back();
			stack.pop_back();
			NumberElement* elem2 = stack.back();
			stack.pop_back();
 
			OperationElement* opelem = (OperationElement*)(*iter);
			NumberElement* evalelem = opelem->Evaluate(elem2, elem1);
 
			stack.push_back(evalelem);
		}
	}
	return stack.back(); <- 그래서 이 부분에서 제대로 된 반환을 하지 못하고 프로그램이 경고창을 띄우면서 종료됩니다. 
}

왜 그런걸까요?? ㅜ 조언 부탁드립니다.

File attachments: 
첨부파일 크기
Package icon scical.zip93.33 KB
jick의 이미지

m_Postfixelements가 비어 있는 거 아닌가요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.