#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int str_type_func(char *str);
int main(int argc,char *argv[])
{
int i = 0;
int sum_int = 0;
double sum_d = 0;
double sum = 0;
int str_type = 0;
char *tmp_str = NULL;
for (i=1; i<argc; i++ )
{
tmp_str = argv[i];
str_type = str_type_func(tmp_str);
if (1 == str_type)
sum_int += atoi(tmp_str);
else if (2 == str_type)
sum_d += atof(argv[i]);
}
sum_d += sum_int;
printf("sum = %.2f\n", sum_d);
return 0;
}
int str_type_func(char *str)
{
int len = 0;
int i = 0;
int digit_cnt = 0;
int alpha_cnt = 0;
int minus_cnt = 0;
int decimal_cnt = 0;
len = strlen(str);
for (i=0; i<len; i++)
{
if (isdigit(str[i]) != 0)
digit_cnt += 1;
else if (isalpha(str[i]) != 0)
alpha_cnt += 1;
else if (str[i] == '-' && i==0)
minus_cnt = 1;
else if (str[i] == '-' && i>0)
minus_cnt = 10;
else if (str[i] == '.')
decimal_cnt += 1;
}
if (alpha_cnt==0 && digit_cnt>0 && minus_cnt<=1 && decimal_cnt==0)
return 1; //整數
else if ( alpha_cnt==0 && digit_cnt>0 && minus_cnt<=1 && decimal_cnt==1)
return 2; //小數
else
return 3;
}
C實現命令行求和
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.