typedef struct height HEIGHT;
struct height
{
char *name;
int m;
int cm;
};
bobheight = { "Bob Smith", 1 , 83 };
or :
bobheight.name = "Bob Smith";
bobheight.m = 1;
bobheight.cm = 83;
typedef struct heights HEIGHTS;
struct heights
{
char *name;
int m;
int cm;
HEIGHTS *tail;
};
typedef struct point POINT; typedef struct list LIST;
struct point struct list
{ {
double x,y; POINT p;
char *label; LIST *tail;
}; };
LIST *insert ( LIST *l, POINT p )
{
LIST *newl;
newl = (LIST*)calloc(1,sizeof(LIST));
newl->p = p;
newl->tail = l;
return new;
}
LIST *append ( LIST *first, LIST *last, POINT p )
{
LIST *newp;
newp = (LIST*)calloc(1,sizeof(LIST));
newp->p = p;
newp->tail = NULL;
if (first == NULL)
last = newp;
else
{
last->tail = newp;
last = newp;
}
return last;
}
The main program to illustrate this append :
#include <stdio.h>
int main()
{
LIST *first,*last;
POINT p;
int n;
first = NULL;
last = NULL;
for (;;)
{
p.label = (char*)calloc(80,sizeof(char));
printf(" Give a point and label : ");
n = scanf("%f%f%s", &p.x, &p.y, p.label);
if (n < 3)
break;
else
{
last = append(first,last,p);
if (first == NULL) first = last;
}
}
}
LIST *insert ( LIST *first, *second, POINT p )
{
LIST *new2nd;
new2nd = (LIST*)calloc(1,sizeof(LIST));
new2nd.p = p;
new2nd->tail = second;
first->tail = new2nd;
return new2nd;
}
A recursive version :
int Count ( LIST *l )
{
if (l == NULL)
return 0;
else
return 1 + Count(l);
}
An iterative version :
int Count ( LIST *l )
{
LIST *p;
int cnt = 0;
for (p = l; p != NULL; p = p->tail)
cnt++;
return cnt;
}
An iterative implementation :
POINT LeftMost ( LIST *l )
{
POINT min;
LIST *ptr;
min.x = 1.0E+99;
for (ptr = l; ptr != NULL; ptr = ptr->tail)
if ((ptr->p).x < min.x)
min = ptr->p;
return min;
}
A recursive implementation :
POINT LeftMost ( LIST *l )
{
POINT min,mintail;
min.x = 1.0E+99;
if (l == NULL)
return min;
else
{
mintail = LeftMost(l->tail);
if ((l->p).x < mintail.x)
return l->p;
else
return mintail;
}
}
An iterative version :
void Print_Labels ( LIST *l )
{
LIST *p;
for (p = l; p != NULL; p = p->tail)
printf("%s\n", (p->p).label);
}
A recursive verson :
void Print_Labels ( LIST *l )
{
if (l != NULL)
{
printf("%s\n", (l->p).label);
Print_Labels(l->tail);
}
}
An iterative implementation :
char *Find_Label ( LIST *l, double x, double y )
{
LIST *ptr;
char *result;
for (ptr = l; ptr != NULL; ptr = ptr->tail)
if ( ( (ptr->p).x == x ) && ( (ptr->p).y == y ) )
return (ptr->p).label;
result = (char*)calloc(1,sizeof(char));
result[0] = '\0';
return result;
}
A recursive implementation :
char *Find_Label ( LIST *l, double x, double y )
{
char *result;
if (l == NULL)
{
result = (char*)calloc(1,sizeof(char));
result[0] = '\0';
return result;
}
else
if ( ( (l->p).x == x ) && ( (l->p).y == y ) )
return (l->p).label;
else
return Find_Label(l->tail,x,y);
}
An iterative version :
int Position ( LIST *l; POINT p )
{
LIST *ptr;
int pos = -1;
int i = 0;
for (ptr = l; ptr != NULL; ptr = ptr->tail)
{
if (((ptr->p).x == p.x) && ((ptr->p).y == p.y)
&& (strcmp((ptr->p).label,p.label)==0))
pos = i;
i++;
}
return pos;
}
The recursive version :
int Position ( LIST *l; POINT p; int pos )
{
if (l == NULL)
return -1;
else
if (((l->p).x == p.x) && ((l->p).y == p.y)
&& (strcmp((l->p).label,p.label)==0))
return pos;
else
return Position(l->tail,p,pos+1);
}
int Relabel ( LIST *l, double x, double y, char *label )
{
LIST *ptr;
int i = 0;
for (ptr = l; ptr != NULL; ptr = ptr->tail)
{
if ( (ptr->p).x == p.x) && ((ptr->p).y == p.y) )
{
(ptr->p).label = strcpy((ptr->p).label,p.label);
return i;
}
i++;
}
return -1;
}
LIST *delete ( LIST *first, LIST *second )
{
first->tail = second->tail;
free(second);
return first-> tail;
}
void delete ( LIST *l )
{
if (l != NULL)
{
delete(l->tail);
free((l->p).label);
free(l);
}
}
#include <stdio.h>
int main ( int argc, char* argv[] )
{
FILE *myfile;
char c = 'y';
myfile = fopen(argv[1],"r");
if (myfile != NULL)
{
printf("File %s exists already. Overwrite ? (y/n) ", argv[1]);
scanf("%c", &c);
fclose(myfile);
}
if (c == 'y')
{
myfile = fopen(argv[1],"w");
fprintf(myfile,"%30s","my name");
}
return 0;
}
Suppose we work with string name :
1. scanf("%[^\n]", name);
2. scanf("%[^,]", name);
3. scanf("%*[^,], %[^\n]", name);
Smith, Bob 34023.23
Little, Ann 56123.32
where the names occupy a field of 30 characters wide and
salaries are floats written with field width 8, precision 2.
In the questions below you may assume the file has been appropriately
opened for the necessary operations.
void Salaries ( FILE *sal )
{
char name[31];
float pay;
while (fscanf(sal,"%30[^\n] %f\n", name, &pay) == 2)
printf("%-30s %8.2f\n", name, pay);
}
char *BestPaid (FILE *sal )
{
char name[31];
float pay;
float bestpay = 0.0;
char *best;
best = (char*)calloc(31,sizeof(char));
while (fscanf(sal,"%30[^\n] %f\n", name, &pay) == 2)
{
if (pay > bestpay)
{
best = strcpy(best,name);
bestpay = pay;
}
}
return best;
}
FILE *TopSal ( FILE *sal, char *name, float s )
{
char persname[31];
float pay;
FILE *topsals;
topsals = fopen(name,"w");
while (fscanf(sal,"%30[^\n] %f\n", persname, &pay) == 2)
{
if (pay > s)
fprintf(topsals,"%-30s %8.2f\n", persname, pay);
}
return topsals;
}
void Split ( FILE *sal, float s )
{
FILE *low,*upp;
char name[31];
float pay;
low = tmpfile();
upp = tmpfile();
while (fscanf(sal,"%30[^\n] %f\n", name, &pay) == 2)
if (pay < s)
fprintf(low,"%-30s%8.2f\n", name, pay);
else
fprintf(upp,"%-30s%8.2f\n", name, pay);
rewind(sal);
rewind(low);
rewind(upp);
while (fscanf(low,"%30[^\n] %f\n", name, &pay) == 2)
fprintf(sal,"%-30s%8.2f\n", name, pay);
while (fscanf(upp,"%30[^\n] %f\n", name, &pay) == 2)
fprintf(sal,"%-30s%8.2f\n", name, pay);
}
int Change ( FILE *sal, char *name, float newsal )
{
char persname[31];
float pay;
while (fscanf(sal,"%30[^\n] %f", persname, &pay) == 2)
{
if (strcmp(name,persname) == 0)
{
fseek(sal,-8,1);
fprintf(sal, "%8.2f", newsal);
return 0;
}
else
fscanf(sal,"\n");
}
return -1;
}
void PayRaise ( FILE *sal, float percent )
{
char name[31];
float pay;
while (fscanf(sal,"%30[^\n] %f", name, &pay) == 2)
{
pay = pay + percent*pay;
fseek(sal,-8,1);
fprintf(sal, "%8.2f", pay);
fscanf(sal,"\n"); /* skip new line */
}
}
FINAL EXAM in LC C3 on Monday 30 April 2001 at 1:00-3:00PM.