#include <iostream>
#include <cstring>
using namespace std;
class mystring
{
char* buf;
public:
mystring(); //构造函数
mystring(const char* str); //构造函数
void show(); //输出函数
void setmystr(const mystring str); //设置函数
const char* getmystr() const; //获取函数
void append(const mystring str); //追加函数
int isEqual(const mystring str); // 比较函数
~mystring(); //析构函数
};
mystring::mystring()
{
buf = (char*)calloc(1,1);
}
mystring::mystring(const char * str)
{
int len = strlen(str);
buf = (char*)calloc(1,len+1);
strcpy(buf , str);
}
void mystring::show()
{
cout << buf << endl;
}
void mystring::setmystr(const mystring str)
{
free(this->buf);
int len = strlen(str.buf);
this->buf = (char*)calloc(1 , len+1);
strcpy(this->buf , str.buf);
}
const char* mystring::getmystr() const
{
return buf;
}
//追加函数
void mystring::append(const mystring str)
{
int len1 = strlen(this->buf);
char* temp = (char*)calloc(1,len1+1);
strcpy(temp , this->buf);
free(this->buf);
int len2 = strlen(str.buf);
this->buf = (char*)calloc(1,len1+len2+1);
strcat(this->buf , temp);
strcat(this->buf , str.buf);
}
//比较函数
int mystring::isEqual(const mystring str)
{
if(strcmp(buf , str.buf) > 0)
{
return 1;
}
else if(strcmp(buf , str.buf) < 0)
{
return -1;
}
else
return 0;
/*
int i = 0;
while(this->buf[i] != '\0' || str.buf[i] != '\0')
{
if(this->buf[i] < str.buf[i] || this->buf[i] == '\0')
{
return -1;
}
else if(this->buf[i] > str.buf[i] || str.buf[i] == '\0')
{
return 1;
}
i++;
}
return 0;
*/
}
//析构函数
mystring::~mystring()
{
free(buf);
buf = NULL;
}
int main()
{
mystring str = "hello world";
mystring ptr;
str.show();
ptr.setmystr("hello kitty");
cout << ptr.getmystr() << endl;
ptr.append(str);
ptr.show();
str.show();
int res = ptr.isEqual(str);
if(res < 0)
{
cout << "ptr < str" <<endl;
}
else if(res > 0)
{
cout << "ptr > str" <<endl;
}
else
{
cout << "ptr = str" <<endl;
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
class myfile
{
FILE * fp;
public:
myfile();
FILE * myfopen(const char * pathname , const char * mode);
size_t mywrite(const void * buf , size_t size , size_t num);
size_t myread(void * buf , size_t size , size_t num);
~myfile();
};
myfile::myfile()
{
fp = NULL;
}
FILE * myfile::myfopen(const char *pathname, const char *mode)
{
fp = fopen(pathname,mode);
if(fp == NULL)
{
return NULL;
}
return fp;
}
size_t myfile::mywrite(const void *buf, size_t size, size_t num)
{
size_t res = fwrite(buf , size , num , this->fp);
cout << "wres = " << res << endl;
if(res == 0 || res < num)
{
return 0;
}
return 1;
}
size_t myfile::myread(void * buf, size_t size, size_t num)
{
size_t res = fread(buf , size , num , this->fp);
cout << "res = " << res << endl;
if(res == 0 || res < num)
{
return 0;
}
return 1;
}
myfile::~myfile()
{
fclose(fp);
}
int main()
{
myfile fp;
fp.myfopen("./test1.txt" , "w");
while(1)
{
char buf[64] = {0};
cout << "请输入:" << endl;
cin >> buf;
if(strcmp(buf , "quit") == 0)
{break;}
fp.mywrite(buf , 1 , sizeof(buf));
}
fp.~myfile();
fp.myfopen("./test1.txt" , "r");
while(1)
{
cout << "111" << endl;
char rbuf[64] = {0};
size_t res = fp.myread(rbuf , 1 ,sizeof(rbuf));
cout << res << endl;
if(res == 0)
{break;}
cout << "rbuf = " << rbuf << endl;
}
return 0;
}