🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.pascal.borland
7 messages
7 total messages Started by Mr. Veli Suorsa Wed, 27 Jun 2001 19:35
Need very good Argument switch program
#155
Author: Mr. Veli Suorsa
Date: Wed, 27 Jun 2001 19:35
132 lines
3992 bytes
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.
>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;
} // 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
}

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

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

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

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

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
}

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

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;
}
---

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:VJSuorsa@Surfeu.Fi
http://members.surfeu.fi/veli.suorsa/
Re: Need very good Argument switch program
#156
Author: The Scarlet Manuka
Date: Wed, 27 Jun 2001 10:30
141 lines
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
Re: Need very good Argument switch program
#158
Author: The Scarlet Manuka
Date: Thu, 28 Jun 2001 09:32
39 lines
2618 bytes
"Mr. Veli Suorsa" <veli.suorsa@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
> >           '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.

> 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
#159
Author: Frank Peelo
Date: Thu, 28 Jun 2001 09:59
73 lines
3106 bytes
"The Scarlet Manuka" <sacha@maths.uwa.edu.au> wrote in message
news:9hetbt$ch0$1@fang.dsto.defence.gov.au...
> "Mr. Veli Suorsa" <veli.suorsa@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

...

> > 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
#160
Author: The Scarlet Manuka
Date: Thu, 28 Jun 2001 10:52
25 lines
2093 bytes
"Frank Peelo" <fpeelo@eircom.net> wrote in message
news:9heus0$al2$1@kermit.esat.net...

> > > 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. ;-)

> 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. ;-)

--
The Scarlet Manuka
Re: Need very good Argument switch program
#157
Author: Mr. Veli Suorsa
Date: Thu, 28 Jun 2001 18:41
111 lines
4014 bytes
Thanks for Your reply, Starlet!

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.


> 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:VJSuorsa@Surfeu.Fi
http://members.surfeu.fi/veli.suorsa/
Re: Need very good Argument switch program
#161
Author: Mr. Veli Suorsa
Date: Sun, 01 Jul 2001 18:40
33 lines
2342 bytes
Thanks for your replies, Scarlet and Frank!

The Scarlet Manuka wrote:

> "Frank Peelo" <fpeelo@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:VJSuorsa@Surfeu.Fi
http://members.surfeu.fi/veli.suorsa/
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads