![]() |
#2
stop12042014-10-24 23:42
|

#include<iostream>
using namespace std;
#include<string>
#include<cmath>
int savenum(char *,int &);
int main()
{ const int n=10;
int num[n],num_found=0,k=0;/*num[n]用来储存找到的数字,num_foud用来记找到的数字的个数,k的功能见while循环*/
char arry[20];
cout<<"please input the arry";
cin>>arry;
char *p=&arry[0];
while(*p!='\0')
{
if(‘0’<=*p<=‘9’ )
{
p=p+savenum(p,num[k]);//此语句目的是将p下移连续数字的位数个单位
k++;//k在找到数字后加1,让num数组准备接收下一个找到的数字
num_found++;
}
else p=p+1;//如果没找到数字,就让p下移一位,进而判断下一位是否是数字
}
for(int i=0;i<num_found;i++)
cout<<num[i]<<" ";
return 0;
}
int savenum(char *p,int &num)
{
num=0;
int k=0,move_p=0;
while(‘0’<=*p<=‘9’)
{
num=num+(*p-48)*pow(10,k);//更改num[k]的值(这里想让(*p-48)乘以10的k次方)
k++;//k随着p的移动增大
move_p++;//每找到一个单个数字,主函数中的指针p应该下移一位
p++;
}
return move_p;
}
using namespace std;
#include<string>
#include<cmath>
int savenum(char *,int &);
int main()
{ const int n=10;
int num[n],num_found=0,k=0;/*num[n]用来储存找到的数字,num_foud用来记找到的数字的个数,k的功能见while循环*/
char arry[20];
cout<<"please input the arry";
cin>>arry;
char *p=&arry[0];
while(*p!='\0')
{
if(‘0’<=*p<=‘9’ )
{
p=p+savenum(p,num[k]);//此语句目的是将p下移连续数字的位数个单位
k++;//k在找到数字后加1,让num数组准备接收下一个找到的数字
num_found++;
}
else p=p+1;//如果没找到数字,就让p下移一位,进而判断下一位是否是数字
}
for(int i=0;i<num_found;i++)
cout<<num[i]<<" ";
return 0;
}
int savenum(char *p,int &num)
{
num=0;
int k=0,move_p=0;
while(‘0’<=*p<=‘9’)
{
num=num+(*p-48)*pow(10,k);//更改num[k]的值(这里想让(*p-48)乘以10的k次方)
k++;//k随着p的移动增大
move_p++;//每找到一个单个数字,主函数中的指针p应该下移一位
p++;
}
return move_p;
}
习题要求:输入一个字符串,内有数字和非数字字符,如:
a123x456 179?320
将其中连续的数字作为一个整数,依次存放到一数组num中。例如123放在num[0]中,456放在num[1],运行后出现无法正常执行的错误,求各位指教!
[ 本帖最后由 a874695162 于 2014-10-24 21:21 编辑 ]