注册 登录
编程论坛 C++教室

请教各位大神,手动输入两个字符串,比较两个字符串的大小,输出大的字符串

唐兵 发布于 2013-04-09 22:53, 6969 次点击
#include <iostream.h>
#include<string>

class A
{public:
void fun(char *p);
void display();
char *p;

};
void A::fun(char *p)
{
   
   
    if(st1.strlen(p)>st2.strlen(p))cout<<st1.p;
    else cout<<(st2.p);//主要是这步该怎样写呢?

}
void A::display()
{cout<<"input strings"<<endl;}
int main()
{
    A st1;

    A st2;
    char a[20];
    st1.display();
    cin>>a;
    st1.fun(a);
    st2.display();
    cin>>a;
    st2.fun(a);
    //st2.display();
    //a[20]={0}
    //cin>>a;
    //st2.fun(a);
    ;
return 0;
}
12 回复
#2
azzbcc2013-04-09 22:58
依照什么比大小,长度?字典序?
#3
唐兵2013-04-09 23:07
回复 2楼 azzbcc
不好意思,长度
#4
azzbcc2013-04-09 23:20
你还是在看看书吧,乱的不行、、

哪来的str1、str2?
#5
fanpengpeng2013-04-09 23:23
你可以的
#6
唐兵2013-04-11 17:15
回复 4楼 azzbcc
str1.str2是定义的对象
#7
aa4959397922013-04-11 20:21
我也刚学面向对象的思维。之前有学过 C 语言,看到你这个贴后,自己试着写了一个。


#include <iostream.h>
#include <string.h>
class Compare
{
public :
    int Comparison_Fun (char *First,char *Second);
    void Show (int a,char *First,char *Second);
};
int Compare :: Comparison_Fun (char *First,char *Second)
{
    if ((strcmp (First,Second) > 0))
    {
        return 1;
    }
    else if ((strcmp (First,Second) < 0))
    {
        return -1;
    }
    else
    {
        return 0;
    }

}
void Compare :: Show (int a,char *First,char *Second)
{
    if (a > 0)
    {
        cout << First << endl << endl;
    }
    else if (a < 0)
    {
        cout << Second << endl << endl;
    }
    else
    {
        cout << "两个字符串相等" << endl << endl;
    }
}
void main()
{
    Compare Comparison_Function;
    int a;
    char First[20],Second[20];
    cout << "请输入第一个字符串 :";
    cin >> First;
    cout << endl;
    cout << "请输入第二个字符串 :";
    cin >> Second;
    cout << endl;
    a = Comparison_ (First,Second);
    Comparison_Function.Show (a,First,Second);
}


仅供参考,个人感觉我这串东西写的不怎么样。面向对象的思维方式还没有形成。
#8
fanflysheep2013-04-11 20:24
#include<iostream>
#include<string>
using namespace std;

void compareString()
{
    string s1="",s2="";
    cin>>s1>>s2;
    int la=0,lb=0;
    la=s1.length();//如果仅比较长度
    lb=s2.length();
    if(la>lb) cout<<s1<<endl;
    else cout<<s2<<endl;
}

int main()
{
    compareString();
    return 0;
}

#9
邓士林2013-04-11 21:31
1、函数fun中if(st1.strlen(p)>st2.strlen(p))cout<<st1.p;你这str1和str2是个对性,但是都没声明,你怎么用的啊!
2、cout<<st1.p;你这样也不对啊!你的p是一个指针变量,怎么能这样引用呢?
3、 st1.display();你调用这个函数格式对的,但是没有给参数啊!你怎么用它做呢?
4、你的fun函数逻辑有错误,每次只能传递一个参数,你想让它比较两个,这样怎么可能做到呢?
我给你修改下你的代码,大致结构没变化,你看看,符合你的要求
#include <iostream.h>
#include<string>
 
class A
 {public:
 void fun(char *p1,char *p2);
 void display();
 char *p;
 
};
 void A::fun(char *p1,char *p2)
 {
   if(strlen(p1)>strlen(p2))cout<<p1<<endl;
     else cout<<p2<<endl;//主要是这步该怎样写呢?
 
}
 void A::display()
 {cout<<"input strings"<<endl;}
 int main()
 {
     A st1;
     A st2;
     char a[20],b[20];
     st1.display();
     cin>>a;
     st2.display();
     cin>>b;
     st2.fun(a,b);
     
 return 0;
 }
#10
唐兵2013-04-12 22:41
回复 7楼 aa495939792
呵呵,谢谢哈
#11
如蜗牛2013-04-12 22:54
#12
唐兵2013-04-12 23:33
回复 8楼 fanflysheep
哇,简洁,谢谢
#13
唐兵2013-04-12 23:37
回复 9楼 邓士林
嗯,麻烦你了哈,真心感谢
1