永发信息网

建立一个CString程序,实现针对字符串的全部操作(例如strcpy、strcmp、strlen

答案:2  悬赏:80  手机版
解决时间 2021-02-19 19:45
建立一个CString程序,实现针对字符串的全部操作(例如strcpy、strcmp、strlen、strcat等,具体可以查Turboc2的帮助说明,严禁使用系统提供的库函数,所有函数必须自己定义)。 (要求:1.源文件采用多文件的工程结构2.数据存储采用文件形式3.采用面向对象方法设计4.功能完善,适当的注释)
最佳答案
char *mystrcat(char *dst, char *src)
{
char *p = dst;
while(*p)++p;
while(*p++ = *src++);
return dst;
}
char *mystrcpy(char *dst, char *src)
{
while(*dst++ = *src++);
return dst;
}
int mystrcmp(char *dst, char *src)
{
全部回答
不需要用到继承、多态。界面就不设计了,累。能看明白就行 #include <iostream> using namespace std; class cstring { public: cstring():buffer(null),length(0){} cstring(const cstring& cstr) { int i; buffer = new char[cstr.length + 1]; for (i = 0; i < cstr.length; i++) { buffer[i] = cstr.buffer[i]; } buffer[i] = &apos;\0&apos;; length = cstr.length; } cstring(char* str):length(0) { char* p = str; int i; while (*p) { length++; p++; } buffer = new char[length + 1]; for (i = 0; i < length; i++) { buffer[i] = str[i]; } buffer[i] = &apos;\0&apos;; } ~cstring() { if (length != 0) { delete []buffer; } } void strcpy(cstring cstr); int strcmp(cstring cstr); int strlen(); cstring strcat(cstring cstr); cstring& operator =(const char* str); cstring& operator =(const cstring cstr); friend ostream& operator <<(ostream& os, const cstring cstr); private: char* buffer; int length; }; void cstring::strcpy(cstring cstr) { delete []buffer; *this = cstr; } int cstring::strlen() { return length; } int cstring::strcmp(cstring cstr) { char* p1 = buffer; char* p2 = cstr.buffer; while (*p1 && *p2) { if (*p1 > *p2) { return 1; } else if (*p1 < *p2) { return -1; } p1++; p2++; } if (*p1) { return 1; } else if (*p2) { return -1; } return 0; } cstring cstring::strcat(cstring cstr) { cstring des_cstr(*this); int i; delete []buffer; buffer = new char[length + cstr.length + 1]; for (i = 0; i < length; i++) { buffer[i] = des_cstr.buffer[i]; } for (i = 0; i < cstr.length; i++) { buffer[length + i] = cstr.buffer[i]; } buffer[length + i] = &apos;\0&apos;; length += cstr.length; return *this; } cstring& cstring::operator =(const char* str) { int len = 0; int i; char* p = (char*)str; while (*p) { len++; p++; } buffer = new char[len + 1]; for (i = 0; i < len; i++) { buffer[i] = str[i]; } buffer[i] = &apos;\0&apos;; length = len; return *this; } cstring& cstring::operator =(const cstring cstr) { *this = cstr.buffer; return *this; } ostream& operator <<(ostream& os, const cstring cstr) { os << cstr.buffer; return os; } int main() { cstring s1 = "abc"; cstring s2 = "def"; cout << "s1=\"" << s1 << "\" s2=\"" << s2 << "\"" << endl; cout << "strcpy:" << endl; s1.strcpy(s2); cout << "s1=\"" << s1 << "\" s2=\"" << s2 << "\"" << endl; cout << "s1.strcmp(s2)=" << s1.strcmp(s2) << endl; cout << "s1.strlen()=" << s1.strlen() << endl; cout << "strcat:" << endl; s1.strcat(s2); cout << "s1=" << s1 << endl; return 0; }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
向女生表白后被拒绝了还被拉黑了,怎么办
【今年有多少考生】浙江省今年的考生有几万?
宁海路街道三步两桥社区养老服务站我想知道这
文道批发部地址在什么地方,想过去办事
苹果电脑怎么查询真伪
“……秦堤牢固,工程精当。闸水陡门三十六,
天津市河西区梅江街社会治安综合治理工作中心
请问今年想考事业单位,不知道岗位说考综合知
平元大药房我想知道这个在什么地方
08年准备在工行买基金.望前辈多指点---风险不
双桶洗衣机使用过程
最大男人和最小女人结婚是多少岁
刚跟认识的女孩聊了有好感,她就说话费要没有
孝恩堂在什么地方啊,我要过去处理事情
【2008年中考试题】(2008年绍兴市中考试题)定
推荐资讯
关于高层建筑箱形基础,下列不正确的是()。A.
儿童手表电话电子围栏怎么取消
打算买房子了,相同的面积,是两居好还是三居
长沙臭豆腐哪里的最好吃?长沙臭豆腐品牌哪个
你好,就是我的小米4.下面的三个按键现在按起
下列小组工作沟通与互动技巧中,属于社会工作
招行10元风暴已经支付 邮寄地址可以更改吗
养老金真的缴越多越划算吗
想摆个地摊卖儿童玩具,大家有什么好的建议。
(1分)一个自然生态系统中,肉食性动物总比
好味道烧烤这个地址在什么地方,我要处理点事
董王庄喜羊羊涮烤地址在哪,我要去那里办事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?