当前位置: 首页 > article >正文

假期2.7

一、填空题

1、在下列程序的空格处填上适当的字句,使输出为:0,2,10。

#include <iostream>

#include <math.h>

class Magic

{double x;

public:

Magic(double d=0.00):x(fabs(d))

{}

Magic operator+(_ _const Magic& c_

_____)

{

return Magic(sqrt(x*x+c.x*c.x));

}

___ friend std::ostream&

____operator<<(ostream & stream,const  Magic & c)

{ stream<<c.x;

return stream;

}

};

int main()

{Magic ma;

cout<<ma<<", "<<Magic(2)<<", "<<ma+Magic(-6)+

Magic(-8)<<endl;

}

二、编程题

1、 定义复数类的加法与减法,使之能够执行下列运算:

  Complex a(2,5),b(7,8),c(0,0);

  c=a+b;

  c=4.1+a;

  c=b+5.6;

#include <iostream>
 
using namespace std;
 
class Complex {
 
public:
 
    Complex(double real = 0.0, double imaginary = 0.0) {
 
        this->real = real;
 
        this->imaginary = imaginary;
 
    }
 
    Complex operator+(const Complex& other) const {
 
        double resultReal = real + other.real;
 
        double resultImaginary = imaginary + other.imaginary;
 
        return Complex(resultReal, resultImaginary);
 
    }
 
    Complex operator-(const Complex& other) const {
 
        double resultReal = real - other.real;
 
        double resultImaginary = imaginary - other.imaginary;
 
        return Complex(resultReal, resultImaginary);
 
    }
 
    friend Complex operator+(double num, const Complex& complex) {
 
        double resultReal = num + complex.real;
 
        double resultImaginary = complex.imaginary;
 
        return Complex(resultReal, resultImaginary);
 
    }
 
    friend Complex operator+(const Complex& complex, double num) {
 
        return num + complex;
 
    }
 
    friend Complex operator-(double num, const Complex& complex) {
 
        double resultReal = num - complex.real;
 
        double resultImaginary = -complex.imaginary;
 
        return Complex(resultReal, resultImaginary);
 
    }
 
    friend Complex operator-(const Complex& complex, double num) {
 
        return -num + complex;
 
    }
 
    void print() const {
 
        cout << real << " + " << imaginary << "i" << endl;
 
    }
 
private:
 
    double real;
 
    double imaginary;
 
};
 
int main() {
 
    Complex a(2, 5);
 
    Complex b(7, 8);
 
    Complex c(0, 0);
 
    c = a + b;
 
    c.print();
 
    c = 4.1 + a;
 
    c.print();
 
    c = b + 5.6;
 
    c.print();
 
    return 0;
 
}


2、 编写一个时间类,实现时间的加、减、读和输出。

#include <iostream>
 
using namespace std;
 
class Time {
 
public:
 
    Time(int h = 0, int m = 0, int s = 0) {
 
        hours = h;
 
        minutes = m;
 
        seconds = s;
 
    }
 
    void setTime(int h, int m, int s) {
 
        hours = h;
 
        minutes = m;
 
        seconds = s;
 
    }
 
    void getTime(int& h, int& m, int& s) const {
 
        h = hours;
 
        m = minutes;
 
        s = seconds;
 
    }
 
    void printTime() const {
 
        cout << hours << ":" << minutes << ":" << seconds << endl;
 
    }
 
    Time operator+(const Time& other) const {
 
        int totalSeconds = seconds + other.seconds;
 
        int carryMinutes = totalSeconds / 60;
 
        int remainingSeconds = totalSeconds % 60;
 
        int totalMinutes = minutes + other.minutes + carryMinutes;
 
        int carryHours = totalMinutes / 60;
 
        int remainingMinutes = totalMinutes % 60;
 
        int totalHours = hours + other.hours + carryHours;
 
        return Time(totalHours, remainingMinutes, remainingSeconds);
 
    }
 
    Time operator-(const Time& other) const {
 
        int totalSeconds = seconds - other.seconds;
 
        int borrowMinutes = 0;
 
        if (totalSeconds < 0) {
 
            totalSeconds += 60;
 
            borrowMinutes = 1;
 
        }
 
        int totalMinutes = minutes - other.minutes - borrowMinutes;
 
        int borrowHours = 0;
 
        if (totalMinutes < 0) {
 
            totalMinutes += 60;
 
            borrowHours = 1;
 
        }
 
        int totalHours = hours - other.hours - borrowHours;
 
        return Time(totalHours, totalMinutes, totalSeconds);
 
    }
 
private:
 
    int hours;
 
    int minutes;
 
    int seconds;
 
};
 
int main() {
 
    Time t1(10, 30, 45);
 
    Time t2(2, 15, 20);
 
    Time sum = t1 + t2;
 
    Time diff = t1 - t2;
 
    cout << "t1: ";
 
    t1.printTime();
 
    cout << "t2: ";
 
    t2.printTime();
 
    cout << "Sum: ";
 
    sum.printTime();
 
    cout << "Difference: ";
 
    diff.printTime();
 
    return 0;
 
}

3、 增加操作符,以允许人民币与double型数相乘。

  friend money operator*(const money&,double);

  friend money operator*(double,const money&);

  注意:两个money对象不允许相乘。

#include <iostream>
 
using namespace std;
 
class Money {
 
private:
 
    double amount;
 
public:
 
    Money(double amt = 0.0) : amount(amt) {}
 
    friend Money operator*(const Money& money, double num) {
 
        return Money(money.amount * num);
 
    }
 
    friend Money operator*(double num, const Money& money) {
 
        return Money(money.amount * num);


http://www.kler.cn/news/234247.html

相关文章:

  • Android 移动应用开发 创建第一个Android项目
  • leetcode:216.组合总和三
  • Mybatis开发辅助神器p6spy
  • 基于JavaWeb的网上订餐项目
  • Unity类银河恶魔城学习记录1-14 AttackDirection源代码 P41
  • 第十四章 以编程方式使用 SQL 网关 - %SQLGatewayConnection 方法和属性
  • 【正在更新】从零开始认识语音识别:DNN-HMM混合系统语音识别(ASR)原理
  • 02 数据库管理 数据表管理
  • 猫头虎分享已解决Bug || KeyError: ‘The truth value of a Series is ambiguous‘
  • nginx stream proxy 模块的ssl连接源码分析
  • python创建pdf文件
  • MySQL篇----第十八篇
  • 20:基于EL与JSTL的产品管理页-Java Web
  • qt-C++笔记之判断一个QLabel上有没有load图片
  • 基于Python的HTTP隧道安全性分析:魔法背后的锁与钥匙
  • 掌握rm命令:Linux文件删除的艺术与安全指南
  • 【书生·浦语大模型实战营】学习笔记1
  • CSS3 基本语法
  • 17:定时器编程实战
  • 微软和苏黎世联邦理工学院开源SliceGPT创新压缩技术节省大量部署资源;OpenAI成立儿童安全团队,防AI误用
  • JavaScript的聚焦:focus/blur
  • Acwing 5469. 有效点对【正难则反+巧妙选择根节点】
  • Netty应用(四) 之 Reactor模型 零拷贝
  • 【算法】排序详解(快速排序,堆排序,归并排序,插入排序,希尔排序,选择排序,冒泡排序)
  • OpenCV-32 膨胀操作
  • 2024PMP考试新考纲-近年PMP真题练一练和很详细解析(3)
  • 【java】简单的Java语言控制台程序
  • golang select两个channel性能稳定,三个channel时性能会发生抖动,为什么?
  • (c语言版)数组去重和排序 题目描述: 给定一个乱序的数组,删除所有的重复元素,使得每个元素只出现一次,并且按照出现的次数从高到低
  • 设计模式-行为型模式(下)