You are on page 1of 5

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1

struct student {

char name[25];
int ID;
float cgpa;
};

struct SBI {

int Acno;
int ID;
int balance;
};

struct student S;
struct SBI M;
FILE *fp1, *fp2;
void nothing_input(FILE *fd);
int insert();
int operators();
int check;

int projection();
int uni_on();
int intersection();
int cartesian();
int naturaljoin();

int main(){

int ch;
char more;
fp1 = fopen("student.txt","a+");
fp2 = fopen("mcastd.txt","a+");
if(fp1 == NULL || fp2 == NULL){
printf("\n Files could not open\n");
return 0;
}

while(TRUE){

printf("\n\t\t MAIN MENU \n\n ");


printf("\t\t 1. Insert_recordns\n");
printf("\t\t 2. Operations \n");
printf("\t\t 3. Exit \n");
printf("\n\t\tChoose Your Choice: \n ");
scanf(" \t\t\t %d",&ch);

switch(ch){

case 1:
insert();
break;
case 2:
operators();
break;
default:
return 0;
}
}
return 0;
}

void nothing_input(FILE *fd) {


int ch;
//Loop through data still in the input buffer
while( (ch = fgetc(fd)) != EOF && ch != '\n' );
}

int insert(){

int choice,pkey,uniq;
char yes;

do {

printf("Choose Record to insert \n");


printf("\t\t 1.Student \n");
printf("\t\t 2.SBI Student Account\n");
scanf("%d",&choice);
uniq = 0;
if(choice == 1){

printf("Enter Name, ID, Cgpa \n");


scanf("%s%d%f",S.name,&S.ID,&S.cgpa);
pkey = S.ID;
while(fread(&S,sizeof(S),1,fp1) == 1 ){
if(pkey == S.ID)
uniq = TRUE;
}
//rewind(fp1);
if(uniq != TRUE){
fwrite(&S,sizeof(S),1,fp1);
printf("Insertion Succesful\n");
}
else
printf(" The ID Already exists..Try with another ID \n");
}
else if(choice == 2){

printf("\t\t Enter Account number, College ID, balance\n");


scanf("%d%d%d",&M.Acno,&M.ID,&M.balance);
pkey = M.Acno;
while(fread(&M,sizeof(M),1,fp2)==1){
if(pkey == M.Acno)
uniq = TRUE;
}
if(uniq != TRUE){
fwrite(&M,sizeof(M),1,fp2);
printf("INsertion SUccesfull \n");
}
else
printf("The Acno already exist.. Try with another.\n");
}
else {
printf("\t\t Wrong choice\n");
return 0;
}
printf("\t\t Do you want to add another record :(y/n)");
nothing_input(stdin);
scanf("%c",&yes);
}while(yes != 'n');
return 0;
}
int operators(){

int choice;
char yes;

do {
printf("\n\t\t Choose the Operator : \n");
printf("\t\t 1. Projection\n");
printf("\t\t 2. Union \n");
printf("\t\t 3. Intersection \n");
printf("\t\t 4. Cartetian product \n");
printf("\t\t 5. Natural Join \n");

scanf("%d",&choice);
//PROJECTION
switch(choice){

case 1 :
projection();
break;
case 2:
uni_on();
break;
case 3:
intersection();
break;
case 4:
cartesian();
break;
case 5:
naturaljoin();
break;
default :
return 0;
}

printf("\n Do you another operation (y/n) : ");


nothing_input(stdin);
scanf("%c",&yes);
}while(yes == 'Y' || yes == 'y' );
return 0;
}
int projection(){
rewind(fp1);
rewind(fp2);
printf("\t****** STUDENT SCHEMA ******\n ");
printf("\t______________________________\n");
printf("\t Name \t ID\t Cgpa \n");
printf("\t______________________________\n");
while(fread(&S,sizeof(S),1,fp1) == 1 ){
printf("\t| %s | %d | %f | ",S.name,S.ID,S.cgpa);
printf("\n\t_ __ _ _ _ _ _ _ _ _ _ _ __\n");
printf("\n");
}
printf("\n\t***** SBI STUDENT SCHEMA ******\n");
printf("\t_____________________________\n");
printf("\t Acno \t ID \t balance \n");
printf("\t______________________________\n");
while(fread(&M,sizeof(M),1,fp2) == 1 ){
printf("\t| %d | %d | %d | ",M.Acno,M.ID,M.balance);
printf("\n\t_ __ _ _ _ _ _ _ _ _ _ _ __\n");
printf("\n");
}
return 0;
}
int cartesian(){ //CARTETION PRODUCT
rewind(fp1);
rewind(fp2);

printf("_______________________________________________________________");
printf("\n Name ID Cgpa A/c.no ID balance
\n");

printf("_______________________________________________________________");
printf("\n");
while(fread(&S,sizeof(S),1,fp1) == 1){
rewind(fp2);
while(fread(&M,sizeof(M),1,fp2) == 1){
printf("| %s | %d | %f | ",S.name,S.ID,S.cgpa);
printf(" %d | %d | %d |
",M.Acno,M.ID,M.balance);
printf("\n_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _");
printf("\n");
}
}
return 0;
}
int uni_on(){ //UNION
rewind(fp1);
rewind(fp2);
printf("\t\t ID \n");
while(fread(&S,sizeof(S),1,fp1) == 1 ){
printf("\n\t\t %d",S.ID);
}

while(fread(&M,sizeof(M),1,fp2) == 1){
check = 0;
while(fread(&S,sizeof(S),1,fp1) == 1){
if(M.ID == S.ID){
check=1;
break;
}
if(check != 1)
printf("%d",M.ID);
}
}
return 0;
}

int intersection(){ //INRERSECTION


rewind(fp2);
rewind(fp1);
printf("\t\t ID \n");
while(fread(&M,sizeof(M),1,fp2) == 1){
while(fread(&S,sizeof(S),1,fp1) == 1){
if(M.ID == S.ID){
printf("\t\t %d \n",M.ID);
break;
}
}
}
return 0;
}
int naturaljoin(){ //NATURAL JOIN
rewind(fp1);
rewind(fp2);
while(fread(&S,sizeof(S),1,fp1)==1){
rewind(fp2);
while(fread(&M,sizeof(M),1,fp2)==1){
if(S.ID == M.ID){

printf("\t__________________________________________________");
printf("\n\t ID | Name | cgpa | Acno | balance
\n");

printf("\t_________________________________________________\n");
printf("\t| %d | %s | %f | %d | %d |
\n",S.ID,S.name,S.cgpa,M.Acno,M.balance);
printf("\t_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _");
}
}

}
return 0;
}

You might also like