Let's see a complete example that uses tables ("arrays"), records ("struct") and also manipulate strings.
The idea will be as follows: We will create a program that can store data up to 1000 files (computer files). For each file, we will store: Filename, Size (in KB, a number from 0 to 8 billion). The program must displays a menu to allow the user do the following:
1- Add a record for a new file
2- Show the names of all the files we have stored
3- Show files that are over a certain size (for example, 2000 KB).
4- View all data in a record (from its name)
5- Exit the application (as we do not use files, data will be lost)
4- View all data in a record (from its name)
5- Exit the application (as we do not use files, data will be lost)
It should not be difficult. Let's see directly one of the ways that it could raise be done and then discuss some of the improvements that we could (maybe even should) do.
For example, to solve this problem we can count the number of records we have stored, and add one at a time. If we have 0 records, we store the following one (the first one) in position 0; if we have two records, they will be in the positions 0 and 1, and then we would add the next add in the position 2. In general, if we have 'n' records, we will add the next one in the position "n". If we want to review all records, we will go from position 0 to n-1, doing something like
for (i=0; i<=n-1; i++) { ... more commands ...}
or something like
for (i=0; i<n; i++) { ... more commands ...}
The rest of the program is not difficult: we know how to read and compare texts and numbers, how to choose from multiple options using "switch", and so on. Also, we will limit the number of records to 1000, so if the user wants to add data, we should make sure before we still have available slots.
With all these considerations, our source would look like:
/*---------------------------*/
/* C# Example #46: */
/* example46.cs */
/* */
/* Array of struct, with */
/* menu */
/* */
/* Intro to C#, */
/* Nacho Cabanes */
/*---------------------------*/
using System;
public class Example46
{
struct recordType {
public string fileName; /* Name of the file */
public long fileSize; /* Size of the file, in bytes */
};
public static void Main()
{
recordType[] records /* Array for the data */
= new recordType[1000];
int numberOfRecords=0; /* Number of stored records */
int i; /* For loops */
int option; /* Menu option chosen by the user */
string searchString; /* To search for certain text */
long searchSize; /* To search for a certain size */
do {
/* Main menu */
Console.WriteLine();
Console.WriteLine("Choose an option:");
Console.WriteLine("1.- Add data of a new file");
Console.WriteLine("2.- Display the names of all the files");
Console.WriteLine("3.- Display files over a certain size");
Console.WriteLine("4.- Display data of a file");
Console.WriteLine("5.- Exit");
option = Convert.ToInt32( Console.ReadLine() );
/* Perform actions depending on the chosen action */
switch(option){
case 1: /* Add data */
if (numberOfRecords < 1000) { /* If there is room enough */
Console.WriteLine("Enter the name of the file: ");
records[numberOfRecords].fileName = Console.ReadLine();
Console.WriteLine("Enter the size in bytes: ");
records[numberOfRecords].fileSize =
Convert.ToInt32( Console.ReadLine() );
/* And we have on more record */
numberOfRecords++;
} else /* If there is no room, we warn the user */
Console.WriteLine("Limit of records reached (1000)!");
break;
case 2: /* Show all the records */
for (i=0; i<numberOfRecords; i++)
Console.WriteLine("Name: {0}; Size: {1} KB",
records[i].fileName, records[i].fileSize);
break;
case 3: /* Show depending on size */
Console.WriteLine("From which size do you want to see files?");
searchSize = Convert.ToInt64( Console.ReadLine() );
for (i=0; i<numberOfRecords; i++)
if (records[i].fileSize >= searchSize)
Console.WriteLine("Name: {0}; Size: {1} KB",
records[i].fileName, records[i].fileSize);
break;
case 4: /* See all data of a file */
Console.WriteLine("For which file do you want to see all the data?");
searchString = Console.ReadLine();
for (i=0; i<numberOfRecords; i++)
if ( records[i].fileName == searchString )
Console.WriteLine("Name: {0}; Size: {1} KB",
records[i].fileName, records[i].fileSize);
break;
case 5: /* Exit: we tell we are exitting */
Console.WriteLine("End of program");
break;
default: /* Other option: not valid */
Console.WriteLine("Unknown option!");
break;
}
} while (option != 5); /* If option 5, execution loop ends */
}
}
It works, and it does everything we need to do, but it can be improved. Of course, in a real situation it is common that each record needs to store more information than just these two example fields we have planned this time. Also, if we display all data on screen we might not have time to read it, so it would be desirable to pause the display when the screen is full (for example, after every 25 data). Of course, might can think of many more questions to ask about our data. And when we leave the program, all the data we had typed is lost, but something we cannot avoid yet, because we do not know how to use files.
Suggested exercises:
· A program that asks for the name, surname and age of a person, stores them in a "struct" and then shows the three data in the same line, separated by commas.
· A program that asks the users for data of 8 persons: name, birth day, birth month and year of birth (which should be stored in an array of structs). It should then repeat the following: ask a month number and display data from individuals who have born in that month. It must repeat until the user enters 0 as the month number.
· A program that is capable of storing data of 50 persons: name, address, telephone number, age (using an array of structs). Data must be entered one at a time, until the user enters an empty name (presses Enter, not typing anything else). Then it should be displayed a menu to allow:
o Show the list of all names.
o Show the people of a certain age.
o Show those whose initial is the one entered by the user.
o Exit the program
(this menu should be repeated until the user chooses the "quit" option)
· Improve the files database (example program 46) so that it does not allow the user to enter incorrect sizes (negative numbers) or empty file names.
· Expand the files database (example program 46) to include a "partial search" option, in which the user indicates part of the name part and the program displays all the files containing that fragment (using "IndexOf"). This search should not be case-sensitive (using ToUpper or ToLower).
· Extend the previous example (which allows partial search), to add it incremental search: the user will indicate the text to search letter by letter, and the program will display all the records that contain it (for example, first the records containing "j", then the ones containing "jo", then "joh" and finally "john").
· Expand the files database (example program 46), so that the user can erase a record (the program will have to "move backwards" all the data that was after that records, and decrease the counter).
· Improve files database (example program 46) so that you can modify any record indicated by its number (for example, the record number 3). While modifying, the user should be allowed to press Enter without typing anything, to indicate that he/she does not want to change that field (instead of replacing it with an empty string).
· Expand the files database (example program 46) so that it allows to sort the data by name. To do this, you will need to find information about a simple sorting method, such as "bubble method" (some are described in the next section).
No comments:
Post a Comment