Visual Studio .NET Academic Student Tools Guide Chapter 6
| Visual Studio .NET Academic Student Tools Guide |
|
CHAPTER 6
Samples
A group of nine samples are shipped with Microsoft Visual Studio .NET Academic. These samples range in difficulty from simple to complex. Samples are organized using the following classification system:
- Simple
Includes programming concepts that are generally presented in first- and second-year computer science courses.
- Intermediate
Includes programming concepts introduced in third- or fourth-year computer science courses.
- Advanced
Includes highly complex programming concepts or uses concepts that are introduced in advanced undergraduate studies.
The following table shows the list of samples included with Visual Studio .NET Academic and their classification.
| Sample |
Classification |
| Hello World Sample |
Simple |
| Tic-Tac-Toe Sample |
Simple |
| Diff Tool Sample |
Simple |
| Elementary Data Structures Sample |
Simple |
| Expression Parser Sample |
Intermediate |
| Sorting Sample |
Intermediate |
| Tile Puzzle Sample |
Advanced |
| Towers of Hanoi Sample |
Console version, Intermediate Windows version, Advanced |
| Network Chat Sample |
Advanced |
To access a specific sample
- In the topic for a specific sample, click Load Sample Solution to open the sample in a new instance of Visual Studio .NET Academic.
Demonstrates how to generate basic screen output and simply prints the phrase "Hello World". This sample is written in Visual C++, Visual C#, and Visual Basic. For an in-depth explanation of each line of code in the Visual C++ implementation of this sample, see Walkthrough: Creating a Console Application in Getting Started.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The HelloWorld.sln file is opened in Solution Explorer.
- From the Build menu, choose Build Solution.
The HelloWorld.sln file contains three separate projects-one project for each language implementation of the Hello World sample.
- To run the executable (.exe) file associated with each of the three projects, right-click the project name in Solution Explorer, choose Debug, and then choose Start New Instance.
-or-
Right-click the project name in Solution Explorer, choose Set as Startup Project, and then, from the Debug menu, choose Start Without Debugging.
Demonstrates using two-dimensional arrays and prompting for user input in a game of Tic-Tac-Toe played between a user and the computer on a 3x3 game board. The sample is written in Visual C++. This sample uses standard C++ iostream classes to perform input and output in a console-mode application.
Program Logic
This program uses a simple algorithm for determining how the computer selects a move. First, the program determines whether there are any rows or columns that it can block. If it locates one of these spaces, then it places an "X" in the appropriate row or column. Failing this, the program places an "X" in the first open space.
The winner of the game is the first player to place three "X" or "O" symbols in a row. A tie is declared when the board is full and if no player is able to place three symbols in a row.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The TicTacToe.sln file is opened in Solution Explorer.
- From the Build menu, choose Build.
- From the Debug menu, choose Start Without Debugging.
- Follow the program instructions to play the game.
Possible Improvements
There are several ways you can modify this sample to improve program logic or playability. The following are a few possible improvements for modifying the program:
- Play on a larger game board.
- Allow two players to play each other.
- Improve the logic used by the program to place an X.
- Increase the program efficiency. For example, knowing the size of the game board, it is not necessary to check for a winner or a tie on each move.
Demonstrates how to handle file input and output. Specifically, this sample:
- Reads input from two text files.
- Compares each line of text in these files.
- Generates an output file that indicates which lines differ between input files.
- Provides a summary of the differences observed.
This sample demonstrates simple file differentiation and is intended as a sample reference on file input and output, not a reference on file differencing.
The text files input to and output from this sample are shown in the following table.
| File |
Description |
Sample file |
| Filename1 |
Input Base text file against which to compare. |
Base.txt |
| Filename2 |
Input Modified version of base text file. |
Compareto.txt |
| Filename3 |
Output Text file that indicates the differences between the first and second text files. This argument is optional. If not provided, then the output is printed to standard output. |
User-specified name |
This sample demonstrates the following:
- How to open files, read from files, write to files, and close files.
- How to sequentially read data from files.
- How to process command line arguments.
The Diff Tool sample is written in Visual C++ using ANSI and using the iostream class libraries to perform console and file stream manipulations. This sample is designed to operate only on English-language ANSI text files. There is no support for UNICODE and MBCS (Multi-Byte Character Set) text files or binary files.
Program Logic
The Diff Tool sample works as follows:
- The program compares the first line in the Filename1 file to all of the lines in the Filename2 file.
- If this line is located at the current line in Filename2, then the line was unmodified.
- If this line is located after the current line in Filename2, then the lines before this line have been added.
- If this line is not located, then the line was deleted from Filename2.
- The program then compares the next line in the Filename1 to the lines following the last found line in Filename2. This check is repeated until all lines in Filename1 have been evaluated.
- After all lines in Filename1 have been compared to the lines in Filename2, all text beyond the last found line in Filename2 file is marked as added.
Implementation Notes and Limitations
- All lines must end in a carriage return to be counted as full lines of text. This means that if the last line of one of the input files does not end with a carriage return, then it will not be read and compared.
- Lines longer than 512 (BUF_SIZE) characters are not handled properly.
- Unicode text files are not supported.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The StudentDiff.sln file is opened in Solution Explorer. This solution contains the source code for the application (StudentDiff.cpp), Base.txt, and Compareto.txt.
- From the Build menu, choose Build.
- In Solution Explorer, right-click the project name, and choose Properties.
The Properties dialog box appears.
- Choose Debugging.
- In the Command Arguments text box type the following:
base.txt compareto.txt
Note Specifying two command arguments prints output to the screen; specifying three command arguments prints output to a file. For example, in the Command Arguments text box, enter base.txt compareto.txt outputfile.txt.
- Click Apply, and click OK.
- From the Debug menu, choose Start Without Debugging.
Possible Improvements
There are several ways you can modify this sample to improve program logic. The following are a few possible improvements for modifying the program:
- Reading all of the lines in the comparison file (in this sample, Compareto.txt) at the beginning of the differencing can require a lot of memory. Change the implementation to add entries to the list as needed.
- As written, this program does not account for entries that are moved from one place to another. Add an algorithm to differentiate between new lines and lines that have been moved.
This sample includes a Readme.txt file in their respective directories. This file provides more information about the sample and suggested improvements.
Demonstrates an implementation of queue and stack data structures. A queue is a first-in first-out (FIFO) buffer. In a queue, the first item added to the queue is also the first item removed. A stack is a last-in first-out (LIFO) buffer, where the last item added is the first item removed from the stack. In this sample, the "push" command adds that item to the stack, while the "pop" command removes an item from the stack. Similarly, the "enqueue" command adds an item to the queue, and "dequeue" removes an item from the queue. This sample is written in Visual C++ and using the iostream classes for console input and output.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The ElementaryStructures.sln file is opened in Solution Explorer.
- From the Build menu, choose Build Solution.
- From the Debug menu, choose Start Without Debugging.
- Follow the program instructions.
Possible Improvements
There are several ways you can modify this sample to improve program logic. The following is one possible improvement for modifying the program:
- There is a lot of shared functionality between the Queue and Stack classes. Create an abstract base class that can be used to reduce some of the code for construction and destruction of the Queue and Stack classes.
This sample includes a Readme.txt file in their respective directories. This file provides more information about the sample and suggested improvements.
Demonstrates the following:
- How to work with string input.
- Parse this input to an expression tree.
- Send output to a scrollable window. In this window, you can enter expressions and select, copy, and print text.
This sample is written in Visual C++ using Microsoft Foundation Classes (MFC). This sample is designed to operate only on English-language ANSI input strings. There is no support for UNICODE or MBCS (Multi-Byte Character Set) text input.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The ExpressionParser.sln file is opened in Solution Explorer.
- From the Build menu, choose Build Solution.
- From the Debug menu, choose Start Without Debugging.
- In the Expression box, type ((a+b)*2), and click Parse.
The expression tree is displayed on the screen.
Note Enclose the entire expression in parentheses.
Practice using this application with other expressions. The following expression operators are supported:
- "+" (addition)
- "-" (subtraction)
- "*" (multiplication)
- "/" (division)
- "^" (exponentiation)
- "(" (left parenthesis)
- ")" (right parenthesis)
Possible Improvements
There are several ways you can modify this sample to improve program logic. The following are a few possible improvements for modifying the program:
- This code does not show errors if there are invalid characters past the end of a valid expression. Check for characters remaining at the end of a parse, and provide an error if characters are found.
- This application only makes it possible for you to enter single expressions. Derive a new class called CompoundExpression that consists of two Expression classes. Then, as you parse the top-level entries, if there is remaining input, that also could be entered into the RetrieveExp function, and the result could be concatenated with the results of the previous expression parser runs.
- Add a method to the base Expression class, allowing it to print the expression tree using a different notation (for example, prefix or postfix notation). Include this information in your output to the user interface.
This sample includes a Readme.txt file in their respective directories. This file provides more information about the sample and suggested improvements.
Graphically demonstrates the following sorting algorithms:
- Insertion sort
Begins with a sorted list of size 1. To insert the next element, this algorithm moves the new item to the left through the list until it finds the correct position in the list. This continues until all items in the original list are sorted into the correct position. This sort requires n-1 passes, where n is the number of elements in the list.
- Quick sort
Selects a pivot point that is the median of the first, middle, and last elements in the array. Then, the array is partitioned into one group that is less than the pivot and one that is greater than the pivot. A quick sort is performed on each partition.
This sample is written in Visual C++ using the C interfaces to the Windows application programming model.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The SortingDemonstration.sln file is opened in Solution Explorer.
- From the Build menu, choose Build Solution.
- From the Debug menu, choose Start Without Debugging.
The Sorting Demonstration window appears.
- From the Sort menu, choose the type of sort you want to perform, Insertion Sort or Quick Sort.
- Click Step to step through the selected sort incrementally.
-or-
Click Auto Sort to run the entire sort through to completion.
Possible Improvements
There are several ways you can modify this sample to improve program logic. The following are a few possible improvements for modifying the program:
- Currently, after you choose Auto Sort, the only way to stop execution is to close the application; otherwise, you have to wait for the selected sort to finish. Add a Cancel button to cancel the AutoSort process at the current point of execution. When a user clicks Cancel, the user can resume sorting using the Step feature. To do this, add a button, handle the additional WM_COMMAND, and stop the timer.
- The Sorting Demonstration window cannot be resized. To support resizing, change the property on the window that prevents it from being resized, and then handle the WM_SIZE message. In the dialog box, you also must reset the sizes of all of the labels and the positions of the command buttons, as well as register the area in which the box is drawn for an update. Modify the code for updating and invalidating the box to determine the new size of the window at run time, rather than from the const static variables.
- Add another algorithm, such as selection sort, using this same user interface.
- Create a faster implementation of the GetBitInBitmap and SetBitInBitmap functions.
This sample includes a Readme.txt file in their respective directories. This file provides more information about the sample and suggested improvements.
This sample is shipped with the Managed Extension for Visual C++ sample set. For more information, see TilePuzzle Sample: Demonstrates Interoperability Between C# and Managed Extensions for C++ in the Microsoft MSDN Library.
The Towers of Hanoi sample demonstrates a recursive algorithm for solving the Towers of Hanoi puzzle using a console application or a graphical application created using the .NET Framework Forms class libraries from Visual C#. This sample is divided into the following projects:
- HanoiEngine
The HanoiEngine project is a Visual C# DLL that is consumed by any project wanting to display the step-by-step output of a Towers of Hanoi solution. The HanoiEngine project cannot be run directly but is accessed by any project implementing IHanoiDisplay. Both the HanoiConsole and HanoiWindows projects implement IHanoiDisplay and use the HanoiEngine project to solve the Towers of Hanoi puzzle.
- HanoiConsole
HanoiConsole provides a basic implementation of the HanoiEngine project's display interface, IHanoiDisplay. HanoiConsole makes it possible for you to test the modifications to the HanoiEngine using a simple console application.
- HanoiWindows
HanoiWindows implements the IHanoiDisplay interface defined by the HanoiEngine project. HanoiWindows makes it possible for users to solve the Towers of Hanoi puzzle through a graphical user interface and reports the number of moves taken to solve the puzzle.
This sample is written in Microsoft Visual C# .NET.
Building and Running the Sample
To build and run this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Microsoft Visual Studio .NET.
The Hanoi.sln file is opened in Solution Explorer.
The Hanoi solution contains a Microsoft Visual C++ .NET and a Visual C# .NET project for the two language implementations of this sample that are provided.
- From the Build menu, choose Build Solution.
- From the Debug menu, choose Start Without Debugging.
- To run the executable (.exe) file associated with the HanoiConsole and HanoiWindows projects, right-click the project name in Solution Explorer, and choose Set as Startup Project.
- From the Debug menu, choose Start Without Debugging.
HanoiConsole displays the output of a three-peg Towers of Hanoi solution in a console window. HanoiWindows makes it possible for you to solve the Towers of Hanoi puzzle through a graphical user interface.
Use the following instructions to play HanoiWindows:
- The Reset button returns the user to the default configuration using the last user-specified number of disks. This button also resets all counters.
- The AutoPlay button moves the disks one-by-one, demonstrating the optimal solution.
- The Number of Disks button displays a dialog box that asks you to input the number of disks (from 2 to 4) with which you want to play the game.
- The Instructions button displays the game instructions.
Possible Improvements
There are several ways you can modify this sample to improve program logic or playability. The following a few possible improvements for modifying the program:
- Modify the sample to accommodate more disks.
- Add a fourth peg to the game.
This sample includes a Readme.txt file in their respective directories. This file provides more information about the sample and suggested improvements.
This sample is shipped with the .NET Framework SDK. For more information, see Wintalk Application Sample in the .NET Framework SDK.
Used in conjunction with the Walkthrough: Debugging a Sample Application topic in Working With Visual Studio .NET to guide you through using the debugger to find and resolve a bug in a simple application.
To build this sample
- Under Sample Files, click the Load Sample Solution link.
The Browse For Folder dialog box appears.
- Browse to select a location for the sample files, and click OK.
Select a location to which you have write access. The sample files are opened automatically in a new instance of Visual Studio .NET.
The FahrenheitToCelsius.sln file is opened in Solution Explorer.
- From the Build menu, choose Build Solution.
- To run the sample, follow the instructions in Walkthrough: Debugging a Sample Application in Working With Visual Studio .NET.
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.