c언어 정수를 입력받아 2진수로 표현하기 질문있습니다.!

dygks8557의 이미지

양수를 입력받았을 때에는 출력이 잘 되는데 음수를 입력받았을 때에 1의 보수까지는 했는데 2의 보수로 넘어가는 부분에서 출력이 안되네요..
제 생각으로는 이상이 없는거같은데 54~63번에 어디에 이상이 있는지 궁금합니다!
아직 지저분한 코딩이지만 이쁘게 봐주세요..ㅎㅎ

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(){
    int story[32];
    int num;
 
    printf("2진법으로 바꾸실 숫자를 적으시오. : ");
    scanf("%d",&num);
 
    int position = 0;
 
    if (num >= 0){
 
    while (1){
        story[position] = num % 2; // 2로 나눈 몫 값을 저장함
        num = num / 2; // 2로 나눈 나머지 값을 저장함
 
        position++;
 
        if (num==0) // num의 값이 0이 되면 멈춤
        break;
    }
      for (int i = position-1 ; i >= 0 ; i--) // 배열의 역순으로 출력
    {
        printf("%d", story[i]);
        if (i % 4 == 0 ) // 4개씩 나눔
        printf("  ");
    }
    }
 
else if (num < 0){
 
    num = -num; // 음수를 양수로 변환
 
    while (1){
        story[position] = num % 2; // 2로 나눈 몫 값을 저장함
        num = num / 2; // 2로 나눈 나머지 값을 저장함
 
        position++;
 
        if (num==0) // num의 값이 0이 되면 멈춤
        break;
    }
    for (int i = position-1 ; i >= 0 ; i--) // 배열의 역순으로 출력
    {
        story[i] = !story[i]; // 1의 보수로 바꿔줌
        printf("%d",story[i]);
    }
 
    printf("\n");
 
    for (int i = position-1 ; i >=0 ; i--){
        if (story[i] == 0){
            story[i] += 1; // 끝자리가 0이면 1을 더해줌 (2의 보수)
            i = -1;
        }
        else if(story[i] == 1){
            story[i] = 0;
        }
        printf("%d",story[i]);
     }
}
    printf("\n");
}
세벌의 이미지

54~63번이 어딘지 몰라요...
지금쯤은 스스로 답을 얻으셨을 거 같은데요?

kldp에서는 글 쓴 사람 본인도 글을 지울 수가 없어요~~~
한번 올린다고 했는데 가끔 두번씩 올라가는 경우가 있네요.

dygks8557의 이미지

지금쯤은 스스로 답을 얻으셨을 거 같은데요? < 헉.. 어떻게 아셨죠..? 지금은 후련하게 스스로 답을 찾아서 기분이 좋답니다 ㅋㅋ

익명 사용자의 이미지

    for (int i = position-1 ; i >=0 ; i--){
        if (story[i] == 0){
            story[i] += 1; // 끝자리가 0이면 1을 더해줌 (2의 보수)
            i = -1; ///// 여기서 i를 -1으로 만들고 나면
        }
        else if(story[i] == 1){
            story[i] = 0;
        }
        printf("%d",story[i]); ///// printf는 무엇을 출력하게 되나요?
     }

Quote:
제 생각으로는 이상이 없는거같은데

좀 더 꼼꼼히 보셔야 됩니다.

dygks8557의 이미지

i = -1; 이 부분에서 배열에 할당할 값이 없는데 제가 저렇게 선언해버려서 값이 안나오는거였어요.. 그래서 저기를 break;로 for문에서 나오게 했고 i의 값을 뒤집어서 하니 값도 뒤집어서 나오길래 int i = 0 으로 주고 조건식과 증감식을 봐꾸고 밑에 다시 for문으로 출력하게 하니 값이 제대로 나오네요!
감사합니다 ㅠㅠ

댓글 달기

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