C語言中關於字符串分割與字符串如何轉換成十進制的問題?
如下面一組字符型數據-0.92,-0.60,341.77,-0.15,0.14,0.11,0.02,-0.01,1.00,1.00,447.19,140.02,159.50我要把它進行分割,並且轉換成十進制的數據分別存儲到相應的數組中,請問怎麼辦?
最佳回答:
// input :-0.92,-0.60,341.77,-0.15,0.14,0.11,0.02
#include <stdio.h>
#include <string.h>
#define size 100
void main(){
char x[size],*p,*q;
printf("input a chars: \n");gets(x);
float y[size],num;
int flag,n,i,sum,j;
for(i=0,p=x,q=x;*p&&i<size-1;i++,p++,q++){
num=0;
flag=1;
n=0;
if (*p=='-'&&*p)
{
flag=-1;
p++;
q=p;
}
while(*p!=','&&*p){
p++;
}
while(p!=q){
if(*q!='.')
num=num*10+*q-'0';
else
n=p-q-1;
q++;
}
for(j=0;j<n;j++)
num/=10;
num*=flag;
y[i]=num;
if(!*p) break;
}
sum=i+1;
for(i=0;i<sum;i++)
printf("%d:\t%.5f\n",i+1,y[i]);
}