Jump to page: 1 24  
Page
Thread overview
Some user-made C functions and their D equivalents
Jul 27, 2022
pascal111
Jul 27, 2022
ryuukk_
Jul 28, 2022
pascal111
Jul 28, 2022
ryuukk_
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
Dennis
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
pascal111
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
pascal111
Jul 28, 2022
kdevel
Jul 28, 2022
pascal111
Jul 28, 2022
frame
Jul 28, 2022
pascal111
Jul 28, 2022
frame
Jul 28, 2022
pascal111
Jul 28, 2022
H. S. Teoh
Jul 28, 2022
pascal111
Jul 28, 2022
frame
Jul 28, 2022
pascal111
Jul 28, 2022
frame
Jul 30, 2022
pascal111
Jul 30, 2022
frame
July 27, 2022

I made a library of some useful functions while I was studying C, and I'm curious to know if D has equivalents or some ones for some of my functions, or I have to retype 'em again in D.

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

July 27, 2022

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

>

I made a library of some useful functions while I was studying C, and I'm curious to know if D has equivalents or some ones for some of my functions, or I have to retype 'em again in D.

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

D ships with libc so everything in there is available

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>

In d the equivalent imports:

import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.stdc.ctype;
import core.stdc.math;
July 28, 2022

On Wednesday, 27 July 2022 at 19:07:26 UTC, ryuukk_ wrote:

>

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

>

I made a library of some useful functions while I was studying C, and I'm curious to know if D has equivalents or some ones for some of my functions, or I have to retype 'em again in D.

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

D ships with libc so everything in there is available

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>

In d the equivalent imports:

import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import core.stdc.ctype;
import core.stdc.math;

Ok, but before that, I faced a problem when I tried to make my module. I tried to make a test. I made a testing module called "dcollect.d" and I put it in this path on my Ubuntu "/usr/lib/gcc/x86_64-linux-gnu/11/include/d".

module dcollect;

import std.stdio;

int foo22()
{

return 5;

}

Then I called "foo22" in a testing program and found an error like (this one in another try by telling code::blocks about local searching folder for the compiler I put the module in) "||=== Build: Debug in temp (compiler: GDC D Compiler) ===|
/home/pascal111/My Projects/D/temp/hello.d|23|undefined reference to `_D8dcollect5foo22FZi'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|"

The testing program:

module main;

import std.stdio;
import std.string;
import std.conv;
import dcollect;

int main(string[] args)
{

string ch;

try{
    ch=readln();
    ch=strip(ch);

    if(to!int(ch)<0)
        throw new Exception ("You entered negative number!");}

catch (Exception e){
    writeln(e.msg);}

writeln(foo22());

return 0;

}

July 28, 2022

I don't remember the exact syntax for GDC, but it should be pretty similar to DMD

You need to pass the module to the compiler

gdc main.d dcollect.d
July 28, 2022

On Thursday, 28 July 2022 at 00:36:54 UTC, ryuukk_ wrote:

>

I don't remember the exact syntax for GDC, but it should be pretty similar to DMD

You need to pass the module to the compiler

gdc main.d dcollect.d

I'm using CODE::BLOCKS IDE. How can I do it through it?

July 28, 2022

On Thursday, 28 July 2022 at 00:46:19 UTC, pascal111 wrote:

>

On Thursday, 28 July 2022 at 00:36:54 UTC, ryuukk_ wrote:

>

I don't remember the exact syntax for GDC, but it should be pretty similar to DMD

You need to pass the module to the compiler

gdc main.d dcollect.d

I'm using CODE::BLOCKS IDE. How can I do it through it?

You should probably ask this question in a CODE::BLOCKS forum.

Further reading:

July 28, 2022

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

>

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

It would help if the functions had a comment explaining what they're supposed to do, but it looks like most of them are string functions. In D, you can concatenate strings with the ~ operator, and utility functions like strip and replace are in the std.string module:

https://dlang.org/phobos/std_string.html

I also think you defined the equivalent of these functions:

import std.algorithm: swap;
import std.math: sgn, trunc;
July 28, 2022

On Thursday, 28 July 2022 at 11:13:19 UTC, Dennis wrote:

>

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

>

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

It would help if the functions had a comment explaining what they're supposed to do, but it looks like most of them are string functions. In D, you can concatenate strings with the ~ operator, and utility functions like strip and replace are in the std.string module:

https://dlang.org/phobos/std_string.html

I also think you defined the equivalent of these functions:

import std.algorithm: swap;
import std.math: sgn, trunc;

As you mentioned, I retyped some of 'em:

module dcollect;

import std.stdio;
import std.conv;

/****************************************/

string strleft(const string ch, int n)
{

    string ch_sub;

    for(int i=0; i<n; i++)
        ch_sub~=ch[i];

    return ch_sub;

}

/************************************/

string strreverse(const string ch)
{

    string ch_rev;

    for(int i=to!int(ch.length-1); i>=0; i--)
        ch_rev~=ch[i];


    return ch_rev;

}

/*********************************************/

string strright(const string ch, int n)
{

    string ch_sub1,
    ch_sub2;

    ch_sub1=strreverse(ch);

    ch_sub2=strleft(ch_sub1, n);

    ch_sub1=strreverse(ch_sub2);

    return ch_sub1;

}

/*********************************************/

void swap(T)(ref T x,ref T y)
{

    T z;

    z=x;
    x=y;
    y=z;

}

/*********************************************/

int sgn(T)(T x)
{

    if(x<0)
        return -1;
    else if(x>0)
        return 1;
    else
        return 0;

}

July 28, 2022

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

>

I made a library of some useful functions while I was studying C, and I'm curious to know if D has equivalents or some ones for some of my functions, or I have to retype 'em again in D.

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

  1. What exact purpose do these
/******************************************/

have?

double fix(double x)
{

double y,* z;

y=modf(x,z);
y=*z;

return y;

}

Besides the remarkable formatting there is an issue with the code. According to the manual the modf function

double modf(double x, double *iptr);

stores "The integral part [of x] [...] in the location pointed to by iptr." If you ask the compiler for help (e.g. gcc -Wall -pedantic) it will tell you what's wrong:

ofix.c: In function 'fix':
ofix.c:7:3: warning: 'z' is used uninitialized [-Wuninitialized]
    7 | y=modf(x,z);
      |   ^~~~~~~~~
ofix.c:5:12: note: 'z' was declared here
    5 | double y,* z;
      |            ^

I would also like to complain about the double assignment to y.

July 28, 2022

On Thursday, 28 July 2022 at 12:15:19 UTC, kdevel wrote:

>

On Wednesday, 27 July 2022 at 18:19:34 UTC, pascal111 wrote:

>

I made a library of some useful functions while I was studying C, and I'm curious to know if D has equivalents or some ones for some of my functions, or I have to retype 'em again in D.

The library link:
https://github.com/pascal111-fra/turbo-c-programs/blob/main/COLLECT2.H

  1. What exact purpose do these
/******************************************/

have?

double fix(double x)
{

double y,* z;

y=modf(x,z);
y=*z;

return y;

}

Besides the remarkable formatting there is an issue with the code. According to the manual the modf function

double modf(double x, double *iptr);

stores "The integral part [of x] [...] in the location pointed to by iptr." If you ask the compiler for help (e.g. gcc -Wall -pedantic) it will tell you what's wrong:

ofix.c: In function 'fix':
ofix.c:7:3: warning: 'z' is used uninitialized [-Wuninitialized]
    7 | y=modf(x,z);
      |   ^~~~~~~~~
ofix.c:5:12: note: 'z' was declared here
    5 | double y,* z;
      |            ^

I would also like to complain about the double assignment to y.

"fix" function is the C version of BASIC standard one I made, it's the equivalent of "trunc" in D, but the code I programmed for "fix" was with TC++ , and it worked in DOS emulators with no problem, I have no idea how gcc will treat my code, but I think you are right that some other compilers will refuse such code, because VC++ 6 refused many of these functions codes I programmed with TC++ compiler.

« First   ‹ Prev
1 2 3 4