Students database program in C

, by Engineer's Vision

/* database of a student */

#include<stdio.h>
#include<conio.h>
#include<string.h>

typedef struct student
{
 int rollno;
 char name[20];
 int marks;
 }student;

void insert(student st[],int position, int n);
void delet(student st[], int position, int n);
int search(student st[], int rollno, int n);
void print(student st[], int n);
void read(student st[], int n );
void sort(student st[], int n);
void modify(student st[], int n);
void main()
{
 student st[30];
 int n,i,op,position,rollno;
 clrscr();
 do
 {
  printf("\n 1)create \n 2)insert \n 3) delete \n 4) search \n 5)print \n 6) sort \n 7) modify \n 8) quit");
  printf("\n enter your choice");
  scanf("%d",&op);
  switch (op)
  {
   case 1:
   printf("\n enter no of students");
   scanf("%d",&n);
   read(st,n);
   break;
  
   case 2:
   printf("\n enter the position (no of records=%d)",n);
   scanf("%d",&position);
   if(position<=n+1)
   {
   insert(st,position,n);
   n++;
   print(st,n);
   }
   else printf("\m cannot insert");
   break;
  
   case 3:
   printf("\n enter rollno");
   scanf("%d",&rollno);
   position=search(st, rollno, n);
   if(position!=-1)
   {
   delet(st,position,n);
   n--;
   print(st,n);
   }
   else printf("\n cannot delete");
   break;
 
   case 4:
   printf("\n enter rollno");
   scanf("%d",&rollno);
   position=search(st,rollno,n);
   if(position==-1)
   printf("\n not found");
   else {
   printf("\n found at the location=%d",position+1);
   printf("\n %s\t%d\t%d",st[position].name,st[position].rollno,st[position].marks);
   }
   break;

   case 5:
   print(st,n);
   break;

   case 6:
   sort (st,n);
   print(st,n);
   break;

   case 7:
   modify(st,n);
   break;
  }
 } while(op!=8);
}

void insert(student st[], int position, int n)
{
int i;
printf("\n enter data(name rollno marks):");
for(i=n-1;i>=position-1;i--)
st[i+1]=st[i];
scanf("%s%d%d", st[position-1].name,&st[position].rollno,&st[position].marks);
}

void delet(student st[], int position, int n)
{
int i;
for (i=position+1;i<n;i++)
st[i-1]=st[i];
}

int search(student st[], int rollno, int n)
{
int i;
for (i=0;i<n;i++)
if(rollno==st[i].rollno)
return(i);
return(-1);
}

void print(student st[], int n)
{
int i;
for (i=0;i<n;i++)
printf("\n %s\n%d\n%d",st[i].name,st[i].rollno,st[i].marks);
}

void read(student st[], int n)
{
int i;
printf("\n enter data(name rollno marks):");
for(i=0;i<n;i++)
scanf("%s%d%d",st[i].name,&st[i].rollno,&st[i].marks);
}

void sort(student st[], int n)
{
int i,j;
student temp;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(st[j].rollno>st[j+1].rollno)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}

void modify(student st[],int n)
{
int rollno,i;
printf("\n enter roll no");
scanf("%d",&rollno);
i=search(st,rollno,n);
if(i==-1)
printf("\n not found");
else {
printf("\n enter data(name rollno marks):");
scanf("%s%d%d",st[i].name,&st[i].rollno,&st[i].marks);
}
}

0 comments: