开发者

How to write in C code?

开发者 https://www.devze.com 2023-02-26 02:40 出处:网络
Here I have some Perl code: sub clustering { ($brapa,$gee) = @_; $g=0; while ($g<$brapa) { if (($Y1XY[$g]-$Y2XY[$g])<=0) { push(@Y1new,$g+$gee);}

Here I have some Perl code:

sub clustering {
    ($brapa,$gee) = @_;
    $g=0;
    while ($g<$brapa) {
        if (($Y1XY[$g]-$Y2XY[$g])<=0) { push(@Y1new,$g+$gee);}
        else {开发者_如何学C push(@Y2new,$g+$gee);}
        $g++;
    }
    $sizeY1new = $#Y1new+1;
    $sizeY2new = $#Y2new+1;
}

The problem is that I have try to write it in C like this:

int clustering (int brapa, int gee){
    int g;
    g=0;
    while (g<brapa) {
        if ((jarakY1-jarakY2)<=0) { /*stuck at here*/ }
        else { }
    }
    return 0;
}

How can I call data/results from jarakY1 and jarakY2? Then, how do I write it perfectly in C? Please help me.


Perl has built in data types and functions which aren't provided in C. It is the case for push. You'll have to write your own stack, list or variable length array handling yourself depending on how you use the result.


My first question is: why are you converting Perl code into C?

There are a great many C libraries providing something like Perl arrays, don't write your own. One is the Gnome C Library (aka glib) which has several Array types that you can push to.

The other alternative is to write it using C++ and the Standard Template Library (aka the STL). They have a list type with push_front.

You can use Perl arrays from C, though that isn't the simplest thing to do.

Finally, here's a question about learning C which has some good answers.

0

精彩评论

暂无评论...
验证码 换一张
取 消