无法获得正确的输出

时间:2011-10-22 05:13:49

标签: c++ vector hex

我在这里有一个项目,我正在努力解决

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");
    }

1 个答案:

答案 0 :(得分:1)

您的操作码INC和DEC错误。如果你看一下ASCII-Table,你会看到'A'不跟随'9',所以你必须“跳过”这个空白。

执行此操作的最佳方法是将十六进制数字转换为整数,递增/递减整数并将其转换回来。例如,您的DEC操作码可能看起来像这样:

int temp = hexToInt(A);

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

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