Skip to content

Instantly share code, notes, and snippets.

@udonmai
Created March 23, 2012 06:27
Show Gist options
  • Save udonmai/2167605 to your computer and use it in GitHub Desktop.
Save udonmai/2167605 to your computer and use it in GitHub Desktop.
liduo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void fun(FILE *stdin) {
char str[20], tmp[20];
int sum, len, i, idx, cnt;
int a1[10], num1 = 0, num2 = 0, ans1 = 0;
double a2[10], ans2 = 0;
while(scanf("%s", str) != EOF) {
len = strlen(str);
sum = 0;
cnt = 0;
idx = -1;
for(i = 0; i < len; ++i) {
if(str[i] > '0' && str[i] <= '9') {
sum = sum * 10 + str[i] - '0';
++cnt;
}
else if(str[i] == '.')
idx = cnt;
}
if(idx == -1) {
a1[++num1] = sum;
if(str[0] == '-')
a1[num1] = -a1[num1];
}
else {
a2[++num2] = sum * 1.0;
for(i = 1; i <= cnt - idx; ++i)
a2[num2] /= 10;
if(str[0] == '-')
a2[num2] = -a2[num2];
}
}
printf("整型有:");
for(i = 1; i <= num1; ++i) {
printf("%d ", a1[i]);
ans1 += a1[i];
}
printf("\n和为:");
printf("%d\n", ans1);
printf("浮点数:");
for(i = 1; i <= num2; ++i) {
printf("%lf ", a2[i]);
ans2 += a2[i];
}
printf("\n和:");
printf("%f\n", ans2);
return 0;
}
int main() {
freopen("tmp", "r", stdin);
fun(stdin);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment