要求:从键盘上输入3个字符串,从大到小排序进行输出.利用拷贝函数和比较函数实现.C++程序设计

#include <iostream.h>
#include <string.h>
void main( )
{
char str1[6],str2[6],str3[6],temp[6];
cout<<"Please input 3 strings:"<<endl;
cin>>str1>>str2>>str3;
//自己写排序过程。
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
cout<<"str3:"<<str3<<endl;
提示:
(1)字符串的拷贝函数strcpy
格式:strcpy(字符数组名1,字符数组名2)
举例: strcpy(str1,str2);
(2)字符串的比较函数strcmp格式: strcmp(字符数组名1,字符数组名2)
如果: 字符串1=字符串2 ,返回值=0
字符串1>字符串2 ,返回值>0
字符串1<字符串2 ,返回值<0

这个程序不是你都写过了吗,只是有些错误

程序修改的地方:

    1、添加命名空间

     using namespace std;   // 添加命名空间引用

    2、主函数和大括号对调,并改主函数类型为 int 

        int  main()           // 主函数和大括号的位置对调一下 
        {

完整的修改后代码如下:

#include <iostream>
#include <string>
using namespace std;   // 添加命名空间引用 
 
int  main()           // 主函数和大括号的位置对调一下 
{  
 char str1[6], str2[6], str3[6], temp[6];
 cout<<"Please input  3 strings:"<<endl;
 cin>>str1>>str2>>str3;   
if( strcmp(str1,str2) < 0)
 {
  strcpy(temp, str1);
  strcpy(str1, str2);
  strcpy(str2, temp);
 }
 if( strcmp(str2,str3) < 0)
 {
  strcpy(temp, str2);
  strcpy(str2, str3);
  strcpy(str3, temp);
  if( strcmp(str1,str2) < 0)
  {
   strcpy(temp, str1);
   strcpy(str1, str2);
   strcpy(str2, temp);
  }
 }
 cout<<"str1:"<<str1<<endl; 
 cout<<"str2:"<<str2<<endl; 
 cout<<"str3:"<<str3<<endl;
  
 return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答