看了好几天,不知道错在哪里

晒月亮的幽灵 2013-05-05 11:07:53

// huawei_test61.cpp : 定义控制台应用程序的入口点。
//
//将字符串中的数字从小到大排序
#include "stdafx.h"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
char *test="a3vues4sdf2kjk9df0v";//排序的结果应该是a2vues3sdf4kjk9df0v


int len=strlen(test);
struct temp
{
char num;
int index;
};
temp*t=new temp[len];
int i=0;
int j=0;
while (i<len )
{
if (*(test+i)>='0'&&*(test+i)<='9') //将字符串中的数和下标取出来,放在结构体中
{
t[j].index=i;
t[j].num=*(test+i);
j++;
}
i++;
}
for (int m=j-1;m>0;m--) //对结构体数组的num排序,即 对数字进行排序,下标不变
{
for(int n=0;n<m;n++)
{
temp t1;
if (t[n].num>t[n+1].num)
{
t1.num=t[n].num;
t[n].num=t[n+1].num;
t[n+1].num=t1.num;
}

}
}

for (int m=0;m<j;m++) //排序之后,将t[m].num放回test[t[m].index],
{
test[t[m].index]=t[m].num;//这句总报错,写入位置时发生访问冲突
}
printf("%s",test);

return 0;
}




前思后想感觉没错呀,望大神指点迷津
...全文
90 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxlengwa 2013-05-05
  • 打赏
  • 举报
回复
1. test是指向字符串常量的,所以不可写,重新非配一块内存,或新定义一个字符数组存放结果把,如果只是输出可以循环字符输出,遇到数字单独处理就OK了 2. 这个应可以用计数排序,线性
hugett 2013-05-05
  • 打赏
  • 举报
回复
test指向的是字符串常量,改成
char test[]="a3vues4sdf2kjk9df0v";
另外。。答案是a0vues2sdf3kjk4df9v吧。。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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