| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1666 人关注过本帖
标题:求助
只看楼主 加入收藏
qurong
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-6-27
收藏
 问题点数:0 回复次数:6 
求助

问题1定义一个分数类fraction(分数),定义运算符“<<”,以分数形式输出结果(如2/3)。写出使用该类的完整程序。

问题2定义一个字符串类string,使其至少具有内容(contents)和长度(length)两个数据成员,并具有显示字符串、求字符串长度、给原字符串后添加一个字符串等功能。

请大侠们不吝指教

搜索更多相关主题的帖子: contents 字符串 
2004-06-27 16:52
qurong
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-6-27
收藏
得分:0 
没人吗?
2004-06-27 17:34
zff_ff
Rank: 1
等 级:新手上路
帖 子:147
专家分:0
注 册:2004-5-12
收藏
得分:0 

#include<iostream.h>

class number{

friend ostream &operator<<(ostream &, const number &);

friend istream &operator>>(istream &, number&);

private:

int numerator;

int denominator;

};

ostream &operator<<(ostream& output, number& num)

{

output<<num.numerator<<"/"<<num.denominator;

return output

}

istream &operator>>(istream &input, number &num)

{

input>>num.numerator;

input.ignore(1);

input>>num.denominator;

return input;

}

这是第一题的答案,主函数我就不写了,你自己加上吧

还有我连>>也顺便帮你重载了,如果你觉得没必要,就把他给删了

我没有调试过你自己调试一下,这可以使你对程序更加了解

其实是我比较懒,不过错的地方应该不是很多)

第二题也很简单,只是写起来比较麻烦

自己在想想吧,睡觉先。。。。。。。。

[此贴子已经被作者于2004-06-28 00:12:46编辑过]


偶是一只想要飞却忘了咋飞的菜鸟
2004-06-28 00:10
qurong
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2004-6-27
收藏
得分:0 
先谢谢先了
谢谢
2004-06-28 05:16
kuangjingbo
Rank: 1
等 级:新手上路
帖 子:312
专家分:0
注 册:2004-4-24
收藏
得分:0 
zff_ff 写的很好。

永不放弃!
2004-07-01 17:21
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

#include<iostream> #include<cstdlib> using namespace std;

class Fraction { private: int numerator; int denominator; public: Fraction(int n, int d) { numerator=n; if(d) denominator = d; } friend ostream & operator<<(ostream &, const Fraction &); };

ostream & operator<<(ostream & output, const Fraction & theFraction) { output<<theFraction.numerator<<"/"<<theFraction.denominator; return output; } int main() { int n = 2; int d = 3; Fraction myFraction(n, d); cout<<myFraction<<endl; system("pause"); return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-07-02 21:54
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

#include<iostream> #include<cstring> #include<cstdlib> using namespace std;

class String { private: char * str; // pointer to string int len; // length of string enum{ CINLIM = 80}; // cin input limit public: String(const char * s); // constructor String(); // default constructor String(const String &); // copy constructor ~String(); // destructor int length() const { return len;} // overloaded operator methods String & operator=(const String &); String & operator=(const char *); char & operator[](int i); const char & operator[](int i) const; // friend function friend ostream & operator<<(ostream & os, const String & st); friend istream & operator>>(istream & is, String & st); };

// class methods String::String(const char * s) // construct String from C string { len = strlen(s); // set size str = new char[len +1]; // allot storage strcpy(str, s); // initialize pointer } String::String() // default constructor { len = 4; str = new char[1]; str[0] = '\0'; // default string } String::String(const String & st) { len = st.len; // same length str = new char[len + 1]; // allot space strcpy(str, st.str); // copy string to new location } String::~String() // destructor { delete [] str; } // assign a String to a String String & String::operator =(const String & st) { if(this == &st) return *this; delete [] str; len = st.len; str = new char[len +1]; strcpy(str, st.str); return * this; } // assign a C string to a String String & String::operator =(const char * s) { delete [] str; len = strlen(s); str = new char[len +1]; strcpy(str,s); return *this; } // read-write char access for non-const String char & String::operator [](int i) { return str[i]; } // read-only char access for const String const char & String::operator [](int i) const { return str[i]; } // String output ostream & operator<<(ostream & os, const String & st) { os<<st.str; return os; } istream & operator>>(istream & is, String & st) { char temp[String::CINLIM]; is.get(temp, String::CINLIM); if(is) st = temp; while(is && is.get() != '\n') continue; return is; } int main() { String name; cout<<"What's your name?\n"; cin>>name; cout<<name.length();

system("pause"); return 0; }


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-07-02 22:40
快速回复:求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015359 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved