Jump to page: 1 2
Thread overview
My programs issues
Aug 10, 2022
pascal111
Aug 10, 2022
Adam D Ruppe
Aug 10, 2022
pascal111
Aug 10, 2022
Andrey Zherikov
Aug 10, 2022
pascal111
Aug 10, 2022
Andrey Zherikov
Aug 10, 2022
pascal111
Aug 11, 2022
pascal111
Aug 12, 2022
pascal111
Aug 12, 2022
frame
Aug 12, 2022
pascal111
Aug 12, 2022
frame
Aug 12, 2022
pascal111
Aug 12, 2022
frame
Aug 12, 2022
pascal111
Aug 12, 2022
Antonio
Aug 12, 2022
pascal111
Aug 10, 2022
rikki cattermole
Aug 10, 2022
pascal111
August 10, 2022
In next program

1) I used "exit()" from "core.stdc.stdlib;" module, but someone can say this isn't the D way to exit the program.

2) I used "goto", I heard from someone before that using "goto" isn't good programming feature.

'''D
module proj07;

import std.stdio;
import std.algorithm;
import std.range;
import core.stdc.stdlib;

int main()
{

    double x, y;
    int choice;

new_again:

    x=0;
    y=0;

    write("Enter a number: ");
    readf(" %s\n", &x);
    y=x;
    writeln;

choice_again:

    writeln("1) addition 2) subtraction 3)
    multiplication 4) division 0) exit -1) new");
    ": ".write;

    readf(" %s\n", &choice);

    if(!only(1,2,3,4,0,-1).canFind(choice))
        goto choice_again;
    else if(choice==0)
        exit(0);
    else if(choice==-1)
        goto new_again;

    write("Enter a number: ");
    readf(" %s\n", &x);

    switch(choice){

        case 1: y+=x; break;
        case 2: y-=x; break;
        case 3: y*=x; break;
        case 4: y/=x; break;
        default: break;}

    y.writeln;

    goto choice_again;

    return 0;
}
'''

https://github.com/pascal111-fra/D/blob/main/proj07.d
August 10, 2022
On Wednesday, 10 August 2022 at 12:36:42 UTC, pascal111 wrote:
> 1) I used "exit()" from "core.stdc.stdlib;" module, but someone can say this isn't the D way to exit the program.

It is better to simply return a value from main instead.

> 2) I used "goto", I heard from someone before that using "goto" isn't good programming feature.

Don't listen to people who are wrong.
August 11, 2022
On 11/08/2022 12:36 AM, pascal111 wrote:
> 2) I used "goto", I heard from someone before that using "goto" isn't good programming feature.

This is mostly a historical debate at this point.

Back 40 years ago, goto wasn't typically limited within a procedure and doesn't have any checks in place to prevent you doing bad things.

These days languages will implement such checks (including D).

Some of the historical papers which debate this can be read from the book Literate Programming by Donald Knuth. If you do get the chance to read it, its a good read for cover to cover and teaches you a lot of the historical context that you may not get elsewhere.

https://www.amazon.com/Literate-Programming-Lecture-Notes-Donald/dp/0937073806
August 10, 2022
On Wednesday, 10 August 2022 at 13:13:20 UTC, Adam D Ruppe wrote:
> On Wednesday, 10 August 2022 at 12:36:42 UTC, pascal111 wrote:
>> 1) I used "exit()" from "core.stdc.stdlib;" module, but someone can say this isn't the D way to exit the program.
>
> It is better to simply return a value from main instead.
>
>> 2) I used "goto", I heard from someone before that using "goto" isn't good programming feature.
>
> Don't listen to people who are wrong.

So, the program will be like this:

'''D

module proj07;

import std.stdio;
import std.algorithm;
import std.range;

int main()
{

    double x, y;
    int choice;

new_again:

    x=0;
    y=0;

    write("Enter a number: ");
    readf(" %s\n", &x);
    y=x;
    writeln;

choice_again:

    writeln("1) addition 2) subtraction 3)
    multiplication 4) division 0) exit -1) new");
    ": ".write;

    readf(" %s\n", &choice);

    if(!only(1,2,3,4,0,-1).canFind(choice))
        goto choice_again;
    else if(choice==0)
        return 0;
    else if(choice==-1)
        goto new_again;

    write("Enter a number: ");
    readf(" %s\n", &x);

    switch(choice){

        case 1: y+=x; break;
        case 2: y-=x; break;
        case 3: y*=x; break;
        case 4: y/=x; break;
        default: break;}

    y.writeln;

    goto choice_again;

}

'''
August 10, 2022
On Wednesday, 10 August 2022 at 13:22:03 UTC, rikki cattermole wrote:
>
> On 11/08/2022 12:36 AM, pascal111 wrote:
>> 2) I used "goto", I heard from someone before that using "goto" isn't good programming feature.
>
> This is mostly a historical debate at this point.
>
> Back 40 years ago, goto wasn't typically limited within a procedure and doesn't have any checks in place to prevent you doing bad things.
>
> These days languages will implement such checks (including D).
>
> Some of the historical papers which debate this can be read from the book Literate Programming by Donald Knuth. If you do get the chance to read it, its a good read for cover to cover and teaches you a lot of the historical context that you may not get elsewhere.
>
> https://www.amazon.com/Literate-Programming-Lecture-Notes-Donald/dp/0937073806

I remember the famous problem at old days called "spaghetti code". But yes, D is modern and structured language and has good checks to prevent "goto" problems, but it's, "goto" still as you said a topic of historical debate.
August 10, 2022

On Wednesday, 10 August 2022 at 13:34:53 UTC, pascal111 wrote:

>

So, the program will be like this:

Is this clearer?


import std.stdio;
import std.algorithm;
import std.range;

double getNumber()
{
    double x;

    write("Enter a number: ");
    readf(" %s\n", &x);
    writeln;

    return x;
}

int getChoice()
{
    while(true)
    {
        writeln("1) addition 2) subtraction 3) multiplication 4) division 0) exit -1) new");
        ": ".write;

        int choice;
        readf(" %s\n", &choice);

        if(only(1,2,3,4,0,-1).canFind(choice))
            return choice;
    }
}

int main()
{
    double x, y;

    y = getNumber();

    while(true)
    {
        int choice = getChoice();

        if(choice==0)
            return 0;
        else if(choice==-1)
			y = getNumber();
        else
        {
            write("Enter a number: ");
            readf(" %s\n", &x);

            switch(choice){

                case 1: y+=x; break;
                case 2: y-=x; break;
                case 3: y*=x; break;
                case 4: y/=x; break;
                default: break;}

            y.writeln;
        }
    }
}
August 10, 2022

On Wednesday, 10 August 2022 at 14:03:53 UTC, Andrey Zherikov wrote:

>

On Wednesday, 10 August 2022 at 13:34:53 UTC, pascal111 wrote:

>

So, the program will be like this:

Is this clearer?


import std.stdio;
import std.algorithm;
import std.range;

double getNumber()
{
    double x;

    write("Enter a number: ");
    readf(" %s\n", &x);
    writeln;

    return x;
}

int getChoice()
{
    while(true)
    {
        writeln("1) addition 2) subtraction 3) multiplication 4) division 0) exit -1) new");
        ": ".write;

        int choice;
        readf(" %s\n", &choice);

        if(only(1,2,3,4,0,-1).canFind(choice))
            return choice;
    }
}

int main()
{
    double x, y;

    y = getNumber();

    while(true)
    {
        int choice = getChoice();

        if(choice==0)
            return 0;
        else if(choice==-1)
			y = getNumber();
        else
        {
            write("Enter a number: ");
            readf(" %s\n", &x);

            switch(choice){

                case 1: y+=x; break;
                case 2: y-=x; break;
                case 3: y*=x; break;
                case 4: y/=x; break;
                default: break;}

            y.writeln;
        }
    }
}

This version has modern features, it's 1) functional 2) goto-less.

August 10, 2022

On Wednesday, 10 August 2022 at 14:08:59 UTC, pascal111 wrote:

>

This version has modern features, it's 1) functional 2) goto-less.

There is nothing modern ;-D

August 10, 2022
On Wednesday, 10 August 2022 at 14:45:05 UTC, Andrey Zherikov wrote:
> On Wednesday, 10 August 2022 at 14:08:59 UTC, pascal111 wrote:
>> This version has modern features, it's 1) functional 2) goto-less.
>
> There is nothing modern ;-D

Take a look in this linear programming and you will know what's the modern:

'''BASIC

10 ' ----------------------------------------------
20 '                 ACEY DUECEY
30 ' ----------------------------------------------
40 '
50 '       Program by G C Lindridge  27/12/90
60 '
70 CLEAR : KEY OFF : CLS : SCREEN 0 : WIDTH 80
80 DEFINT A-Z
90 GOSUB 220                                       'INPUT SCREEN
100 GOSUB 510                                      'CREATE PACK
110 GOSUB 750                                      'SHUFFLE CARDS
120 SCREEN 1                                       'LARGE DISPLAY
130 TD = 1500                                      'TIME DELAY
140 GOSUB 930                                      'PLAY THE GAME
150 WIDTH 80 : SCREEN 0
160 PRINT "END ACEY DUECEY"
170 PRINT : PRINT "Do you want to play again?  Y/N" : PRINT
180 CH$ = INKEY$ : IF CH$ = "" THEN 180
190 IF CH$ = "Y" THEN 70 ELSE SYSTEM
200 END
210 ' ----------------------------------------------
220 '                                   INPUT SCREEN
230 ' ----------------------------------------------
240 PRINT TAB(33);"ACEY DUECEY"
250 PRINT TAB(33);"-----------"
260 PRINT : PRINT
270 PRINT TAB(24);"You and the computer play the game."
280 PRINT TAB(24);"You both start with 1000 units."
290 PRINT TAB(24);"Bets are from 1 to 200 units."
300 PRINT
310 PRINT TAB(24);"Can you send the computer broke?"
320 PRINT : PRINT
330 PRINT TAB(24);"[ 1 ]  To see the User Guide"
340 PRINT TAB(24);"[ 2 ]  Play Acey Duecey"
350 CH$ = INKEY$ : IF CH$ = "" THEN 350
360 IF CH$ = "1" THEN GOSUB 2150
370 MIN = 1
380 LIMIT = 200
390 HUMANBANK = 900
400 COMPBANK = 900
410 KITTY = 200
420 RETURN
430 ' ----------------------------------------------
440 '                                  PRESS ANY KEY
450 ' ----------------------------------------------
460 LOCATE 25,1 : PRINT ,,,"Press any key to continue";
470 WHILE INKEY$ = "" : WEND
480 CLS
490 RETURN
500 ' ----------------------------------------------
510 '                                    CREATE PACK
520 ' ----------------------------------------------
530 DIM CARD(52)
540 'GIVE GAME VALUES TO CARDS
550 FOR J = 0 TO 3
560   FOR K = 1 TO 13
570     CARD(J*13+K) = K+1
580   NEXT K
590 NEXT J
600 RETURN
610 ' ----------------------------------------------
620 '                                     SHOW CARDS
630 ' ----------------------------------------------
640 LOCATE 1,1
650 FOR K = 52 TO 1 STEP-1
660   IF CARD(K) < 11 THEN PRINT USING "####";CARD(K);
670   IF CARD(K) = 11 THEN PRINT "   J";
680   IF CARD(K) = 12 THEN PRINT "   Q";
690   IF CARD(K) = 13 THEN PRINT "   K";
700   IF CARD(K) = 14 THEN PRINT "   A";
710 NEXT K
720 PRINT : PRINT
730 RETURN                                          'END DISPLAY
740 ' ----------------------------------------------
750 LOCATE 24,1 :           PRINT "[SHUFFLE]       ";
760 ' ----------------------------------------------
770 RANDOMIZE TIMER
780 FOR K = 52 TO 2 STEP -1
790   X = INT(RND(1)*K)+1
800   SWAP CARD(K),CARD(X)
810 NEXT K
820 SHOE = 52                                       'PUT CARDS IN SHOE
830 RETURN
840 ' ----------------------------------------------
850 '                                     TIME DELAY
860 ' ----------------------------------------------
870 FOR K = 1 TO TD
880   FOR L = 1 TO TD
890   NEXT L
900 NEXT K
910 RETURN
920 ' ----------------------------------------------
930 '                                  PLAY THE GAME
940 ' ----------------------------------------------
950 WHILE (BET$ <> "Q") AND (HUMANBANK > MIN) AND (COMPBANK > MIN) 'CONTINUE
960   P = 0 : BET$ = ""                              'INITIALISE
970   PLHAND = 0
980   FOR K = 1 TO 3
990     PLCARD(K) = 0
1000   NEXT K
1010   BADCARDS = 0
1020   IF BET$ = "Q" THEN 1100                       'QUIT THE GAME
1030   IF (SHOE < 5) THEN GOSUB 750                  'RESHUFFLE POINT
1040   GOSUB 850                                     'TIME DELAY
1050   CLS
1060   GOSUB 1180                                    'HUMAN PLAYER
1070   IF (SHOE < 5) THEN GOSUB 750                  'RESHUFFLE POINT
1080   GOSUB 1540                                    'COMPUTER PLAYER
1090 WEND
1100 RETURN                                          'END DEALING
1110 ' ----------------------------------------------
1120 '                                    DEAL A CARD
1130 ' ----------------------------------------------
1140 CARD = CARD(SHOE)                               'THE TOP CARD
1150 SHOE = SHOE - 1                                 'REMOVE CARD FROM PACK
1160 RETURN                                          'DEAL CARD
1170 ' ----------------------------------------------
1180 '                                   HUMAN PLAYER
1190 ' ----------------------------------------------
1200 LOCATE 24, 1 : PRINT "HUMAN BANK";HUMANBANK;
1210 LOCATE 24,21 : PRINT "COMPUTER BANK";COMPBANK;
1220 GOSUB 1910
1230 HUMANBET = 0
1240 GOSUB 1120                                      'DEAL A CARD
1250 PLCARD(1) = CARD
1260 GOSUB 1120                                      'DEAL A CARD
1270 PLCARD(2) = CARD
1280 IF PLCARD(1) < PLCARD(2) THEN SWAP PLCARD(1),PLCARD(2)  'CARD(1) IS HIGH
1290 P = 1
1300 R = 80 : C = P*32 - 32 : GOSUB 2000             'SHOW CARD IN BOX
1310 P = 2
1320 R = 80 : C = P*32      : GOSUB 2000             'SHOW CARD IN BOX
1330 IF PLCARD(2)+1 >= PLCARD(1) THEN 1500           'NO SPACE BETWEEN CARDS
1340 LOCATE 25, 1 : INPUT;"MAKE A BET ",BET$
1350 IF BET$ = "Q" THEN 1520                         'QUIT THE GAME
1360 HUMANBET = INT(ABS(VAL(BET$)))                  'BET UNITS
1370 IF HUMANBET = 0 THEN 1500                       'REJECT HAND
1380 IF HUMANBET > KITTY THEN LET HUMANBET = KITTY   'LIMIT BET
1390 IF HUMANBET > LIMIT THEN LET HUMANBET = LIMIT   'MAXIMUM BET
1400 IF HUMANBET > HUMANBANK THEN LET HUMANBET = HUMANBANK   'BET THE LOT
1410 GOSUB 1120                                      'DEAL A CARD
1420 P = 3
1430 PLCARD(P) = CARD
1440 R = 16 : C = P*32 - 64 : GOSUB 2000             'SHOW CARD IN BOX
1450 HUMANBANK = HUMANBANK-HUMANBET
1460 KITTY = KITTY + HUMANBET
1470 IF (PLCARD(3)>= PLCARD(1)) OR (PLCARD(3) < =  PLCARD(2)) THEN 1500
1480 HUMANBANK = HUMANBANK+2*HUMANBET
1490 KITTY = KITTY - 2*HUMANBET
1500 FOR K = 1 TO 1000 : NEXT K
1510 GOSUB 1910                                      'KITTY
1520 RETURN
1530 ' ----------------------------------------------
1540 '                                COMPUTER PLAYER
1550 ' ----------------------------------------------
1560 LOCATE 24,21 : PRINT "COMPUTER BANK";COMPBANK;
1570 COMPBET = 0
1580 GOSUB 1120                                      'DEAL A CARD
1590 PLCARD(1) = CARD
1600 GOSUB 1120                                      'DEAL A CARD
1610 PLCARD(2) = CARD
1620 IF PLCARD(1) < PLCARD(2) THEN SWAP PLCARD(1),PLCARD(2)  'CARD(1) IS HIGH
1630 P = 1
1640 R = 80 : C = P*32 + 160 : GOSUB 2000            'SHOW CARD IN BOX
1650 P = 2
1660 R = 80 : C = P*32 + 192 : GOSUB 2000            'SHOW CARD IN BOX
1670 IF PLCARD(2)+1 >= PLCARD(1) THEN 1870           'NO SPACE BETWEEN CARDS
1680 FOR K = 1 TO SHOE
1690   IF CARD(K) >= PLCARD(1) OR CARD(K) <= PLCARD(2)THEN LET BADCARDS = BADCARDS+1
1700   IF BADCARDS >= SHOE/2 THEN LET K = SHOE
1710 NEXT K
1720 COMPBET = INT((SHOE-2*BADCARDS)/SHOE*COMPBANK)  'ADVANTAGE BET
1730 IF COMPBET < 1 THEN LET COMPBET = 0 : GOTO 1870
1740 IF COMPBET > KITTY THEN LET COMPBET = KITTY     'LIMIT BET
1750 IF COMPBET > LIMIT THEN LET COMPBET = LIMIT     'MAXIMUM BET
1760 IF COMPBET > COMPBANK THEN LET COMPBET = COMPBANK       'BET THE LOT
1770 LOCATE 25,21 : PRINT "COMPUTER  BET";COMPBET;
1780 GOSUB 1120                                      'DEAL A CARD
1790 P = 3
1800 PLCARD(P) = CARD
1810 R = 16 : C = P*32 + 128 : GOSUB 2000            'SHOW CARD IN BOX
1820 COMPBANK = COMPBANK-COMPBET
1830 KITTY = KITTY + COMPBET
1840 IF (PLCARD(3)>= PLCARD(1)) OR (PLCARD(3) < =  PLCARD(2)) THEN 1870
1850 COMPBANK = COMPBANK+ 2*COMPBET
1860 KITTY = KITTY - 2*COMPBET
1870 FOR K = 1 TO 2000 : NEXT K
1880 GOSUB 1910                                      'KITTY
1890 RETURN
1900 ' ----------------------------------------------
1910 '                                          KITTY
1920 ' ----------------------------------------------
1930 IF KITTY <> 0 THEN 1970
1940 HUMANBANK = HUMANBANK-100
1950 COMPBANK = COMPBANK-100
1960 KITTY = 200
1970 LOCATE 25,21 : PRINT "        KITTY";KITTY;
1980 RETURN
1990 ' ----------------------------------------------
2000 '                         PICTURE PLAYERS' CARDS
2010 ' ----------------------------------------------
2020 PICTURE = PLCARD(P)
2030 LINE (C+4,R+4)-(C+30,R+44),,B                   'DRAW CARD BORDER
2040 IF PICTURE = 10 THEN LET PIP$ = "1O"
2050 IF PICTURE = 11 THEN LET PIP$ = "J "
2060 IF PICTURE = 12 THEN LET PIP$ = "Q "
2070 IF PICTURE = 13 THEN LET PIP$ = "K "
2080 IF PICTURE = 14 THEN LET PIP$ = "A "
2090 IF PICTURE < 10 THEN LET PIP$ = RIGHT$(STR$(PICTURE),1)+" "
2100 LOCATE R/8+2,C/8+2 : PRINT PIP$;                'DRAW PIPS
2110 SUIT$ = CHR$(INT (RND(1)*4)+3)                  'CARD SUIT
2120 LOCATE R/8+4,C/8+3 : PRINT SUIT$;               'DRAW SUIT
2130 RETURN
2140 ' ----------------------------------------------
2150 CLS :              PRINT ,,"      ACEY  DUECEY" : PRINT
2160 ' ----------------------------------------------
2170 PRINT ,"This is a gambling card game. A player is dealt a"
2180 PRINT ,"two card hand face up. The player now has the choice"
2190 PRINT ,"of folding the hand or betting on the hand."
2200 PRINT ,"If he folds, the next player is dealt a hand."
2210 PRINT ,"If he bets, he is dealt a third card."
2220 PRINT ,"If this card is between the other two cards he wins."
2230 PRINT ,"If this card is equal to either card or is more than"
2240 PRINT ,"the big card or less than the small card he loses."
2250 PRINT ,"Cards are as normal with Ace a high card. No suits."
2260 PRINT
2270 PRINT ,"TO FOLD: Bet 0. Alternatively press [Return] key."
2280 PRINT ,"TO QUIT: Enter Q as a bet."
2290 PRINT
2300 PRINT ,"BETTING: All bets are in units of 1."
2310 PRINT ,"Each player contributes 100 units to a kitty."
2320 PRINT ,"Bet Limits:  Minimum 1 unit.  Maximum 200 units."
2330 PRINT ,"A bet is unable to exceed what remains in the kitty."
2340 PRINT ,"A player bets to win 'EVENS'. Winnings are taken"
2350 PRINT ,"from the kitty, losing bets are added to the kitty."
2360 PRINT
2370 PRINT ,"When the kitty is empty, the players again contribute"
2380 PRINT ,"100 units each.";
2390 GOSUB 440                                       'PRESS ANY KEY
2400 RETURN

'''
August 11, 2022
Next code receiving an input from the user and reprint it in capital letters with a separator between each letter, but I think that there is more to add or to modify the way this program working with:

'''D

module main;

import std.stdio;
import std.uni;
import std.string;
import std.algorithm;
import std.ascii;
import dcollect;
import std.range;


int main(string[] args)
{

    string user_word,
    sep_symbol;

again:

    write("Enter a word (\"none\" = exit): ");
    user_word=strip(readln());
    if(user_word=="none")
        return 0;

    write("Enter a seperator: ");
    sep_symbol=strip(readln());

    writeln;

    user_word.strtoupper.filter!(a=>!isNonCharacter(a)).
    map!only.joiner(sep_symbol).writeln;

    goto again;

}
'''

https://github.com/pascal111-fra/D/blob/main/proj08.d
« First   ‹ Prev
1 2