对随机的“复数”进行排序。按实数 [英] Sort random "complex number " by real number

查看:86
本文介绍了对随机的“复数”进行排序。按实数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在C#中的代码,我想用实数对随机复数进行排序。

问题是没有按实数排序!



任何想法?



喜欢这样:

120 + 160 i

121 + 105 i

122 + 189 i

...







Here is my code in C# , i want to Sort random "complex number " by real number.
Problem is didn't sort by real number!

any idea?

like this :
120 + 160 i
121 + 105 i
122 + 189 i
...



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tamrine2
{
    class Complex
    {
        public int real, imag;


        private static Random TheRandom = new Random();

        public void Set()
        {
          
            
            real = TheRandom.Next(100, 200);
            
            imag = TheRandom.Next(100, 200);
            

        }



        public void Write()
        {
            char sign;
            sign = (imag < 0) ? '-' : '+';
            
            Console.WriteLine("{0} {1} {2} i ", real, sign, Math.Abs(imag));
        }

    }
    class Program
    {
        private static int n;
        static void Main(string[] args)
        {
            Console.Write("How Many Number : ");
            n = Convert.ToInt32(Console.ReadLine());

            Complex[] complexArray = new Complex[n];

            for (int i = 0; i < n; ++i)
            {
                complexArray[i] = new Complex();
               
            }

           
            for (int i = 0; i < n; ++i)
            {
                
                complexArray[i].Set();
                
                complexArray[i].Write();
             
            }
         


            realSort(complexArray);



            Console.ReadKey();
        }

        public static void realSort(Complex[] s)
        {
            Complex temp;
            for (int i = 1; i <= s.Length - 1; ++i)
                for (int j = 0; j < s.Length - i; ++j)
                    if (s[j].real > s[j + 1].real)
                    {
                        temp = s[j];
                        s[j] = s[j + 1];
                        s[j + 1] = temp;
                    }
        }
    }
}

推荐答案

阅读: http://stackoverflow.com/questions/7851387/sorting-complex-numbers [ ^ ]


didn按实数排序!



好​​的 - 但我们不知道你在看什么数据,或者你怎么决定它没有'工作。

另外,这是你的功课,所以你自己解决这个问题是个好主意 - 你会学到更多东西,而不是修理它并给你排序代码。



所以,它取决于你。

在函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但是我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
"didn't sort by real number !"

OK - but we have no idea what data you were looking at, or how you decided that it didn't work.
Plus, it's your homework, so it's a good idea that you sort this yourself - you will learn a lot more than if I fix it and give you the sorted code.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我看到你的程序未列出的2个理由排序的数字。



首先:你在排序之前列出数字

第二:排序例程被窃听到核心。总体布局是一种插入排序,但是它被窃听并且不起作用。



因为它是HomeWork,没有解决方案,但建议。

- 查看您的计划,列出 排序后的数字。

- 查看插入排序算法并进行必要的更正。



更一般的建议:

您的代码行为不符合您的预期,您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

在Visual Studio 2010中进行调试 - 初学者指南 [ ^ ]



这个解决方案的缺点:

- 这是一个DIY,你是跟踪t他找到问题并找到了根源,这导致了解决方案。

这个解决方案的优点:

- 你看到你的代码行为,你将它与你的期望相匹配。 br $>


次要效果

- 你会为自己找到虫子感到骄傲。

- 你的技能会提高。



您应该很快发现错误。
I see 2 reasons for your program not listing the numbers sorted.

First: you list the numbers before sorting them.
Second: the sort routine is bugged to the core. The general layout is of an insertion sort, but it is bugged and don't work.

Since it is HomeWork, no solution, but advices.
- Review your program to list the numbers after sorting them.
- Review the insertion sort algorithm and do necessary corrections.

More general advice:
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- You see your code behaviour, you match it against your expectations.

secondary effects
- Your will be proud of finding bugs yourself.
- Your skills will improve.

You should find pretty quickly what is wrong.


这篇关于对随机的“复数”进行排序。按实数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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