开发者社区> 问答> 正文

使用指针处理下面的问题,输入四个字符串,按由小到大顺序输出

#include "stdafx.h"
#include 
#include 
using namespace std;
int main()
{
void swap(char *,char *);
char str1[20],str2[20],str3[20],str4[20];
cout<<"请输入四个字符串:"< cin>>str1[20];
cin>>str2[20];
cin>>str3[20];
cin>>str4[20];
if(strcmp(str1,str2)>0) swap(str1,str2);
if(strcmp(str1,str3)>0) swap(str1,str3);
if(strcmp(str1,str4)>0) swap(str1,str4);
if(strcmp(str2,str3)>0) swap(str2,str3);
if(strcmp(str2,str4)>0) swap(str2,str4);
if(strcmp(str3,str4)>0) swap(str3,str4);
cout<<"从小到大的排序是:"<<endl;
cout<<str1<<" "<<str2<<" "<<str3<<" "<<str4<<endl;
return 0;
}
void swap(char *p1,char *p2,char *p3)
{
char p[20];
strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p3);strcpy(p3,p);strcpy(p,p2);strcpy(p1,p3);
}
最后程序错误,他显示:
(1)warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
(2)参见“strcpy”的声明
(3)error LNK2019: 无法解析的外部符号 "void __cdecl swap(char *,char *)" (?swap@@YAXPAD0@Z),该符号在函数 _main 中被引用
(4)fatal error LNK1120: 1 个无法解析的外部命令
我不知道最后void swap那里要怎么写,strcpy要怎么用啊???请各位解决!!!!

展开
收起
a123456678 2016-03-04 16:42:12 3222 0
1 条回答
写回答
取消 提交回答
  • 正确的方式如下

    #include 
    #include 
    #include 
    using namespace std;
    
    void swap(char p1,char *p2/,char p3/);
    
    int main()
    {
    void swap(char *,char *);
    char str1[20],str2[20],str3[20],str4[20];
    cout<<"请输入四个字符串:"< cin>>str1;
    cin>>str2;
    cin>>str3;
    cin>>str4;
    if(strcmp(str1,str2)>0) swap(str1,str2);
    if(strcmp(str1,str3)>0) swap(str1,str3);
    if(strcmp(str1,str4)>0) swap(str1,str4);
    if(strcmp(str2,str3)>0) swap(str2,str3);
    if(strcmp(str2,str4)>0) swap(str2,str4);
    if(strcmp(str3,str4)>0) swap(str3,str4);
    cout<<"从小到大的排序是:"<<endl;
    cout<<str1<<" "<<str2<<" "<<str3<<" "<<str4<<endl;
    Sleep(INFINITE);
    return 0;
    }
    
    void swap(char p1,char *p2/,char p3/)
    {
    char p[20];
    //strcpy(p,p1);strcpy(p1,p2);strcpy(p2,p3);strcpy(p3,p);strcpy(p,p2);strcpy(p1,p3);
    strcpy(p,p1);strcpy(p1,p2);strcpy(p2, p);
    }
    2019-07-17 18:52:31
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载