Tuesday, May 19, 2009

Structure Manipulatons

/*
Title: Structure Manipulatons
Problem Statement: Write a program to perform structure manipulation.
*/

#include
#include

struct student
{
char name[20];
int roll;
int cls;
char section;
} *p[20];

int n;

void add (int i)
{
--i;
p[i] = (struct student*) malloc ( sizeof ( struct student ) );
clrscr ();
printf ("\nSerial Number: %d", i+1);
flushall();
printf ("\nName: ");
gets (p[i]->name);
flushall();
printf ("Roll Number: ");
scanf ("%d", &p[i]->roll);
flushall();
printf ("Class: ");
scanf ("%d", &p[i]->cls);
flushall();
printf ("Section: ");
scanf ("%c", &p[i]->section);
}

void modify (int i)
{
--i;
clrscr ();
printf ("\nSerial Number: %d", i+1);
flushall();
printf ("\nName: ");
gets (p[i]->name);
flushall();
printf ("Roll Number: ");
scanf ("%d", &p[i]->roll);
flushall();
printf ("Class: ");
scanf ("%d", &p[i]->cls);
flushall();
printf ("Section: ");
scanf ("%c", &p[i]->section);
}

void view (int i)
{
--i;
clrscr ();
printf ("\nSerial Number: %d", i+1);
printf ("\nName: %s", p[i]->name);
printf ("\nRoll Number: %d", p[i]->roll);
printf ("\nClass: %d", p[i]->cls);
printf ("\nSection: %c", p[i]->section);
}

void del (int i)
{
int ch;
--i;
view (i);
printf ("\n\nAre you sure you wish to delete this record?\n1)\tYes\n2)\tNo");
if (ch==1)
free (p[i]);
p[i]=NULL;
}


int valid (int i)
{
--i;
if (i>=n)
return 0;
else if (p[i]==NULL)
return 0;
else
return 1;
}


void main ()
{
int ctr, ch=1;
int ch1=1;

for (ctr=0; ctr<20; ctr="1;">=20)
{
printf ("Database is full");
break;
}
else
{
add(n);
n++;
}
break;

case 2:
printf ("\nEnter the serial number of record to edit: ");
scanf ("%d", &ch);

if (valid (ch))
modify (ch);
else
printf ("\n\nRECORD DOES NOT EXIST");
break;

case 3:
printf ("\nEnter the serial number of record to delete: ");
scanf ("%d", &ch);

if (valid (ch))
{
del (ch);
n--;
}
else
printf ("\n\nRECORD DOES NOT EXIST");
break;

case 4:
printf ("\n1)\tDisplay all\n2)\tDisplay specific\n");
scanf ("%d", &ch);
switch (ch)
{
case 1:
for (ctr=1; ctr<=n; ctr++)
{
if (valid (ctr))
view (ctr);
printf ("\nPress any key to continue");
getch();
}
break;

case 2:
printf ("\nEnter the serial number of record to view: ");
scanf ("%d", &ch);

if (valid (ch))
view (ch);
else
printf ("\n\nRECORD DOES NOT EXIST");
break;
}
break;

case 5:
ch1=0;
}
getch ();
}
}

No comments:

Post a Comment