chapter12test2

写了一上午终于把test2搞定,技术还有待提高,要多练习,一点半了,有点饿,去吃点什么好的补补脑子

我把树上的程序改了,题目里面没有要求的都删减了,留下的都用上了,最费时的是类复制和字符指针赋值,然后就是重载==,差点把我绕歇菜了!

String.h

#ifndef STRING_H_
#define STRING_H_
#include<iostream>
using std::istream;
using std::ostream;
class String
{
private:
char *str;
int len;
public:
String();
~String();
String(const char *s);
String(const String &s);
String &operator=(const char *s);
String &operator=(const String &s);
String operator+(const String &s);
bool operator==(const String &s);
String &stringup(const String &s);
String &stringlow(const String &s);
int has(char a);
friend String operator+(const char* c,const String &s);
friend ostream &operator<<(ostream &os, const String &s);
friend istream &operator>>(istream &is, String &st);
};


#endif


String.cpp

#include<iostream>
#include"String.h"
#include<string>
#include<cstring>
#include<cctype>


String::String()
{
str = new char[10];
strcpy_s(str,10,"default.");
len = 10;
}
String::~String()
{
delete[]str;
}
String::String(const char *s)
{
len = strlen(s) + 1;
str = new char[len];
strcpy_s(str,len,s);
}
String::String(const String &s)
{
len = strlen(s.str) + 1;
str = new char[len];
strcpy_s(str, len, s.str);
}
String &String::operator=(const char *s)
{
len = strlen(s)+1;
str = new char[len];
strcpy_s(str, len, s);
return *this;
}
String &String::operator=(const String &s)
{
len = strlen(s.str) + 1;
str = new char[len];
strcpy_s(str, len, s.str);
return *this;
}
String String::operator+(const String &s)
{
String sum;
len = strlen(str)+strlen(s.str) + 1;
char *temp = new char[len];
strcpy_s(temp, len, str);
strcat_s(temp, len, s.str);
sum.str = temp;
return sum;
}
String &String::stringup(const String &s)
{
len = strlen(s.str);
for (int i = 0; i < len; i++)
str[i] = toupper(s.str[i]);
return *this;
}
String &String::stringlow(const String &s)
{
len = strlen(s.str);
for (int i = 0; i < len; i++)
str[i] = tolower(s.str[i]);
return *this;
}
int String::has(char a)
{
int num=0;
len = strlen(str);
for (int i = 0; i < len; i++)
{
if (str[i] == a)
num++;
}
return num;
}
String operator+(const char* c, const String &s)
{
String temp;
temp.len = strlen(c) + strlen(s.str) + 1;
char *tem = new char[temp.len];
strcpy_s(tem, temp.len, c);
strcat_s(tem, temp.len, s.str);
temp.str=tem;
return temp;
}
bool String::operator==(const String &s)
{
return (std::strcmp(str, s.str) == 0);
}
ostream &operator<<(ostream &os, const String &s)
{
os << s.str<<'\n';
return os;
}
istream &operator>>(istream &is, String &st)
{
char temp[30];
is.get(temp, 30);
if (is)
st = temp;
while (is&&is.get() != '\n')
continue;
return is;
}


user.cpp

#include<iostream>
#include"String.h"
using namespace std;
int main()
{
String s1(" and I am a C++ student");
String s2 = "please enter your name :";
String s3;
cout << s2; 
cin >> s3;
s2 = "My name is " + s3;
cout << s2;
s2 = s2 + s1;
s2.stringup(s2);
cout << "The String :" << s2 << ", contains " << s2.has('A') << " 'A' characters in it.\n";
s1 = "red";
String rgb[3] = { String(s1), String("green"), String("blue") };
cout << "Enter the name of a primary color for mixing light:";
String ans;
bool success = false;
while (cin >> ans)
{
ans.stringlow(ans);
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i])
{
cout << "That's righr.\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again !\n";
}
cout << "BYE !\n";
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值