Board index » delphi » Need very good Argument switch program

Need very good Argument switch program

Hi!

How can I write this C- program on TPascal 5.5/7.0 programming language?

It should works when you write from command line.

Quote
>argun -N -R -W -S -C -V -O

---
#include <stdio.h>
#include <string.h>

/* functions */
void doLongDistanceMobileCall();
void doNuclearPower();
void doOil();
void doRenewables();
void doSaving();
void doVote();
void doWindPower();
void doSyntax();

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;

Quote
} // end of main()

/* all functions can run together (or separately) in one command line */

void doLongDistanceMobileCall()
{
   printf("Run doLongDistanceMobileCall() !\n");
   printf("Please, call your partner to buy to you 3G mobile phone!\n");

   // etc

Quote
}

void doNuclearPower()
{
   printf("Run doNuclearPower() !\n");
   printf("Clean, economical 35 p share in EU by 2010 (better
KIO2)!\n");
   // etc

Quote
}

void doOil()
{
   printf("Run doOil() !\n");
   printf("At public request, still driving gasoline car!\n");
   // etc

Quote
}

void doRenewables()
{
   printf("Run doRenewables() !\n");
   printf("Clean, 14 p share in EU by 2010!\n");
   // etc

Quote
}

void doSaving()
{
   printf("Run doSaving() !\n");
   printf("Clean, 15 p saving in electric power and heating in EU by
2010!\n");
   // etc

Quote
}

void doVote()
{
   printf("Run doVote() !\n");
   printf("Use your big vote! Nothing to do in the parliament, if you
have under 5 p share!\n");
   // etc

Quote
}

void doWindPower()
{
   printf("Run doWindPower() !\n");
   printf("Clean, 40 p growth/ year in EU by 2010!\n");
   // etc

Quote
}

void doSyntax()
{
   printf("Copyright (C), 1990 by VS Energy Company ;-)\n");
   printf("Syntax : argun [options]\n");
   printf("options: -R -N -C -W -V -S -O\n");

   return EXIT_FAILURE;

Quote
}

---

Have you any better ideas?

Thanks in advance!

Veli Suorsa
---
"People must believe to the future to be able to live!"
---------------------------------- J.V.Snellman, 1890.

Oulu, FINLAND
Mailto:VJSuo...@Surfeu.Fi
http://members.surfeu.fi/veli.suorsa/

 

Re:Need very good Argument switch program


"Mr. Veli Suorsa" <veli.suo...@surfeu.fi> wrote in message
news:3B3A3561.13B23C0F@surfeu.fi...

Quote
> 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.

Quote
> #include <stdio.h>
> #include <string.h>

You don't need these...

Quote
> /* 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.)

Quote
> 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.

Quote
> 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

Re:Need very good Argument switch program


Thanks for Your reply, Starlet!

Quote
The Scarlet Manuka wrote:
> Program Argun;
> 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.

Can I use in Pascal something like this case- command (Note: many case-
characters)?
...
   case arg of
          '-N': doNuclearPower;
...
And of cource all other arguments same way.

- Show quoted text -

Quote
> 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.

I will write whole program again, with yours very good advices, thanks. You
must be a teacher or a professional.

With Best Regards

Veli Suorsa
---
"People must believe to the future to be able to live!"
---------------------------------- J.V.Snellman, 1890.

Oulu, FINLAND
Mailto:VJSuo...@Surfeu.Fi
http://members.surfeu.fi/veli.suorsa/

Re:Need very good Argument switch program


"Mr. Veli Suorsa" <veli.suo...@surfeu.fi> wrote in message
news:3B3B7A4E.471A1A3F@surfeu.fi...

Quote
> The Scarlet Manuka wrote:

> >       arg := ParamStr(x);
> >       if (length(arg) = 2) and (arg[1] = '-') then
> >         case arg[2] of
> >           'C': doLongDistanceMobileCall;
> >           'N': doNuclearPower;
> >           ...
> >           'W': doWindPower;
> >         end

> Can I use in Pascal something like this case- command (Note: many case-
> characters)?
> ...
>    case arg of
>           '-N': doNuclearPower;
> ...
> And of cource all other arguments same way.

No (I would have suggested it if so; it's much easier). You can't use a
string variable as a case selector, it needs to be an ordinal expression.
Single characters are OK (the char type is an ordinal type), but strings
are not - the best you can do is "if... else if... else if... " chains
for those.

In this case you are only interested in one character, so I suggested the
case construction I gave previously. I think it's probably the nicest way
in this sort of situation; but "if... else if..." is reasonable too.

Quote
> I will write whole program again, with yours very good advices, thanks.
You
> must be a teacher or a professional.

Not really, I just have a bit of experience.

--
The Scarlet Manuka

Re:Need very good Argument switch program


"The Scarlet Manuka" <sa...@maths.uwa.edu.au> wrote in message
news:9hetbt$ch0$1@fang.dsto.defence.gov.au...

Quote
> "Mr. Veli Suorsa" <veli.suo...@surfeu.fi> wrote in message
> news:3B3B7A4E.471A1A3F@surfeu.fi...
> > The Scarlet Manuka wrote:

> > >       arg := ParamStr(x);
> > >       if (length(arg) = 2) and (arg[1] = '-') then
> > >         case arg[2] of

or case UpCase(arg[2]) if you didn't want to be case-sensitive

...

Quote
> > Can I use in Pascal something like this case- command (Note: many
case-
> > characters)?
> > ...
> >    case arg of
> >           '-N': doNuclearPower;
> > ...
> > And of cource all other arguments same way.

> No (I would have suggested it if so; it's much easier). You can't
use a
> string variable as a case selector, it needs to be an ordinal
expression.
> Single characters are OK (the char type is an ordinal type), but
strings
> are not - the best you can do is "if... else if... else if... "
chains
> for those.

it is possible to convert the string to an integer by searching in a
larger string, as in the following example which compiles in BP7:

Const
  parCall = '-C';
  parNuke = '-N';
  parWind = '-W';

  Params : String =
      parCall+#0
     +parNuke+#0
     +parWind+#0;

  posCall=1;
  posNuke=posCall+length(parCall)+1;
  posWind=posNuke+length(parNuke)+1;

var
  i : Integer;
Begin
  writeln(ParamCount,' parameters');
  For i := 1 to ParamCount do
  Begin
    write(i,': "',ParamStr(i),'" - ');
    case pos(paramStr(i)+#0, Params) of
      posCall: Writeln('Call');
      posNuke: Writeln('Nuke');
      posWind: Writeln('Wind');
    else
      writeln('Unrecognised');
    end;
  end;
end.

but it's probably more trouble than it's worth.

FP

Re:Need very good Argument switch program


Quote
"Frank Peelo" <fpe...@eircom.net> wrote in message

news:9heus0$al2$1@kermit.esat.net...

Quote
> > > The Scarlet Manuka wrote:

> > > >       arg := ParamStr(x);
> > > >       if (length(arg) = 2) and (arg[1] = '-') then
> > > >         case arg[2] of

> or case UpCase(arg[2]) if you didn't want to be case-sensitive

Yes, I mentioned that. ;-)

Quote
> it is possible to convert the string to an integer by searching in a
> larger string, as in the following example which compiles in BP7:

[snip]

Quote
> but it's probably more trouble than it's worth.

Er... yes. I don't think I *want* to understand how that bit of code
works. ;-)

--
The Scarlet Manuka

Re:Need very good Argument switch program


Thanks for your replies, Scarlet and Frank!

Quote
The Scarlet Manuka wrote:
> "Frank Peelo" <fpe...@eircom.net> wrote in message
> news:9heus0$al2$1@kermit.esat.net...

> > it is possible to convert the string to an integer by searching in a
> > larger string, as in the following example which compiles in BP7:

> [snip]

> > but it's probably more trouble than it's worth.

> Er... yes. I don't think I *want* to understand how that bit of code
> works. ;-)

Using long string (all arguments connected together) position is very good
idea, but...

I am still looking for easier way to switch procedures.
Please let me know if you find better way to do it.

Chears!!!

Veli Suorsa
---
"People must believe to the future to be able to live!"
---------------------------------- J.V.Snellman, 1890.

Oulu, FINLAND
Mailto:VJSuo...@Surfeu.Fi
http://members.surfeu.fi/veli.suorsa/

Other Threads