home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_09_04
/
9n04023a
< prev
next >
Wrap
Text File
|
1991-02-05
|
7KB
|
283 lines
#include <stdio.h>
#include <ctype.h>
#include <limits.h>
#define MAX_NODES 4 /* max number of nodes in array */
#define ADD_NODE 'A'
#define CHANGE_NODE 'C'
#define DISPLAY_NODE 'D'
#define EXIT 'E'
#define SEARCH_FNODE 'F'
#define HELP '?'
#define INSERT_NODE 'I'
#define SEARCH_LNODE 'L'
#define COUNT_NODES 'N'
#define REMOVE_NODE 'R'
#define SORT_NODES 'S'
#define DUMP_TABLE 'T'
/* ------------------------------------------------------------------- */
main()
{
int ary[MAX_NODES];
int nodes_in_use = 0;
int index;
int temp, i, j;
int inchar = 'x'; /* force initial prompt */
while (1) {
if (inchar != ' ')
printf("\nEnter Action Code (%c for help): ", HELP);
switch(toupper(inchar = getchar())) {
/* ------------------------------------------------------------------- */
case EXIT:
goto end; /* break won't do so use goto */
/* ------------------------------------------------------------------- */
default:
printf("\n Invalid command. Please try again\n");
break;
/* ------------------------------------------------------------------- */
case HELP:
printf("\n The action codes are:\n");
printf("\t%c - produces this help message\n", HELP);
printf("\t%c - Add a new node to the end\n", ADD_NODE);
printf("\t%c - Change a user-selected node\n", CHANGE_NODE);
printf("\t%c - Display a user-selected node\n", DISPLAY_NODE);
printf("\t%c - Exit this program\n", EXIT);
printf("\t%c - Search for first occurrence\n", SEARCH_FNODE);
printf("\t%c - Insert a new node\n", INSERT_NODE);
printf("\t%c - Search for last occurrence\n", SEARCH_LNODE);
printf("\t%c - Report the number of nodes\n", COUNT_NODES);
printf("\t%c - Remove a user-selected node\n", REMOVE_NODE);
printf("\t%c - Sort the nodes in ascending order\n", SORT_NODES);
printf("\t%c - Show all entries in the table\n", DUMP_TABLE);
break;
/* ------------------------------------------------------------------- */
case '\n': /* ignore white space on input */
case ' ' :
case '\t':
case '\v':
case '\f':
inchar = ' '; /* indicate white space input */
break;
/* ------------------------------------------------------------------- */
case ADD_NODE:
if (nodes_in_use == MAX_NODES) {
printf("\n Table is full\n");
break;
}
printf("\n Enter new node's value: ");
scanf("%3d", &ary[nodes_in_use]);
++nodes_in_use;
printf("\n Node added");
break;
/* ------------------------------------------------------------------- */
case DUMP_TABLE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
printf("\n Table nodes are as follows:\n");
for (index = 0; index < nodes_in_use; ++index)
printf("\tNode %2d => %3d\n", index, ary[index]);
break;
/* ------------------------------------------------------------------- */
case COUNT_NODES:
printf("\tThere are %2d nodes in the table\n", nodes_in_use);
break;
/* ------------------------------------------------------------------- */
case DISPLAY_NODE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
while (1) {
printf("\n Enter node number: ");
scanf("%d", &index);
if (index >= 0 && index < nodes_in_use)
break;
printf("\n No such node (%d) in table\n", index);
}
printf("\tNode %2d => %3d\n", index, ary[index]);
break;
/* ------------------------------------------------------------------- */
case CHANGE_NODE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
while (1) {
printf("\n Enter node number: ");
scanf("%d", &index);
if (index < 0 || index >= nodes_in_use)
printf("\n No such node (%d) in table\n", index);
break;
}
printf("\tNode %2d => %3d\n", index, ary[index]);
printf("\n Enter new value: ");
scanf("%3d", &ary[index]);
printf("\n Node changed");
break;
/* ------------------------------------------------------------------- */
case SEARCH_FNODE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
printf("\n Enter search value: ");
scanf("%d", &temp);
for (index = 0; index < nodes_in_use; ++index) {
if (ary[index] == temp) {
printf("\tValue %3d found in node %3d\n",
temp, index);
goto found1;
}
}
printf("\n No such value (%d) in table\n", temp);
found1:
break;
/* ------------------------------------------------------------------- */
case SEARCH_LNODE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
printf("\n Enter search value: ");
scanf("%d", &temp);
for (index = nodes_in_use - 1; index >= 0; --index) {
if (ary[index] == temp) {
printf("\tValue %3d found in node %3d\n",
temp, index);
goto found2;
}
}
printf("\n No such value (%d) in table\n", temp);
found2:
break;
/* ------------------------------------------------------------------- */
case SORT_NODES: /* simple bubble sort */
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
for (i = nodes_in_use - 2; i >= 0; --i) {
for (j = 0; j <= i; ++j) {
if (ary[j] > ary[j + 1]) {
temp = ary[j];
ary[j] = ary[j + 1];
ary[j + 1] = temp;
}
}
}
printf("\n Nodes sorted");
break;
/* ------------------------------------------------------------------- */
case INSERT_NODE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
if (nodes_in_use == MAX_NODES) {
printf("\n Table is full\n");
break;
}
while (1) {
printf("\n Enter number of node to insert before: ");
scanf("%d", &index);
if (index >= 0 && index < nodes_in_use)
break;
printf("\n No such node (%d) in table\n", index);
}
++nodes_in_use;
for (i = nodes_in_use - 1; i > index; --i) {
ary[i] = ary[i - 1];
}
printf("\n Enter new node's value: ");
scanf("%3d", &ary[index]);
printf("\n Node inserted");
break;
/* ------------------------------------------------------------------- */
case REMOVE_NODE:
if (nodes_in_use == 0) {
printf("\n Table contains no nodes\n");
break;
}
while (1) {
printf("\n Enter node number: ");
scanf("%d", &index);
if (index >= 0 && index < nodes_in_use)
break;
printf("\n No such node (%d) in table\n", index);
}
for (i = index; i < nodes_in_use; ++i) {
ary[i] = ary[i + 1];
}
--nodes_in_use;
printf("\n Node removed");
break;
/* ------------------------------------------------------------------- */
}
}
end: return (0);
}
/* ------------------------------------------------------------------- */