Saturday, January 30, 2010

CHAP 14[A](b) Newspaper Problem (Using bits and FILE I/O)

A company planning to launch a new newspaper in market conducts a survey. The various parameters considered in the survey were, the economic status (upper, middle, and lower class) the languages readers prefer (English, Hindi, Regional language) and category of paper (daily, supplement, tabloid). Write a program, which reads data of 10 respondents through keyboard, and stores the information in an array of integers. The bit-wise information to be stored in an integer is given below:

Bit Number Information

0 Upper class
1 Middle class
2 Lower class
3 English
4 Hindi
5 Regional Language
6 Daily
7 Supplement
8 Tabloid

At the end give the statistical data for number of persons who read English daily, number of upper class people who read tabloid and number of regional language readers.


/*Note: if your input is Upper,English,Tabloid the program does not work as intended*/
#include
void main()
{
FILE *fs,*ft;
struct res
{
char name[25];
char a;
};
struct res resp;
int i,choice,a,b,c;
char another='y',name2[25],j,ch;
fs=fopen("c:\\ctxt\\AAA.txt","r+");
clrscr();

while(1)
{
clrscr();
a=0,b=0,c=0;
printf("1)Add new respondent");
printf("\n2)Delete respondent");
printf("\n3)Display all the respondent's name");
printf("\n4)Give statistical data");
printf("\n5)Exit\n");
scanf("%d",&choice);

switch(choice)
{
case 1:

fseek(fs,0,SEEK_END);
while(another=='y')
{
resp.name[0]='\0';
clrscr();
printf("Key in respondent's name\n");
scanf("%s",resp.name);
clrscr();
printf("Choose your social status");
printf("\n1)Upper class\n2)Middle class\n3)Lower class\n");
scanf("%d",&a);
clrscr();
printf("Choose which language you prefer");
printf("\n1)English\n2)Hindi\n3)Regional Language\n");
scanf("%d",&b);
clrscr();
printf("Choose category of paper");
printf("\n1)Daily\n2)Supplement\n3)Tabloid\n");
scanf("%d",&c);
resp.a=funcgen(a,b,c);
fprintf(fs,"%s\t%c\n",resp.name,resp.a);
printf("\nAdd another record (Y/N)?\n");
fflush(stdin);
another=getche();
}
break;

case 2:

fseek(fs,0,SEEK_SET);
while(another=='y')
{
clrscr();
printf("\nKey in respondent's name to delete it's data\n");
scanf("%s",name2);
ft=fopen("c:\\ctxt\\AAA2.txt","w");
while(fscanf(fs,"%s %c",resp.name,&resp.a)!=EOF)
{
if(strcmp(resp.name,name2)!=0)
fprintf(ft,"%s %c\n",resp.name,resp.a);


}
fclose(fs);
fclose(ft);
remove("c:\\ctxt\\\AAA.txt");
rename("c:\\ctxt\\AAA2.txt","c:\\ctxt\\AAA.txt");
fs=fopen("c:\\ctxt\\AAA.txt","r+");
printf("Delete another record?(Y/N)");
fflush(stdin);
another=getche();
}
break;

case 3:

fseek(fs,0,SEEK_SET);
while(1)
{
i=0;
clrscr();
ch=0;
fseek(fs,0,SEEK_SET);
while(ch!=EOF)
{
resp.name[0]='\0';
ch=fscanf(fs,"%s %c",resp.name,&resp.a);
fflush(stdin);
if(ch==EOF)
continue;
i++;
printf("\n%d) %s",i,resp.name);
printf("\n");
printf(" Bit number is ");
showbits(resp.a);
}
fflush(stdin);
printf("\nKey q to quit\n");
another=getche();
if(another=='q')
break;
}
break;

case 4:

fseek(fs,0,SEEK_SET);
while(1)
{
clrscr();
a=0,b=0,c=0;
while(fscanf(fs,"%s %c",resp.name,&resp.a)!=EOF)
{
j=funcget(resp.a);
if(j==1)
a++;
if(j==2)
b++;
if(j==3)
c++;
}
printf("\nThere are %d people who read English daily\nThere are %d upper class people reading tabloid\nThere are %d regional readers",a,b,c);

printf("\nKey q to quit\n");
fflush(stdin);
another=getche();
if(another=='q')
break;
}
break;

case 5:

fclose(fs);
exit();
}

}
}

funcgen(a,b,c)
{
int d,e=0;
d=1<<(a-1);
e|=d;
d=8<<(b-1);
e|=d;
d=64<<(c-1);
e|=d;
return(e);
}

funcget(char r)
{
char d;
char mask1,mask2,mask3;
mask1=72;
mask2=257;
mask3=32;
d=mask1&r;
if(d==72)
return(1);
d=mask2&r;
if(d==257)
return(2);
d=mask3&r;
if(d==32)
return(3);
else
return(0);
}

showbits(char a)
{
int i;
char b,mask;

for(i=8;i>=0;i--)
{
mask=1< b=a&mask;
b==0?printf("0"):printf("1");
}
}

No comments:

Post a Comment