在C中添加一个整数数组作为整数

时间:2018-05-16 21:23:55

标签: c arrays loops math

我必须编写一个程序,将一个整数位的数字存储在一个数组中。例如,1234是[0] 1,a [1] 2,a [2] 3,a [3] 4.这些数字最初将存储为char,因此我需要将它们转换为int类型。目标是获取长度为20或更少的2个正整数并输出数字的总和。

我必须使用纸笔加法算法。解决方案必须存储在大小为20的数组中,然后写在屏幕上。如果输出大于20的数组,则不应给出输出,屏幕应显示"整数溢出。"

到目前为止,我已完成此编码:

int max;
int max_1;
char number_1[100];
int a;
int max_2;
char number_2[100];
int b;


printf( "\nEnter the total number of digits you wish to display:\n");
scanf_s( "%d", &max);

printf("\nEnter the size of the first integer you wish to add:\n");
scanf("%d", &max_1);
printf("\nEnter the first integer one number at a time seperated by spaces:\n");
while (a = 0, a < max_1, ++a)
{
    scanf_s("%c", number_1[a]);
}

printf("\nEnter the size of the second integer you wish to add:\n");
scanf("%d", &max_2);
printf("\nEnter the second integer one number at a time seperated by spaces:\n");
while (b = 0, b < max_2, ++b)
{
    scanf_s("%c", number_2[b]);
}
    return 0;

我似乎无法弄清楚如何将输入的数字转换为实际的整数形式,因为它们都是分别存储在数组中的。另外,我不确定如何将整数转换回大小为20的数组以及如何确定输出的大小是否为20或更大。

我为我的长期问题道歉。我是编码的新手,并且在推理方面并不过分。我确信有一个简单的解决方案,但我无法想到它。任何评论,解决方案或建议都会有所帮助。

1 个答案:

答案 0 :(得分:1)

简单示例显示如何逐个读取数字作为整数:

OnRecieve

在此之后,数组Log.d("Notifiction", "notificationCreated"); 中的每个元素都是一位数的<receiver android:name=".MainActivity.Receiver" android:exported="false" > <intent-filter> <action android:name="NOTIFICATION_SERVICE" /> </intent-filter> </receiver> 值。索引int max_1; printf("\nEnter the size of the first integer you wish to add:\n"); scanf("%d", &max_1); int number_1[max_1]; // Use variable-length array printf("\nEnter the first integer one number at a time seperated by spaces:\n"); for (int i = 0; i < max_1; ++i) { scanf_s("%1d", &number_1[i]); } 处的最高有效数字,索引number_1处的最低有效数字。

相关问题