[英]How to get summation in C?
如何使程序从输入中读取元素,例如:
1 3
并给我总结一下:
4
#include <stdio.h>
int main(void){
char x[3];
scanf(" %c",&x);
printf("%d\n",x[0]+x[2]);
}
在您的方法中,您似乎读了一个字符串并将该字符串的多个位置视为数字。 除了在实施此方法方面存在多个错误的事实外,主要的事实实际上是您采用了错误的方法。 缺点(不是全部,只是一些)是:您只考虑精确一位的数字; 您假设用户输入正好映射到数组,并且在感兴趣的数字之间恰好有一个“空白”位置(当您使用硬编码索引0
和2
访问x[0]+x[2]
时); 您只能将两个“数字”相加; ...
我宁愿在一个循环中扫描整数值(即使用%d
和数据类型int
),直到输入的不是有效数字。 这解决了上述所有问题:
int main() {
int sum=0;
int num=0;
printf("type in numbers to be summed up (type a non-number to exit):\n");
while (scanf("%d",&num)==1) {
sum += num;
}
printf("sum: %d\n",sum);
}
输入/输出:
type in numbers to be summed up (type a non-number to exit):
10 20 30 x
sum: 60
这里缺少一些东西。
一方面,您只使用%c
读取一个字符。 您将其存储在&x
中,尽管这很混乱,但从技术上讲是合法的:由于它是内存中3
char
大小的元素的序列,因此&x
是有效的字符地址。 但是, x[1]
和x[2]
保持未初始化; 您没有在任何地方设置它们。
其次,您没有将其转换为整数值,因此它仍然具有字符“ 1”而不是小数点1的值。 '1' + '1'
(请注意单引号)的计算结果为49 + 49
(请注意缺少引号),49是与字符'1'
等效的ascii-与十进制值1
截然不同。
最后,您只求和了第一个和第三个字符(未初始化的第三个字符具有未知值,肯定不是您输入的值)。 第二个字符不是最终结果的一部分。
如果要读取3个integers
,则应扫描int
而不是字符,并应扫描希望读取的integers
。 这样您就可以正确读取9以上的数字。
但是,也许您确实想一次扫描一位数字 。 在这种情况下,您肯定希望将每个数字字符转换为等效的整数 。 由于数字0
到9
是连续的,并且按ASCII升序排列,因此您可以从字符中简单地减去'0'
以获得其十进制等效值( '1' - '0' == 1
, '9'-'0'==9
等),但是要使其正常工作,必须确保您确实读过一个数字,而不是任何char
。 您可以通过验证其值在'0'
和“ 9
”之间(包括'0'
和9
)来实现。
无论您希望对整数还是数字求和,都需要确保在计算最终总和之前先读取要求和的每个值。
在给定用例的情况下,保持循环扫描整数直到您用完输入流中的整数可能更有意义。 您实际上并不需要分别存储它们。 您可以一次读取一个int并将其添加到运行总计中。
放在一起,您可能最终会得到这样的结果。 采取这些想法并实现您的总和,您将拥有想要的角色。
#include <stdio.h>
int main() {
char c; // we'll store our input here as we go
while( scanf(" %c", &c) == 1 ) { //one thing matched
if(c >= '0' && c <= '9'){ // it's a digit
printf("Read %c, decimal value of digit is %d\n", c, (int)(c-'0') );
}else {
printf("Invalid digit %c\n", c);
}
}
}
我这样跑:
$ gcc -o t t.c && echo '1 2 3 4 5' | ./t
Read 1, decimal value of digit is 1
Read 2, decimal value of digit is 2
Read 3, decimal value of digit is 3
Read 4, decimal value of digit is 4
Read 5, decimal value of digit is 5
如下所述更改为scanf("%d")
来读取多位整数,从而相应地更改代码。
#include <stdio.h>
int main() {
int c; // we'll store our input here as we go
while( scanf(" %d", &c) == 1 ) { //one thing matched
printf("Read %d; wasn't that easy?\n", c);
}
}
$ gcc -o t2 t2.c && echo '1 2 3 4 5' | ./t2
Read 1; wasn't that easy?
Read 2; wasn't that easy?
Read 3; wasn't that easy?
Read 4; wasn't that easy?
Read 5; wasn't that easy?
该方法可以读取最大为int
的最小/最大大小的int
,包括多个数字甚至是负数:
$ gcc -o t2 t2.c && seq -1 -10 | ./t2
Read -1; wasn't that easy?
Read -2; wasn't that easy?
Read -3; wasn't that easy?
Read -4; wasn't that easy?
Read -5; wasn't that easy?
Read -6; wasn't that easy?
Read -7; wasn't that easy?
Read -8; wasn't that easy?
Read -9; wasn't that easy?
Read -10; wasn't that easy?
您可以尝试:
int n1;
int n2;
scanf("%d %d", &n1, &n2);
int sum = n1 + n2;
printf("%d\n", sum);
如果要将两个以上的数字加在一起,可以尝试:
printf("Enter how many numbers you want to add:\n");
int n;
scanf("%d", &n);
int sum;
for (int i = 0; i < n; i++) {
int in;
scanf("%d", &in);
sum += in;
}
printf("%d\n", sum);
注意:
首先,答案的标题中包含C ++,所以我用C ++进行了这样的回答:
如果您不介意使用cin
和cout
,则可以尝试:
int n1;
int n2;
cin >> n1 >> n2;
cout << n1 + n2;
用n个整数运行该程序将通过从argv [1]迭代到argv [n]返回它们的总和。 argv [0]是程序的名称。
例:
./sum 1 3返回4
码:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int sum;
if (argc > 1)
{
for (i = 1; i < argc; i++)
{
sum += (int)strtol(argv[i], NULL, 10);
}
printf("%d\n", sum);
}
else
{
fprintf(stderr,"Invalid number of arguments\n");
return(EXIT_FAILURE);
}
return(EXIT_SUCCESS);
}
您可以使用数组和循环。 这是一个简单的方法。
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0, allocation[5],i,num;
printf("enter the number of elements");
scanf("%d",&num); // how many numbers are there??
printf("Enter the elements");
for(i=0;i<num;i++)
{
scanf("%d",&allocation[i]); //allocate the elements in the array say 3,4,5
sum=sum+allocation[i];
//0+3, sum=3
//3+4, sum=7
//7+5, sum=11
}
printf("Sum= %d",sum); //print Sum=11
getch();
}
#include <stdio.h>
int main () {
int num1,num2;
printf("Enter two numbers");
scanf("%d %d", &num1 &num2);
printf("Sum is = %d", num1+num2);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.