1 #include<iostream>
2 #include<string>
3 using namespace std;
4 //-----------------
5 class GNumber
6 {
7 char chuan[50];
8 public:
9 void set();
10 friend int search (GNumber& str);
11 };
12 //-------------------
13 int search(GNumber& s)
14 {
15 int num=0; //定义num用于整数个数的统计
16 for(int i=0;i<50;i++)
17 { string str1=""; //新建一个用于储存整数的字符串
18
19 if(s.chuan[i]>='0'&&s.chuan[i]<='9') //用以判断是否为数字
20 for(int j=i;;j++) // 判断其后是否有连续数字,若有,则储存
21 {
22 if(s.chuan[j]<'0'||s.chuan[j]>'9') //若不是数字,则跳过非数字区域,并跳出判断后一个是否是数字的循环体
23 { i=j;break;}
24 str1=str1+s.chuan[j];
25 } //若是数字,则储存起来
26
27 if(str1!=""&&str1.length()>0) //判断是否是“整数”
28 {
29 cout<<str1<<endl;
30 num++; //统计整数个数
31 }
32 }
33 return num; //返回统计的个数,调用即可返回
34 }
35 //-----------------
36 void GNumber::set()
37 {
38 for (int i=0;(chuan[i]=cin.get())!='\n';i++) //调用cin 类的get这样一个成员函数,当输入的不是回车的时候执行空操作;否则终止循环
39 ;
40 }
41 //----------------------
42 void main()
43 {
44 GNumber s1;
45 cout<<"please input a string:"<<endl;
46 s1.set();
47 cout<<endl;
48 cout<<"有下列数字:\n";
49 int num=search(s1); //将查找统计出来的整数个数赋值给main函数中的变量 num;
50 cout<<"共计"<<num<<"个\n";
51 system("pause");
52 }