无法获得正确的输出

Can't get correct output

本文关键字:输出      更新时间:2023-10-16

我这里有一个项目,我想解决

http://webcache.googleusercontent.com/search?q=cache: M_EjCxt7T34J: acm.uva.es/归档/nuevoportal/数据/problem.php % 3 fp % 3 d2286 +和cd = 2, hl = en& ct = clnk&美国gl =

我遇到的问题是输出总是有点错误,而不是最后两个是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 << "bebn";
        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 << "wahhn";
        vector<char> memory(256);
        while(ifs>>memory[0]){
           // cout << "wehn";
            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");
    }

您的操作码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();