【每日一贴】留下学习的足迹,寻各种意见和建议

solo7773 2011-11-23 01:16:30
2011年11月22日

输入一个字符串,内有字母和数字,类似
a123x456 17960? 302tab5876
将其中连续的数字作为整数一次存储到数组a中。例如123放在a[0],456放在a[1]... ...统计共有多少个数字并输出这些数字
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
#define LEN sizeof(char)
void main()
{
char str[N],**p,temp[N];
int i,j,flag,i_p=0,i_num=0,len_str;
printf("请输入一个含有数字的字符串:\n");
gets(str);
len_str=strlen(str);
for(i=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
if(i==0)
i_num++;
else if(str[i-1]<'0'||str[i-1]>'9')
i_num++;
}
}
printf("共有%d个数字\n",i_num);

/*把数字存数组去*/
p=(char **)calloc(i_num,LEN);
for(i=0,j=0,flag=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
flag=1;
else flag=0;
if(flag==1)
temp[j++]=str[i];
if(flag==0||i==len_str-1)
{
if(i==0) continue;
else if((str[i-1]>='0'&&str[i-1]<='9')||i==len_str-1)
{
temp[j]='\0';
p[i_p]=(char *)calloc(N,LEN);
strcpy(p[i_p],temp);
i_p++;
j=0;
}
}
}
/*output*/
printf("含有的数字如下:\n");
for(i_p=0;i_p<i_num;i_p++)
printf("%s\n",p[i_p]);

/*free*/
for(i=0;i<i_num;i++)
free(p[i_p]);
free(p);
}

gcc调试运行,上面的那些测试数据都正常。
可是当用下面这一行字符串去测试的时候,问题就来了,第1个数组元素会出错,其它的却是正常的,然后程序运行完后还会显示一堆我没看懂的东西
12 3 4 2 d c d,,vr444444444444 244
...全文
150 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
solo7773 2011-11-23
  • 打赏
  • 举报
回复
ipconfig /flushdns
今天居然碰到了这类问题
solo7773 2011-11-23
  • 打赏
  • 举报
回复
多谢啊,确实没仔细看,学习了
[Quote=引用 13 楼 xxyxxb 的回复:]

敢情我给你修改的代码(在3楼)你都没仔细看,你的问题出在
C/C++ code

p=(char **)calloc(i_num,LEN);


p指向的是(char *),不是char,所以应该是
C/C++ code

p=(char **)calloc(i_num,sizeof(char *));
[/Quote]
Sad4This 2011-11-23
  • 打赏
  • 举报
回复
很好解决
调试一下应该就能看出问题
小笨同学 2011-11-23
  • 打赏
  • 举报
回复
敢情我给你修改的代码(在3楼)你都没仔细看,你的问题出在

p=(char **)calloc(i_num,LEN);

p指向的是(char *),不是char,所以应该是

p=(char **)calloc(i_num,sizeof(char *));
goldbeef 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xialeijie368 的回复:]

干吗不把data定义成string 这样更方便引用 6 楼 goldbeef 的回复:
LZ要模块化编程啊,这样思路清晰,且可扩张性较高

C/C++ code

#include <iostream>
#include <string>
#define SIZE 10
using namespace std;

bool IsDigit(char ch)//判断某个字符是……
[/Quote]当然也可以,程序是比较灵活的
xialeijie368 2011-11-23
  • 打赏
  • 举报
回复
干吗不把data定义成string 这样更方便[Quote=引用 6 楼 goldbeef 的回复:]
LZ要模块化编程啊,这样思路清晰,且可扩张性较高

C/C++ code

#include <iostream>
#include <string>
#define SIZE 10
using namespace std;

bool IsDigit(char ch)//判断某个字符是否是数字字符
{
if (ch>='0'&&ch<='9')
……
[/Quote]
solo7773 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xxyxxb 的回复:]
呵呵,这个东西慢慢来,当初我也花了大功夫才弄懂的,我3楼的帖子提到不用”free(p2)“,是因为p2声明时是一个指针数组(char *p2[i_num]),如果声明为"char **p",后面一定用free(p)。
至于为什么通不过,没看代码之前我也不好说,你可以把代码贴出来
[/Quote]
代码如下,只要输入含有3个或以上的数字,就会出问题
你可以输入"1a","1a2","1a2b3"来测试
vc2010能正确编译执行,但是执行完后弹出以下提示:
Debug Assertion Failed!
Expression:_CrtIsValidHeapPointer(pUserData)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
#define LEN sizeof(char)
void main()
{
char str[N],**p,temp[N];
int i,j,flag,i_p=0,i_num=0,len_str;
printf("请输入一个含有数字的字符串:\n");
gets(str);
len_str=strlen(str);
for(i=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
if(i==0)
i_num++;
else if(str[i-1]<'0'||str[i-1]>'9')
i_num++;
}
}
printf("共有%d个数字\n",i_num);

/*把数字存数组去*/
p=(char **)calloc(i_num,LEN);
for(i=0,j=0,flag=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
flag=1;
else flag=0;
if(flag==1)
temp[j++]=str[i];
if(flag==0||i==len_str-1)
{
if(i==0&&i!=len_str-1) continue;
if((str[i-1]>='0'&&str[i-1]<='9')||i==len_str-1)
{
temp[j]='\0';
p[i_p]=(char *)calloc(N,LEN);
strcpy(p[i_p],temp);
i_p++;
j=0;
}
}
}
/*output*/
printf("含有的数字如下:\n");
for(i_p=0;i_p<i_num;i_p++)
printf("%s\n",p[i_p]);

/*不确定是不是这下面的代码出的问题,但是只要删掉程序就正常了*/
for(i_p=0;i_p<i_num;i_p++)
free(p[i_p]);
//free(p);这行必须注释,否则连输入一个含数字的字符串"1"都会出问题
}
小笨同学 2011-11-23
  • 打赏
  • 举报
回复
呵呵,这个东西慢慢来,当初我也花了大功夫才弄懂的,我3楼的帖子提到不用”free(p2)“,是因为p2声明时是一个指针数组(char *p2[i_num]),如果声明为"char **p",后面一定用free(p)。
至于为什么通不过,没看代码之前我也不好说,你可以把代码贴出来

[Quote=引用 8 楼 solo7773 的回复:]

又在vc2010下折腾了1个多小时了,无果
xxyxxb,我按照你说的最后不用free(p),我还不明白是为什么
1、如果只输入短字符串,如2f3,程序是正常运行
2、但是只要我输入一长串,如:“12 3 4 2 d c d,,vr444444444444 24”,最后问题依旧,vc弹出个看不懂个的框框
但是只要我在程序后面不执行释放地址的操作,不论长串短串都正常了,这是什么原因哦
上……
[/Quote]
solo7773 2011-11-23
  • 打赏
  • 举报
回复
又在vc2010下折腾了1个多小时了,无果
xxyxxb,我按照你说的最后不用free(p),我还不明白是为什么
1、如果只输入短字符串,如2f3,程序是正常运行
2、但是只要我输入一长串,如:“12 3 4 2 d c d,,vr444444444444 24”,最后问题依旧,vc弹出个看不懂个的框框
但是只要我在程序后面不执行释放地址的操作,不论长串短串都正常了,这是什么原因哦
上个帖子你说执行几次就释放几次,这程序我执行i_num次,释放i_num次,难道不对?

单步调试在第一次执行内存分配的时候
p=(char **)calloc(i_num,LEN);
查看p的状态如下显示:
= CXX0030: Error: expression cannot be evaluated
小笨同学 2011-11-23
  • 打赏
  • 举报
回复
纠正一下

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
#define LEN (sizeof(char))
int main()
{
char str[N];
char **p;
char temp[N];
int i;
int j;
int flag;
int i_p=0;
int i_num=0;
int len_str;
printf("请输入一个含有数字的字符串:\n");
gets(str);
len_str=strlen(str);
for(i=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
if(i==0)
i_num++;
else if(str[i-1]<'0'||str[i-1]>'9')
i_num++;
}
}
printf("共有%d个数字\n",i_num);

/*把数字存数组去*/
p=(char **)calloc(i_num,sizeof(char *)); //用来存放指针的
for(i=0,j=0,flag=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
flag=1;
else flag=0;
if(flag==1)
temp[j++]=str[i];
if(flag==0||i==len_str-1)
{
if(i==0) continue;
else if((str[i-1]>='0'&&str[i-1]<='9')||i==len_str-1)
{
temp[j]='\0';
p[i_p]=(char *)calloc(N,LEN);
strcpy(p[i_p],temp);
i_p++;
j=0;
}
}
}

//版本2
char *p2[i_num];
i_p = 0;
for (i = 0; i < len_str; i++) {
if (str[i] >= '0' && str[i] <= '9') {
p2[i_p] = malloc(N * LEN);
j = 0;
while (str[i] >= '0' && str[i] <= '9') {
p2[i_p][j++] = str[i++];
}
p2[i_p++][j] = '\0';
}
}

/*output*/
printf("含有的数字如下:\n");
for(i_p=0;i_p<i_num;i_p++){ //不是i,而是i_p
printf("%s\n",p[i_p]);
printf("%s\n", p2[i_p]);
}

/*free*/
for(i_p=0;i_p<i_num;i_p++) {
free(p[i_p]);
free(p2[i_p]);
}
free(p); //不用free(p2),至于为什么,自己想
return 0;
}
小笨同学 2011-11-23
  • 打赏
  • 举报
回复

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 100
#define LEN (sizeof(char))
int main()
{
char str[N] = "12 3 4 2 d c d,,vr444444444444 24";
char **p;
char temp[N];
int i;
int j;
int flag;
int i_p=0;
int i_num=0;
int len_str;
printf("请输入一个含有数字的字符串:\n");
//gets(str);
len_str=strlen(str);
for(i=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
{
if(i==0)
i_num++;
else if(str[i-1]<'0'||str[i-1]>'9')
i_num++;
}
}
printf("共有%d个数字\n",i_num);

/*把数字存数组去*/
p=(char **)calloc(i_num,sizeof(char *)); //用来存放指针的
for(i=0,j=0,flag=0;i<len_str;i++)
{
if(str[i]>='0'&&str[i]<='9')
flag=1;
else flag=0;
if(flag==1)
temp[j++]=str[i];
if(flag==0||i==len_str-1)
{
if(i==0) continue;
else if((str[i-1]>='0'&&str[i-1]<='9')||i==len_str-1)
{
temp[j]='\0';
p[i_p]=(char *)calloc(N,LEN);
printf("%s\n", temp);
strcpy(p[i_p],temp);
i_p++;
j=0;
}
}
}

//版本2
char *p2[i_num];
i_p = 0;
for (i = 0; i < len_str; i++) {
if (str[i] >= '0' && str[i] <= '9') {
p2[i_p] = malloc(N * LEN);
j = 0;
while (str[i] >= '0' && str[i] <= '9') {
p2[i_p][j++] = str[i++];
}
p2[i_p++][j-1] = '\0';
}
}

/*output*/
printf("含有的数字如下:\n");
for(i_p=0;i_p<i_num;i_p++){ //不是i,而是i_p
printf("%s\n",p[i_p]);
printf("%s\n", p2[i_p]);
}

/*free*/
for(i_p=0;i_p<i_num;i_p++) {
free(p[i_p]);
free(p2[i_p]);
}
free(p); //不用free(p2),至于为什么,自己想
return 0;
}
solo7773 2011-11-23
  • 打赏
  • 举报
回复
ls的都是强人啊,努力学习
[Quote=引用 6 楼 goldbeef 的回复:]

LZ要模块化编程啊,这样思路清晰,且可扩张性较高
……
[/Quote]
goldbeef 2011-11-23
  • 打赏
  • 举报
回复
LZ要模块化编程啊,这样思路清晰,且可扩张性较高
#include <iostream>
#include <string>
#define SIZE 10
using namespace std;

bool IsDigit(char ch)//判断某个字符是否是数字字符
{
if (ch>='0'&&ch<='9')
{
return true;
}
else
{
return false;
}
}

void CopySubStr(char *str,int start_idnex,int end_index,string &num)//将str的start_index,end_index之间的字符复制到num中
{
int i;
for (i=start_idnex;i<=end_index;i++)
{
num=num+str[i];
}
}

void Opt(char *data,string *str_group,int &str_count)//整体操作
{
int i,j;
int start_index,end_index;
int data_length=0;
while (data[data_length]!='\0')
{
data_length++;
}

for (i=0;i<data_length;i++)
{
if (IsDigit(data[i]))
{
start_index=i;
while (IsDigit(data[++i]));
end_index=i-1;
CopySubStr(data,start_index,end_index,str_group[str_count]);
str_count++;
}
}

}


int main()
{
string str_group[SIZE];//用于存储提取的数字
int str_count=0;//对数字的个数计数
char data[256];//输入的字符串
gets(data);

Opt(data,str_group,str_count);

cout<<endl;
cout<<str_count<<" number :"<<endl;
for (int i=0;i<str_count;i++)//输出提取到的数字
{
cout<<str_group[i]<<" ";
}
cout<<endl;

return 0;
}
solo7773 2011-11-23
  • 打赏
  • 举报
回复
问题描述

请输入一个含有数字的字符串:
12 3 4 2 d c d,,vr444444444444 24
共有6个数字
含有的数字如下:
��� ��
3
4
2
444444444444
24
*** glibc detected *** ./ex.exe: free(): invalid next size (fast): 0x09c2e008 ***
======= Backtrace: =========
/lib/libc.so.6[0x45ac12b5]
./ex.exe[0x804881d]
/lib/libc.so.6(__libc_start_main+0xf3)[0x45a69413]
./ex.exe[0x8048421]
======= Memory map: ========
00f63000-00f64000 r-xp 00000000 00:00 0 [vdso]
08048000-08049000 r-xp 00000000 fd:01 393282 /home/zsf/mycpro/ex.exe
08049000-0804a000 rw-p 00000000 fd:01 393282 /home/zsf/mycpro/ex.exe
09c2e000-09c4f000 rw-p 00000000 00:00 0 [heap]
45a2f000-45a4c000 r-xp 00000000 fd:01 1807 /lib/ld-2.14.so
45a4c000-45a4d000 r--p 0001d000 fd:01 1807 /lib/ld-2.14.so
45a4d000-45a4e000 rw-p 0001e000 fd:01 1807 /lib/ld-2.14.so
45a50000-45bd5000 r-xp 00000000 fd:01 1809 /lib/libc-2.14.so
45bd5000-45bd6000 ---p 00185000 fd:01 1809 /lib/libc-2.14.so
45bd6000-45bd8000 r--p 00185000 fd:01 1809 /lib/libc-2.14.so
45bd8000-45bd9000 rw-p 00187000 fd:01 1809 /lib/libc-2.14.so
45bd9000-45bdc000 rw-p 00000000 00:00 0
4a72c000-4a748000 r-xp 00000000 fd:01 1350 /lib/libgcc_s-4.6.1-20110908.so.1
4a748000-4a749000 rw-p 0001b000 fd:01 1350 /lib/libgcc_s-4.6.1-20110908.so.1
b78c8000-b78c9000 rw-p 00000000 00:00 0
b78d7000-b78db000 rw-p 00000000 00:00 0
bf8bd000-bf8de000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
[zsf@zsf mycpro]$ ./ex.exe

70,034

社区成员

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

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