| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 785 人关注过本帖
标题:编程题,新手上路,求指教……
只看楼主 加入收藏
阿布Scofield
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-10-27
收藏
 问题点数:0 回复次数:6 
编程题,新手上路,求指教……
1、设计一个日期类,其中含 以下方法,a、已知从1980年1月1日以来的天数,求该天的年月日;b、在某天比如 10月22日后的多少天比如 1000天应该是哪个年月日;
2、设计一个复数类,含两个复数的加、减、乘、除方法,以及复数与实数的加、减、乘、除方法。
我刚学,这是老师出的题目,我了解一下前辈是怎么想的
搜索更多相关主题的帖子: 年月日 设计 
2012-10-27 20:31
王Beryl
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-10-27
收藏
得分:0 
第一道挺容易的,直接定义三个变量,年月日,把天数除以365的整除部分赋值给年,余数除以12,整除部分赋给月,再除以30,整除部分赋给日,这样就行啦!第二道还不清楚复数是啥?好久没看数学~还有什么问题,QQ聊392989505
2012-10-27 23:28
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_Complex
{
    // 自定义复数类
    public class Complex
    {
        private Double _real;           // 实部
        private Double _imaginary;      // 虚部

        // 构造函数
        public Complex(Double real, Double imaginary)
        {
            _real = real;
            _imaginary = imaginary;
        }

        //
        public Double Size()
        {
            return Math.Sqrt(_real * _real + _imaginary * _imaginary);
        }

        // 定义加法运算
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1._real + c2._real, c1._imaginary + c2._imaginary);
        }

        // 定义减法运算
        public static Complex operator -(Complex c1, Complex c2)
        {
            return new Complex(c1._real - c2._real, c1._imaginary - c2._imaginary);
        }

        // 定义乘法运算
        public static Complex operator *(Complex c1, Complex c2)
        {
            Double x = c1._real * c2._real - c1._imaginary * c2._imaginary;
            Double y = c1._real * c2._imaginary + c2._real * c1._imaginary;
            return new Complex(x, y);
        }

        // 定义除法运算
        public static Complex operator /(Complex c1, Complex c2)
        {
            Double x = 0;
            Double y = 0;
            if (c2.Size() != 0)
            {
                x = (c1._real * c2._real + c1._imaginary * c2._imaginary) / (c2._real * c2._real + c2._imaginary * c2._imaginary);
                y = (c2._real * c1._imaginary - c1._real * c2._imaginary) / (c2._real * c2._real + c2._imaginary * c2._imaginary);
            }
            return new Complex(x, y);
        }

        public override String ToString()
        {
            return String.Format("({0:N})+i({1:N})", _real, _imaginary);
        }
    };

    class Program
    {
        static void Main(String[] args)
        {
            Complex c1 = new Complex(1, 0);
            Complex c2 = new Complex(0, 1);
            Console.WriteLine("c1 = {0}", c1);
            Console.WriteLine("c2 = {0}", c2);
            Console.WriteLine("c1 + c2 = {0}", c1 + c2);
            Console.WriteLine("c1 - c2 = {0}", c1 - c2);
            Console.WriteLine("c1 * c2 = {0}", c1 * c2);
            Console.WriteLine("c1 / c2 = {0}", c1 / c2);
            Console.ReadLine();
        }
    }
}

授人以渔,不授人以鱼。
2012-10-28 02:05
阿布Scofield
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-10-27
收藏
得分:0 
回复 2楼 王Beryl
谢谢

Be the change you want to see in the world
2012-10-28 11:15
阿布Scofield
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-10-27
收藏
得分:0 
回复 3楼 TonyDeng
谢谢版主,确实弄懂了一些问题

Be the change you want to see in the world
2012-10-28 11:17
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
剩下实数和复数的运算,以及个别的完善,你自己会做了吧?

授人以渔,不授人以鱼。
2012-10-28 14:43
juvar
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-11-6
收藏
得分:0 
如果除12好像就有问题了
2012-12-13 11:24
快速回复:编程题,新手上路,求指教……
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018550 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved