Wednesday 30 October 2013

A database written in C that implements a quick sort and a binary chop search.

#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];
}addr_list[MAX];

struct addr temp;
struct addr comp;
int counter;
int lend=1;
void init_list(void), enter(void);
void delete(void), list(void);
void load(void), save(void), sort(int left, int right);
void txtoutput(void);
void search(void);
void readLine(char buffer[]);
void swap(int i, int j);
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.  Output to a text file for printing or import into wp\n");
printf("9.  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(1, counter);
if(choice==7) search();
if(choice== 8) txtoutput();
if(choice== 9) exit(0);
}while(choice < 1 && choice >9);
}
return 0;
}

void init_list(void)
{
register int t;

for(t=0; t<MAX; ++t) addr_list[t].name[0] ='\0';
}

void enter(void)
{
char s[40];
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';
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(int left, int right)
{
int i, last;
i=left;
if(left>=right) /*do nothing if array contains less than*/
return;        /*two elements*/

swap(left, (left+right)/2); /*move partition element to*/
last = left;               /*addr_list[0]*/
for(i=left; i<=right; i++) /*partition*/
if(strcmp(addr_list[i].name,addr_list[left].name)<0)
swap(++last, i);
swap(left, last);         /*restore partition element*/
sort(left, last - 1);
sort(last+1, right);
}
void swap(int i, int j)
{
temp = addr_list[i];
addr_list[i]=addr_list[j];
addr_list[j]=temp;
i++; j--;
}


void save(void)
{
FILE *fp;
register int i;

if((fp=fopen("maillist", "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; 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 loadbackup(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");

if((fp=fopen(filename, "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 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.\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");
}
}
fclose(stream);
return;
}



void outputtwo(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; 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 search(void)
{

int low, mid, top, found,count,countdown;
char s[40];
printf("\nEnter a name.\n");
readLine(s);
low = 1;
top = counter;
do
{
found = 0;
mid = (low + top)/2;


if (strcmp(s, addr_list[mid].name)==0)
{
printf("\nRecord number: %i\n", mid);
printf("\nName: %s\n",addr_list[mid].name);
printf("\nStreet: %s\n",addr_list[mid].street);
printf("\nDistrict: %s\n",addr_list[mid].district);
printf("\nTown: %s\n",addr_list[mid].town);
printf("\nCounty: %s\n",addr_list[mid].county);
printf("\nCountry: %s\n",addr_list[mid].country);
printf("\nPostcode:%s\n",addr_list[mid].postcode);
printf("\nTelephone number: %s\n",addr_list[mid].telno);
printf("\nEmail address: %s\n",addr_list[mid].email);
printf("\n");
/*break;*/
count = mid;
countdown = mid;

do
{
countdown=countdown-1;
if(strcmp(s, addr_list[countdown].name)==0)
{
printf("\nRecord number: %i\n", countdown);
printf("\nName: %s\n",addr_list[countdown].name);
printf("\nStreet: %s\n",addr_list[countdown].street);
printf("\nDistrict: %s\n",addr_list[countdown].district);
printf("\nTown: %s\n",addr_list[countdown].town);
printf("\nCounty: %s\n",addr_list[countdown].county);
printf("\nCountry: %s\n",addr_list[countdown].country);
printf("\nPostcode:%s\n",addr_list[countdown].postcode);
printf("\nTelephone number: %s\n",addr_list[countdown].telno);
printf("\nEmail address: %s\n",addr_list[countdown].email);
printf("\n");
}
}while(strcmp(s,addr_list[countdown].name)==0);

do{
count=count +1;
if(strcmp(s, addr_list[count].name)==0)
{
printf("\nRecord number: %i\n", count);
printf("\nName: %s\n",addr_list[count].name);
printf("\nStreet: %s\n",addr_list[count].street);
printf("\nDistrict: %s\n",addr_list[count].district);
printf("\nTown: %s\n",addr_list[count].town);
printf("\nCounty: %s\n",addr_list[count].county);
printf("\nCountry: %s\n",addr_list[count].country);
printf("\nPostcode:%s\n",addr_list[count].postcode);
printf("\nTelephone number: %s\n",addr_list[count].telno);
printf("\nEmail address: %s\n",addr_list[count].email);
printf("\n");
}
}while(strcmp(s,addr_list[count].name)==0);

return;
}
else
if(strcmp(s, addr_list[mid].name)<0)
top = mid -1;
else
if(strcmp(s, addr_list[mid].name)>0)
low = mid + 1;
}while(low<=top);
}
void readLine(char buffer[])
{
char character;
int i=0;
do
{
character=getchar();
buffer[i]=character;
++i;
}
while(character !='\n');
buffer[i-1]='\0';
}

Tuesday 6 August 2013

A simpler C database to organise your contacts.

#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];
}addr_list[MAX];

struct addr temp;
int counter;
void init_list(void), enter(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);
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.  Output to a text file for printing or import into wp\n");
printf("9.  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) txtoutput();
if(choice== 9) exit(0);
}while(choice < 1 && choice >9);
}
return 0;
}

void init_list(void)
{
register int t;

for(t=0; t<MAX; ++t) addr_list[t].name[0] ='\0';
}

void enter(void)
{
char s[40];
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';
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<MAX; 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 loadbackup(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");

if((fp=fopen(filename, "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 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.\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");
}
}
fclose(stream);
return;
}



void outputtwo(void)
{
FILE *fp;
register int i;
char filename[30];
printf("Enter a file name.\n");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename, "wb"))==NULL) {
printf("Cannot open file.\n");
return;
}

for(i=1; i<MAX; 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 search(void)
{
int i;
int count;
count=0;
char s[40];
printf("\nEnter a name.\n");
readLine(s);
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';
}

Monday 5 August 2013

Pascal database with output for printer

const maxsize = 99;
type
address = record
name : string[40];
street : string[40];
town : string[40];
county : string[40];
postcode : string[40];
telephone : string[40];
email : string[40];
end;
type personinfo = array[1..maxsize] of address;
var
details : personinfo;
diskfile : file of address;
txtfile : file of string;
output : text;
counter : integer;
choice : integer;
 
procedure save;
var
loop : integer;
begin
assign(diskfile,'address.data');
rewrite(diskfile);
for loop := 1 to counter do
write(diskfile,details[loop]);
close(diskfile);
end;
procedure printrecords;
var
loop : integer;
begin
assign(output,'printout.prn');
rewrite(output);
for loop := 1 to counter do
with details[loop] do
begin
writeln(output,details[loop].name);
writeln(output,details[loop].street);
writeln(output,details[loop].town);
writeln(output,details[loop].county);
writeln(output,details[loop].postcode);
writeln(output,details[loop].telephone);
writeln(output,details[loop].email);
writeln(output);
writeln(output);
end;
close(output);
end;
 
 
procedure load;
begin
assign(diskfile,'address.data');
reset(diskfile);
while not eof(diskfile) do
begin
counter := counter + 1;
read(diskfile, details[counter]);
end;
close(diskfile);
end;
procedure newaddress;
begin
counter :=counter + 1;
with details[counter] do
begin
write('Enter name: ');
readln(name);
write('Enter street: ');
readln(street);
write('Enter town / city: ');
readln(town);
write('Enter county: ');
readln(county);
write('Enter postcode: ');
readln(postcode);
write('Enter telephone number: ');
readln(telephone);
write('Enter email address: ');
readln(email);
end;
end;
 
 
procedure display;
var
loop : integer;
begin
for loop := 1 to counter do
begin
with details[loop] do
begin
writeln('RECORD NO. ',loop);
writeln(name);
writeln(street);
writeln(town);
writeln(county);
writeln(postcode);
writeln(telephone);
writeln(email);
writeln('');
end;
end;
end;
procedure search;
var
loop : integer;
target : string[40];
begin
writeln('Enter name to search for: ');
readln(target);
for loop := 1 to counter do
if details[loop].name = target then
with details[loop] do
begin
writeln('RECORD NO. ',loop);
writeln(name);
writeln(street);
writeln(town);
writeln(county);
writeln(postcode);
writeln(telephone);
writeln(email);
writeln('');
end;
end;
procedure delete;
var
target : integer;
loop : integer;
begin
write('Record number? ');
readln(target);
if(target>0) and (target<counter)
then begin
for loop := target to counter - 1 do
details[loop] := details[loop+1];
counter := counter -1;
end;
end;
procedure sort;
var
noswaps : boolean;
current : address;
x : integer;
begin
repeat
noswaps := true;
for x := 1 to counter -1 do
begin
if details[x].name > details[x + 1].name then
begin
current := details[x];
details[x] := details[x+1];
details[x+1] := current;
noswaps := false;
end;
end;
until noswaps;
end;
 
begin
counter:= 0;
{load;}
writeln('Address details');
repeat
{while choice <> '6' do
begin}
writeln('Load file 1');
writeln('New address 2');
writeln('Display addresses 3');
writeln('Select by name 4');
writeln('Delete record 5');
writeln('Sort records 6');
writeln('Save 7');
writeln('Save records to file for printing 8');
writeln('Exit 9');
writeln('Choose ...');
readln(choice);
{case choice of
'1' : load;
'2' : newcd;
'3' : display;
'4' : search;
'5' : delete;
'6' : sort;
'7' : save;
end;}
if choice = 1 then load;
if choice = 2 then newaddress;
if choice = 3 then display;
if choice = 4 then search;
if choice = 5 then delete;
if choice = 6 then sort;
if choice = 7 then save;
if choice = 8 then printrecords;
until choice = 9;
{end;}
end.