C++将一个字符串按字符大小排序,数字字符排在字母后面,要求定义函数实现,不使用库函数,求源代码。
代码如下:
#include <iostream>
#include <string>
using namespace std;
void fun(char buf[])
{
int i,j;
char ch;
int n = strlen(buf);
for (i=0;i<n-1;i++)
{
for (j=0;j<n-1-i;j++)
{
if(buf[j] > buf[j+1])
{
ch = buf[j];
buf[j]=buf[j+1];
buf[j+1]= ch;
}
}
}
//cout << buf << endl;
//将数字字符移动到末尾
//找到数字字符的起始位置和终止位置#4342abv
int start=0,end = 0,k=0;
char* st = new char[n+1];
for(i=0;i<n;i++)
{
if(buf[i]>='0' && buf[i]<='9')
{
st[k++] = buf[i];
if(start=0&&end==0)
start = i;
else
end = i;
}
}
st[k] = 0;
//cout << st<<endl;
//数组迁移
for (i=start;i<n-(end-start+1);i++)
{
buf[i]= buf[i+end-start+1];
}
//cout << buf << endl;
for (i=0;i<end-start+1;i++)
{
buf[n-i] = st[end-start-i];
}
delete[] st;
st=0;
}
int main()
{
char buf[100]={0};
cin.getline(buf,100);
fun(buf);
cout << buf;
return 0;
}
那你就用冒泡排序,快排 堆排 选择排序。先把数字挑出来。
然后分别排序。最后组合。