Code:
// definition file for any Windows application
#include <windows.h>
// definition file for I/Os
#include <stdio.h>
#define STR_LEN 20
#define NOF_ALPHA_GRADES 7
typedef struct grade
{
float value;
struct grade *next;
} GRADE;
typedef struct student
{
GRADE *first;
struct student *next;
} STUDENT;
// prototype of the call back function for this application (see below)
LRESULT CALLBACK CBFunc(HWND, UINT, WPARAM, LPARAM);
// main function of a Windows application
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("ex13-2");
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = CBFunc; // "install" the call back function
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Class not registered"), szAppName, MB_ICONERROR);
return(0);
}
hwnd = CreateWindow(szAppName, TEXT("ex13-2"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
// endless message dispatching loop
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
STUDENT *ReadLine(FILE *fptr)
{
char str1[STR_LEN + 1];
char str2[STR_LEN + 1];
float f;
STUDENT *sptr;
GRADE *gptr;
// read past the name and the date of birth
if (fscanf(fptr, "%s%s", str1, str2) == 0) return(NULL);
// reserve memory for a new student
sptr = malloc(sizeof(STUDENT));
if (sptr == NULL) return(NULL);
// initialize the new student's attributes
sptr->first = NULL;
sptr->next = NULL;
// read the grades
while (fscanf(fptr, "%f", &f) != 0)
{
// reserve memory for a new grade
gptr = malloc(sizeof(GRADE));
if (gptr == NULL) return(sptr);
// set the new grade's attributes
gptr->value = f;
gptr->next = sptr->first;
sptr->first = gptr;
}
return(sptr);
}
STUDENT *ReadInputFile(char *filename)
{
FILE *fptr;
STUDENT *sptr, *first = NULL;
fptr = fopen(filename, "r");
if (fptr == NULL) return(NULL);
while ((sptr = ReadLine(fptr)) != NULL)
{
sptr->next = first;
first = sptr;
}
fclose(fptr);
return(first);
}
void ComputeStats(STUDENT *firsts, int histogram[], float *avrg_addr, float *sd_addr)
{
GRADE *gptr;
float avrg;
float tot_avrg = 0.0;
int count;
int tot_count = 0;
int index;
float sd = 0.0;
while (firsts != NULL)
{
avrg = 0.0;
count = 0;
gptr = firsts->first;
while (gptr != NULL)
{
avrg += gptr->value;
count++;
gptr = gptr->next;
}
if (count != 0)
{
tot_avrg += avrg;
tot_count += count;
avrg /= count;
index = ConvertNumGrade(avrg);
histogram[index]++;
}
firsts = firsts->next;
}
// compute the standard deviation
if (tot_count != 0)
{
tot_avrg /= tot_count;
???
}
???
}
void DispStats(int histogram[], float avrg, float sd)
{
}
// definition of the call back function, i.e. the function which gets automatically called
// by the OS whenever a Windows event (msg) occurs for the application
LRESULT CALLBACK CBFunc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static STUDENT *first = NULL;
static int histogram[NOF_ALPHA_GRADES] = {0, 0, 0, 0, 0, 0, 0};
static float avrg = 0.0;
static float sd = 0.0;
switch(msg)
{
case WM_SIZE:
// read the input file (only) at the beginning of the program
if (first == NULL)
{
first = ReadInputFile("records.txt");
}
ComputeStats(first, histogram, &avrg, &sd);
DispStats(histogram, avrg, sd);
return(0);
case WM_PAINT:
// do something
return(0);
// other "case" statements according to needs
case WM_DESTROY:
PostQuitMessage(0);
return(0);
}
return(DefWindowProc(hwnd, msg, wParam, lParam));
}