2011-10-22 93 views
-1

我有一个项目在这里我试图解决无法获得正确的输出

http://webcache.googleusercontent.com/search?q=cache:M_EjCxt7T34J:acm.uva.es/archive/nuevoportal/data/problem.php%3Fp%3D2286+&cd=2&hl=en&ct=clnk&gl=us

,我有被输出不断走出来稍微错了,而不是最后两个问题作为E1它是E4,在零中间它应该是11F,而不是12D或者其他东西,这意味着它大部分是正确的,但我不明白什么是错的。这让我疯狂。

#include <vector> 
    #include <fstream> 
    #include <iostream> 
    #include <cstdlib> 

    using namespace std; 

    //int count = 0; 
    //GOOD 
    int hexToInt(vector<char>& c); 
    vector<char> intToHex(int n); 
    int hexToInt(char c){ 
     if(c>='A')return c-'A'+10; 
     else return c-'0'; 
    } 
    void execute(vector<char>& memory){ 
     // cout << "beb\n"; 

     char A = '0'; 
     char B = '0'; 
     if ((B>'9'&&B<'A')||(B<'0')||(B>'F')) cout << B << endl; 
     for(size_t i = 0; i<memory.size();i++){ 
      // cout << i << " "<< memory[i] <<endl; 
      // if(count<100){ 
     //  cout << i << " " << memory[i]<<" "<< A << " "<< B <<endl; 
      // count++; 
      //  cout << A << endl; 
      // cout << "I: "<< i<< " memory value: "<<memory[i]<<endl; 
      //} 
      vector<char> c; 
      if(memory[i]-'0' == 0){ 
       c.push_back(memory[i+1]); 
       c.push_back(memory[i+2]); 
       A = memory[hexToInt(c)]; 
       i+=2; 
      } 
      else if(memory[i] -'0' == 1){ 
       c.push_back(memory[i+1]); 
       c.push_back(memory[i+2]); 
       memory[hexToInt(c)] = A; 
       i+=2; 
      } 
      else if(memory[i] -'0' == 2){ 
       char temp = A; 
       A = B; 
       B = temp; 
      } 
      else if(memory[i] -'0' == 3){ 
       int tempy = hexToInt(A)+hexToInt(B); 
      // if (count<100) cout << tempy<< "aaa"<<endl; 
       c = intToHex(tempy); 
      // cout << "LOOK " <<tempy<< temp[0] << " "<<temp[1]<< " "<<A << " "<< B << endl; 
       //Questionable**** 
       A = c[1]; 
       B = c[0]; 
      } 
      else if(memory[i] -'0' == 4){ 
       if(A == 'F') A='0'; 
       else A +=1; 
      } 
      else if(memory[i] -'0' == 5){ 
       if(A == '0') A='F'; 
       else A -=1; 
      } 
      else if(memory[i] -'0' == 6){ 
       if(A=='0'){ 
        c.push_back(memory[i+1]); 
        c.push_back(memory[i+2]); 
        if(hexToInt(c)>=240) i+=2; 
        else i = hexToInt(c)-1; 
       } 
       else i+=2; 
      } 
      else if(memory[i] -'0' == 7){ 
       c.push_back(memory[i+1]); 
       c.push_back(memory[i+2]); 
       if(hexToInt(c)>=240) i+=2; 
       else i = hexToInt(c)-1; 
      // i+=2; 
      } 
      else /*if(memory[i] -'0' == 8)*/{ 
       if(i!=0) { 
       for(size_t b = 0; b<memory.size();b++) cout << memory[b]; 
       } 
       i = memory.size(); 
      // cout <<"yayyyy"; 
      } 
      if(memory[254]=='E'&& memory[64]=='1'&& memory[65]=='1'&& memory[66]=='F'&&memory[255]=='1')cout <<"fuuuu"; 
     // cout << memory[255]<<endl; 
     } 
     cout << endl; 
    } 

    int hexToInt(vector<char>& c){ 
     int ret = 0; 
     int place = 1; 
     for (int i = c.size()-1; i>=0; i--){ 
      if(c[i]>='A') ret= ret+place*(c[i]-'A'+10); 
      else ret= ret+place*(c[i]-'0'); 
      place*=16; 
     } 
     return ret; 
    } 
    //GOOD 
    vector<char> intToHex(int n){ 
     vector<char> ret; 
     int place = 16; 
     while (!(place/16>n)){ 
      int curr = n%place/(place/16); 
    //  if(count<100) cout<<n <<"bbb"<< curr << endl; 
      if(curr<10) ret.push_back('0'+curr); 
      else ret.push_back('A'+(curr-10)); 
      n-=curr; 
      place*=16; 
     } 
     while(ret.size()<2) ret.push_back('0'); 
    // if(count<100)cout << ret[0]<<ret[1]<<endl; 
     int low = 0; 
     int high = ret.size()-1; 
     while(low<high){ 
      char temp = ret[low]; 
      ret[low]=ret[high]; 
      ret[high]=temp; 
      high--; 
      low++; 
     } 
     return ret; 
    } 

    int main() { 
    // int a = 7; 
    // vector<char> b = intToHex(a); 
    // for(int i = 0; i<b.size();i++){ 
    //  cout << b[i]; 
    // } 
    // cout <<" " << hexToInt(b) << hexToInt('F')<< " "<< hexToInt('0') << endl; 
     //GOOD 
     ifstream ifs("test.txt"); 
     if(!ifs) cout << "wahh\n"; 
     vector<char> memory(256); 
     while(ifs>>memory[0]){ 
      // cout << "weh\n"; 
      for (size_t i = 1; i<memory.size();i++){ 
       ifs>>memory[i]; 
       cout<<memory[i]; 
      // cout << memory[i]; 
      } 
      cout<<endl; 
     // cout << endl; 
      execute(memory); 
     } 
    // vector<int> wtf(256); 
    // for(int i = 0; i<wtf.size();i++){ 
    //  cout << i << " "; 
    //  vector<char> temp = intToHex(i); 
    //  for(int d = 0; d<temp.size(); d++){ 
    //   cout <<temp[d]; 
    //  } 
    //  cout <<" "<< hexToInt(temp); 
    //  if(i<16) cout<< " "<<hexToInt(intToHex(i)[1]); 
    //  if(i!=hexToInt(intToHex(i)[1])) cout <<" HIOOOOOOOOOOOOOOOOOOOO"; 
    //  cout << endl; 
    // 
    // } 
     char c = 'A'+1; 
     cout << c; 
     system("PAUSE"); 
    } 
+0

您可以尝试编辑出所有调试内容并解释您的算法吗?你有没有考虑做一些重构? –

+0

重构是什么意思? – mystycs

+0

http://www.refactoring.com –

回答

1

您的操作码INC和DEC错误。如果你看看ASCII码表,你会发现'A'不符合'9',所以你必须“跳过”这个差距。

要做到这一点,最好的方法是只将十六进制数字转换为整数,递增/递减整数并将其转换回来。例如,您的DEC操作码可能如下所示:

int temp = hexToInt(A); 

if(temp == 0) 
    temp = 0xf; 
else 
    temp -= 1; 

vector<char> tempHex = intToHex(temp); 
A = tempHex.back(); 
+0

A确实按照9但不是吗? http://www.asciitable.com/ – mystycs

+0

这将是很好,如果它是这样的,但不,在'9'之后':'so'('9'+ 1)==':''和'(' A' - 1)=='@''。他们之间有一大堆人物。 – vstm

+0

我没有看到那个错误,虽然也许我们正在恋爱不同的东西?在十六进制A来到9之后 – mystycs