/* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:定义Complex类中的<<和>>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。 * 作 者: 朱亚楠 * 完成日期: 2012 年 04 月 17 日 * 版 本 号: V1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: * 程序输出: * 程序头部的注释结束 */ #include<iostream.h> class Complex { public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c2); Complex operator-(Complex &c2); Complex operator*(Complex &c2); Complex operator/(Complex &c2); friend ostream& operator << (ostream&,Complex&); friend istream& operator >> (istream&,Complex&); private: double real; double imag; }; //下面定义成员函数 istream& operator >> (istream& input,Complex& c) { input>>c.real>>c.imag; return input; } ostream& operator << (ostream& output,Complex& c) { output<<"("<<c.real<<" "<<c.imag<<"i)"<<endl; return output; } //复数相加: (a+bi)+(c+di)=(a+c)+(b+d)i. Complex Complex::operator+(Complex &c2) { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; } //复数相减:(a+bi)-(c+di)=(a-c)+(b-d)i. Complex Complex::operator-(Complex &c2) { Complex c; c.real=real-c2.real; c.imag=imag-c2.imag; return c; } //复数相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i. Complex Complex::operator*(Complex &c2) { Complex c; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; return c; } //复数相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i Complex Complex::operator/(Complex &c2) { Complex c; double d=c2.real*c2.real+c2.imag*c2.imag; c.real=(real*c2.real+imag*c2.imag)/d; //此处有危险未排除:除法溢出 c.imag=(imag*c2.real-real*c2.imag)/d; return c; } int main() { Complex c1,c2,c3; cout<<"请输入复数c1的值:(以a b的形式输入)"; cin>>c1; cout<<"请输入复数c2的值:(以a b的形式输入)"; cin>>c2; cout<<"c1="; cout<<c1; cout<<"c2="; cout<<c2; c3=c1+c2; cout<<"c1+c2="; cout<<c3; c3=c1-c2; cout<<"c1-c2="; cout<<c3; c3=c1*c2; cout<<"c1*c2="; cout<<c3; c3=c1/c2; cout<<"c1/c2="; cout<<c3; return 0; }
相关文章
- 201521123054 《Java程序设计》 第2周学习总结
- CCF CSP 第37次(2025.03)(2_机器人饲养指南_C++)
- 第3讲:ggplot2完美入门与美化细节打磨——从基础绘制到专业级润色
- 第11课 std::bind和std::function(2)_std::bind绑定器
- 201521123013 《Java程序设计》第2周学习总结
- 20175320 2018-2019-2 《Java程序设计》第8周学习总结
- Java 理论与实践: 修复 Java 内存模型,第 2 部分(转载)
- #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }">设编号为1,2,… n的n个人围坐一圈,约定编号为k(1数组实现: #include <> #include <> int Josephu(int n, int m) { int flag, i, j = 0; int *arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; ++i) arr[i] = 1; for (i = 1; i < n; ++i) { flag = 0; while (flag < m) { if (j == n) j = 0; if (arr[j]) ++flag; ++j; } arr[j - 1] = 0; printf("第%4d个出局的人是:%4d号\n", i, j); } free(arr); return j; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; } 链表实现: #include <> #include <> typedef struct Node { int index; struct Node *next; }JosephuNode; int Josephu(int n, int m) { int i, j; JosephuNode *head, *tail; head = tail = (JosephuNode *)malloc(sizeof(JosephuNode)); for (i = 1; i < n; ++i) { tail->index = i; tail->next = (JosephuNode *)malloc(sizeof(JosephuNode)); tail = tail->next; } tail->index = i; tail->next = head; for (i = 1; tail != head; ++i) { for (j = 1; j < m; ++j) { tail = head; head = head->next; } tail->next = head->next; printf("第%4d个出局的人是:%4d号\n", i, head->index); free(head); head = tail->next; } i = head->index; free(head); return i; } int main { int n, m; scanf("%d%d", &n, &m); printf("最后胜利的是%d号!\n", Josephu(n, m)); system("pause"); return 0; }
- java实现斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。 n public class Solution_feibonaqi { public int Fibonacci(int n) { int result[] = { 0, 1 }; if (n < 2) { return result[n]; } int f0 = 0; int f1 = 1; int f2 = 0; for (int i = 2; i <= n; i++) { f2 = f1 + f0; f0 = f1; f1 = f2; } return f2; } public static void main(String[] args) { Scanner sc = new Scanner; int n = ; Solution_feibonaqi fei = new Solution_feibonaqi; ((n)); } }
- 把只包含因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。