2011-10-22 181 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