从键盘输入一字符串(不多于80个字符),分别将其中每组连续的数字转换为相应的整数,并统计整数的个数,依次输出整数的个数和每个整数乘以2的积(假定不会超出长整数范围),如字符串中无数字,则输出None.。
#include <stdio.h>int main(){ char str[100]; long long an[100]; int cnt=0,i=0,f=0; printf("Please input a string:"); gets(str); while(str[i]) { if(str[i]>='0'&&str[i]<='9') { if(f==1) { an[cnt]=an[cnt]*10+str[i]-'0'; } if(f==0) { cnt++; f=1; an[cnt]=str[i]-'0'; } } else { f=0; } i++; } if(cnt==0) { printf("None\n"); } else { printf("Total number: %d\n",cnt); for(i=1;i<=cnt;i++) { printf("%lld*2=%lld\n",an[i],an[i]*2); } } return 0;}
#include "stdio.h"void main(){ char str[81]; long num[40]; long n; int cnt=0; int i; gets(str); for(i=0;str[i];i++) { n=0; if(str[i]>='0' && str[i]<='9') { while(str[i] && str[i]>='0' && str[i]<='9') { n=n*10+str[i]-'0'; i++; } num[cnt++]=n*2; } } if(cnt) { printf("%d个数\n",cnt); for(i=0;i<cnt;i++) printf("%ld ",num[i]); } else printf("None.\n");}
#include<stdio.h>void print_num(char *str){ char string[20][20]; int i=0,num=0,new_num=0,counter=0; while(*(str+i))//即*(str+i)!='\0' { if(*(str+i)>='0'&&*(str+i)<='9'&&new_num==0) { string[num][counter]=*(str+i); counter++; new_num=1; } else if(*(str+i)>='0'&&*(str+i)<='9'&&new_num==1) { string[num][counter]=*(str+i); counter++; } else if((*(str+i)<'0'||*(str+i)>'9')&&new_num==1) { string[num][counter]='\0'; new_num=0; num++; counter=0; } else if((*(str+i)<'0'||*(str+i)>'9')&&new_num==0); i++; } string[num][counter]='\0'; num=num+1; printf("there are %d num.\nthere are:\n",num); for(i=0;i<num;i++) puts(string[i]);} int main(){ char string[100],*str; printf("Please enter a string:\n"); gets(string); str=string; print_num(str); return 0;}至于每个整数*2,可以作为练习来做,可以好好看一下这个代码,值得思考的
#include <stdio.h>#include <string.h>#include <stdlib.h>void main(){ char str[80]; int i; int j = 0; int nFlag = 0; int count = 0; int a[20]; scanf("%s", str); for (i = 0; i < strlen(str); i++) { if (str[i] >= '0' && str[i] <= '9' && nFlag == 0) { count++; a[j] = atoi(str + i); j++; nFlag = 1; } else { nFlag = 0; } } if (count == 0) { printf("None\n"); } printf("%d ", count); for (i = 0; i < j; i++) { printf("%d ", 2 * a[i]); }}
/*12 31 534 77 912 × 12 = 242 × 31 = 622 × 534 = 10682 × 77 = 1542 × 91 = 182nn = 5Press any key to continue*/#include <stdio.h>int main() { int num,n = 0; while(scanf("%d",&num) == 1) { ++n; printf("2 × %d = %d\n",num,2*num); } if(n == 0) printf("None.\n"); else printf("\nn = %d\n",n); return 0;}