struct StudRec
{
char* name;
int idNum;
double mark;
};
typedef struct StudRec Student;
Student stud1;
Student* studptr;
int max_int(int,int);
float max_float(float,float);
void LOI_Next(ListOfint*);
int f(); /* f(); f(1); f(stud1); ETC. ALL OKAY */
int g(void); /* ONLY g() ALLOWED */
#include "list.h"
> cc -c list.c
> cc main.c list.o sort.o pray.o -o progname
static int studCounter = 0;
static Student* allocateStudent(void)
{
return (Student*)malloc(sizeof(Student));
}
Student* newStudent(char* name, int idNum)
{
Student* s = allocateStudent();
strcpy(s->name,name);
s->idNum = idNum;
s->mark = -1.0;
studCounter++;
return s;
}
void deleteStudent(Student* sptr)
{
studCounter--;
free sptr;
}
int studentCount(void)
{
return studCounter;
}
int winner(int bet, int bank)
{
printf("You're a winner! You made $%d\n", 2*bet);
return bank + 2*bet;
}
int loser(int bet, int bank)
{
printf("Better luck next time. You lost $%d\n", bet);
return bank - bet;
}
typedef int (*BettingFunc)(int,int);
BettingFunc selectfunc(void)
{
int randVal = randIn(1,1000000);
if (randVal==42) { return &winner; }
else { return &loser; }
}
int main(void)
{
int credit = 100;
while (credit>0)
{
BettingFunc func;
int bet;
func = selectFunc();
printf("Place your bet: ");
scanf("%d", &bet);
credit = (*func)(bet,credit);
}
printf("Thank-you for contributing to");
printf("Victoria's $700 million surplus!\n");
}
int bigwinner(int bet, int bank)
{
printf("You're a big winner! You made $1,000,000");
return bank + 1000000;
}
int winner(int bet, int bank)
{
printf("You're a winner! You made $%d\n", 2*bet);
return bank + 2*bet;
}
int loser(int bet, int bank)
{
printf("Better luck next time. You lost $%d\n", bet);
return bank - bet;
}
int bigloser(int bet, int bank)
{
printf("Bad luck! We now own your children\n");
return bank - 1000000;
}
typedef int (*BettingFunc)(int,int);
BettingFunc selectfunc(void)
{
int randVal = randIn(1,1000000);
switch (randVal)
{
case 42: return &bigwinner;
case 666: return &bigloser;
default:
if (randVal%4==0) { return &winner; }
else { return &loser; }
}
}
int main(void)
{
int credit = 100;
while (credit>0)
{
BettingFunc func;
int bet;
func = selectFunc();
printf("Place your bet: ");
scanf("%d", &bet);
credit = (*func)(bet,credit);
}
printf("Thank-you for contributing to");
printf("Victoria's $700 million surplus!\n");
}
This material is part of the CSE2305 - Object-Oriented
Software Engineering course.
Copyright © Jon McCormack & Damian Conway, 1998–2005. All
rights reserved.