从一个字符数组中读出相应的整数、实数

xiaoxiao2021-02-28  111

#include <stdio.h> #include <string.h> void my_seek(char *str,int a[],float b[]) { int i = 0; //存放整数的个数 int j = 0; //存放浮点数的个数 int k = 0; int temp1 = 0; float ch = 0.1; float temp2 = 0.0; while(*str) { while(((*str >= '0')&&(*str <= '9'))||*str == '.') //判断是否为整数 { while(*str >= '0'&&*str <='9') { temp1 = temp1*10+*str-'0'; str++; } if(*str == '.') //判断是否为浮点数 { temp2 = temp1; str++; while(*str >= '0'&&*str <='9') { temp2 = temp2+(*str-'0')*ch; str++; ch = ch * 0.1; } b[j] = temp2; j++; } else { a[i] = temp1; //将整数存放在a数组中 i++; } } temp1 = 0; temp2 = 0.0; str++; } printf("字符串中的整数为:\n"); for(k = 0;k < i; k++ ) { printf("%d ",a[k]); } printf("\n"); printf("字符串中的浮点数为:\n"); for(k = 0;k < j;k++) { printf("%g ",b[k]); } printf("\n"); } int main() { char str[100] = {0}; int a[100] = {0}; float b[100] = {0.0}; printf("please input string:\n"); scanf("%s",str); my_seek(str,a,b); return 0; }
转载请注明原文地址: https://www.6miu.com/read-36511.html

最新回复(0)