#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000
struct addr {
char name[40];
char street[40];
char district[40];
char town[40];
char county[40];
char country[40];
char postcode[40];
char telno[40];
char email[40];
int phone;
int mail;
int Email;
int visit;
}addr_list[MAX];
struct addr temp;
int counter;
void init_list(void), enter(void);
void booklist(void);
void delete(void), list(void);
void load(void), save(void), sort(void);
void txtoutput(void);
void search(void);
void readLine(char buffer[]);
void swap(int u, int v);
void addresponse(void), contactresponse(void);
void listresponses(void);
void searchclient(void);
void listtotalresponses(void), deleteresponses(void);
void saveresppipe(void);
int main(void)
{
int choice;
char s[3];
counter = 0;
init_list();
for(;;){
printf("\n");
printf("1. Create a record\n");
printf("2. Delete a record\n");
printf("3. List the file\n");
printf("4. Save the file\n");
printf("5. Load the file\n");
printf("6. Sort the file\n");
printf("7. Search\n");
printf("8. Response information for contact\n");
printf("9. Output to a text file .txt for printing or import into wp\n");
printf("10. Quit\n");
do{
printf("\nEnter your choice: ");
readLine(s);
choice=atoi(s);
if(choice== 1) enter();
if(choice==2) delete();
if(choice==3) list();
if(choice==4) save();
if(choice==5) load();
if(choice==6) sort();
if(choice==7) search();
if(choice==8) contactresponse();
if(choice== 9) txtoutput();
if(choice== 10) exit(0);
}while(choice < 1 && choice >10);
}
return 0;
}
void init_list(void)
{
register int t;
for(t=0; t<MAX; ++t) addr_list[t].name[0] ='\0';
}
void enter(void)
{
int i;
for(i=1; i<MAX; i++)
if(!*addr_list[i].name)
break;
if(i==MAX)
{
printf("Subscriber array full\n");
return;
}
printf("Enter information on new customer\n");
printf("Enter name: ");
readLine(addr_list[i].name);
printf("Enter street: ");
readLine(addr_list[i].street);
printf("Enter district / parish: ");
readLine(addr_list[i].district);
printf("Enter town / city: ");
readLine(addr_list[i].town);
printf("Enter county: ");
readLine(addr_list[i].county);
printf("Enter country: ");
readLine(addr_list[i].country);
printf("Enter postcode: ");
readLine(addr_list[i].postcode);
printf("Enter telephone number: ");
readLine(addr_list[i].telno);
printf("Enter email address: ");
readLine(addr_list[i].email);
counter=counter+1;
}
void delete(void)
{
int t;
int u;
printf("enter a record number #: ");
scanf("%i", &t);
getchar();
for(u=t; u<=counter; u++){
addr_list[u] = addr_list[u+1];
}
addr_list[counter].name[0] = '\0';
addr_list[counter].street[0] = '\0';
addr_list[counter].district[0] = '\0';
addr_list[counter].town[0] = '\0';
addr_list[counter].county[0] = '\0';
addr_list[counter].country[0] = '\0';
addr_list[counter].postcode[0] = '\0';
addr_list[counter].telno[0] = '\0';
addr_list[counter].email[0] = '\0';
addr_list[counter].phone='\0';
addr_list[counter].mail='\0';
addr_list[counter].Email='\0';
addr_list[counter].visit='\0';
counter = counter -1;
}
void list(void)
{
register int t;
int count;
count=0;
for(t=1; t<=counter; t++) {
count = count +1;
printf("Record number: %i\n",count);
printf("%s\n", addr_list[t].name);
printf("%s\n", addr_list[t].street);
printf("%s\n", addr_list[t].district);
printf("%s\n", addr_list[t].town);
printf("%s\n", addr_list[t].county);
printf("%s\n", addr_list[t].country);
printf("%s\n", addr_list[t].postcode);
printf("%s\n", addr_list[t].telno);
printf("%s\n", addr_list[t].email);
printf("\n");
}
}
void sort(void)
{
register int u;
register int v;
for(u=1; u<counter; u++)
for (v=u+1; v<=counter; v++){
if( strcmp(addr_list[u].name, addr_list[v].name) >0){
swap(u, v);
}
}
}
void swap(int u, int v)
{
temp = addr_list[u];
addr_list[u]=addr_list[v];
addr_list[v]=temp;
}
void save(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}
for(i=1; i<=counter; i++){
if(*addr_list[i].name)
if(fwrite(&addr_list[i],
sizeof(struct addr), 1, fp)!=1)
printf("File write error.\n");
}
fclose(fp);
}
void load(void)
{
FILE *fp;
register int i;
if((fp=fopen("maillist", "rb"))==NULL) {
printf("Cannot open file,\n");
return;
}
init_list();
for(i=1; i<MAX; i++){
if(fread(&addr_list[i],
sizeof(struct addr), 1, fp)!=1) {
if(feof(fp)) break;
printf("File read error.\n");
}
counter=counter +1;
}
fclose(fp);
}
void txtoutput(void)
{
int t;
int count;
FILE *stream;
char filename[30];
count = 0;
printf("Enter a file name such as filename.txt.\n");
scanf("%s",filename);
printf("\n");
stream = fopen(filename, "w+");
for(t=1; t<MAX; t++) {
if(addr_list[t].name[0]) {
count = count +1;
fprintf(stream,"%i\n",count);
fprintf(stream,"%s\n", addr_list[t].name);
fprintf(stream,"%s\n", addr_list[t].street);
fprintf(stream,"%s\n", addr_list[t].district);
fprintf(stream,"%s\n", addr_list[t].town);
fprintf(stream,"%s\n", addr_list[t].county);
fprintf(stream,"%s\n", addr_list[t].country);
fprintf(stream,"%s\n", addr_list[t].postcode);
fprintf(stream,"%s\n", addr_list[t].telno);
fprintf(stream,"%s\n", addr_list[t].email);
fprintf(stream,"\n");
fprintf(stream,"Phone calls %i\n",addr_list[t].phone);
fprintf(stream,"Mail %i\n",addr_list[t].mail);
fprintf(stream,"Emails %i\n",addr_list[t].Email);
fprintf(stream,"Visits %i\n",addr_list[t].visit);
fprintf(stream,"\n");
}
}
fclose(stream);
return;
}
void search(void)
{
int i;
int count;
count=0;
char s[40];
printf("\nEnter a name.\n");
readLine(s);
if(strlen(s)!=0)
{
for(i=1;i<MAX;i++)
{
if(addr_list[i].name[0]) {
count = count +1;
}
if (strcmp(s, addr_list[i].name)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",addr_list[i].name);
printf("\nStreet: %s\n",addr_list[i].street);
printf("\nDistrict: %s\n",addr_list[i].district);
printf("\nTown: %s\n",addr_list[i].town);
printf("\nCounty: %s\n",addr_list[i].county);
printf("\nCountry: %s\n",addr_list[i].country);
printf("\nPostcode:%s\n",addr_list[i].postcode);
printf("\nTelephone number: %s\n",addr_list[i].telno);
printf("\nEmail address: %s\n",addr_list[i].email);
printf("\n");
}
}
}
}
void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}
void contactresponse(void)
{
int choice;
char s[3];
do{
printf("\n");
printf("1. List the records\n");
printf("2. Add a response\n");
printf("3. List the responses for a contact\n");
printf("4. Delete the responses for a contact\n");
printf("5. List total number of responses for all contacts\n");
printf("6. Save responses to a filename.dat file for piping to Python\n");
printf("7. Quit\n");
printf("\nEnter your choice: ");
readLine(s);
choice=atoi(s);
if(choice==1) list();
if(choice== 2) searchclient();
if(choice==3) listresponses();
if(choice==4) deleteresponses();
if(choice==5) listtotalresponses();
if(choice==6) saveresppipe();
if(choice==7) break;
}while(choice>=1 && choice <= 6);
}
void listresponses()
{
int recno;
char s[40];
printf("\nEnter a record number.\n");
readLine(s);
recno=atoi(s);
printf("\nRecord number: %i\n", recno);
printf("\nName: %s\n",addr_list[recno].name);
printf("\nStreet: %s\n",addr_list[recno].street);
printf("\nDistrict: %s\n",addr_list[recno].district);
printf("\nTown: %s\n",addr_list[recno].town);
printf("\nCounty: %s\n",addr_list[recno].county);
printf("\nCountry: %s\n",addr_list[recno].country);
printf("\nPostcode:%s\n",addr_list[recno].postcode);
printf("\nTelephone number: %s\n",addr_list[recno].telno);
printf("\nEmail address: %s\n",addr_list[recno].email);
printf("\n");
printf("\nPhone: %i",addr_list[recno].phone);
printf("\nMail: %i",addr_list[recno].mail);
printf("\nEmail: %i",addr_list[recno].Email);
printf("\nVisit: %i",addr_list[recno].visit);
}
void searchclient(void)
{
int record;
int choice;
char s[40], t[40];
printf("\nEnter a record number.\n");
readLine(s);
record = atoi(s);
printf("\nRecord number: %i\n", record);
printf("\nName: %s\n",addr_list[record].name);
printf("\nStreet: %s\n",addr_list[record].street);
printf("\nDistrict: %s\n",addr_list[record].district);
printf("\nTown: %s\n",addr_list[record].town);
printf("\nCounty: %s\n",addr_list[record].county);
printf("\nCountry: %s\n",addr_list[record].country);
printf("\nPostcode:%s\n",addr_list[record].postcode);
printf("\nTelephone number: %s\n",addr_list[record].telno);
printf("\nEmail address: %s\n",addr_list[record].email);
printf("\n");
do
{
printf("1. Phone. ");
printf("2. Mail. ");
printf("3. Email. ");
printf("4. Visit. ");
printf("5. Exit. ");
printf("Enter choice ...");
readLine(t);
choice=atoi(t);
if(choice== 1) addr_list[record].phone = addr_list[record].phone+1;
if(choice==2) addr_list[record].mail = addr_list[record].mail+1;
if(choice==3) addr_list[record].Email = addr_list[record].Email+1;
if(choice==4)addr_list[record].visit = addr_list[record].visit+1;
if(choice== 5) break;
}while(choice>=1 && choice <= 5);
}
void listtotalresponses()
{
int k, totalphone, totalmail, totalEmail, totalvisits;
totalphone=0;
totalmail=0;
totalEmail=0;
totalvisits = 0;
for(k=1; k<= counter; k++){
totalphone=totalphone +addr_list[k].phone;
totalmail = totalmail +addr_list[k].mail;
totalEmail =totalEmail + addr_list[k].Email;
totalvisits = totalvisits + addr_list[k].visit;
}
printf("Total number of phone calls: %i\n",totalphone);
printf("Total number of mail: %i\n",totalmail);
printf("Total number of Emails: %i\n",totalEmail);
printf("Total number of visits: %i\n",totalvisits);
}
void deleteresponses()
{
int recno;
char s[40];
printf("\nEnter a record number.\n");
readLine(s);
recno=atoi(s);
printf("\nRecord number: %i\n", recno);
printf("\nName: %s\n",addr_list[recno].name);
printf("\nStreet: %s\n",addr_list[recno].street);
printf("\nDistrict: %s\n",addr_list[recno].district);
printf("\nTown: %s\n",addr_list[recno].town);
printf("\nCounty: %s\n",addr_list[recno].county);
printf("\nCountry: %s\n",addr_list[recno].country);
printf("\nPostcode:%s\n",addr_list[recno].postcode);
printf("\nTelephone number: %s\n",addr_list[recno].telno);
printf("\nEmail address: %s\n",addr_list[recno].email);
printf("\n");
printf("\nPhone: %i",addr_list[recno].phone);
printf("\nMail: %i",addr_list[recno].mail);
printf("\nEmail: %i",addr_list[recno].Email);
printf("\nVisit: %i",addr_list[recno].visit);
printf("\n");
addr_list[recno].phone='\0';
addr_list[recno].mail ='\0';
addr_list[recno].Email='\0';
addr_list[recno].visit='\0';
printf("\nNew values for responses for record: %i\n",recno);
printf("\nPhone: %i",addr_list[recno].phone);
printf("\nMail: %i",addr_list[recno].mail);
printf("\nEmail: %i",addr_list[recno].Email);
printf("\nVisit: %i",addr_list[recno].visit);
printf("\n");
}
void saveresppipe()
{
int t;
int count;
FILE *stream;
char filename[30];
count = 0;
printf("Enter a file name such as filename.dat\n");
scanf("%s",filename);
printf("\n");
stream = fopen(filename, "w+");
fprintf(stream,"%i\n",counter);
for(t=1; t<MAX; t++) {
if(addr_list[t].name[0]) {
count = count +1;
fprintf(stream,"%i\n",addr_list[t].phone);
fprintf(stream,"%i\n",addr_list[t].mail);
fprintf(stream,"%i\n",addr_list[t].Email);
fprintf(stream,"%i\n",addr_list[t].visit);
}
}
fclose(stream);
return;
}
Sunday, 14 February 2021
C database to implement an AT-AT predicate with contacts
Subscribe to:
Posts (Atom)