首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

第一次写c++,麻烦大家帮一下忙,诚挚感谢解决方法

2012-02-16 
第一次写c++,麻烦大家帮一下忙,诚挚感谢/*编写一个程序,从键盘接收一个字符串,然后按照字符串从小到大进*

第一次写c++,麻烦大家帮一下忙,诚挚感谢
/*编写一个程序,从键盘接收一个字符串,然后按照字符串从小到大进
  *行排序,并删除重复的字符。
  */
#include   <iostream.h>
#include   <stdio.h>
#include   <string.h>
//定义函数   接收一个字符串   按照字符串从小到大进行排序,并删除重复的字符
void   sort_funciton(char   *putin_receive)
{//函数体


char   *r;
        char   *p,*q;
char   temp;

for(p=putin_receive;*p;p++)
{
for(q=r=p;*q;q++)
{
if(*r <*q)//扫描字符串p,由r指向其中最小的字符
r=q;
if(r!=p)
{
temp=*r;*r=*p;*p=temp;   //交换*r和*p两个字符
}
}
}
}
void   delsame_funciton(char   *str)   //删除str中重复的字符
{
char   *   p,*q;
for(p=str;*p;p++)

for(q=p;*p==*q;q++);
strcpy(p+1,q);   //前移一个字符

}
void   main()
{
char   putin_receive[10];
cout < < "please   put   into   one   string: " < <endl;
gets(putin_receive);    
sort_funciton(putin_receive);
delsame_funciton(putin_receive);
cout < < "the   result   is: " < <putin_receive < <endl;
}


------------------------------------
上面是程序,编译没错误,但是执行结果不对,找不到原因了,实在是不好意思

[解决办法]
C风格的写法:

#include <string.h>
#include <iostream>
using namespace std;
void sort(char* str)
{
if(!str) return;
for(;*str;++str){
char* first=str+1;
//找到第一个比*str小的地方,该地址为first
while(*first && !(*first <*str)) ++first;
if(!*first) continue;//没有比*str小的字符了
char* second=first+1;//从first开始继续查找比*first更小的
while(*second){
if(*second <*first) first=second;
++second;//如果找到比*first更小的,保留位置
}

char ch=*str;
*str=*first;
*first=ch;

}
}
void del_same(char* str)
{
for(;*str;++str){
char* start=str+1;//从下一个字符开始找到第一个不同的字符
while(*start && *start==*str) ++start;
if(*start && start!=str+1){//找到不同字符且需要拷贝
strcpy(str+1,start);
}
}
}

int main()
{
char buff[128];
cin> > buff;
sort(buff);
cout < <buff < < '\n ';
del_same(buff);
cout < <buff < < '\n ';
}
[解决办法]
C++风格的;

#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
string str;
cin> > str;
sort(str.begin(),str.end());
cout < <str < < '\n ';
str.erase(unique(str.begin(),str.end()),str.end());
cout < <str < < '\n ';
}

G:\test> c
r56re5g5jUYFY45fdsFDSrgf
455556DFFSUYYdeffggjrrrs
456DFSUYdefgjrs
[解决办法]
按照你的思路把程序改了一下,经测试没有问题.不过你的这种写法还是属于C风格的写法.程序代码修改后如下:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
//定义函数 接收一个字符串 按照字符串从小到大进行排序,并删除重复的字符
void sort_funciton(char *putin_receive)
{//函数体

char *r;
char *p,*q;
char temp;
//冒泡法排序
for(p = putin_receive; *p ; p++)
{
r = p;//不能放在上边的for条件中

for(q =p + 1; *q ;q++)


{
if (*r > *q)
{
temp=*r;*r=*q;*q=temp; //交换*r和*p两个字符
}
}
}
}
void delsame_funciton(char *str) //删除str中重复的字符
{
char *p,*q;
char m[100];
int i,nlen = strlen(str);
p =str;
for (i = 0; i < nlen; i++,p++)
{
m[i] = *str;
for(q =p+1 ; *q ;q++)
{
if (*p == *q)
{
p++;
}
}
}
m[i] = '\0 ';
strcpy(str,m);
}
void main()
{
char putin_receive[10];
cout < < "please put into one string: " < <endl;
gets(putin_receive);
cout < < "你输入的字符串为 " < <putin_receive < <endl;
sort_funciton(putin_receive);
cout < < "排序后的字符串为 " < <putin_receive < <endl;
delsame_funciton(putin_receive);
cout < < "the last result is: " < <putin_receive < <endl;
}

热点排行