菜鸟的问题

cnbj8607 2010-03-11 09:19:59
假如输入一个字符串:
a123b456cd789,321,oid83
现在需要将123 456 789 321 83分别提取出来,存入一个整型数组中:
如何处理?
#define N 100
#include<stdio.h>
void main()
{
void count_num(char *p,int *pt);
char str[N],*p=str;
int a[N],*pt=a;
printf("Please enter a string:\n");
gets(p);
count_num(p,pt);
printf(pt);
}

void count_num(char *p,int *pt)
{
char string[N];
int i=0,j=0,num=0;
while(*p++)
{
函数写到着写不出来了,帮帮忙!
}
}
...全文
129 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
看来我比你还菜鸟啊!!!
liuxingjin 2010-03-11
  • 打赏
  • 举报
回复
字符串遍历

int i,flag,a[256];
flag=0;
while(*s++) //上面忘了++
{
if(isdigit(*s))
{
flag=1;
a[i++]=*s+'0'; //刚错了,忘了转化成数字
}
if(flag==1)
{
a[i++]=' ';
flag=0;
}
}
liuxingjin 2010-03-11
  • 打赏
  • 举报
回复
字符串遍历

int i,flag,a[256];
flag=0;
while(*s)
{
if(isdigit(*s))
{
flag=1;
a[i++]=*s+'0'; //刚错了,忘了转化成数字
}
if(flag==1)
{
a[i++]=' ';
flag=0;
}
}
liuxingjin 2010-03-11
  • 打赏
  • 举报
回复
字符串遍历

int i,flag,a[256];
flag=0;
while(*s)
{
if(isdigit(*s))
{
flag=1;
a[i++]=*s;
}
if(flag==1)
{
a[i++]=' ';
flag=0;
}
}
ithiker 2010-03-11
  • 打赏
  • 举报
回复
发现楼上两位给出代码的同志输入0都有问题~
ithiker 2010-03-11
  • 打赏
  • 举报
回复
这个问题不算菜鸟问题滴
昵称很不好取 2010-03-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cnbj8607 的回复:]
思考了三个小时,写出来了,不过真存在一个漏洞:
[/Quote]
我7楼的代码中考虑了最后一位的问题,楼主可以参考下
另外数据太大溢出也是个问题,我没有考虑到~~
cnbj8607 2010-03-11
  • 打赏
  • 举报
回复
思考了三个小时,写出来了,不过真存在一个漏洞:
看下边如何解决:
#define N 100
#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
int count_num(char *p,int *pt);
char str[N],*p=str;
int a[N],*pt=a,i,n;
printf("Please enter a string:\n");
gets(p);
n=count_num(p,pt);
printf("\nOutput:\n");
for(i=0;i<n;i++)
printf("%d ",*(pt+i));
printf("\n");
}

int count_num(char *p,int *pt)
{
int change(char string[]);
char string[N];
int i=0,num=0;
while(*p)//此处*p不能取到'\0',当源字符串最后一位是数字时,不能提取出最后一个数;
{
if(*p>'0'&&*p<'9')
{
string[i++]=*p++;
}
else
{
string[i]='\0';
if(i)
{
*pt++=change(string);
i=0;
num++;
}
p++;
}
}
return num;
}

int change(char string[])
{
char *p=string;
int i,j,number=0;
for(i=strlen(p)-1,j=0;i>=0;i--,j++)
{
number=number+(*(p+i)-'0')*(int)pow(10,j);
}
return number;
}
这个该如何解决呢?
M0521 2010-03-11
  • 打赏
  • 举报
回复
LZ 注意判断下转出的整数是否上溢or下溢 曾经调试过这样一个bug
findcsdn 2010-03-11
  • 打赏
  • 举报
回复


#define N 100

#include<stdio.h>
#include <stdlib.h>

void count_num(char *p,int *pt);
void out_num(int* pt);

void main()
{
char str[N],*p=str;
int a[N],*pt=a;

printf("Please enter a string:\n");
gets(p);

for(int i=0; i<N; i++)
a[i]=0;

count_num(p,pt);
out_num(pt);
}

void count_num(char *p,int *pt)
{
char string[N];
int i=0,j=0,num=0;

do
{
if (('0' <= *p) && (*p <= '9'))
{
string[i++]=*p;
}
else
{
if(i)
{
string[i] = 0;
*pt = atoi(string);
i=0;
pt++;
}
}
}
while(*p++);
}

void out_num(int* pt)
{
for(int i=0; i<N; i++)
{
if (pt[i])
printf("%d\n", pt[i]);
else
break;
}
}
昵称很不好取 2010-03-11
  • 打赏
  • 举报
回复
写了一个没怎么调用库函数的繁杂版,楼主如果没有合适的,可以看下
#include <stdio.h>
#include <stdlib.h>
#define N 100

int count_num(char *p,int *pt);

int main(){
char str[N] = {0};
int a[N],*pt=a;
int i,j;
printf("Please enter a string:\n");
gets(str);
j = count_num(str,pt);
printf("你输入的数字为: ");
for(i=0; i<j; ++i)
printf("%d ",pt[i]);
printf("\n");

system("PAUSE");
return(0);
}

int count_num(char *p,int *pt)
{
int j=0,num=0;
char *temp = p;
while(*temp)
{
if(*temp>='0' && *temp<='9'){
num = num*10 + *temp - '0';
if((*(temp+1)) == 0)
pt[j++] = num;
}else{
if(num)
pt[j++] = num;
num = 0;
}
temp++;
}
return j;
}
cnbj8607 2010-03-11
  • 打赏
  • 举报
回复
还是不对,结果要是
*pt=123
*(pt+1)=456
*(pt+2)=789
这种才算正确
上边运行后结果是123456789...了!
dubiousway 2010-03-11
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
void main(){
char *org="a123b456cd789,321,oid83",
tmp[32];
int dst[64],i=0,j=0;
char* p=org;
while(*p){
while(*p>=0x30 && *p<=0x39)
tmp[i++]=*p++;
if(i){
tmp[i]=0;
dst[j++]=atoi(tmp);
i=0;
}
else p++;
}
for(i=0;i<j;i++)
printf("%d\n",dst[i]);
}
cnbj8607 2010-03-11
  • 打赏
  • 举报
回复
wuyu1998方法不正确哦
wuyu1998 2010-03-11
  • 打赏
  • 举报
回复
那个写得不正确,改正如下:
while (*p++) {
if ('0' <= *p && *p <= '9')
string[i++] = *p;
else if (*p == '\0')
break;
}
wuyu1998 2010-03-11
  • 打赏
  • 举报
回复

while (*p++) {
if ('0' <= *p && *p <= '9')
string[i++] = *p;
else if (*p == ',')
break;
}
某某9 2010-03-11
  • 打赏
  • 举报
回复
isdigit(c)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧