Vous êtes sur la page 1sur 4

Java Algorithms and Discrete Computations Project, 2nd Year

Problem: Develop a new class "student" that can extract data from a text file co
ntaining the student's id number and course marks. The class should implement th
e following algorithms for accessing data: linear search, binary search, backwar
ds print, withdraw data, and a list clear.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct { int studid; int project; int exam; float finalgrade;} student;
student **create_class_list(char *filename, int *sizePtr);
int find_linsrch(int idNo, student **list, int size);
int find_binsrch(int idNo, student **list, int size);
void input_grades(char *filename, student **list, int size);
void compute_final_course_marks(student **list, int size);
void output_final_course_mark(student **list, int size);
void print_backwards(student **list, int x, int size);
void withdraw(int idNo, student **list, int *sizePtr);
void destroy_list(student **list, int *size);
student **create_class_list(char *filename, int *sizePtr)
{
int x=0;
int n=0;
*sizePtr;
FILE* infile = fopen("I:\\2SH4 - Lab 4\\new\\input.txt", "r");
fscanf(infile,"%d",&n);
student **classList = calloc(sizeof (student*), (n));
while (x<n)
{
classList[x] = (student*)calloc(1, sizeof (student));
fscanf(infile, "%d", &classList[x]->studid);
x++;
}
*sizePtr = n;
fclose(infile);
return (classList);
}
int find_linsrch(int idNo, student **list, int size)
{
int x=0;
while (x<size)
{
if (idNo == (*list[x]).studid)
{
return x;
}
x++;
}
return -1;

}
int find_binsrch(int idNo, student **list, int size)
{
int low=0;
int mid;
int high=size;
printf("Using binary search, the following indices were accessed: ");
while (low <= high)
{
mid = (low+high)/2;
if(idNo == (*list[mid]).studid)
{
printf("%d ",mid);
printf("\n");
return mid;
}
if(idNo > (*list[mid]).studid)
{
printf("%d ",mid);
low = mid + 1;
}
else if(idNo < (*list[mid]).studid)
{
printf("%d ",mid);
high = mid-1;
}
}
printf("\n");
return -1;
}
void input_grades(char *filename, student **list, int size)
{
int x=0;
int position;
int temp;
FILE* infile = fopen("I:\\2SH4 - Lab 4\\new\\grades.txt", "r");
while (x<size)
{
fscanf(infile, "%d", &temp);
position = find_linsrch(temp, list, size);
fscanf(infile, "%d", &list[position]->project);
fscanf(infile, "%d", &list[position]->exam);
x++;
}
fclose(infile);
}
void compute_final_course_marks(student **list, int size)
{
int x=0;
while (x<size)
{
(*list[x]).finalgrade = (.6)*((*list[x]).exam)+(.4)*((*list[x]).project)
;

x++;
}
}
void output_final_course_mark(student **list, int size)
{
int x=0;
FILE *output = fopen("I:\\2SH4 - Lab 4\\new\\output.txt", "w");
fprintf(output, "ID Number | Final Grade\r\n");
while(x<size)
{
fprintf(output, "%d %f\r\n", (*list[x]).studid, (*list[x]).finalgrade);
x++;
}
fclose(output);
}
void print_backwards(student **list, int x, int size)
{
if ((size-1) != x)
{
print_backwards(list,x+1,size);
}
printf("backwards order id: %d\n", (*list[x]).studid);
}
void withdraw(int idNo, student **list, int *sizePtr)
{
int x=0;
int initPos;
initPos = find_binsrch(idNo, list, *sizePtr);
if (initPos != -1)
{
free(list[initPos]);
x=initPos;
while (x<*sizePtr-1)
{
list[x] = list[x+1];
x++;
}
list[*sizePtr-1]=NULL;
*sizePtr = *sizePtr-1;
}
else
printf("the following student id : '%d' is not in the class list.\n", id
No);
}
void destroy_list(student **list, int *size)
{
int x;
while (x<size)
{
free(list[x]);

x++;
}
*size=0;
free(list);
}
void main()
{
}

Vous aimerez peut-être aussi