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/)

Kenny_Strawn 05-06-2010 06:05 PM

Source Code Superthread
 
If any of you write code, this is the thread to post it in. Show us some samples of code if you like, and also use this thread to give tips to writing good code. I decided to create a superthread for code lessons and code posted just for the fun of it. But code to help people has to stay in the support thread it was posted in.

Jeebizz 05-06-2010 06:48 PM

Code:

#include <iostream>

int main()
  {
  cout<<"Hello World!"<<endl;
  return 0;
  }

And I'm patenting it! Next person to write a hello world program in C++ owes me money! :mad:

:rolleyes:

exvor 05-06-2010 07:46 PM

Thats ok because I patented hello world in C and thats where the C++ variant came from so you actually owe me money :P.

Code:


#include<stdio.h>

int main (int argc, char * argv[])
{
  printf("Hello world!\n");
  return 0;
}


Jeebizz 05-06-2010 08:02 PM

And to quote Homer Simpson: DOH! :doh:

exvor 05-06-2010 08:25 PM

Thus illustrating why software patents are the worst idea ever in the software industry.

Dogs 05-06-2010 08:28 PM

Ok... For an assignment that took 1 minute to read and understand, I wrote all of this code.

Literally, the assignment said get num from user, raise it to power, print results, check if they want to do another number.


main.cpp

Code:


#include <iostream>
#include "user.h"
#include "powerizer.h"
#include "replay.h"
#include "header_footer.h"
using namespace std;


/*

        C++ Beginning - Lab 4 - 1307.xxx
        April 9, 2010.
       
        A-Option -
        Inputs:
                User enters number and power
               
        Outputs:

                Program prints number raised to power to stdout.

        Error conditions are handled during number submission
        and answer submission. Option to repeat the program is
        included.
*/

int main_driver();

int main(){

        // greet the user
       
        print_intro();

        // Initiate the main controlling section of the program
       
        main_driver();

        // die

        return 0;
               
}


int main_driver(){
   
    int number;
    int power;
    long int result;


    // get number from user
    // get power from user
    // raise number to power
    // print results
    // check for replay
   
    do{
         
          // get number and power
         
          cout << "Enter the number to raise to a power." << endl;
          number = user_submitted.g_number();
          cout << "Enter the power to raise number to." << endl;
          power = user_submitted.g_number();

          // do it
         
          result = process.raise_number_to_power(number, power);
         
          // print out the results
         
          print_results(number, power, result);

          // check for replay
         
    }while(option_for_replay());
   
return 0;
}


user.h

Code:

/*
 *        Welcome to user.h. This header file contains routines to
 *        handle user input regarding the numbers they want to
 *        work with in the program (and not for user input
 *        regarding whether or not the user wants to restart
 *        the program. That is handled by option_for_replay.h)
 */


#include <iostream>
using namespace std;

#define MAX_ELEMENT 20 // max elements in any array in this program.

#define debug 0 //  <- make this positive for debug messages.

/*
  In an effort to better understand C, I'll use
  structures for this assignment. In an effort to write
  better code, I've done all of this in an 80x25 terminal
  and using vi, which is the most challenging text editor
  I've come across so far. Also with a serious attempt at
  readable code.
*/

// Build structures for user submitted info


/* user_submitted_entires pertains to the numer, and the power
    that the user entered. It contains public functions
    for getting a number, and printing number and power. */



struct user_submitted_entries{

    // g_number() gets a number and returns it.
   
    int g_number();
         
    private:

          // Private variables number and power.

          int power;
          int number;

}user_submitted; // <-- create an object user_submitted that can be accessed globally.


/*        user_input pertains to the validity of the user's submissions.
          it contains public functions to prepare the array for
          the users entry, to check it, to get the length,
          and to convert the array to an integer. */

struct user_input{
   
    // dot_array() goes from 0 to max_element filling the array with '.'s
   
    void dot_array(char array[]);

    // validate_entry() checks to see if the users submission was useable in the
    // rest of the program. It returns 1 if the answer is bad, 0 if it is good.
   
    int validate_entry(char ent_num[]);
   
    // g_num_of_elements_in() scans the array for things that aren't '.'s, and returns where the last one was found.
   
    int g_num_of_elements_in(char num[]);

    // make_char_array_a_number() takes an array and its length, and converts it into an integer.
   
    int make_char_array_a_number(char num[], int num_of_elements);

}input; // <-- create object input that can be accessed globally.



// I was happy to learn that part of an idea
// I came up with for multiplying numbers
// in a matrix that is designed to
// replicate physically writing math
/* on a piece of notebook paper would work in this program.


    ( the matrix-math program is useful because I can store more than
          2^32 in a 100 element vector.. a lot more
          in the end the idea would simulate long-hand multiplication,
          addition, subtraction, and division of problems that could be
          multiple pages wide.

          --- UPDATE --- Professor says Arrays != Matrices

              For example -


          2342342342378297834234
          4534534534534543523423
          *_____________________
          //////////// A ////////
          /////////// lot of//////
          //////////work  here///
          +_____________________
          /// bigger answer here      ///
          // but remember that everything
          // you see is an element in the
          // matrix                // )
*/



/*
        this function moves through a char array,
        strips each element of its ASCII code, multiplies the
        the element by the power of 10 represented by its
        place in the array (123 = 3*1+2*10+1*100), and
        assigns the result to "number", which is then returned.
        The char array is started from the end, because 123 is
        stored in elements [0][1][2] respectively. I did this
        because I didn't know how to handle situations where
        a user is entering an integer number, but instead inputs
        a letter or some other nonsense. The results were generally overflows.
*/
       
int user_input::make_char_array_a_number(char num[], int num_of_elements){

        if(debug){ cout << "Entered make_char_array_a_number()" << endl; }

        int converted_to_number = 0;
        int current_power_of_ten;
        int current_element;

        for(        current_element = num_of_elements,  current_power_of_ten = 1;

                                  --current_element >= 0;
       
          current_power_of_ten *= 10          ){
           
                    converted_to_number += (num[current_element] - '0') * current_power_of_ten;
        }

if(debug){ cout << "Leaving make_char_array_a_number()." << endl; }
       
        return converted_to_number;
}                       


// get the number, and make sure it is a number!

int user_submitted_entries::g_number(){
        char entered_num[MAX_ELEMENT];
        int gotten_number;
        int length;
       
       
        if(debug){ cout << "Entered g_number()." << endl; }

        // prompt toggle changes the prompt from default friendly to
        // "restart and enter the right information" mode after the first run through the program
        // because, presumably, if you come back through for any reason, it is because you entered something wrong.
       
        int prompt_toggle = 0;
        do{
                if(prompt_toggle == 0){
                        cout << ":";
                }else{
                        cout << "Positive Entries Only" << endl;
                        cout << ":";
                }
                prompt_toggle++;


                if(debug){ cout << "In g_number() -> dotting array" << endl; }

              /* to avoid having the array cluttered with old answers, especially
                  in the case of wrong answers, it is necessary to clean the array
                  before each use. I hereby state that being full of periods is being clean.*/

                input.dot_array(entered_num);

                if(debug){ cout << "In g_number() -> Done dotting array" << endl; }
       
                cin >> entered_num;


                /*
                  if entered_num turns out to be
                  something other than a number or a '.',
                  then validate_entry will be positive.
                */
       
        if(debug){ cout << "In g_number() -> validating entry" << endl; }
               
        }while( input.validate_entry(entered_num) );

        if(debug){ cout << "In g_number() -> Entry validated" << endl; }
       
       
        /* g_num_of_elements_in searches from 0 to MAX_ELEMENT
          for a period. This will indicate the end of the
          entered number. It depends on validate_entry being called
          before it. Convenient, since validate_entry is designed
          to fit in the condition of a do{}while() loop.
          g_num_of_elements_in does not care what you entered, just
          how much you entered. As opposed to validate_entry
          which does not care about how much you entered, but what
          you actually entered.
          */
                   

        if(debug){ cout << "In g_number() -> Getting Length" << endl; }

        length = input.g_num_of_elements_in(entered_num);       

        if(debug){ cout << "In g_number() -> Length Obtained" << endl; }

       
        /*
                this aptly named function does exactly
                what it says it does... as it tears apart
                an array of characters, walks through
                each one and multiplies it by the
                power of 10 represented by its position
                in the array, the results are stored in an integer.
                It returns that integer.
        */
       
        if(debug){ cout << "In g_number() -> Making char* to number" << endl; }


              gotten_number = input.make_char_array_a_number(entered_num, length);
       

        if(debug){ cout << "In g_number() -> char* is now a number." << endl; }


        cout << "You entered [" << gotten_number << "]\n" << endl;

        if(debug){ cout << "Leaving g_number()" << endl; }

        // Finally, return the number you got.

        return gotten_number;
}


    /*
    my answer to the question of, "How do I initialize an array
    so that it is uniform except for situations I'm looking for,
    and that will allow me to perform mathematical operations on
    each element without any issues was to intialize the character
    array with periods, and use my tests based on whether or not
    something was a period, a number, or something I don't want.

    */


// Write to array[i] with periods.

inline void user_input::dot_array(char array[]){
       
        if(debug){ cout << "Entered dot_array()" << endl; }

        int current_element;
        for(current_element = MAX_ELEMENT;
              --current_element >= 0;                ){

            array[current_element] = '.';
        }
        if(debug){ cout << "Verification of write to array" << endl;
                  cout << "Start value [" << array[0] << "] End value [";
                  cout << array[MAX_ELEMENT - 1] << "]" << endl;
        }

        if(debug){ cout << "Leaving dot_array()" << endl; }

}

/*
  This exceedingly inefficient, but Russian Reliable method
  for finding the length of an array is part of another idea I have.
  It works by going from one end of an array of dots to the other,
  looking for things that aren't dots. Mostly this is a result
  of my lack of understanding when it comes to reliably validating data.

  Though I will say this.. The issue with my user input method
  is not caused by cin or by the looping structure. It is caused by
  some buffer I am not aware of that retains the information even
  when my program rejects it. Some buffer that feeds cin. I don't know
  if it is operating system specific, or if it is specific to C++,
        but I have the same issue when I don't use cin (ex, happens with getch, getchar, scanf, etc)
        so stdin must have a buffer or something that bridges stdin to the program.
*/

inline int user_input::g_num_of_elements_in(char check[]){

        if(debug){ cout << "Entered g_num_of_elements_in()" << endl; }


        int found_length;       
        int cur_elem;


        // go from 0 to max_element looking for things that are '.'
       
        for( cur_elem = 0; cur_elem  < MAX_ELEMENT; cur_elem++    ){

       
                        if( check[cur_elem] == '.'){
                                found_length = cur_elem-1;
                               
                                if(debug){       
                                        cout << "Found Last digit in element[";
                                        cout << cur_elem-2 << "]";
                                        cout << "\nRETURNING:  " << found_length;
                                        cout << endl;
                                }
                                if(debug){ cout << "leaving g_number()\n"; }

                               
                                return found_length;
                        }else{
                        // do nothing because it is not the number, but part of the number.
                        }
        }

        if(debug){ cout << "Failed in g_num_of_elements_in()." << endl; }

        return 0;
}


// check to make sure the input is valid.

inline int user_input::validate_entry(char ent_num[]){

        if(debug){ cout << "Entered validate_entry" << endl; }


        int i;

        // TEST CONDITIONS FOR VALIDITY

        bool failed_entry = false;
        for(i = 0; i < MAX_ELEMENT && ent_num[i] != '\0'; i++){

            if( !(ent_num[i] == '.') && !(isdigit(ent_num[i]))){ failed_entry = true; }

        }
           


          if(debug && !failed_entry){ cout << "Answer was good." << endl; }

    return failed_entry;
}


replay.h

Code:

#include <iostream>
#include <cctype>
using namespace std;

// Brain freeze at 4AM..
// super obvious example ensues.

/*
        Basically, allow the user to enter whatever.
        After that, check only the first element of the char array
        that the user just filled with whatever. If it isn't
        yes or no, ask again (but also switch the message.)
        First pass through for the message is a greeting,
        the next is a courteous error message.
*/

int option_for_replay(){
        char users_response_to_question[100];
        char extracted_answer;
        bool wrong_answer = false;     

        do{
                if(!wrong_answer){

                cout << "Would you like to go again? enter Yyes or Nno\n";

                }else{
               
                cout << "Please enter a valid response of Yyes or Nno" << endl;

                }

                cin >> users_response_to_question;
                extracted_answer = users_response_to_question[0];

                if(        isalpha(      extracted_answer    )          ){
                        if(        extracted_answer                        //is
                                        == 'y'                        ||        //or
                                extracted_answer                        //is
                                        == 'Y'                        ){
                                // then the answer is yes

                                cout << "Ok, restarting the program.\n\n";
                                return true;

                        }else if(        extracted_answer                //is
                                                == 'n'                ||        //or
                                        extracted_answer                //is
                                                =='N'          ){
                                // then the answer is no
                       
                                cout << "Shutting down the program.\n" << endl;       
                                return false;

                        }else{
                                wrong_answer = true;
                        }
                }else{
                        wrong_answer = true;
                }
        }while(wrong_answer);
       
cout <<  "Never should have gotten here.. go ahead and restart then." << endl;
return true;

}

powerizer.h

Code:

#include <iostream>
using namespace std;

// Struct declaration and definition

/*
        pretty basic way of raising num to pow.
        take the number that has been interrogated
        thoroughly, and multiply it by its power, which has
        also been interrogated.
        Store that result in number_raised_to_power;
*/

struct powerizer{

        // variable for storing the results

        long int number_raised_to_power;

        // function for factorializing a number

        int raise_number_to_power(int &num, int &pow);


}process; // object process to be accessible globally.



int powerizer::raise_number_to_power(int &base, int &pow){
      int p = pow;
      if(debug){ cout << "Entered raise_number_to_power(int base, int pow)" << endl; }

        // number_raised_to_power is assigned 1
        // because its first use is in multiplication.
        // pow controls the loop, num is the base

        number_raised_to_power = 1;



        for( ; --p >= 0; ){
                number_raised_to_power *= base;
                if(debug){ cout << pow << endl; }
        }
       
        return number_raised_to_power;

        if(debug){ cout << "Leaving raise_number_to_power()" << endl; }

}


header_footer.h

Code:

#include <iostream>
using namespace std;

// Print welcome screen

void print_intro(){
        cout << "\n\n==================================================================" << endl;
        cout << "Welcome to Jonathan Findley's Powerizer v1.00" << endl;
        cout << "Part of C++ Lab 4 on loops, powers, and that sort of thing...." << endl;
        cout << "Enter the number you want to raise to a power, " << endl;
        cout << "and the program will print the result after exponentiation." << endl;
        cout << "==================================================================\n" << endl;
}

// Print results

void print_results(int &number, int &power, long int &result){
        cout << "======================================================" << endl;
        cout << number << " raised to the power of ";
        cout << power << " is equal to ";
        cout << result;
        cout << endl;
        cout << "======================================================" << endl;
}


brianL 05-06-2010 09:29 PM

You're mistaken, these two are my Intellectual Property:
Code:

#include <iostream>

int main()
  {
  cout<<"Hello Brian!"<<endl;
  return 0;
  }

Code:

#include<stdio.h>

int main (int argc, char * argv[])
{
  printf("Hello Brian!\n");
  return 0;
}


MrCode 05-06-2010 10:22 PM

Written after seeing this xkcd:

Code:

#include <stdio.h>
#include <stdlib.h>

int numIters = 0;

int Collatz(int num,int maxIters)
{
        printf("%i\n",num);
        numIters += 1;

        if(numIters <= maxIters)
        {
                if(num > 1)
                {
                        if(num % 2 == 0)
                                Collatz(num / 2,maxIters);
                        else
                                Collatz((num * 3) - 1,maxIters);
                }
                else
                {
                        printf("The number provided reduces to 1.\nThis would be a point in a Collatz fractal.\n");
                        return 0;
                }
        }
        else
        {
                printf("The number provided causes the iterations to escape to infinity.\nThis would not be a point in a Collatz fractal.\n");
                return 1;
        }

        return 0;
}

int main(int argc,char* argv[])
{
        if(argc != 3)
                printf("Usage: ./collatz <number> <max iterations>\nUsed for calculating a Collatz sequence based on the number passed to the\nprogram.\n");

        int numParam,iterParam;

        numParam = atoi(argv[1]);
        iterParam = atoi(argv[2]);

        Collatz(numParam,iterParam);

        return 0;
}

I would totally use the algorithm to write a fractal generator, but (as of yet) I don't know how to handle pixel-by-pixel graphics in GTK+. Time to do some reading! :D

smeezekitty 05-07-2010 12:03 AM

Quote:

Originally Posted by MrCode (Post 3959936)
Written after seeing this xkcd:

Code:

#include <stdio.h>
#include <stdlib.h>

int numIters = 0;

int Collatz(int num,int maxIters)
{
        printf("%i\n",num);
        numIters += 1;

        if(numIters <= maxIters)
        {
                if(num > 1)
                {
                        if(num % 2 == 0)
                                Collatz(num / 2,maxIters);
                        else
                                Collatz((num * 3) - 1,maxIters);
                }
                else
                {
                        printf("The number provided reduces to 1.\nThis would be a point in a Collatz fractal.\n");
                        return 0;
                }
        }
        else
        {
                printf("The number provided causes the iterations to escape to infinity.\nThis would not be a point in a Collatz fractal.\n");
                return 1;
        }

        return 0;
}

int main(int argc,char* argv[])
{
        if(argc != 3)
                printf("Usage: ./collatz <number> <max iterations>\nUsed for calculating a Collatz sequence based on the number passed to the\nprogram.\n");

        int numParam,iterParam;

        numParam = atoi(argv[1]);
        iterParam = atoi(argv[2]);

        Collatz(numParam,iterParam);

        return 0;
}

I would totally use the algorithm to write a fractal generator, but (as of yet) I don't know how to handle pixel-by-pixel graphics in GTK+. Time to do some reading! :D

Why not use SDL, it beats the pants off of GTK for pixel-by-pixel.

jiml8 05-07-2010 02:40 AM

I got you all beat. Here is my original patented Fortran 66 "Hello World" program.

Code:


      PROGRAM HIWORLD(INPUT,OUTPUT,TAPE60=INPUT,TAPE61=OUTPUT)
      CHARACTER HIW*11
      PARAMETER HIW='Hello World"
      WRITE (200,61) HIW
200  FORMAT(1X, 11A1)
      END

Y'all owe ME, since all of your work is a derivative of my original work.

Kenny_Strawn 05-16-2010 08:31 PM

Any idea WHY you like software patents? Other than to lock software (and any competitors) to the GPL, I don't see any need for them other than to create a monopoly and violate antitrust law.

exvor 05-16-2010 08:33 PM

Quote:

Originally Posted by Kenny_Strawn (Post 3970911)
Any idea WHY you like software patents? Other than to lock software (and any competitors) to the GPL, I don't see any need for them other than to create a monopoly and violate antitrust law.

Kenny I don't think anyone here likes software patents? The whole point of our jesting is to make fun of it.

fbsduser 05-25-2010 09:25 PM

Code:

SECTION .data

        msg        db        "Hello, world!",0xa ;
        len        equ    $ - msg

        SECTION .text
        global main

main:
        mov    eax,4                ; write system call
        mov    ebx,1          ; file (stdou)
        mov    ecx,msg        ; string
        mov    edx,len        ; strlen
        int    0x80                ; call kernel

        mov        eax,1                ; exit system call
        mov    ebx,0     
        int    0x80                ; call kernel

Ya'll OWE ME 'cause I patented Hello World in assembler :rolleyes: ;)

linuxlover.chaitanya 05-26-2010 01:08 AM

Nobody owes me money :( I use bash

Code:

#!/bin/bash

        echo "Hello world"


MrCode 05-26-2010 01:35 AM

askme revised (WIP):

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...",
                    "Who are YOU?",
                    "Why do you care?"};

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,questions[i]) == 0)
                                printf("%s",answers[i]);
                        else
                                break;
                }
        }       

        return 0;
}

Can't get it to print anything when I issue one of the queries, though. I'm giving the argument in quotes, too. :scratch:

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/

exvor 11-05-2012 09:55 PM

No this thread died like 2 years ago. Strange to see it come back up in my box though :)

sundialsvcs 11-05-2012 10:02 PM

The (non-existent) sex-life of tacos ... a most unusual "siggy." ;) But, very logical.


All times are GMT -5. The time now is 03:36 PM.