C++-4

  1. 在Complex类的基础上,完成^,>,~运算符的重载
    #include <iostream>using namespace std;
    class Complex
    {int rel;    //实部int vir;    //虚部
    public:Complex(){}Complex(int rel,int vir):rel(rel),vir(vir){}/* Complex operator+(const Complex c1);Complex operator-(const Complex c1);Complex operator*(const Complex c1);Complex operator/(const Complex c1);*/friend Complex operator+(const Complex c1,const Complex c2);friend Complex operator-(const Complex c1,const Complex c2);friend Complex operator*(const Complex c1,const Complex c2);friend Complex operator/(const Complex c1,const Complex c2);bool operator>(const Complex c1);bool operator<(const Complex c1);bool operator>=(const Complex c1);bool operator<=(const Complex c1);bool operator==(const Complex c1);bool operator!=(const Complex c1);bool operator!();bool operator&&(const Complex c1);bool operator||(const Complex c1);Complex operator++();Complex operator--();friend Complex operator++(Complex &c1,int );friend Complex operator--(Complex &c1,int );friend ostream &operator<<(ostream &out,Complex c1);friend istream &operator>>(istream &in,Complex &c1);Complex operator^(Complex c1);Complex operator<<(int a);Complex operator>>(int a);Complex operator~();Complex show(){cout << rel << "+"<< vir << "i" << endl;}operator int(){return this->rel;}operator double(){return this->rel;}operator char(){return 'a';}int operator()(int a,int b ){return a>b?a:b;}
    };
    //^,<<,>>,~
    Complex Complex::operator^(Complex c1)
    {Complex temp;temp.rel=(this->rel)^c1.rel;temp.vir=(this->vir)^c1.vir;return temp;
    }
    Complex Complex::operator<<(int a)
    {rel=rel<<a;vir=vir<<a;return *this;
    }
    Complex Complex::operator>>(int a)
    {rel=rel>>a;vir=vir>>a;return *this;
    }
    Complex Complex::operator~()
    {rel=~rel;vir=~vir;return *this;
    }
    //自增自减
    Complex Complex::operator++()
    {++this->rel;++this->vir;return *this;
    }
    Complex Complex::operator--()
    {--this->rel;--this->vir;return *this;
    }
    Complex operator++(Complex &c1,int )
    {Complex temp=c1;c1.rel++;c1.vir++;return temp;
    }
    Complex operator--(Complex &c1,int )
    {Complex temp=c1;c1.rel--;c1.vir--;return temp;
    }//关系
    bool Complex::operator!()
    {return !(this->rel || this->vir);
    }
    bool Complex::operator&&(const Complex c1)
    {return this->rel && c1.rel && this->vir && c1.vir;
    }
    bool Complex::operator||(const Complex c1)
    {return this->rel || c1.rel || this->vir || c1.vir;
    }
    bool Complex::operator>(const Complex c1)
    {return this->rel>c1.rel;
    }
    bool Complex::operator<(const Complex c1)
    {return this->rel<c1.rel;
    }
    bool Complex::operator>=(const Complex c1)
    {return this->rel>=c1.rel;
    }
    bool Complex::operator<=(const Complex c1)
    {return this->rel<=c1.rel;
    }
    bool Complex::operator==(const Complex c1)
    {return this->rel==c1.rel&&this->vir==c1.vir;
    }
    bool Complex::operator!=(const Complex c1)
    {return this->rel!=c1.rel||this->vir!=c1.vir;
    }//加减乘除
    Complex operator+(const Complex c1,const Complex c2)
    {Complex temp;temp.rel =  c1.rel + c2.rel;temp.vir =  c1.vir + c2.vir;return temp;
    }
    Complex operator-(const Complex c1,const Complex c2)
    {Complex temp;temp.rel =  c1.rel - c2.rel;temp.vir =  c1.vir - c2.vir;return temp;
    }
    Complex operator*(const Complex c1,const Complex c2)
    {Complex temp;temp.rel =  c1.rel * c2.rel-c1.vir * c2.vir;temp.vir =  c1.vir * c2.rel+c1.rel * c2.vir;return temp;
    }
    Complex operator/(const Complex c1,const Complex c2)
    {Complex temp;int a = c1.rel;int b = c1.vir;int c = c2.rel;int d = c2.vir;temp.rel =  (a*c+b*d)/(c*c-d*d);temp.vir =  (b*c-a*d)/(c*c+d*d);return temp;
    }
    /*
    Complex Complex::operator+(const Complex c1)
    {Complex temp;temp.rel = this ->rel+c1.rel;temp.vir = this->vir +c1.vir;return temp;
    }
    Complex Complex::operator-(const Complex c1)
    {Complex temp;temp.rel = this->rel - c1.rel;temp.vir = this->vir - c1.vir;return temp;
    }Complex Complex::operator*(const Complex c1)
    {Complex temp;temp.rel = this->rel * c1.rel;temp.vir = this->vir * c1.vir;return temp;
    }Complex Complex::operator/(const Complex c1)
    {Complex temp;temp.rel = this->rel / c1.rel;temp.vir = this->vir / c1.vir;return temp;
    }*///输出
    ostream &operator<<(ostream &out,Complex c1)
    {out << c1.rel << "+" << c1.vir << "i" ;return out;
    }
    //输出
    istream &operator>>(istream &in,Complex &c1)
    {in >> c1.rel >> c1.vir;return in;
    }int main()
    {Complex c1(4,6),c2(4,0),c3(1,2),c4;// cin >> c3 >> c4 ;// cout << c3 << endl << c4 << endl;cout << (c1<<1) << endl << (c2>>1) << endl;cout << ~c3 <<endl;c4 =  c1^c2 ;cout << c4 <<endl;return 0;
    }
    

    myString类的基础上,完成+、关系运算符、逻辑运算符、输入输出运算符的重载

#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class myString
{private:char *str;          //记录c风格的字符串int size;           //记录字符串的实际长度public://无参构造myString():str(new char[20]),size(0){}//有参构造myString(char *p,int size):str(new char[size+1]),size(size){strcpy(str,p);}myString(string s1):str(new char[s1.size()+1]),size(s1.length()){strcpy(str,s1.c_str());}//拷贝构造myString(const myString &other):str(new char[other.size+1]),size(size){strcpy(str,other.str);}//拷贝赋值函数myString &operator=(const myString &other){//提前把申请的空间释放,重新申请空间,为了防止原有的空间不够存下新的内容if(&other!=this){delete []str;str = new char[other.size+1];strcpy(str,other.str);this->size = other.size;}return *this;}//析构函数~myString(){delete []str;}//判空函数bool empty(){return size==0;}//size函数int size_(){return size;}//c_str函数const char *c_str(){return str;}//at函数char &at(int pos){//判断位置合理性if(pos<0||pos>=size){cout << "位置不合理" << endl;return c;}return str[pos];}myString operator+(const myString s1);bool operator>(const myString s1);bool operator&&(const myString s1);bool operator||(const myString s1);friend ostream &operator<<(ostream &out,myString s1);friend istream &operator>>(istream &in,myString &s1);
};
ostream &operator<<(ostream &out,myString s1)
{out << s1.str;return  out;
}
istream &operator>>(istream &in,myString &s1)
{in >> s1.str;return  in;
}
myString myString::operator+( myString s1)
{strcat(this->str,s1.str);return *this;
}
bool myString::operator>( myString s1)
{return strcmp(this->str,s1.str);
}
bool myString::operator&&( myString s1)
{return this->str&&s1.str;
}
bool myString::operator||( myString s1)
{return this->str||s1.str;
}
int main()
{myString s1,s2;cin >> s1 >> s2;cout <<"s1 = "<< s1 << endl << "s2 = " <<  s2 << endl;cout << "s1+s2 = " << s1+s2 << endl;cout << "s1>s2 = " << (s1>s2) <<endl;cout << "s1&&s2 = " << (s1&&s2) <<endl;return 0;
}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/652193.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

03 spring-boot+mybatis+jsp 的增删改查的入门级项目

前言 主要是来自于 朋友的需求 项目概况 就是一个 用户信息的增删改查然后 具体到业务这边 使用 mybatis xml 来配置的增删改查 后端这边 springboot mybatis mysql fastjson 的一个基础的增删改查的学习项目, 简单容易上手 前端这边 jsp 的 基础的试题的增删改查 学习项…

全志ARM-官方库SDK安装和验证

进入界面&#xff0c;输入以下指令 git clone https://github.com/orangepi-xunlong/wiringOP //下载源码 cd wiringOP //进入文件夹 sudo ./build clean //清除编译信息 sudo ./build …

photoshop如何使用PS中的吸管工具吸取软件外部的颜色?

第一步&#xff0c;打开PS&#xff0c;随意新建一个画布&#xff0c;或打开一个图片。 第二步&#xff0c;将PS窗口缩小&#xff0c;和外部窗口叠加放置&#xff0c;以露出后面的其它页面。 第三步&#xff0c;选中吸管工具&#xff0c;在PS窗口内单击一点吸取颜色&#xff0c;…

当您的计算机在屏幕显示器熄灭时,需要输入密码才能打开

打开“任务计划程序”&#xff08;可以在Windows搜索栏中输入“任务计划程序”来查找&#xff09;。在左侧面板中&#xff0c;单击“任务计划程序库”下的“创建任务”。在弹出的对话框中&#xff0c;输入任务名称和描述&#xff0c;然后选择“配置为 Windows 10”并勾选“使用…

maven修改默认编码格式为UTF-8

执行mvn -version查看maven版本信息发现&#xff0c;maven使用的编码格式为GBK。 为什么想到要修改编码格式呢&#xff1f;因为idea中我将文件格式统一设置为UTF-8&#xff08;如果不知道如何修改文件编码&#xff0c;可以参考文末&#xff09;&#xff0c;然后使用maven打包时…

Unity类银河恶魔城学习记录15-1,2 p153 Audio Manager p154 Audio distance limiter

Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释&#xff0c;可供学习Alex教程的人参考 此代码仅为较上一P有所改变的代码 【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili AudioManager.cs using System.Collections; using System.Collections.Gen…

python基础之元组、集合和函数的定义与返回值

1.元祖 1.元祖的定义 元组的数据结构跟列表相似 特征&#xff1a;有序、 有序&#xff1a;有&#xff08;索引/下标/index&#xff09; 正序、反序标识符&#xff1a; ( ) 里面的元素是用英文格式的逗号分割开来关键字&#xff1a;tuple 列表和元组有什么区别&#xff1f; 元组…

代码随想录训练营Day 33|Python|Leetcode|● 理论基础 ● 509. 斐波那契数 ● 70. 爬楼梯 ● 746. 使用最小花费爬楼梯

理论基础 动态规划五步曲 确定dp数组&#xff08;dp table&#xff09;以及下标的含义确定递推公式dp数组如何初始化确定遍历顺序举例推导dp数组 509. 斐波那契数 斐波那契数 &#xff08;通常用 F(n) 表示&#xff09;形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始…

原型链prototype、__proto、constructor的那些问题整理

再了解原型链之前,我们先来看看构造函数和实例对象的打印结构 - 函数 这里我们定义一个构造函数Fn,然后打印它的结构吧 function Fn(){} console.dir(Fn)控制台得到结构 从上面结构我们能看的出来,函数有两种原型,一种是作为函数特有的原型:prototype,另一种是作为对象的__…

Kafka 生产者应用解析

目录 1、生产者消息发送流程 1.1、发送原理 2、异步发送 API 2.1、普通异步发送 2.2、带回调函数的异步发送 3、同步发送 API 4、生产者分区 4.1、分区的优势 4.2、生产者发送消息的分区策略 示例1&#xff1a;将数据发往指定 partition 示例2&#xff1a;有 key 的…

Jackson 2.x 系列【31】Spring Boot 集成之字典回写

有道无术&#xff0c;术尚可求&#xff0c;有术无道&#xff0c;止于术。 本系列Jackson 版本 2.17.0 本系列Spring Boot 版本 3.2.4 源码地址&#xff1a;https://gitee.com/pearl-organization/study-jaskson-demo 文章目录 1. 场景描述2. 案例演示2.1 修改枚举2.2 定义注解…

拖拽式工作流开发有什么突出优势?

想要实现高效率的办公方式&#xff0c;可以试着了解低代码技术平台及拖拽式工作流开发的优势特点。具有好操作、好维护、够灵活、可视化界面操作等优势特点的低代码技术平台可以助力企业实现流程化办公&#xff0c;在发展越来越快速的今天&#xff0c;拖拽式工作流开发得到了很…