C++primer plus第六版课后编程题答案12.2
来源:互联网 发布:linux 安装jdk1.7 编辑:程序博客网 时间:2024/06/04 23:34
string2.cpp
#include <iostream>#include <cctype>#include <string>using namespace std;static int num_string;//不是在h头文件中只能在这里static const int CINLIM=80;//在头文件中只能定义const常量class String{private://char *str;string str;//改为string吧int len;public:String(){len=4;//str=new char[1];str='\0';num_string++;}String(string s){//len=strlen(s);//str=new char[len+1];//strcpy(str,s);len=s.length();str=s;num_string++;}String(const String &st){len=st.len;//st.len()???额,忘了这个是一个String类对象,还是自己定义的..//str=new char[len+1];//strcpy(str,st.str);str=st.str;num_string++;}~String(){--num_string;//delete []str;}int length()const{return len;}String &operator=(const String &st){if(this==&st)//this的应用return *this;//不能写成return this,*this才是对象,this只是一个引用//delete []str;//重新分配大小int len=st.len;//str=new char[len+1];//strcpy(str,st.str);//num_string++;str=st.str;return *this;}String &operator=(const string s)//const char *s){//delete []str;int len=s.length();//str=new char[len+1];str=s;return *this;}char &operator[](int i){return str[i];}const char &operator[](int i)const{return str[i];}//a,重载+String operator+(const String &st){//int lenS=len+st.len;//新的str的大小//char *strS=new char[len+1];//存放加起来的字符串//strcpy(strS,str);//for(int i=len,j=0;i<lenS;i++,j++)//strS[i]=st.str[j];//暂时想不到其他更好的,只能这样了//j=0开始string strS=str+st.str;return String(strS);}//overload +charString operator+(const string s)//const char *s){//int lenS=len+strlen(s);//char *strS=new char[len+1];//strcpy(strS,str);//delete []str;//str=new char[lenS+1];//strcpy(str,strS);//for(int i=len;i<lenS;i++)//str[i]=s[i];string strS=str+" "+s;return String(str);}//overload +=String operator+=(const String &st){*this=*this+st;return *this;}//ShowLowvoid ShowLow(){for(int i=0;i<len;i++)str[i]=tolower(str[i]);}void ShowUp(){for(int i=0;i<len;i++)str[i]=toupper(str[i]);}//char return 次数int num(char ch){int count=0;for(int i=0;i<len;i++)if(str[i]==ch)count++;return count;}//重载char +Stringfriend String operator+(const string s,String &st){/*int lenS=st.len+strlen(s);//strlen()不能识别空格键//我用string类吧char *strS=new char[lenS+1];strcpy(strS,s);for(int i=strlen(s)-1,j=0;i<lenS;i++,j++)strS[i]=st.str[j];*/string strS=s+" "+st.str;return String(strS);}friend bool operator<(const String &st,const String &st2){/*int len1=st.len;int len2=st2.len;int LEN=len1>len2?len1:len2;int i;for(i=0;i<LEN;i++){if(st.str[i]>st2.str[i])return true;}if(i==LEN)return true;return false;*///忘了内置的<return st.str<st2.str;}friend bool operator>(const String &st,const String &st2){return st2<st;}friend bool operator==(const String &st,const String &st2){return st.str==st2.str;}friend ostream &operator<<(ostream &os,const String &st){os<<st.str<<endl;return os;}friend istream &operator>>(istream &is,String &st){char temp[CINLIM];//???is.get(temp,CINLIM);if(is)st=temp;while(is&&is.get()!='\n')continue;return is;}};main122.cpp
#include <iostream>#include "string2.cpp"using namespace std;void main122(){{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<<s3<<endl;//cout<<s2<<endl;//cout<<s1;s2=s2+s1;//cout<<s2;//cout<<"The string \n"<<s2<<"\ncontains "<<s2.num('a')//<<" 'A' characters in it. "<<endl;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.ShowLow();for(int i=0;i<3;i++){if(ans==rgb[i]){cout<<"That's right!"<<endl;success=true;break;}}if(success)break;elsecout<<"Try again!"<<endl;}cout<<"Bye "<<endl;}cin.get();}
有点困,全世界晚安
0 0
- C++primer plus第六版课后编程题答案12.2
- C++primer plus第六版课后编程练习答案12.2
- C++primer plus第六版课后编程题答案 6.1
- C++primer plus第六版课后编程题答案 6.2
- C++primer plus第六版课后编程题答案 6.3
- C++primer plus第六版课后编程题答案 6.4
- C++primer plus第六版课后编程题答案 6.5
- C++primer plus第六版课后编程题答案 6.6
- C++primer plus第六版课后编程题答案 6.7
- C++primer plus第六版课后编程题答案 6.8
- C++primer plus第六版课后编程题答案 6.9
- C++primer plus第六版课后编程题答案7.1
- C++primer plus第六版课后编程题答案7.2
- C++primer plus第六版课后编程题答案7.3
- C++primer plus第六版课后编程题答案7.4
- C++primer plus第六版课后编程题答案7.5
- C++primer plus第六版课后编程题答案7.6
- C++primer plus第六版课后编程题答案 7.7
- ZOJ-1857
- 第7周作业1-循环大战------while
- windows驱动在线帮助
- Python3.2 官方文档翻译--错误与异常
- Python3.2官方文档翻译--异常处理
- C++primer plus第六版课后编程题答案12.2
- jQuery ajax - getScript() 方法
- ubuntu下创建快捷方式
- 检测iPhone/iPad设备方向的三种方法
- 哈夫曼树实现 python
- php.ini的位置
- C++代码操作MYSQL
- 不是技术牛人,如何拿到国内IT巨头的Offer
- 几个常用函数的总结(atoi,memcpy,malloc,memset)