c - 在 C 中减去任意大整数

标签 c arrays

问题:

  • 我想知道数字 n 和 a 的区别,它们都存储在 char 中 ALI 结构中的数组。基本上,我正在做的是初始化 两个整数(temp_n 和 temp_a),当前数字为 n 和 a, 将它们相减并将结果放入名为的新 ALI 实例中 k.如果 a 的第 j 位大于 n 的第 i 位,则 我把 if n 的数字加 10,完成减法,在接下来的 反过来,我将 temp_a 增加一。数字a的值肯定会下降 在 1 和 n - 1 之间(已给定)。如果 a 比 n 短,尽快 当我到达 a 的最后一位时,我将 n 的剩余数字放到 结果数组 k。我把这一切都倒过来了,所以初始化 i 的值将是 n -1 的大小。

示例:

  • 我将数字存储在这样的结构中:

    typedef struct Arbitrary_Large_Integer
    {
         char digits[];
    } ALI;
    

要求:

  • 我知道使用字符数组比使用字符数组更容易 只有一个成员的结构几乎没有意义,但我 这次被迫将结构放入我的代码中(这是一项要求 为了我的任务)。

代码:

ALI *subtraction(ALI n, ALI a, int nLength, int aLength)
{
    ALI *result;
    result = (ALI*)malloc(nLength * sizeof(ALI));
    if (result == NULL)
    printf("ERROR");

    int temp_n, temp_a, difference; 
    int i = nLength - 1; //iterator for number 'n'
    int j = aLength - 1; //iterator for number 'a'
    int k = 0; //iterator for number 'k', n - a = k
    bool carry = false; //to decide whether a carry is needed or not the turn

    for (i; i >= 0; i--)
    {
         //subtracting 48 from n.digits[i], so temp_n gets the actual number 
         //and not its ASCII code when the value is passed
         temp_n = n.digits[i] - ASCIICONVERT; 
         temp_a = a.digits[j] - ASCIICONVERT;

        //Performing subtraction the same way as it's used on paper
        if (carry) //if there is carry, a needs to be increased by one
       {
              temp_a++;
              carry = false;
        }
              if (temp_n >= temp_a)
              {
                   difference = temp_n - temp_a;
               }
              //I wrote else if instead of else so I can clearly see the condition
             else if (temp_a > temp_n) 
            {
                 temp_n += 10;
                 difference = temp_n - temp_a;
                 carry = true;
            }

            //placing the difference in array k, but first converting it back to ASCII
            result->digits[k] = difference + ASCIICONVERT; 
            k++;

           //n is certainly longer than a, so after every subtraction is performed on a's digits,
           //I place the remaining digits of n in k
           if (j == 0) 
           {
                for (int l = i - 1; l >= 0; l--)
               {
                    result->digits[k] = n.digits[l];
                    k++;
                }

            //don't forget to close the array
            result->digits[k] = '\0';
            break;
          }
          j--;
      }

      //reverse the result array
     _strrev(result->digits);
     return result;
}

输出/错误:

Output results

  • 好像当数组传递给函数时,它的值 由于某种原因而改变。我不知道它有什么问题。

最佳答案

问题:

非标准 C

typedef 不是有效的标准 C 结构。除了灵活数组成员之外,灵活数组成员 (FAM) .digits 还必须至少伴有一个先前命名的成员。建议将.nLength作为第一个成员。

// Not standard 
typedef struct Arbitrary_Large_Integer {
   char digits[];
} ALI;

malloc(0)??

由于代码使用的是非标准 C,请注意 nLength * sizeof(ALI) 可能与 nLength * 0 相同。

空字符没有空间

代码正在尝试使用 .digits 作为 string_strrev()mallloc()至少小了 1。

可能存在其他问题

A Minimal, Complete, and Verifiable example对于其他修复/解决方案很有用

关于c - 在 C 中减去任意大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54138953/

相关文章:

arrays - 如何在 Swift 中转换字符串?

c - tcsetattr() : what are the differences between TCSANOW, TCSADRAIN、TCSAFLUSH 和 TCSASOFT

c - 如何在C中读取bin文件的结构数组

c - 为什么这个缓冲区定义在一个循环内?

javascript - 检查数组索引的一个元素是否等于第二个元素

php - PHP 的 array() 的缩写

c++存储超出数组范围的值

javascript - 将在线 CSV 文件(Google Spreadsheet)引入 JavaScript 数组

c - 如何将数组的地址传递给函数?

c - 程序跳过输入并无故崩溃