Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct customer
{
char fname[20];
char lname[20];
char initial[3];
char socialnum[10];
double balance;
struct customer *next;
}CUSTOMER;
/****************************************************************************
PROGRAMMER : Eric Austin
LANGUAGE : C
CLASS : CSE 1320
PLATFORM : (OMEGA, UTARLG, CSE, etc.)
OS : (UNIX, VAX, Win Me, etc.)
COMPILER : g++ (for CSE 1325) or j2sdk (for CSE 4301)
ASSIGNMENT : Lab 5
ASSIGNED : week of Monday, March 25, 2002
DUE : week of Monday, April 15, 2002
FILED AS : eja7288Lab5.c
FILES USED : eja7288Lab5.h
CONCEPTS : structures,
file i/o,
command line parameters,
linked lists
WEIGHT : 8%
PURPOSE : Write a C program that will use a linked list of structures to
represent the customers of a bank.
****************************************************************************/
void printlist(CUSTOMER **bankrecords)
{
CUSTOMER *tempptr;
int counter=1;
tempptr = *bankrecords;
printf("NUM ADDRESS LAST FIRST MI SSN BALANCE NEXT\n");
printf("-------------------------------------------------------------------------\n\n");
while(tempptr!=NULL)
{
printf("%2d %5X %-14s%-12s%s %8s %10.2lf ",counter,tempptr,tempptr->lname,tempptr->fname,tempptr->initial,tempptr->socialnum,tempptr->balance);
if(tempptr->next==NULL)
printf(" NULL\n");
else
printf("%8X\n",tempptr->next);
tempptr=tempptr->next;
counter++;
}
printf("\n\n");
}
void addtolist(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *tempptr2;
if(*bankrecords==NULL)
{
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
tempptr->next=NULL;
}
else /* RECORDS ALREADY EXIST */
{
tempptr = *bankrecords;
while((tempptr->next!=NULL) && (strcmp(tempptr->next->lname,temprecord->lname)<0))
tempptr=tempptr->next;
if((*bankrecords==tempptr) && (strcmp(tempptr->lname,temprecord->lname)>0)) /* RECORD BELONGS AT START OF LIST */
{
tempptr2=tempptr;
*bankrecords = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr = *bankrecords;
}
else /* RECORD IS SOMEWHERE IN MIDDLE OR END */
{
tempptr2=tempptr->next;
tempptr->next = (CUSTOMER *) malloc(sizeof(CUSTOMER));
tempptr=tempptr->next;
}
tempptr->next=tempptr2;
}
/* COPY ALL DATA FROM TEMP RECORD TO NEW ONE */
strcpy(tempptr->fname,temprecord->fname);
strcpy(tempptr->lname,temprecord->lname);
strcpy(tempptr->initial,temprecord->initial);
strcpy(tempptr->socialnum,temprecord->socialnum);
tempptr->balance = temprecord->balance;
}
int findssn(CUSTOMER **bankrecords,CUSTOMER *temprecord)
{
CUSTOMER *tempptr;
CUSTOMER *prev;
prev=NULL;
tempptr = *bankrecords;
if(tempptr!=NULL)
while((tempptr!=NULL) && (atoi(tempptr->socialnum)!=atoi(temprecord->socialnum))) {
prev=tempptr;
tempptr=tempptr->next;
}
if(tempptr==NULL)
{
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is being added to the list with a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
addtolist(bankrecords,temprecord);
printlist(bankrecords);
return 0;
}
else
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf the account does not exist.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
printlist(bankrecords);
return 0;
}
}
if((temprecord->balance) > 0 )
{
printf("%s %s %s, %s, is making a deposit of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,temprecord->balance);
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if(((temprecord->balance) < 0 ) && ((fabs(temprecord->balance) < tempptr->balance)))
{
printf("%s %s %s, %s, is making a withdrawal of $%.2lf.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
tempptr->balance = tempptr->balance + temprecord->balance;
printlist(bankrecords);
return 0;
}
if((temprecord->balance < tempptr->balance))
{
printf("%s %s %s, %s, is attempting a withdrawal of $%.2lf, but does not have sufficient funds,you will be deleted from list.\n\n",temprecord->fname,temprecord->initial,
temprecord->lname,temprecord->socialnum,fabs(temprecord->balance));
if(tempptr == *bankrecords) {
tempptr = tempptr->next;
free(tempptr);
printlist(bankrecords);
}
else {
prev->next=tempptr->next;
free (tempptr);
printlist(bankrecords);
}
return 0;
}
}
int main()
{
FILE *infile,*infile2;
CUSTOMER *bankrecords;
CUSTOMER *secondlist=NULL;
int test;
CUSTOMER temprecord;
bankrecords=NULL;
infile=fopen("data.dat","r");
if(infile == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
/* READ IN FILE TO THE LIST */
while(!feof(infile))
{
fscanf(infile,"%s %s %s %s %lf",temprecord.lname,temprecord.fname,(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
addtolist(&bankrecords,&temprecord);
printlist(&bankrecords);
}
}
infile2=fopen("data_2.dat","r");
if(infile2 == NULL)
{
printf("Data file could not be opened.\n");
}
else
{
while(!feof(infile2))
{
fscanf(infile2,"%s %s %s %s %lf",temprecord.lname,temprecord.fname,(temprecord.initial),temprecord.socialnum,&(temprecord.balance));
test=findssn(&bankrecords,&temprecord);
}
}
return 0;
}