当前位置: 首页 > 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/a/234247.html

相关文章:

  • 【AI日记】24.11.14 复习和准备 RAG 项目 | JavaScript RAG Web Apps with LlamaIndex
  • 开源模型应用落地-qwen模型小试-Qwen2.5-7B-Instruct-tool usage入门-Qwen-Agent深入学习(四)
  • docker配置代理解决不能拉镜像问题
  • 基于碎纸片的拼接复原算法及MATLAB实现
  • Appium配置2024.11.12
  • 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误用