很急!!請幫我寫程式用C語言輸入一個任意字串,將裡面的數字(包含float)取出後印出 example output: 12 54 46 15.2 1.23 2.222 33.33 444.4 55 77.7?

2016-12-19 8:07 pm
更新1:

example input: 12csac54d46dvs15.2scas 1.23a2.222bbb33.33dd444.4ggg55gg77.7 example output: 12 54 46 15.2 1.23 2.222 33.33 444.4 55 77.7

回答 (3)

2016-12-29 11:03 am
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char c_in=1;
int flag=0;
while(c_in!=0)
{
c_in=0;
scanf("%c",&c_in);
if((c_in>='0'&&c_in<='9')||c_in=='\n'||(c_in=='.'&&flag==1))
{
printf("%c",c_in);
flag=1;
}
else if(flag==1)
{
printf(" ");
flag=0;
}
}
system("pause");
return 0;
}
2016-12-19 11:23 pm
#include<stdio.h>
#include<ctype.h>
int getFloat(
int c
){
for(; putchar(c) && (c=getchar()) != EOF && isdigit(c););
putchar(' ');
return c;
}
int getInt(
int c
){
for(; putchar(c) && (c=getchar()) != EOF && isdigit(c););
if('.' == c) return getFloat(c);
putchar(' ');
return c;
}
int main(){
int c;
for(;(c=getchar()) != EOF && c>0;)
if(isdigit(c)) getInt(c);
else if('.' == c) getFloat(c);
else if('\n' == c) putchar(c);
putchar('\n');
return 0;
}


收錄日期: 2021-05-04 02:17:20
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20161219120722AAw4bff

檢視 Wayback Machine 備份