if문의 condition부분에다가 함수의 return값을 받으면 안되나요?(c++)

hep93의 이미지

 
      tmp3 = tmp;
        while(true){
            tmp2 = tmp;
            tmp = bigSum(tmp, tmp3);
 
            cout << "a :";
            print_bigNum(a);
            cout << ", tmp :";
            print_bigNum(tmp);
         //   cout << ", " << abscmp(a, tmp);
            cout << endl;
 
            if(abscmp(a, tmp)){
                div2 = bigSum(div2, one);
            } else{
                tmp = tmp2;
                break;
            }
        }

tmp를 일정하게 늘려나가면서 a보다 같거나 작은 최대의 tmp를 찾는 코드인데요
디버그하려고 표준출력으로 변수값을 출력해보니

...
a :80, tmp :60
a :80, tmp :70
a :80, tmp :80
Program ended with exit code: 0

이런식으로 나오는데 원래는 tmp가 70 -> 80 -> 90 -> 80 이렇게 끝나야 되거든요
그래서 위 코드에서 주석처리한부분을 주석없애고 실행시키니까

a :80, tmp :50, 1
a :80, tmp :60, 1
a :80, tmp :70, 1
a :80, tmp :80, 1
a :80, tmp :90, 0
Program ended with exit code: 0

이렇게 정상적으로 나와요... abscmp을 앞에서 실행시켰을 뿐인데 도대체 왜 결과가 다른거죠?

abscmp은 확실히 true나 false를 반환하도록 되어있는데요

bool abscmp(bigNum a, bigNum b){
    int c = MAX_DIGIT-1;
    while(a.n[geshifilter-c] == b.n[c]) {&#10;        c--;&#10;    }&#10;    if(a.n[c] &gt;= b.n[c]) return true;&#10;    else return false;&#10;}&#10;
도대체 뭐가 문제인걸까요 ㅠㅠ[/geshifilter-c]
익명 사용자의 이미지

bigNum이 어떻게 생긴 구조인지는 모르겠는데, 두 수가 같다면 a.n와 b.n은 아마도 완전히 같겠죠?
그러면 abscmp의 while루프는 언제 끝날까요? c가 유효한 값(0 이상)인 한 절대로 안끝나겠죠?
그러면 c가 0을 넘어 음수로 치달을 거고 프로그램은 이상한 데를 참조하게 되지 않을까요?

while(a.n[geshifilter-c] == b.n[c])while(c &gt; 0 &amp;&amp; a.n[c] == b.n[c])으로 고쳐보세요. 이게 문제를 해결해드릴 거라고 장담은 못 하겠습니다. 어쨌든 틀린 건 고쳐야죠.[/geshifilter-c]

hep93의 이미지

고치니까 해결됐어요 ㅠㅠ 감사합니다

hep93의 이미지

고치니까 해결됐어요 ㅠㅠ 감사합니다

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.