就是要先用字符串读入,然后根据空格拆分出所有整数呗
#include<stdio.h>
#include<string.h>
int get_count(const char* pInput, const int nLen)
{
int count = 0;
for(int i=0;i<nLen;i++)
if(pInput[i] == ' ')
count++;
return count+1;
}
int calc(const char* pInput, const int nLen, float* pAvg, int* pAsc)
{
int sum = 0,n = 0,j=0,num = 0,k=0,t,flag = 1;
for(int i=0;i<nLen;i++)
{
if(pInput[i] == '-')
flag = 0;
else if(pInput[i] != ' ')
n = n*10 + pInput[i] - '0';
else
{
num++;
if(flag == 0)
pAsc[j] = -n;
else
pAsc[j] = n;
sum += pAsc[j];
j++;
n=0;
flag = 1;
}
}
if(flag == 0)
pAsc[j] = -n;
else
pAsc[j] = n;
sum += pAsc[j];
j++;
for(int i=0;i<j-1;i++)
{
for(k=0;k<j-1-i;k++)
{
if(pAsc[k] > pAsc[k+1])
{
t = pAsc[k];
pAsc[k] = pAsc[k+1];
pAsc[k+1] = t;
}
}
}
*pAvg = sum/(num+1.0);
return sum;
}
int main()
{
char s[100];
printf("Please input numbers, conject them with blank:\n");
gets(s);
int len = strlen(s);
int count = get_count(s,len);
int *data = (int*)malloc(sizeof(int)*count);
float avg;
int sum = calc(s,len,&avg,data);
printf("MAX = %d\n",data[count-1]);
printf("MIN = %d\n",data[0]);
printf("SUM = %d\n",sum);
printf("AVG = %.2f\n",avg);
printf("ASC = ");
for(int i=0;i<count;i++)
printf("%d ",data[i]);
printf("\n");
printf("DESC = ");
for(int i=count-1;i>=0;i--)
printf("%d ",data[i]);
return 0;
}