c++学习笔记之 数组类与负数类
2014-09-27 23:44
183 查看
//////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef ARRAY_H #define ARRAY_H #include <iostream> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <string> #include <cstring> class array { public: //friend std::ostream &operator<<(std::ostream &os, const array &a); //friend array& operator+(const array& a, const array& b); array& operator+(const array& a); array &operator+=(const array &a); bool operator>(const array &a) const; //friend bool operator<=(const array&a,const array&b); bool operator<=(const array&a) const; bool operator>=(const array&a) const; bool operator<(const array&a) const; bool operator==(const array&a) const; int& operator[](int index); //array(); //array(); void operator=(const array& a); array(const array & a); array(int n); //void operator=(const array &a); ~array(); void print(); private: int* arr_; int len_; }; #endif
#include "array.h" using namespace std; int& array::operator[](int index) { return *(arr_+index); } // array::array():arr_(new int[1]) // { // } void array::operator=(const array& a) { int l=a.len_; arr_=new int[l]; for(int i=0;i<l;++i) { *(arr_+i)=*(a.arr_+i);//cout<<"exe"<<endl; } } array::array(const array & a) { int l=a.len_; arr_=new int[l]; for(int i=0;i<l;++i) { *(arr_+i)=*(a.arr_+i);//cout<<"exe"<<endl; } } // void array::operator=(const array &a) // { // arr_=new int[1]; // } array::array(int n) { arr_=new int ; len_=n; } array::~array() { delete[] arr_; } void array::print() { cout<<*arr_<<endl; } // ostream &operator<<(std::ostream &os, const array &a) // { // for(int i=0;i<a.len_;++i) // { // os<<*(a.arr_+i)<<" "; // } // } array & array::operator+=(const array &a) { // "foo" + "bar" int *pt=new int[a.len_+len_]; int j=0; for(int i=0;i<a.len_+len_;++i) { if(i<len_) { *(pt+i)=*(arr_+i); }else { *(pt+i)=*(a.arr_+j); j++; } } delete[] arr_; arr_=pt; return *this; } array& array::operator+(const array& a) { array* c=new array(a.len_+len_); int *pt=new int[a.len_+len_]; int j=0; for(int i=0;i<a.len_+len_;++i) { if(i<len_) { *(pt+i)=*(arr_+i); }else { *(pt+i)=*(a.arr_+j); j++; } } c->arr_=pt; return *c; } //bool array::operator<=(const array&a,const array&b) //{ // string bs=""; // string as=""; // char buf[5]; // for(int i=0;i<a.len_;++i) // { // memset(buf,0,10); // sprintf(buf,"%d",*(a.arr_+i)); // as+=buf; // } // for(int i=0;i<b.len_;++i) // { // memset(buf,0,10); // sprintf(buf,"%d",*(b.arr_+i)); // bs+=buf; // } // return as<=bs; // return ture; //} bool array::operator<=(const array &a) const { string ps=""; string as=""; char buf[1]; for(int i=0;i<a.len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(a.arr_+i)); as+=buf; } for(int i=0;i<len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(arr_+i)); ps+=buf; } return ps<=as; } bool array::operator>(const array &a) const { string ps=""; string as=""; char buf[1]; for(int i=0;i<a.len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(a.arr_+i)); as+=buf; } for(int i=0;i<len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(arr_+i)); ps+=buf; } return ps>as; } bool array::operator>=(const array&a) const { string ps=""; string as=""; char buf[1]; for(int i=0;i<a.len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(a.arr_+i)); as+=buf; } for(int i=0;i<len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(arr_+i)); ps+=buf; } return ps>=as; } bool array::operator<(const array&a) const { string ps=""; string as=""; char buf[1]; for(int i=0;i<a.len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(a.arr_+i)); as+=buf; } for(int i=0;i<len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(arr_+i)); ps+=buf; } return ps<as; } bool array::operator==(const array&a) const { string ps=""; string as=""; char buf[1]; for(int i=0;i<a.len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(a.arr_+i)); as+=buf; } for(int i=0;i<len_;++i) { memset(buf,0,1); sprintf(buf,"%d",*(arr_+i)); ps+=buf; } return ps==as; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class complex { public: complex(); complex(int real, int img); complex(const complex &c); friend std::ostream &operator<<(std::ostream &os,const complex &c); //std::ostream &operator<<(std::ostream &os); complex& operator=(const complex &c); complex operator+(const complex &c); complex operator-(const complex &c); complex operator*(const complex &c); void print(); int mod(); private: int real_; int img_; }; #endif
#include "complex.h" using namespace std; complex::complex() { real_=0; img_=0; } complex::complex(int real, int img):real_(real),img_(img) { } void complex::print() { cout<<real_<<"+"<<img_<<"i"<<endl; } complex::complex(const complex &c) { real_=c.real_; img_=c.img_; } complex& complex::operator=(const complex &c) { real_=c.real_; img_=c.img_; return *this; } complex complex::operator+(const complex &c) { complex ns; ns.real_=real_+c.real_; ns.img_=img_+c.img_; return ns; } complex complex::operator-(const complex &c) { complex ns; ns.real_=real_-c.real_; ns.img_=img_-c.img_; return ns; } complex complex::operator*(const complex &c) { complex ns; ns.real_=real_*c.real_; ns.img_=img_*c.img_; return ns; } int complex::mod() { return real_*real_+img_*img_; } // ostream &complex::operator<<(ostream &os) // { // os<<real_<<"+"<<img_<<"i"; // return os; // } ostream& operator<<(ostream &os,const complex &c) { os<<c.real_<<"+"<<c.img_<<"i"<<endl; return os; }二元操作写成友元的形式更好,便于查看
相关文章推荐
- C++学习笔记二 —— 3.3 指针类型
- more effective c++学习笔记 ---- 条款31
- unix_c++学习笔记1
- c++学习笔记
- c++学习笔记 2005-9
- 一个C++程序员的Delphi学习笔记
- C++ 学习笔记 I/O流的常用控制符
- 高质量C++/C编程指南学习笔记(上)
- c++入门学习笔记指针篇
- C++学习笔记-类的继承(派生类)
- 不能空着,发篇以前的C++学习笔记,不过现在在学C#
- C++学习笔记(1)——基类、派生类的对象空间
- C++学习笔记
- C++学习笔记(5)
- 从今天开始每天写C++或其他学习的知识的笔记,以激励自己
- C++ :学习Proxy Class之笔记2
- 一个C++程序员的Delphi学习笔记
- C++的学习笔记
- c++学习笔记
- C++ 学习笔记