LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (https://www.linuxquestions.org/questions/general-10/)
-   -   Source Code Superthread (https://www.linuxquestions.org/questions/general-10/source-code-superthread-806394/)

MTK358 05-26-2010 06:56 AM

Quote:

Originally Posted by MrCode (Post 3981402)
strcmp(*argv,questions[i])

You're comparing the question to the name of the program, not the first argument!

argv[0] (equivalent to *argv) contains the command used to execute the program.

argv[1] contains the first argument.

schneidz 05-26-2010 11:27 AM

http://www.linuxquestions.org/questi...center-719104/

frieza 05-26-2010 12:16 PM

i've probably posted this somewhere else in this forum at some point in time but.. a simple image randomization script for message board signatures (yes i probably could have randomized based on a directory list but i was too lazy and didn't know how at the time when i wrote it)
Code:

<?php
$me = rand(1,8);
$pic=imagecreatefromjpeg($me.".jpg");
list($width, $height, $type, $attr) = getimagesize($me.".jpg");
$layer = imagecreatetruecolor  ( 150  , 150  );
imagecopyresized  ( $layer  , $pic  , 0  , 0  , 0  , 0  , 150  , 150  , $width  , $height  );
header("Content-type: image/png");
imagejpeg($layer);
imagedestroy($layer);
imagedestroy($pic);
?>


MrCode 05-26-2010 02:31 PM

Quote:

Originally Posted by MTK358
argv[0] (equivalent to *argv) contains the command used to execute the program.

argv[1] contains the first argument.

:doh: Duh! I knew it was probably a problem with the for loop! :rolleyes:

Well, I changed the *argv to *(argv+1), and that makes the first question work, but I'm still trying to figure out why the others won't... :scratch:

EDIT: Removing the else break; clause did it.

The revised code:

Code:

#include <stdio.h>

char* questions[3] = {"What are you doing now?",
                      "Who are you?",
                      "How are you feeling?"};

char* answers[3] = {"None of your beeswax...\n",
                    "Who are YOU?\n",
                    "Why do you care?\n"};

int main(int argc,char** argv)
{
        if(argc < 2)
        {
                printf("Usage: ./askme <phrase>\n");
                printf("(where <phrase> is a question)\n");
               
                return 1;
        }
        else
        {
                int i;
                for(i = 0; i <= 2; i++)
                {
                        if(strcmp(*(argv+1),questions[i]) == 0)
                                printf("%s",answers[i]);
                }
        }
       
        return 0;
}


MTK358 05-26-2010 02:41 PM

That's good.

I still don't understand why you wrote *(argv+1) instead of the equivalent, but shorter and easier to understand argv[1].

MrCode 05-26-2010 02:47 PM

Quote:

I still don't understand why you wrote *(argv+1) instead of the equivalent, but shorter and easier to understand argv[1].
It helps to reinforce the concept of how pointers really work, in my mind. It reminds me that the "pointer" is really just a counter that keeps track of a memory address, and that the "array indices" are simply relative address points to the initial address that the "pointer" counter keeps track of, incrementing/decrementing by one or more units of the variable data type size (1 byte for char, 4 bytes for int, etc.).

Wow, even reading just part of that asm tutorial really solidified these concepts for me quite well! :D

MTK358 05-26-2010 02:51 PM

But the [] operator is designed as a shortcut for pointers treated as arrays.

tuxdev 05-26-2010 02:55 PM

I could show you the code I've written lately, but then I'd have to kill you.

I mean, kill you *sooner*

MTK358 06-19-2010 09:52 AM

I made a really cool PyQt4 animation test:

Code:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class DrawingTest(QFrame):
        def __init__(self, parent=None):
                QFrame.__init__(self, parent)

                self.paint_count = 0 # count frames
                self.angle = 0      # current angle

                self.ballx = self.width() / 2  # ball properties
                self.bally = self.height() / 2
                self.balldx = 1
                self.balldy = -2.5
                self.balld = 16
                self.gravity = 0.05

                # animation timer
                self.timer = QTimer()
                QObject.connect(self.timer, SIGNAL('timeout()'), self.animate)
                self.timer.start(10)

        def animate(self):
                self.angle += 16 # increment angle

                self.ballx += self.balldx # apply horizontal velocity
                if self.ballx < 0:        # bounce off left and right walls
                        self.ballx = 0
                        self.balldx = -self.balldx
                elif (self.ballx + self.balld) >= self.width():
                        self.ballx = self.width() - self.balld - 1
                        self.balldx = -self.balldx

                self.bally += self.balldy # apply vertical velocity
                if self.bally < 0:        # bounce off top and bottom walls
                        self.bally = 0
                        self.balldy = -self.balldy
                elif (self.bally + self.balld) >= self.height():
                        self.bally = self.height() - self.balld - 1
                        self.balldy = -self.balldy
                self.balldy += self.gravity # apply gravity
               
                self.repaint() # force repaint

        def paintEvent(self, event):
                self.paint_count += 1            # count frames
                print 'Paint:', self.paint_count
               
                p = QPainter(self) # draw background rectangle
                p.setPen(Qt.blue)
                p.setBrush(Qt.red)
                p.drawRect(10, 10, self.width() - 20, self.height() - 20)

                width = self.width() - 40 # calculate pie slice dimensions
                height = self.height() - 40
                x = (self.width() / 2) - (width / 2)
                y = (self.height() / 2) - (height / 2)

                p.setPen(Qt.NoPen) # draw pie slice
                p.setBrush(Qt.green)
                p.drawPie(x, y, width, height, self.angle, 45 * 16)

                p.setPen(Qt.NoPen) # draw ball
                p.setBrush(Qt.blue)
                p.drawEllipse(self.ballx, self.bally, self.balld, self.balld)
                p.setBrush(Qt.cyan) # draw ball highlight
                p.drawEllipse(self.ballx+self.balld/4, self.bally+self.balld/4, self.balld/3, self.balld/3)

class MainWindow(QMainWindow):
        def __init__(self):
                QMainWindow.__init__(self)

                drawing = DrawingTest()
                self.setCentralWidget(drawing)

app = QApplication(sys.argv)
w = MainWindow();
w.show()
sys.exit(app.exec_())

# EOF

Basically it shows a rotating pie slice shape on a red rectangle with a blue ball bouncing in the foreground.

rsciw 06-19-2010 03:41 PM

Quote:

Originally Posted by frieza (Post 3981974)
i've probably posted this somewhere else in this forum at some point in time but.. a simple image randomization script for message board signatures (yes i probably could have randomized based on a directory list but i was too lazy and didn't know how at the time when i wrote it)
Code:

<?php
$me = rand(1,8);
$pic=imagecreatefromjpeg($me.".jpg");
list($width, $height, $type, $attr) = getimagesize($me.".jpg");
$layer = imagecreatetruecolor  ( 150  , 150  );
imagecopyresized  ( $layer  , $pic  , 0  , 0  , 0  , 0  , 150  , 150  , $width  , $height  );
header("Content-type: image/png");
imagejpeg($layer);
imagedestroy($layer);
imagedestroy($pic);
?>


neat, I've got something like that too :)
in one I display a few characters randomly of an MMO I play in the respective forum, in the other I just have a mockup of Schroedinger's cat, randomly one of the two given possibilities ^^

frieza 09-02-2010 06:35 PM

a bit of nostalgia
Code:

CLS
INPUT "number of columns"; cols
CLS
FOR x = 1 TO cols
        FOR z = 1 TO (cols - x)
                st$ = st$ + " "
        NEXT
        FOR y = 1 TO x
                st$ = st$ + "* "
        NEXT
        PRINT st$
        st$ = ""
NEXT

any guess as to the programming language? :p

same program in c++
Code:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
        if (argv[1] == NULL) {
                cout << "usage: pyramid number of rows\n";
                return 1;
        }
        int cols=atoi(argv[1]);
        for (int n=0; n<=cols; n++) {
                for (int z=0;z<=(cols-n);z++) {
                    cout << " ";
                }
                for (int y=0;y<=n;y++) {

                        cout << "* ";
                }
                cout << "\n";
        }
        return 1;
}

and in c++ as a cgi program
accessable when compiled and placed in the cgi-bin folder by
Code:

http://{host}/cgi-bin/pyramid.cgi?{number of colums}
Code:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main (int argc, char* argv[])
{
              cout<<"Content-type: text/html"<<endl<<endl;
        if (argv[1] == NULL) {
                cout << "usage: pyramid number of rows\n";
                return 1;
        }
        int cols=atoi(argv[1]);
        for (int n=0; n<=cols; n++) {
                for (int z=0;z<=(cols-n);z++) {
                    cout << "&nbsp;";
                }
                for (int y=0;y<=n;y++) {

                        cout << "*&nbsp;";
                }
                cout << "<br />";
        }
        return 1;
}


frieza 09-10-2010 06:38 PM

Code:

<?php
include("Archive/Tar.php");
$tar = new Archive_Tar("stuff.tar.bz2");
$filec=$tar->extractInString($_GET['file'].'.php');
$sanitized=ereg_replace("<\?php",'',ereg_replace("\?>",'',$filec));
eval($sanitized);
?>

:p

MTK358 09-10-2010 07:28 PM

What does it do?

frieza 09-11-2010 11:42 AM

its extracts php code from a file ($_GET['file']) into memory from a tarball (on the fly) and executes it with eval()
(requires PEAR and the PEAR archive module)
would be invoked by
Code:

http://address/{whatevernameyougivethescript}.php?file={fileinarchive} (.php extension assumed)

schneidz 11-03-2012 09:25 AM

xbmc addons
 
is this thread still alive ?; here is a link to a few of my xbmc addons written in python:

http://hyper.homeftp.net/xbmc/


All times are GMT -5. The time now is 04:56 PM.