#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int sum=0;
char ch;
printf("请输入一串整数和任意含空格的数:");
while(scanf("%d",&i)==1)
{
sum+=i;
while((ch=getchar())==' ')
;
if(ch=='\n')
{
break;
}
ungetc(ch,stdin); //
}
printf("结果是:%d\n",sum);
return 0;
}
//OR
#include<iostream>
using namespace std;
int main ()
{
int sum=0;
cout<<"请输入一串整数:";
int i;
while(cin>>i)
{
sum+=i;
while(cin.peek()==' ')
{
cin.get();
}
if(cin.peek()=='\n')
{
break;
}
}
cout<<"结果是:"<<sum<<endl;
return 0;
}