c++写一个复数类,完成复数的加减乘运算。应包括构造函数。编写主函数,完成对类的使用 5
1个回答
展开全部
string.hpp:
#ifndef __STRING_HPP__
#define __STRING_HPP__
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
class String
{
private:
char* data;
int len;
public:
String();//构造函数
String(int,char);//构造函数的存在
String(const char*);
~String();//析构函数
String(const String&);//拷贝构造函数
//赋值运算符函数
String& operator=(const String&);
void show();
friend ostream& operator<< (ostream&,const String&);//插入运算符的重载
//提取运算符的重载
friend istream& operator>>(istream&,String&);
const String operator +(const String&)const;
String& operator+=(const String&);
char& operator[](int);
char* getdata();
int getlength();
operator char*();
friend bool operator==(const String&,const String&);
friend bool operator!=(const String&,const String&);
friend bool operator<(const String&,const String&);
friend bool operator>(const String&,const String&);
};
#endif //__STRING_HPP__
string.cpp中:
#include"string.hpp"
String::String()//构造函数
{
data=new char[1];
len=0;
*data='\0';
}
String::String(int n,char ch)//构造函数的存在
{
data=new char[1+n];
len=n;
char* p=data;w
while(n-->0)
*p++=ch;
}
String::String(const char* str)
{
/*if(str!=NULL)
{
len=strlen(str);
data=new char[1+len];
strcpy(data,str);
}
else
{
data=new char[1];
len=0;
*data='\0';
}*/
len=strlen(str?str:"");
data=new char[1+len];
strcpy(data,str?str:"");
}
String::~String()//析构函数
{
delete[]data;
data=NULL;
}
String::String(const String& that)//拷贝构造函数
{
//cout<<"拷贝构造函数被调用"<<endl;
len=that.len;
data=new char[1+len];
strcpy(data,that.data);
}
//赋值运算符函数
String& String::operator=(const String& that)
{ // obj3=obj4;
//初级程序员的写法
/*if(this==&that)
return *this;//防止自赋值
delete[]data;//释放原内存
data=new char[1+that.len];//开辟新的内存
len=that.len;
strcpy(data,that.data);//拷贝新内容
return *this;//返回自引用*/
//中级程序员的写法
//a=that;
if(&that==this)
return *this;
char* p=new char[1+that.len];
if(!p)
{
cout<<"内存开辟失败"<<endl;
exit(-1);
}
//如果开辟内存不成功的话,就抛出一个异常
delete[]data;
data=strcpy(p,that.data);
len=that.len;
return *this;
//高级程序员的写法
//a=that
if(&that!=this)
{
String other(that);
len=that.len;
swap(data,other.data);
}
return *this;
}
void String::show(/*int a*/)
{
cout<<data<<" "<<len<<endl;
}
ostream& operator<<(ostream& os,const String& that)
{
//在插入运算符中可以把流对象os当作cout来使用
//最后还要返回流对象自身,以备连用
return os<<that.data<<" "<<that.len;
//return os;
}
istream& operator>>(istream& is,String& that)
{
//return is>>that.data;
char buf[256]={};
is.getline(buf,100);
that.len=strlen(buf);
delete[]that.data;
that.data=new char[1+that.len];
strcpy(that.data,buf);
return is;
}
const String String::operator+(const String& that) const
{
String other;
other.len=len+that.len;
delete[]other.data;
other.data=new char[1+other.len];
//strcat(strcpy(other.data,data),that.data);
strcpy(other.data,data);
strcat(other.data,that.data);
//that.len=0;
return other;
}
//obj1 =obj2 + obj3
//int a,b,c;
//a=b+c;//ok
//b+c=a;//error匿名变量只能做右值
/*a=1,b=2,c=3;
(a=b)=c;
a=3 b=2 c=3*/
String& String::operator+=(const String& that)
{
/*String other(*this);
delete[]data;
len+=that.len;
data=new char[1+len];
strcat(strcpy(data,other.data),that.data);*/
*this=*this+that;
return *this;
}
char& String::operator[](int i)
{
if(i<0||i>len)
{
cout<<"数组下标越界"<<endl;
exit(-1);
}
return data[i];
}
/*obj1("abcdf");
cout<<obj1[0]<<endl;//a
obj1[0]='A';
cout<<obj1<<endl;//Abcdf
*/
char* String::getdata()
{
return data;
}
int String::getlength()
{
return len;
}
String::operator char*()
{
static char buf[256]={};
sprintf(buf,"%s%d",data,len);
return buf;
}
//obj1("abcdef");
//cout<<(char*)obj1<<endl;//abcdef6
bool operator==(const String&that,const String&other)
{
return strcmp(that.data,other.data)==0;
}
bool operator!=(const String&that,const String&other)
{
//return strcmp(that.data,other.data)!=0;
return !(that==other);
}
bool operator>(const String&that,const String&other)
{
return strcmp(that.data,other.data)>0;
}
bool operator<(const String&that,const String&other)
{
return strcmp(that.data,other.data)<0;
}
至于对类的使用,你可以自己写个程序测试下吧
#ifndef __STRING_HPP__
#define __STRING_HPP__
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
using namespace std;
class String
{
private:
char* data;
int len;
public:
String();//构造函数
String(int,char);//构造函数的存在
String(const char*);
~String();//析构函数
String(const String&);//拷贝构造函数
//赋值运算符函数
String& operator=(const String&);
void show();
friend ostream& operator<< (ostream&,const String&);//插入运算符的重载
//提取运算符的重载
friend istream& operator>>(istream&,String&);
const String operator +(const String&)const;
String& operator+=(const String&);
char& operator[](int);
char* getdata();
int getlength();
operator char*();
friend bool operator==(const String&,const String&);
friend bool operator!=(const String&,const String&);
friend bool operator<(const String&,const String&);
friend bool operator>(const String&,const String&);
};
#endif //__STRING_HPP__
string.cpp中:
#include"string.hpp"
String::String()//构造函数
{
data=new char[1];
len=0;
*data='\0';
}
String::String(int n,char ch)//构造函数的存在
{
data=new char[1+n];
len=n;
char* p=data;w
while(n-->0)
*p++=ch;
}
String::String(const char* str)
{
/*if(str!=NULL)
{
len=strlen(str);
data=new char[1+len];
strcpy(data,str);
}
else
{
data=new char[1];
len=0;
*data='\0';
}*/
len=strlen(str?str:"");
data=new char[1+len];
strcpy(data,str?str:"");
}
String::~String()//析构函数
{
delete[]data;
data=NULL;
}
String::String(const String& that)//拷贝构造函数
{
//cout<<"拷贝构造函数被调用"<<endl;
len=that.len;
data=new char[1+len];
strcpy(data,that.data);
}
//赋值运算符函数
String& String::operator=(const String& that)
{ // obj3=obj4;
//初级程序员的写法
/*if(this==&that)
return *this;//防止自赋值
delete[]data;//释放原内存
data=new char[1+that.len];//开辟新的内存
len=that.len;
strcpy(data,that.data);//拷贝新内容
return *this;//返回自引用*/
//中级程序员的写法
//a=that;
if(&that==this)
return *this;
char* p=new char[1+that.len];
if(!p)
{
cout<<"内存开辟失败"<<endl;
exit(-1);
}
//如果开辟内存不成功的话,就抛出一个异常
delete[]data;
data=strcpy(p,that.data);
len=that.len;
return *this;
//高级程序员的写法
//a=that
if(&that!=this)
{
String other(that);
len=that.len;
swap(data,other.data);
}
return *this;
}
void String::show(/*int a*/)
{
cout<<data<<" "<<len<<endl;
}
ostream& operator<<(ostream& os,const String& that)
{
//在插入运算符中可以把流对象os当作cout来使用
//最后还要返回流对象自身,以备连用
return os<<that.data<<" "<<that.len;
//return os;
}
istream& operator>>(istream& is,String& that)
{
//return is>>that.data;
char buf[256]={};
is.getline(buf,100);
that.len=strlen(buf);
delete[]that.data;
that.data=new char[1+that.len];
strcpy(that.data,buf);
return is;
}
const String String::operator+(const String& that) const
{
String other;
other.len=len+that.len;
delete[]other.data;
other.data=new char[1+other.len];
//strcat(strcpy(other.data,data),that.data);
strcpy(other.data,data);
strcat(other.data,that.data);
//that.len=0;
return other;
}
//obj1 =obj2 + obj3
//int a,b,c;
//a=b+c;//ok
//b+c=a;//error匿名变量只能做右值
/*a=1,b=2,c=3;
(a=b)=c;
a=3 b=2 c=3*/
String& String::operator+=(const String& that)
{
/*String other(*this);
delete[]data;
len+=that.len;
data=new char[1+len];
strcat(strcpy(data,other.data),that.data);*/
*this=*this+that;
return *this;
}
char& String::operator[](int i)
{
if(i<0||i>len)
{
cout<<"数组下标越界"<<endl;
exit(-1);
}
return data[i];
}
/*obj1("abcdf");
cout<<obj1[0]<<endl;//a
obj1[0]='A';
cout<<obj1<<endl;//Abcdf
*/
char* String::getdata()
{
return data;
}
int String::getlength()
{
return len;
}
String::operator char*()
{
static char buf[256]={};
sprintf(buf,"%s%d",data,len);
return buf;
}
//obj1("abcdef");
//cout<<(char*)obj1<<endl;//abcdef6
bool operator==(const String&that,const String&other)
{
return strcmp(that.data,other.data)==0;
}
bool operator!=(const String&that,const String&other)
{
//return strcmp(that.data,other.data)!=0;
return !(that==other);
}
bool operator>(const String&that,const String&other)
{
return strcmp(that.data,other.data)>0;
}
bool operator<(const String&that,const String&other)
{
return strcmp(that.data,other.data)<0;
}
至于对类的使用,你可以自己写个程序测试下吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询