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

问题描述

这是我在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;
                    }
        }
    }
}

推荐答案




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

08-21 12:22