POJ 1049

题意:模拟微处理机。

题解:大模拟~

View Code
  1 #include<cstdio>

  2 #include<cstring>

  3 #include<algorithm>

  4 #include<cctype>

  5 using namespace std;

  6 char s[300];

  7 int pos;

  8 char A,B;

  9 char D_H(int x)

 10 {

 11     if(x<10)

 12         return x+'0';

 13     else

 14         return x-10+'A';

 15 }

 16 int H_D(char x)

 17 {

 18     if(isdigit(x))

 19         return x-'0';

 20     else

 21         return x-'A'+10;

 22 }

 23 void LD()

 24 {

 25     int w=H_D(s[++pos]);

 26     w=w*16+H_D(s[++pos]);

 27     pos++;

 28     A=s[w];

 29 }

 30 void ST()

 31 {

 32     int w=H_D(s[++pos]);

 33     w=w*16+H_D(s[++pos]);

 34     pos++;

 35     s[w]=A;

 36 }

 37 void SWP()

 38 {

 39     pos++;

 40     swap(A,B);

 41 }

 42 void ADD()

 43 {

 44     pos++;

 45     int a,b,c;

 46     a=H_D(A);

 47     b=H_D(B);

 48     c=a+b;

 49     A=D_H(c%16);

 50     B=D_H(c/16);

 51 }

 52 void INC()

 53 {

 54     pos++;

 55     A=D_H((H_D(A)+1)%16);

 56 }

 57 void DEC()

 58 {

 59     pos++;

 60     A=D_H((H_D(A)+15)%16);

 61 }

 62 void BR();

 63 void BZ()

 64 {

 65     if(A!='0')

 66         pos+=3;

 67     else

 68     {

 69         int w=H_D(s[++pos]);

 70         w=w*16+H_D(s[++pos]);

 71         pos=w;

 72     }

 73 }

 74 void BR()

 75 {

 76     int w=H_D(s[++pos]);

 77     w=w*16+H_D(s[++pos]);

 78     pos=w;

 79 }

 80 int main()

 81 {

 82     while(gets(s),s[0]!='8')

 83     {

 84         pos=0;

 85         A=B='0';

 86         while(s[pos]!='8'&&pos<256)

 87         {

 88             switch(s[pos])

 89             {

 90                 case '0':LD();break;

 91                 case '1':ST();break;

 92                 case '2':SWP();break;

 93                 case '3':ADD();break;

 94                 case '4':INC();break;

 95                 case '5':DEC();break;

 96                 case '6':BZ();break;

 97                 case '7':BR();break;

 98             }

 99         }

100         puts(s);

101     }

102     return 0;

103 }

你可能感兴趣的:(poj)