Using the struct statement in C.
Structures in C.
Date Created:Thursday May 17th, 2007 04:07 PM
Date Modified:Sunday August 03rd, 2008 12:45 PM
Matin program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "structure.h"
int main()
{
struct xrec x[20];
FILE * infile;
int size = 0;
int com;
readNames( x, &size );
computeGrade( x, size );
do {
com=displayMenu();
switch(com) {
case 1 : printName(x, size); break;
case 2 : printRoses(x, size); break;
case 3 : printGrade(x, size); break;
case 4 : addName(x, &size);writeName(x, size ); break;
case 5 : deleteName( x, &size );writeName(x, size ); break;
case 6 : exit(1); break;
default: writeName( x, size );
}
} while ( com !=6 );
return 0;
}
Header file:
/*
* Author: Dan Lynch
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct xrec
{
char fname[15];
char ssn[12];
char gender;
double hw, t1, t2, final, avg;
char grade;
};
void computeGrade( struct xrec * a, int n) {
int i;
double average;
for ( i = 0; i < n; i++ ) {
average = a[i].hw*0.20 + a[i].t1*0.20 + a[i].t2*0.20 + a[i].final*0.40;
if ( average >= 90 )
a[i].grade='A';
else if ( average >= 80 )
a[i].grade='B';
else if ( average >= 70 )
a[i].grade='C';
else if ( average >= 60 )
a[i].grade='D';
else a[i].grade='F';
}
}
int search( struct xrec * a, int n, char * t ) {
int i=0;
for ( i = 0; i < n; i++ ) {
if ( strcmp( a[i].ssn, t) == 0 ) return 0;
else return 1;
}
return -1;
}
void deleteName( struct xrec * a, int * n ) {
char tssn[12];
int d;
printf("Enter SSN:");
fflush(stdin);
scanf("%s",tssn);
d=search(a,*n, tssn);
if (d < 0) printf("Does not exist\n");
else a[d]=a[--(*n)];
}
void writeName( struct xrec * a, int n ) {
int i;
FILE * infile;
infile = fopen("data.c", "w");
for ( i =0; i < n ; i++ ) fprintf(infile,"%14s %11s %c %lf %lf %lf %lf\n", a[i].fname, a[i].ssn, a[i].gender, a[i].hw, a[i].t1, a[i].t2, a[i].final);
fclose(infile);
printf("Saved\n");
}
void readNames( struct xrec * a, int * n ) {
struct xrec temp;
FILE * infile;
infile = fopen("data.c", "r");
if ( infile == NULL ) {
printf("File not found");
exit(1);
}
while ( (fscanf ( infile, "%14s %11s %c %lf %lf %lf %lf", temp.fname, temp.ssn, &temp.gender, &temp.hw, &temp.t1, &temp.t2, &temp.final ) )!= EOF ) {
temp.ssn[11]='\0';
temp.fname[14]='\0';
a[(*n)++]=temp;
}
fclose(infile);
}
void addName( struct xrec * a, int * n) {
struct xrec temp;
printf("Enter FirstName:");
fflush(stdin);
scanf("%s",temp.fname);
temp.fname[14]='\0';
printf("Enter SSN:");
fflush(stdin);
scanf("%s",temp.ssn);
temp.ssn[11]='\0';
printf("Enter Gender M/F:");
fflush(stdin);
scanf(" %c",&temp.gender);
printf("Enter Homework, test 1, test 2, and Final:");
scanf("%lf %lf %lf %lf", &temp.hw, &temp.t1, &temp.t2, &temp.final );
a[(*n)++]=temp; computeGrade(a, *n);
}
void printRoses(struct xrec * a, int n) {
int i;
for ( i = 0; i < n; i++ ) if ( a[i].gender == 'F' ) printf("%s\n",a[i].fname);
}
void printName(struct xrec * a, int n) {
int i;
for ( i = 0; i < n; i++ ) {
printf("%s \n", a[i].fname);
}
}
void printGrade(struct xrec * a, int n) {
int i;
for ( i = 0; i < n; i++ ) {
printf("%s %c\n", a[i].fname, a[i].grade);
}
}
int displayMenu() {
int c;
printf("1. list of students\n");
printf("2. list of roses\n");
printf("3. list of grades\n");
printf("4. add student to list\n");
printf("5. delete student from list\n");
printf("6. exit\n");
printf("Enter Your choice\n");
scanf("%d", &c);
return c;
}
This is the format for the data file that is read into the program:
Mike 786-94-8876 M 100.000000 100.000000 100.000000 100.000000
jack 123-12-1231 M 90.000000 90.000000 90.000000 100.000000
dan 899-23-2342 M 100.000000 100.000000 100.000000 100.000000
joe 383-23-2342 M 50.000000 50.000000 50.000000 100.000000
Newbi 345-55-3535 M 100.000000 100.000000 100.000000 100.000000
Downloads:
Download: data.c 364 B
Download: readnames.c 649 B
Download: structure.h 3 KB
Please login or Click Here to register for downloads
Struct by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
