Article View: comp.lang.pascal.borland
Article #156Re: Need very good Argument switch program
From: The Scarlet Manuka
Date: Wed, 27 Jun 2001 10:30
Date: Wed, 27 Jun 2001 10:30
141 lines
4737 bytes
4737 bytes
"Mr. Veli Suorsa" <veli.suorsa@surfeu.fi> wrote in message
news:3B3A3561.13B23C0F@surfeu.fi...
> Hi!
>
> How can I write this C- program on TPascal 5.5/7.0 programming language?
Answers below for 7.0; I'm not sure if everything I've written applies to
5.5.
> #include <stdio.h>
> #include <string.h>
You don't need these...
> /* functions */
> void doLongDistanceMobileCall();
> void doNuclearPower();
You don't need these either; instead put the main bodies of the functions
here. (Function prototyping is used in units, where it's part of the
"interface section"; but in the main program code functions are in general
just written in.)
> int main(int argc, char *argv[])
> {
> int x;
>
> /* if no argument, show syntax help */
> if( argc == 1 )
> doSyntax();
>
> for (x =1; x < argc; x++)
> {
> /* Note exact string compare (strcmp()), must be -N, not -n */
> /* using case- command should be better to read? */
>
> if( strcmp("-C", argv[x]) == 0 )
> doLongDistanceMobileCall();
> if( strcmp("-N", argv[x]) == 0 )
> doNuclearPower();
> if( strcmp("-O", argv[x]) == 0 )
> doOil();
> if( strcmp("-R", argv[x]) == 0 )
> doRenewables();
> if( strcmp("-S", argv[x]) == 0 )
> doSaving();
> if( strcmp("-V", argv[x]) == 0 )
> doVote();
> if( strcmp("-W", argv[x]) == 0 )
> doWindPower();
>
> // etc
> }
>
> return EXIT_SUCCESS;
> } // end of main()
var
x: integer;
begin
if ParamCount = 0 then
doSyntax;
for x := 1 to ParamCount do
begin
if ParamStr(x) = '-C' then
doLongDistanceMobileCall;
if ParamStr(x) = '-N' then
doNuclearPower;
if ParamStr(x) = '-O' then
doOil;
if ParamStr(x) = '-R' then
doRenewables;
if ParamStr(x) = '-S' then
doSaving;
if ParamStr(x) = '-V' then
doVote;
if ParamStr(x) = '-W' then
doWindPower;
{etc}
end
end.
Slightly more efficient would be:
for x := 1 to ParamCount do
begin
if ParamStr(x) = '-C' then
doLongDistanceMobileCall
else if ParamStr(x) = '-N' then
doNuclearPower
else if ParamStr(x) = '-O' then
doOil {etc}
Note that the semicolons have been removed, since the "else" means we are
continuing in the same statement, not starting a new statement.
Better would be to have another couple of variables and do something like
this:
var
x: integer;
arg: string;
begin
...
for x := 1 to ParamCount do
begin
arg := ParamStr(x);
if (length(arg) = 2) and (arg[1] = '-') then
case arg[2] of
'C': doLongDistanceMobileCall;
'N': doNuclearPower;
...
'W': doWindPower;
end
end
end.
You can avoid case sensitivity by using UpCase(arg[2]) instead of arg[2]
above.
> void doLongDistanceMobileCall()
> {
> printf("Run doLongDistanceMobileCall() !\n");
> printf("Please, call your partner to buy to you 3G mobile phone!\n");
>
> // etc
> }
function doLongDistanceMobileCall;
begin
writeln("Run doLongDistanceMobileCall !");
writeln("Please, call your partner to buy to you 3G mobile phone!");
end;
Other functions similarly. Note that your main program needs to come
*after* all your function definitions.
--
The Scarlet Manuka
Technical Details
Message-ID:
<9hccch$npp$1@fang.dsto.defence.gov.au>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news1.optus.net.au!optus!yorrell.saard.net!fang.dsto.defence.gov.au!not-for-mailReferences:
<3B3A3561.13B23C0F@surfeu.fi>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111659,325c6695c14dd242 X-Google-Attributes: gid111659,public X-Google-Thread: 108ebd,325c6695c14dd242 X-Google-Attributes: gid108ebd,public X-Google-ArrivalTime: 2001-06-27 03:30:16 PST From: "The Scarlet Manuka" <sacha@maths.uwa.edu.au> Newsgroups: comp.lang.pascal.borland,borland.public.turbopascal Subject: Re: Need very good Argument switch program Date: Wed, 27 Jun 2001 18:30:16 +0800 Organization: Defence Science and Technology Organisation Lines: 143 Message-ID: <9hccch$npp$1@fang.dsto.defence.gov.au> References: <3B3A3561.13B23C0F@surfeu.fi> NNTP-Posting-Host: un-oranged.dsto.defence.gov.au X-Trace: fang.dsto.defence.gov.au 993637585 24377 203.6.60.95 (27 Jun 2001 10:26:25 GMT) X-Complaints-To: postmaster@dsto.defence.gov.au NNTP-Posting-Date: 27 Jun 2001 10:26:25 GMT X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news1.optus.net.au!optus!yorrell.saard.net!fang.dsto.defence.gov.au!not-for-mail