[C#]练习了下数组和类,C++ 基础还算可以,不是很吃力

using System;

namespace _02
{
    
class SortNum  //输入几个数、排序、输出
    
{
        
private int[] iNum;

        
public SortNum()
        {
            
Init();
            
InputValue();
        }

        
private void Init()  //读入数字总数并分配空间
        
{
            
Console.WriteLine("请输入数字的总数:");
            
try
            
{
                
iNum new int[Int16.Parse(Console.ReadLine())];
            }
            
catch
            
{
                
Console.WriteLine("您输入的不是有效的数字。");
                
Init();
            }
        }

        
private void InputValue()  //输入数字
        
{   
            
for(int 0;iNum.Lengthi++)
            {
                
Console.WriteLine("请输入第 {0} 个数字:"1);
                
try
                
{
                    
iNum[i] = Int16.Parse(Console.ReadLine());
                }
                
catch
                
{
                    
Console.WriteLine("您输入的不是有效的数字。");
                    
i--;
                }
            }
            
Console.WriteLine("输入结束。");
        }

        
public void Sort(bool inverse)
        
//选择法排序, 若 inverse 为真则从大到小排序, 否则从小到大排序
        
{
            
int ijtemp;

            
for (0iNum.Lengthi++)
            {
                
for (1iNum.Lengthj++)
                {
                    
if (inverse)
                    {
                        
if (iNum[i] < iNum[j])
                        {
                            
temp iNum[i];
                            
iNum[i] = iNum[j];
                            
iNum[j] = temp;
                        }
                    }
                    
else
                    
{
                        
if (iNum[i] > iNum[j])
                        {
                            
temp iNum[i];
                            
iNum[i] = iNum[j];
                            
iNum[j] = temp;
                        }
                    }
                }
            }
        }

        
public void Sort()  //从小到大
        
{
            
Sort(false);
        }

        
public void Print()  //输出
        
{
            
foreach (int num in iNum)
            {
                
Console.Write("{0, 3}"num);
            }
            
Console.Write("/n");
        }
    }

    
class Program
    
{
        
static void Main(string[] args)
        {
            
do
            
{
                
SortNum a new SortNum();

                
Console.WriteLine("您输入的数字是:");
                
a.Print();
                
a.Sort();
                
Console.WriteLine("从小到大排序后是:");
                
a.Print();
                
a.Sort(true);
                
Console.WriteLine("从大到小排序后是:");
                
a.Print();

                
Console.Write("按 ESC 退出, 其他任意键继续.../n/n");
            }
            
while (Console.ReadKey(true).Key != ConsoleKey.Escape);
        }
    }
}

/*
 * 学习手记
 * 
 * Console.ReadLine() 从标准输入设备读入一行字符串
 * Int16.Parse() 将字符串转换为 Int16 类型数值
 * try...catch... 出错控制,感觉用起来还挺方便
 * 跟 C/C++ 的一个重大区别是,定义数组、类其实都相当于定义指针
 * 并没有分配空间,还要用 new 去分配空间
 * 据说不用 delete 它会自动回收空间
 * 那么析构函数似乎用不太着了
 * 居然不支持默认参数,只好用重载
 * Console.ReadKey() 返回一个 ConsoleKeyInfo 对象, 其 Key 成员表示相应的键
 * 
 * 2005-06-09
 */

(原发表于 CSDN:https://blog.csdn.net/cnStreamlet/article/details/1646808)

posted on 2007-06-10 16:21  溪流  阅读(31)  评论(0编辑  收藏  举报