21_1.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:
C#编程
开发平台:
Visual C++
- //ch21_1.cpp
- #include <iostream.h>
- #include <string.h>
- class String{
- public:
- String(char*, int);
- int Length(){ return len; }
- class Range{ //异常类1
- public:
- Range(int j):index(j){}
- int index;
- };
- class Size{}; //异常类2
- class Pastm{}; //异常类3
- char& operator[](int k)
- {
- if(0<=k && k<len){
- if(p[k]=='m' && k!=len-1 && p[k+1]<='z' && p[k+1]>='a')
- throw Pastm();
- if(k && p[k-1]=='m' && p[k]>='a' && p[k]<='z')
- throw Pastm();
- return p[k];
- }
- throw Range(k);
- }
- private:
- char* p;
- int len;
- static int max;
- };
- int String::max = 20;
- String::String(char* str, int si)
- {
- if(si<0 || max<si)
- throw Size();
- p=new char[si];
- strncpy(p, str, si);
- len=si;
- }
- void g(String& str)
- {
- int num=str.Length();
- for(int n=0; n<num; n++)
- cout <<str[n];
- cout <<endl;
- }
- void f()
- {
- //代码区1