stevengreen22
19-03-2011, 04:57 PM
Hi guys,
I've this assignment to create a menu for a pub based on the season. The tutor has given a small part of the code, but within my program the if - else statements become redundant.
I'm thinking it's unlikley for him to give us code that doesn't mean anything in the long run.
Would you mind having a look and seeing if there's an alternative way for this program to work that you think is more efficient. I'm getting a bit anal and wanting the perfect program now :) Have only dropped 4 marks out of a possible 600 so far so looking good....
his code:
summer:= (month > 5) and (month <=8);
if summer then
writeln('Melon')
else writeln('Oysters');
write('Roast Chicken with ');
if summer then
writeln('Green Salad')
else writeln('Two Veg');
My code - You can see where I've commented out the if-else (I also REALLy like using procedures.
program EX8PUBMENU(input,output);
uses crt;
const Title:string = ' EX8 ';
By:string = ' By Steve Green ';
var month:integer;
autumn,winter,spring,summer:boolean;
procedure sumr;
begin
summer:= (month > 5) and (month <=8);
//if summer then
writeln('Melon')
// else writeln('Oysters');
write('Roast Chicken with ');
// if summer then
writeln('Green Salad')
// else writeln('Two Veg');
end;
procedure autm;
begin
autumn:=(month>8) and (month<=11);
if autumn then
writeln ('Soup')
else writeln('no soup');
write('Broth with ');
if autumn then
writeln('Bread')
else writeln('Dumplings');
end;
----------THE OTHER 2 SEASONS GO HERE--------------
begin {main program}
begin
repeat
menu; -------IS A PROCEDURE
if month>12 then
begin
writeln('Sorry, Surely you know there are only 12 months in a year?!?!?!?');
cont; ------------THIS IS A PRESS RETURN TO CONT PROCEDURE
end;
case month of------------THIS NEEDS TO BE TIDIED, THE 'WRITELN' MAY BE INC
1:begin IN THE PROCEDURE.
clrscr;
WRITELN('Jan!');
writeln; wint;cont;
end;
2:begin
WRITELN('Feb!');
writeln;wint;cont;
end;
3:begin
clrscr;
WRITELN('Mar!');
writeln; spri;cont;
end;
4:begin
clrscr;
WRITELN('April!');
writeln;
spri;cont;
end;
5:begin
clrscr;
WRITELN('May!');
writeln;
sumr; cont;
end;
6:begin
clrscr;
WRITELN('June!');
writeln; sumr;cont;
end;
7:begin
WRITELN('July!');
writeln;
sumr;cont; end;
8:begin
clrscr;
WRITELN('August!');
writeln;sumr;cont; end;
9:begin
clrscr;
WRITELN('September!');
writeln;
autm;cont;
end;
10:begin
clrscr;
WRITELN('October!');
writeln;
autm; cont; end;
11:begin
clrscr;
WRITELN('November!');
writeln; autm; cont;
end;
12:begin
WRITELN('December!');
writeln; wint;cont;
end;
end;
until month = 0;
end;
//end;
end.
Thanks for taking the time to look. This is fully functional and does what is asked in the question but...I want to make it better, it needs to be cleaned up and so on with colours and spacing but you get the idea.
The worse thing about it that I can see is the case statement is drawn out but I'm new to these and arrays.
I've this assignment to create a menu for a pub based on the season. The tutor has given a small part of the code, but within my program the if - else statements become redundant.
I'm thinking it's unlikley for him to give us code that doesn't mean anything in the long run.
Would you mind having a look and seeing if there's an alternative way for this program to work that you think is more efficient. I'm getting a bit anal and wanting the perfect program now :) Have only dropped 4 marks out of a possible 600 so far so looking good....
his code:
summer:= (month > 5) and (month <=8);
if summer then
writeln('Melon')
else writeln('Oysters');
write('Roast Chicken with ');
if summer then
writeln('Green Salad')
else writeln('Two Veg');
My code - You can see where I've commented out the if-else (I also REALLy like using procedures.
program EX8PUBMENU(input,output);
uses crt;
const Title:string = ' EX8 ';
By:string = ' By Steve Green ';
var month:integer;
autumn,winter,spring,summer:boolean;
procedure sumr;
begin
summer:= (month > 5) and (month <=8);
//if summer then
writeln('Melon')
// else writeln('Oysters');
write('Roast Chicken with ');
// if summer then
writeln('Green Salad')
// else writeln('Two Veg');
end;
procedure autm;
begin
autumn:=(month>8) and (month<=11);
if autumn then
writeln ('Soup')
else writeln('no soup');
write('Broth with ');
if autumn then
writeln('Bread')
else writeln('Dumplings');
end;
----------THE OTHER 2 SEASONS GO HERE--------------
begin {main program}
begin
repeat
menu; -------IS A PROCEDURE
if month>12 then
begin
writeln('Sorry, Surely you know there are only 12 months in a year?!?!?!?');
cont; ------------THIS IS A PRESS RETURN TO CONT PROCEDURE
end;
case month of------------THIS NEEDS TO BE TIDIED, THE 'WRITELN' MAY BE INC
1:begin IN THE PROCEDURE.
clrscr;
WRITELN('Jan!');
writeln; wint;cont;
end;
2:begin
WRITELN('Feb!');
writeln;wint;cont;
end;
3:begin
clrscr;
WRITELN('Mar!');
writeln; spri;cont;
end;
4:begin
clrscr;
WRITELN('April!');
writeln;
spri;cont;
end;
5:begin
clrscr;
WRITELN('May!');
writeln;
sumr; cont;
end;
6:begin
clrscr;
WRITELN('June!');
writeln; sumr;cont;
end;
7:begin
WRITELN('July!');
writeln;
sumr;cont; end;
8:begin
clrscr;
WRITELN('August!');
writeln;sumr;cont; end;
9:begin
clrscr;
WRITELN('September!');
writeln;
autm;cont;
end;
10:begin
clrscr;
WRITELN('October!');
writeln;
autm; cont; end;
11:begin
clrscr;
WRITELN('November!');
writeln; autm; cont;
end;
12:begin
WRITELN('December!');
writeln; wint;cont;
end;
end;
until month = 0;
end;
//end;
end.
Thanks for taking the time to look. This is fully functional and does what is asked in the question but...I want to make it better, it needs to be cleaned up and so on with colours and spacing but you get the idea.
The worse thing about it that I can see is the case statement is drawn out but I'm new to these and arrays.