2024/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 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>
 
class Complex {
private:
    double real;
    double imag;
 
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
 
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }
 
    Complex operator-(const Complex& other) const {
        return Complex(real - other.real, imag - other.imag);
    }
 
    Complex operator+(double num) const {
        return Complex(real + num, imag);
    }
 
    Complex operator-(double num) const {
        return Complex(real - num, imag);
    }
 
    friend Complex operator+(double num, const Complex& c) {
        return Complex(num + c.real, c.imag);
    }
 
    friend Complex operator-(double num, const Complex& c) {
        return Complex(num - c.real, -c.imag);
    }
 
    void display() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};
 
int main() {
    Complex a(2, 5), b(7, 8), c(0, 0);
    
    c = a + b;
    c.display(); // 输出:(9, 13)
    
    c = 4.1 + a;
    c.display(); // 输出:(6.1, 5)
    
    c = b + 5.6;
    c.display(); // 输出:(12.6, 8)
    
    return 0;
}

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

#include <iostream>
#include <ctime>
using namespace std;
class Time
{
private:
    int hours;  //小时
    int minutes;  //分钟
    int seconds;  //秒
public:
    //构造函数
    Time(int h,int m,int s):hours(h),minutes(m),seconds(s){}
    //加法操作
    Time operator+(const Time &other)const
    {
        int total_seconds=seconds+other.seconds+60*(minutes+other.minutes)+3600*(hours+other.hours);
        int new_seconds=total_seconds%60;
        int new_minutes=(total_seconds/60)%60;
        int new_hours=total_seconds/3600;
        return Time(new_hours,new_minutes,new_seconds);
    }
    //减法操作
    Time operator-(const Time &other)const
    {
        int total_seconds=seconds-other.seconds+60*(minutes-other.minutes)+3600*(hours-other.hours);
        if(total_seconds<0)
        {
            total_seconds+=3600*24;//如果小于0,借一天
        }
        int new_seconds=total_seconds%60;
        int new_minutes=(total_seconds/60)%60;
        int new_hours=total_seconds/3600;
        return Time(new_hours,new_minutes,new_seconds);
    }
    //读取时间
    void setTime(int h,int m,int s)
    {
        hours=h;
        minutes=m;
        seconds=s;
    }
    //输出时间
    void show()const
    {
        cout << hours << ":" << minutes << ":" << seconds << endl;
    }
};
int main()
{
    Time t1(14,25,13);
    Time t2(3,12,23);
    //加法
    Time t3=t1+t2;
    cout << "t1+t2=";
    t3.show();
    //减法
    Time t4=t1-t2;
    cout << "t1-t2=";
    t4.show();
    //读取时间
    t1.setTime(15,30,0);
    cout << "Set t1 to ";
    t1.show();
    return 0;
}

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

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

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

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我无法直接回答你的问题。引用和引用提到了Java中如何读取动态配置值的问题,但与你提到的读取文件时出错的问题没有直接关联。 要解决读取文件时出错的问题,你可以尝试以下步骤: 1. 确保文件路径正确:检查文件路径是否正确,包括文件名、文件夹路径和文件扩展名。确保文件存在并且可读。 2. 检查文件编码:确认文件的编码格式是否为UTF-8。可以使用文本编辑器打开文件并查看编码格式。 3. 使用正确的读取方法:根据你的需求选择适当的方法来读取文件。常见的方法包括使用FileInputStream、BufferedReader或Scanner等类来读取文件内容。 4. 处理异常:在读取文件时,可能会出现各种异常,如FileNotFoundException、IOException等。确保在代码中正确处理这些异常,并提供适当的错误处理机制。 以下是一个示例代码,演示了如何使用Java读取UTF-8编码的文件内容: ```java import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; public class ReadFileExample { public static void main(String[] args) { String filePath = "E:/小白做毕设/克隆代码/honey2024/springboot/src/main/java/com/example/springboot/controller/UserController.java"; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); isr.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,以上代码仅演示了如何读取文件内容,并没有处理具体的业务逻辑。你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值