c - 通过使用 getchar 而不是 scanf 将 2 个大整数存储在数组中 - C 编程

标签 c arrays getchar

我需要这个程序的帮助,下面是我到目前为止编写的对象和程序:

目标:编写一个允许用户输入 100 位正整数的 C 程序,然后打印出这两个数字的和。

到目前为止,我使用scanf 编写的程序:

#include <stdio.h>
int main(void) {

int sum=0,i,j,array[100];



for(i=1;i<3;i++)
{
printf("operand #%d :",i);
scanf("%d",&array[i]);
printf("value entered: %d\n", array[i]);
}



for(j=1;j<i;j++)
{
sum=sum+array[j];
}



printf("The sum of array is %d ", sum);

return 0;
}

以下是我使用getchar()的代码:

#include <stdio.h>
int main(void) {

int c,i,j,sum=0;

char a[100];

for(i=1;i<3;i++)
{
printf("operand #%d :",i);


do{
    if(i < 100){
        a[i] = (char)c;
        i++;
        }
    else{
      printf("Error: Number must be greater than 0,try again");
    }
} while((c = getchar()) != '\n');


printf("value entered: %d\n", a[i]);
}



for(j=1;j<i;j++)
{
sum=sum+a[j];
}



printf("The sum of array is %d ", sum);

return 0;
}

任何帮助都是合适的!

最佳答案

您的问题之一是您同时将 i 用于两个不同的作业。这是行不通的。

for(i=1;i<3;i++)  // Use #1
{
    printf("operand #%d :",i);

    do{
        if(i < 100){
            a[i] = (char)c;  // Use #2
            i++;
            }
        else{
          printf("Error: Number must be greater than 0,try again");
        }
    } while((c = getchar()) != '\n');

    printf("value entered: %d\n", a[i]);
}

外层循环可能只执行一次——如果第一个数字是个位数,它会执行两次。

此代码还在您调用 getchar() 之前分配了 c,这也不会改善事情。您可能还需要将 ASCII 数字转换为一位数字(从数字中减去 '0' 但您应该首先检查它是否是数字,并且应该在非数字)。

如果您要存储 3 个数字,每个数字最多 100 位,则您需要存储最多 300 位数字。 char a[100] 不够大。

你可以使用类似的东西:

char a[3][100];
int  n[3];
for (int i = 0; i < 3; i++)
{
    int c;
    int j;
    printf("Operand #%d: ", i);
    for (j = 0; j < 100 && (c = getchar()) != EOF && isdigit(c); j++)
        a[i][j] = c - '0';
    n[i] = j;
    while (c != EOF && c != '\n')
        c = getchar();
}

如果您输入 80、60 和 20 位数字,则这会将 80 位数字存储在 a[0][0..79] 中并将 80 放入n[0](所以你知道这个数字有多长);它将 60 位数字存储在 a[1][0..59] 中,并将 60 放入 n[1] 中;并将 20 位数字存储在 a[2][0..19] 并将 20 放入 n[2]

在进行加法运算时,您需要注意正确对齐数字,并确保如果您的加法运算有 101 位,则不会溢出答案缓冲区。对于三个最多 100 位的正十进制数,答案的长度不能超过 101 位。


However, your code doesn't work.

正确:之前的版本使用for (int j = 0; …) 然后尝试在循环外访问j。解决方法显然是在循环之前声明 j

但是,除此之外,代码确实有效。我已经将 3 的出现次数调整为 2,因为你说你只需要两个数字。我拒绝搞乱从 1 开始的索引;这是 C,数组在 C 中从 0 开始索引。如果你想要一种基于 1 的语言,请使用 Pascal 或其他语言。

示范代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

int main(void)
{
    char a[2][100];
    int n[2];
    for (int i = 0; i < 2; i++)
    {
        int c;
        int j;
        printf("Operand #%d: ", i);
        for (j = 0; j < 100 && (c = getchar()) != EOF && isdigit(c); j++)
            a[i][j] = c - '0';
        n[i] = j;
        while (c != EOF && c != '\n')
            c = getchar();
    }
    for (int i = 0; i < 2; i++)
    {
        printf("%d: %2d digits: ", i, n[i]);
        for (int j = 0; j < n[i]; j++)
            putchar(a[i][j] + '0');
        putchar('\n');
    }
    return 0;
}

示例数据:

124232345289086098234232398098403242380980256454798796324635
98068704234280980243242349080928402342398408920482080980482034278795847396

示例输出:

Operand #0: 124232345289086098234232398098403242380980256454798796324635
Operand #1: 98068704234280980243242349080928402342398408920482080980482034278795847396
0: 60 digits: 124232345289086098234232398098403242380980256454798796324635
1: 74 digits: 98068704234280980243242349080928402342398408920482080980482034278795847396

这就是它应该工作的方式。您可能想以不同的方式进行;那是你的特权。努力吧!我不会为你解决问题的加法部分——我已经指出了最明显的陷阱(主要是,将 a[0][1] 添加到 a[1 ][1] 将为给定的输入产生废话)。


这只使用了微小的函数。其中有一些棘手的代码;小心上交它,因为您可能会被要求详细解释它的作用,为了安全起见,您需要了解所有内容。

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

static inline int max(int x, int y) { return (x > y) ? x : y; }

/* Return dth digit from RH end of string of n digits in x */
static inline int digit(char *x, int n, int d)
{
    return (d < n) ? x[n - 1 - d] : 0;
}

int main(void)
{
    char a[2][100];
    int n[2];

    /* Input - can probably be tightened up */
    for (int i = 0; i < 2; i++)
    {
        int c;
        int j;
        printf("Operand #%d: ", i);
        for (j = 0; j < 100 && (c = getchar()) != EOF && isdigit(c); j++)
            a[i][j] = c - '0';
        n[i] = j;
        if (j == 0)
        {
            printf("No number - exiting\n");
            exit(0);
        }
        if (c != EOF && c != '\n')
        {
            if (j < 100 && !isdigit(c) && !isblank(c))
            {
                printf("Bogus data in input (%c)\n", c);
                exit(1);
            }
            while ((c = getchar()) != EOF && c != '\n')
            {
                if (!isblank(c))
                {
                    printf("Bogus data in input (%c)\n", c);
                    exit(1);
                }
            }
        }
    }

    /* Print for validation */
    int n_max = max(n[0], n[1]);
    for (int i = 0; i < 2; i++)
    {
        printf("V-%d: %2d digits: ", i, n[i]);
        int n_blanks = n_max - n[i] + 1;
        for (int j = 0; j < n_blanks; j++)
            putchar(' ');
        for (int j = 0; j < n[i]; j++)
            putchar(a[i][j] + '0');
        putchar('\n');
    }

    /* Addition */
    char sum[101];
    int carry = 0;
    int max_digits = max(n[0], n[1]);
    for (int i = 0; i < max_digits; i++)
    {
        int d0 = digit(a[0], n[0], i);
        int d1 = digit(a[1], n[1], i);
        int r = d0 + d1 + carry;
        if (r > 9)
        {
            carry = 1;
            r -= 10;
        }
        else
            carry = 0;
        sum[max_digits - i] = r;
    }
    if (carry)
        sum[0] = 1;
    else
        sum[0] = 0;

    /* Print result */
    printf("Sum: %2d digits: ", (sum[0] == 0) ? max_digits : max_digits + 1);
    if (sum[0] == 0)
        putchar(' ');
    for (int j = ((sum[0] == 0) ? 1 : 0); j <= max_digits; j++)
        putchar(sum[j] + '0');
    putchar('\n');

    return 0;
}

样本运行:

Operand #0: 888
Operand #1: 888
V-0:  3 digits:  888
V-1:  3 digits:  888
Sum:  4 digits: 1776


Operand #0: 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
Operand #1: 9876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210
V-0: 100 digits:  1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
V-1: 100 digits:  9876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210
Sum: 101 digits: 11111111101111111110111111111011111111101111111110111111111011111111101111111110111111111011111111100

Operand #0: 9876543210a
Bogus data in input (a)


Operand #0: 9876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109876543210a
Bogus data in input (a)

Operand #0: 98765432109876543210987654321098765432109876543210987654321098765432109876543210987654321098765432109
Bogus data in input (9)

Operand #0: 
No number - exiting

它并不完美:

Operand #0: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Operand #1: 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
V-0: 100 digits:  0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
V-1: 100 digits:  0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Sum: 100 digits:  0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

从输入(和输出)中去除前导零作为练习留给您。

关于c - 通过使用 getchar 而不是 scanf 将 2 个大整数存储在数组中 - C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22494328/

相关文章:

c - 尝试理解 c 中的 for 循环和 getchar()

C多类型函数

c - POSIX 文件锁是否可重入?

c - 使用标记为 volatile 的变量,而不是 protected 互斥锁

c - char * 与 getchar 到 printf 的奇怪输出

c - `getchar()` 给出与输入字符串相同的输出

c - C 中的 vector 叉积

javascript - 如何从给定的字符数组中查找所有单词的列表

Java从多个数组中删除重复项

python - 为什么 a[1 :-1:-1] with a=[1, 2,3] 返回 []?