复杂数字运算的C ++运算符重载 [英] C++ operator overloading for complex number operations

查看:194
本文介绍了复杂数字运算的C ++运算符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++的作业,我在开始时遇到麻烦。目标是设计一个类,它使用以下重载运算符用于复数:>><< + - * /

I have an assignment in C++ and I'm having trouble getting started. The goal is to "design a class that uses the following overloaded operators for complex numbers: >> << + - * / "

我的问题不是这个的语法,但更多的关于逻辑。

My question isn't about the syntax of this, but more about the logic. I could use some help brain storming.

输入范例:

2.5 -2.2

1.0 1.0

Input Sample:
2.5 -2.2
1.0 1.0

OutPut示例:

A =(2.5)+(-2.2)i

B = )+(1.0)i



A + B =(3.5)+(-1.2)i

A - B = ... ...........

A * B = ..............

A / B =。 .............

OutPut Sample:
A = (2.5) + (-2.2)i
B = (1.0) + (1.0)i

A + B = (3.5) + (-1.2)i
A - B = ..............
A * B = ..............
A / B = ..............

那么我该如何开始呢?类Complex重载这些运算符,那么是否意味着我只能在类中使用这些运算符(即在公共函数内部)?如果是这样,我想这样做吗?

So how do I start this? The class "Complex" overloads these operators, so does that mean that I can only use these operators in the class (i.e. inside public functions)? If so would I want to do it this way? Or would I want to do it in my client/driver code?

第二,它只是将i添加到每行的第二个值?这看起来太容易了。任何方向将不胜感激。 (只是为了记录,我不是在找任何人为我做家庭作业...只能使用一些输入)

Second, is it just adding i to the second value of each line? That seems too easy. Any direction would be much appreciated. (Just for the record, I'm not looking for anybody to do my homework for me... could just use some input)

推荐答案

在我看来,关键是要演示类操作重载,所以我认为这个想法是为了让你创建一个类Complex,它保存有关实数和虚数的信息(i表示它的虚数)。

It seems to me that the point is to demonstrate class operation overloading, so I think the idea is for you to make a class Complex which holds information about the real number and the imaginary number (the i means it's imaginary). Handle various operations between complex numbers in operator overrides you do yourself.

一旦你有了,你会看到它的工作原理(使一个静态测试方法,进行各种操作和打印结果到屏幕),然后担心使用该类来处理输入,因为解析输入将是其自身的另一个任务。有时,将问题分成更小的问题比尝试同时做两个问题更简单。

Once you have that and you see that it works (make a static test method which does various operations and prints the results to the screen), then worry about using that class to work with input since parsing input will be another task in of itself. Sometimes it's just simpler to divide problems into smaller problems than to attempt to do both at the same time.

希望有帮助。祝你好运!

Hope that helps. Good luck!

这篇关于复杂数字运算的C ++运算符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆