This is a Simple Progam that gets input from the user and then stores it in an array of structure made user difined data-type using Typedef. Then we give user choice regarding the way s/he wants to search the inputed data then we compare users choice with the data inserted earlier if the search item was found in the earlier data we display it.
Here is the Code:-
/*Program to search a structure for information*/
#include<stdio.h>
#include<string.h>
typedef struct {
char title[30];
char auther[30];
char pub[30];
}database;
void tf(int n, database a[20], char b[20]){
int i,found=0;
for(i=0; i<n; i++){
if(strcmp(a[i].title, b) == 0){
printf("---Match No. %i---\n",found+1);
printf("Book Title:\t%s\n", a[i].title);
printf("Book Auther:\t%s\n", a[i].auther);
printf("Book Publisher:\t%s\n", a[i].pub);
found++;
}
}
if(found==0){
printf("\n[X]No Matching Titles were found\n");
}
else {
printf("'%i' %s found", found,found>1?"Matchs":"Match");
}
}
void af(int n, database a[20], char b[20]){
int i,found=0;
for(i=0; i<n; i++){
if(strcmp(a[i].auther, b) == 0){
printf("---Match No. %i---\n",found+1);
printf("Book Title:\t%s\n", a[i].title);
printf("Book Auther:\t%s\n", a[i].auther);
printf("Book Publisher:\t%s\n", a[i].pub);
found++;
}
}
if(found == 0){
printf("\n[X]No Matching Auther was found\n");
}
else {
printf("'%i' %s found", found,found>1?"Matchs":"Match");
}
}
void pf(int n, database a[20], char b[20]){
int i, found=0;
for(i=0; i<n; i++){
if(strcmp(a[i].pub, b) == 0){
printf("---Match No. %i---\n",found+1);
printf("Book Title:\t%s\n", a[i].title);
printf("Book Auther:\t%s\n", a[i].auther);
printf("Book Publisher:\t%s\n", a[i].pub);
found++;
}
}
if(found == 0){
printf("\n[X]No Matching Publisher was found\n");
}
else {
printf("'%i' %s found", found,found>1?"Matchs":"Match");
}
}
int main(){
database a[20];
int n = 0, i = 0, choice = 0;
char s[20];
printf("Enter How many enteries you want: ");
scanf("%d", &n);
for(i=0; i<n; i++){
printf("---Enter Book [%i]---\n",i+1);
printf("Enter Title: ");
scanf("%s", a[i].title);
printf("Enter Auther: ");
scanf("%s", a[i].auther);
printf("Enter Publisher: ");
scanf("%s", a[i].pub);
}
printf("\nEnter Your Choice");
printf("\n1: for Title wise search\n"
"2: for Auther wise search\n"
"3: for Publisher wise search\n");
scanf("%i", &choice);
switch(choice) {
case 1:
printf("\nEnter Title to look for: ");
scanf("%s", s);
tf(n,a,s);
break;
case 2:
printf("\nEnter Auther to look for: ");
scanf("%s", s);
af(n,a,s);
break;
case 3:
printf("\nEnter Publisher to look for: ");
scanf("%s", s);
pf(n,a,s);
break;
default:
printf("\nWrong Choice");
}
return 0;
}
Comments
Post a Comment