本入门指南旨在帮助您用 Visual Studio 构建一个简单的 C# 项目它无法进行全面的介绍我们鼓励您查询关于 C# 和 NET 的其他资源以便更多地学习这些技术在完成本教程之后您至少有了一个可用的项目在您研究 Visual C# 时可以从修改此这些代码开始
为了方便起见我们提供了完整的源程序和项目文件您可以通过本文档顶部的目录来访问它们
其他资源我们强烈推荐下面这些关于 C# 和 NET 平台的书籍它们是开发人员尝试学习这些新技术的有益资源
Archer TomInside C#RedmondMicrosoft Press
Deitel HarveyC#How to ProgramUpper Saddle River NJPrentice Hall
Gunnerson EricA Programmers Introduction to C#New YorkApress
Platt DavidIntroducing Microsoft NETRedmondMicrosoft Press
补遗QuickSort C# NET 的源代码下面是 QuickSort C# NET 示例应用程序的完整源代码您可以复制使用和分发这些代码(无版权费)注意这些源代码以原样提供并且不作任何保证
//
// QuickSort C# NET Sample Application
// Copyright Microsoft Corporation All rights reserved
//
// MSDN ACADEMIC ALLIANCE []
// This sample is part of a vast collection of resources we developed for
// faculty members in K and higher education Visit the MSDN AA web site for more!
// The source code is provided as is without warranty
//
// Import namespaces
using System;
using SystemCollections;
using SystemIO;
// Declare namespace
namespace MsdnAA
{
// Declare application class
class QuickSortApp
{
// Application initialization
static void Main (string[] szArgs)
{
// Print startup banner
ConsoleWriteLine (\nQuickSort C#NET Sample Application);
ConsoleWriteLine (Copyright (c) Microsoft Corporation All rights reserved\n);
ConsoleWriteLine (MSDN ACADEMIC ALLIANCE []\n);
// Describe program function
ConsoleWriteLine (This example demonstrates the QuickSort algorithm by reading an input file);
ConsoleWriteLine (sorting its contents and writing them to a new file\n);
// Prompt user for filenames
ConsoleWrite (Source: );
string szSrcFile = ConsoleReadLine ();
ConsoleWrite (Output: );
string szDestFile = ConsoleReadLine ();
// Read contents of source file
string szSrcLine;
ArrayList szContents = new ArrayList ();
FileStream fsInput = new FileStream (szSrcFile FileModeOpen FileAccessRead);
StreamReader srInput = new StreamReader (fsInput);
while ((szSrcLine = srInputReadLine ()) != null)
{
// Append to array
szContentsAdd (szSrcLine);
}
srInputClose ();
fsInputClose ();
// Pass to QuickSort function
QuickSort (szContents szContentsCount );
// Write sorted lines
FileStream fsOutput = new FileStream (szDestFile FileModeCreate FileAccessWrite);
StreamWriter srOutput = new StreamWriter (fsOutput);
for (int nIndex = ; nIndex < szContentsCount; nIndex++)
{
// Write line to output file
srOutputWriteLine (szContents[nIndex]);
}
srOutputClose ();
fsOutputClose ();
// Report program success
ConsoleWriteLine (\nThe sorted lines have been written to the output file\n\n);
}
// QuickSort implementation
private static void QuickSort (ArrayList szArray int nLower int nUpper)
{
// Check for nonbase case
if (nLower < nUpper)
{
// Split and sort partitions
int nSplit = Partition (szArray nLower nUpper);
QuickSort (szArray nLower nSplit );
QuickSort (szArray nSplit + nUpper);
}
}
// QuickSort partition implementation
private static int Partition (ArrayList szArray int nLower int nUpper)
{
// Pivot with first element
int nLeft = nLower + ;
string szPivot = (string) szArray[nLower];
int nRight = nUpper;
// Partition array elements
string szSwap;
while (nLeft <= nRight)
{
// Find item out of place
while (nLeft <= nRight && ((string) szArray[nLeft])CompareTo (szPivot) <= )
nLeft = nLeft + ;
while (nLeft <= nRight && ((string) szArray[nRight])CompareTo (szPivot) > )
nRight = nRight ;
// Swap values if necessary
if (nLeft < nRight)
{
szSwap = (string) szArray[nLeft];
szArray[nLeft] = szArray[nRight];
szArray[nRight] = szSwap;
nLeft = nLeft + ;
nRight = nRight ;
}
}
// Move pivot element
szSwap = (string) szArray[nLower];
szArray[nLower] = szArray[nRight];
szArray[nRight] = szSwap;
return nRight;
}
}
}
补遗关于 QuickSort C# NET为了演示 QuickSort Visual C# NET 示例应用程序实际是如何运行的我们提供了编译好的可执行文件您可以通过编译这些项目文件来创建自己的可执行文件单击 Quicksort_Visual_CSharp_NETexe下载源代码项目文件和可执行文件包
使用应用程序启动 Command Prompt(从开始菜单运行cmdexe)使用 CD 命令将目录更改为可执行文件所在的目录然后运行quicksortexe
程序将提示您提供输入和输出文件的名称任何包含多行的文本文件均可使用如果需要可以使用记事本来创建一个此类文件然后该程序将对输入文件的内容进行排序并且将其写入输出文件
示例程序输出下面是来自此 QuickSort C# NET 应用程序的一个实例的输出此示例演示了 QuickSort 算法方法是读取输入文件对文件的内容进行排序然后将其写入新的文件用户输入的文本以下划线标记
您可以查看下面的示例输入文件 exampletxt 和输出文件 outputtxt
QuickSort C# NET Sample Application
Copyright (c) Microsoft Corporation All rights reserved
MSDN ACADEMIC ALLIANCE []
This example demonstrates the QuickSort algorithm by reading an input file
sorting its contents and writing them to a new file
Source: exampletxt
Output: outputtxt
The sorted lines have been written to the output file
查看示例输入文件exampletxt
Visual C#
Windows Embedded
JavaScript
Speech API
ASPNET
VBScript
Windows Media
Visual Basic
NET Framework
BizTalk Server
XML Parser
Internet Explorer
Visual C#
SQL Server
Windows XP
DirectX API
查看示例输出文件outputtxt
NET Framework
ASPNET
BizTalk Server
DirectX API
Internet Explorer
JavaScript
Speech API
SQL Server
VBScript
Visual Basic
Visual C#
Visual C#
Windows Embedded
Windows Media
Windows XP
XML Parser