1001. The String Class

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class String {

public:

String();             // str = ""

String(const char*);    // str = "abc"

String(const String&);   // str = other_string

String& operator=(const char *);

String& operator=(const String&);

String operator+(const String&);

~String();

char& operator[](int i);

char operator[](int i) const;

int size() const;

String& operator+=(const String&);

String& operator+=(const char*);

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

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

friend bool operator==(const String& x, const char* s);

friend bool operator==(const String& x, const String& y);

friend bool operator!=(const String& x, const char* s);

friend bool operator!=(const String& x, const String& y);

private:

char *str;

};

String::String(){
    str = new char[1];
    str[0]=‘\0‘;
  }

String::String(const char* a){
       int t = strlen(a)+1;
       str = new char[t];
    str = strcpy(str,a);
    str[t-1]=‘\0‘;
   }
  
   String::String(const String& b){
       int t=strlen(b.str)+1;
       str = new char[t];
       str = strcpy(str,b.str);
       str[t-1]=‘\0‘;
   }

String& String::operator=(const char *b){
       int t=strlen(b)+1;
       str = new char[t];
       str = strcpy(str,b);
       str[t-1]=‘\0‘;
       return *this;
   }

String& String::operator=(const String& b){
       int t=strlen(b.str)+1;
       str = new char[t];
       str =strcpy(str,b.str);
       str[t-1]=‘\0‘;
       return *this;
   }

String String::operator+(const String& b){

String str1;
       str1 = new char[strlen(b.str)+1+strlen(this->str)];
       for(int i=0;i<strlen(this->str);i++){
           str1.str[i]=this->str[i];
       }
       for(int j=strlen(this->str);j<strlen(b.str)+strlen(this->str);j++){
           str1.str[j]=b.str[j-strlen(this->str)];
       }
       str1[strlen(b.str)+strlen(this->str)]=‘\0‘;
       return str1;
   }

String::~String(){
      delete [] str;
   }
  
   char& String::operator[](int i){
      return str[i];
   }
  
   char String::operator[](int i) const{
       return str[i];
   }
  
   int String::size() const{
       return strlen(str);
   }
  
   String& String::operator+=(const String& b){
    
       int l=strlen(str)+strlen(b.str);
    char *p=new char[l+10];
       strcpy(p,str);
       strcat(p,b.str);
       strcpy(str,p);
       return *this;  
   }
  
   String& String::operator+=(const char* b){
    
        int l=strlen(str)+strlen(b);
     char *p=new char[l+1];
        strcat(p,str);
        strcat(p,b);
        strcpy(str,p);
        return *this; 
   }

ostream& operator<<(ostream& a, const String& b){
       a<<b.str;
       return a;
   }
  
   istream& operator>>(istream& a, String& b){
       a>>b.str;
       return a;
   }

bool operator==(const String& x, const char* s){
       if(strlen(x.str)!=strlen(s)) return false;
       for(int i=0;i<strlen(x.str);i++){
            if(x.str[i]!=s[i]) return false;
       }
       return true;
   }

bool operator==(const String& x, const String& s){
       if(strlen(x.str)!=strlen(s.str)) return false;
       for(int i=0;i<strlen(x.str);i++){
            if(x.str[i]!=s.str[i]) return false;
       }
       return true;
   }
  
   bool operator!=(const String& x, const char* s){
      if(strlen(x.str)!=strlen(s)) return true;
      int count=0;
      for(int i=0;i<strlen(s);i++){
           if(x.str[i]!=s[i]);
     count++;
      }
      if(count==strlen(s)) return false;
      else return true;
   }

bool operator!=(const String& x, const String& s){
      if(strlen(x.str)!=strlen(s.str)) return true;
      int count=0;
      for(int i=0;i<strlen(s.str);i++){
           if(x.str[i]!=s[i]);
     count++;
      }
      if(count==strlen(s.str)) return false;
      else return true;
    }

String f1(String a, String b)

{

a[2] = ‘x‘;

char c = b[2];

cout << "in f: " << a << ‘ ‘ << b << ‘ ‘ << c << ‘\n‘;

return b;

}

void f2(String s, const String& r)

{

char c1 = s[1];  // c1 = s.operator[](1).operator char()

s[1] = ‘c‘;    // s.operator[](1).operator=(‘c‘)

char c2 = r[1];  // c2 = r.operator[](1)

//  r[1] = ‘d‘;    // error: assignment to non-lvalue char, r.operator[](1) = ‘d‘

}

void f()

{

String x, y, s;

cout << "Please enter two strings\n";

cin >> x >> y;

cout << "x= " << x << " , y = " << y << ‘\n‘;

y = f1(x,y);

cout << y << endl;

f2(x,y);

cout << "s = \"" << s << "\"" << endl;

s = "abc";

cout << "s = \"" << s << "\"" << endl;

cout << "\"" << x << "\" + \"" << y << "\" = " << "\"" << x+y << "\"\n";
 
  String z = x;
 
  cout<<z<<endl;

if (x != z) cout << "x corrupted!\n";

x[0] = ‘!‘;

if (x == z) cout << "write failed!\n";

cout << "exit: " << x << ‘ ‘ << z << ‘\n‘;

z = s;

if (s != z) cout << "s corrupted!\n";

s[0] = ‘!‘;

if (s == z) cout << "write failed!\n";

cout << "exit: " << s << ‘ ‘ << z << ‘\n‘;

}

int main()

{

int T;

cin >> T;

while (T--)

{

f();

}

}

时间: 2024-07-29 16:37:55

1001. The String Class的相关文章

Dubbo之旅--结果缓存

在上篇文章中我们队Dubbo的扩展进行了一些基本了解.这些扩展能够很好的帮助我们在实际的项目中发挥作用,接下来对于dubbo的一些高级特征,针对特殊情况而进行的处理进行进一步的介绍,这里我们要说的是结果缓存. 为什么要用到结果缓存,主要是用于加速热门数据的访问速度,Dubbo提供声明式缓存,以减少用户加缓存的工作量. 下面我们将通过一个例子来对结果缓存进行一个接触. 1.客户端和服务提供端共用接口类 packagecom.alibaba.dubbo.demo; public interfaceC

多线程中共享变量——CCF总决赛试题

题目要求 数据格式 Q 系统的输入为纯文本格式的文件,由若干行组成,每一行由城市编号.年龄.收入组成,相邻两项之间用一个空格分隔.以下是输入的一个片段: 1001 20 12000 1001 50 24200 1020 30 30000 其中,城市编号是四位数(第一位不为 0),年龄与收入为整数类型. 查询描述 Q 系统需要实现以下三个常用的查询功能: Q1: 查询城市 X 某个年龄段的平均收入: Q2: 查询城市 X 的收入最高的前 K 位的收入: Q3: 分别查询某些城市某一年龄段收入的中位

帮助小伙伴写的组装xml字符串类

1 import java.io.IOException; 2 import java.io.StringWriter; 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.Map.Entry; 9 import org.dom4j.Docu

淘宝SOA框架dubbo学习(5)--结果缓存

1.客户端和服务提供端共用接口类 ? 1 2 3 4 5 package com.alibaba.dubbo.demo; public interface CacheService {     String findCache(String id); } 2.服务提供端接口实现类 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.alibaba.dubbo.demo.provider; import java.util.conc

Java Load Properties 文件,定义message信息

初始化Properties对象,load properties文件: private static final Properties MESSAGERESOURCES = new Properties(); static { try { MESSAGERESOURCES.load(ClassLoader.getSystemResourceAsStream("/messages.properties")); MessageFormat.format("", "

基于轻量级ORM框架Dapper的扩展说明

这里简单的介绍一下本人基于Dapper作的一些简单的扩展,供大家参考. 为何要使用这款框架,相信大家看到下面排名就清楚了 其实在各大网站上,我们大概都会看到这样的一个对比效果图,在超过500次poco serialization的过程中所表现的性能,我们发现dapper是第二名, 当然第一名谁也无法超越,越底层的当然久越快,同时也就越麻烦. 至于如何使用进行基本的数据操作,我这里就不再阐述,http://www.cnblogs.com/Sinte-Beuve/p/4231053.html这里介绍

[C++]现行的试卷封面并获取学生题目得分信息以及学号信息的原型系统

大二的时候写的一个CV小玩意,最终决定还是把它放出来,也许会帮助到很多人,代码写的很丑,大家多多包涵.附加实验报告主要部分.大家会给这个课设打多少分呢? 课题背景及意义: 本项目主要目标是设计一套能自动分析我校现行的试卷封面并获取学生题目得分信息以及学号信息的原型系统. 本项目的实现有助于提升我校成绩管理的自动化程度以及试卷分析的量化程度,分担一部分期末教师阅卷的工作. 课题相关研究情况概述: 本项目进行至今已经完成了单个数字的识别,并且准确率高达98.74%.完成了试卷卷面的基本分析工作,可以

JavaSE-代码块

package com.btp.t2; /* * 类的第四个成员:初始化块(代码块) * 1.代码块如果有修饰的话,那么只能使用static * 2.代码块分类: * ①静态代码块(static修饰): * 1.里面可以有输出语句 * 2.随着类的加载而加载,而且只被加载一次 * 3.多个静态代码块之间按照顺序结构执行 * 4.静态代码块的执行要早于非静态代码块的执行 * 5.静态的代码块中,只能执行静态的结构(类属性,类方法) * 6.静态代码块初始化和显式赋值也是按照顺序结构执行 * ②非静

android 从主activity传值到子activity再把结果返回到主界面的示例

在原文档中是:Start Activity and Getting Results The startActivity(android.content.Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to