2014-12-21 2 views
-2

입력시 이진수와 출력시 MLT-3 코드의 같은 번호 (전압 +, 0, -)가 필요한 C 프로그램을 작성해야합니다. 전압은 '1'에서 '0'으로 변하지 만 변하지는 않습니다 :MLT-3의 이진수

10010111 
+++00-0+ 

이 값들은 어떻게 롤백합니까?

답변

0

코드에 표시된 기본 흐름입니다.

#include <stdio.h> 

int main(void){ 
    int direction = 1;//1: upto, -1: downto 
    int state = 0; 
    char *seq = "10010111"; 
    while(*seq){ 
     if(*seq == '1'){ 
      state += direction; 
      if(state == 1 || state == -1) 
       direction = -direction;//reversal of direction 
     } 
     switch(state){//putchar("-0+"[state+1]); 
     case 1: putchar('+');break; 
     case 0: putchar('0');break; 
     case -1: putchar('-');break; 
     } 
     ++seq; 
    } 
    return 0; 
} 
1
#include <stdio.h> 

int main() 
{ 
    char states[4] = { '+', '0', '-', '0' }; 
    int index = 3; 
    int b = 0x97;   // OP example 
    int i; 
    for (i=0; i<8; i++) { 
     if (b & 0x80) 
      index = (index + 1) % 4; 
     printf ("%c", states[index]); 
     b <<= 1; 
    } 
    printf("\n"); 
    return 0; 
} 

프로그램 출력

+++00-0+