简体   繁体   English

使用char向量的10的补数函数

[英]10's Complement function using char vector

If you already know tens complement you can skip this whole paragraph explaining what it is. 如果您已经知道十进制补码,则可以跳过整个段落以解释其含义。

Hello! 你好! So if you do not know to find 10's complement of a decimal number you are suppose to do 9's complement then add 1 to that number. 因此,如果您不知道找到十进制数字的10的补码,那么您假设做9的补码,然后将该数字加1。 To do 9's complement you are suppose to subtract 9 to each number, so for example your decimal number is 20. You subtract 9 to both 2 and 0 which make up 20 then you get 7 and 9. Finally you'll end up with 79 and that is the 9's complement and add 1, so 10's complement of 20 is 080. The zero in front signifies that + number. 要进行9的补码,您假设每个数字都减去9,例如,您的十进制数是20。您将2和0都减去9,就组成了20,然后得到7和9。最后得到79。并且这是9的补数加1,所以20的10的补数是080。前面的零表示+号。 Lets say the number is a negative, -20, you need to multiply by -1 and do the same but this time we are to add a 9 in front so the number would be 980. 可以说这个数字是负数-20,您需要乘以-1并做同样的事情,但是这次我们要在前面加一个9,这样数字便是980。

   vector<char> tensComplement(){

    for(std::vector<char>:: iterator i = storage.begin(); i != storage.end(); i++){
        storage[*i] = 9 - storage[*i];
    }
    storage.end() += 1;

    for(std::vector<char>:: iterator it = storage.end(); it != storage.begin(); it--){ // error for this entire loop
        if(storage[*it] == 10){
            storage[*it] = 0;
            storage[*it - 1] += 1;
        }
    }

    return storage;

}

Sorry if this is a bad question I'm still fairly new to C++. 抱歉,如果这是一个不好的问题,我还是C ++的新手。 My problem is that whatever number I input I'm receiving the wrong answer. 我的问题是,无论输入什么数字,我都会收到错误的答案。 (It only works when I comment out the 2nd for loop .) The first for loop is trying to iterate through the whole vector<char> storage and subtracting it by 9. Also, for the 2nd for loop I'm receiving an error Thread 1: EXC_BAD_ACCESS (code=1, address=0x103400000) and what it is suppose to do is when the last number ends up being 10 it will carry over the one. (仅当我注释掉第二个for loop时,它才起作用。)第一个for loop试图遍历整个vector<char> storage并减去9。此外,对于第二个for循环,我收到一个错误Thread 1: EXC_BAD_ACCESS (code=1, address=0x103400000) ,应该做的是,当最后一个数字最终为10时,它将继续。

BigInt(int x){
    char digit;
    while (x > 0){
        digit = x % 10;
        storage.push_back(digit);
        x /= 10;
    }
    if (x < 0){
        x *= -1;
        tensComplement();
        storage.push_back(9);

    }
    else{
        tensComplement();
        storage.push_back(0);
    }

    reverse(storage.begin(),storage.end());

This is my constructor for the number, first it stores each number into my vector<char> storage and check if it is either negative or positive and do tens complement. 这是我的数字构造函数,首先将每个数字存储到我的vector<char> storage并检查它是负数还是正数并进行十进制补码。

I just need some advice on how to fix my function and see if my constructor looks correct. 我只需要一些有关如何修复我的函数的建议,看看我的构造函数看起来是否正确。 Thank you so much! 非常感谢!

for(std::vector<char>:: iterator it = storage.end(); it != storage.begin(); it++){ // error for this entire loop
    if(storage[*it] == 10){
        storage[*it] = 0;
        storage[*it - 1] += 1;
    }
}

The code iterating from storage.end() to storage.begin() is fishy. 从迭代代码storage.end()storage.begin()是腥。 If you're trying to go backwards in the vector, you have to it-- ! 如果要在向量中向后移动,则必须it-- Otherwise it'll go out of bounds in the first iteration - it's starting at the end of the vector already (which is out of bounds). 否则,它将在第一次迭代中超出范围-它已经从向量的末尾开始(超出范围)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM