Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

pls halp

Name: Anonymous 2010-12-15 4:32

Bros. BROS. Why wont 'x' exit proper? WHERE DID I GO WRONG
[code]#include <stdio.h>

/* DINGA LINGA DOO!!! */

int derp();

main()
{
    int c = 0;
    int leaf = 0,rock = 0;
    int i;

printf("random text.\n");
    while ((c = getchar()) != EOF)
        if (c == 'l')
            ++leaf;
        else if (c == 'r')
            ++rock;
        else if (c == 'z')
            derp();
        else if (c == 'x')
            printf("Goodbye, world!\n");
            return 0;
        else
            printf("leaf:%d\trock:%d\n", leaf, rock);
}

int derp()
{
    printf("random text.\n");
    return 0;
}[/code[

Name: Anonymous 2010-12-15 4:34

shit i fail

#include <stdio.h>

/* DINGA LINGA DOO!!! */

int derp();

main()
{
    int c = 0;
    int leaf = 0,rock = 0;
    int i;

printf("random text.\n");
    while ((c = getchar()) != EOF)
        if (c == 'l')
            ++leaf;
        else if (c == 'r')
            ++rock;
        else if (c == 'z')
            derp();
        else if (c == 'x')
            printf("Goodbye, world!\n");
            return 0;
        else
            printf("leaf:%d\trock:%d\n", leaf, rock);
}

int derp()
{
    printf("random text.\n");
    return 0;
}

Name: Anonymous 2010-12-15 4:46

(Post expanded.)

Name: Anonymous 2010-12-15 5:07

int derp();
Find two problems with this line.

Name: Anonymous 2010-12-15 5:12

>>4
Unnecessary int for a function that should be void and derp.

Name: Anonymous 2010-12-15 5:19

>>4,5
Also no void between parens

Name: herp 2010-12-15 6:22

derp

Name: Anonymous 2010-12-15 8:29

C isn't python, you need {} to define your while block.

Name: Anonymous 2010-12-15 8:38

The fuck's wrong with switch?

Name: Anonymous 2010-12-15 8:39

>>1
Your program needs more semicolons. You should have at least five semicolons at the end of a return statement and at least three after a printf call.

Name: Anonymous 2010-12-15 8:54

thx, gang

Name: Anonymous 2010-12-15 9:23

You people are miserable.

Name: Anonymous 2010-12-15 9:23

You people are miserable.

Name: Anonymous 2010-12-15 11:53

>>12-13
Your assumption that there are "people" here was your undoing.

Name: Anonymous 2010-12-15 23:02

Hey everybody! Come look at a Pythonista's shot at C!

Name: Anonymous 2010-12-16 8:18

My python doesn't have a switch.
How does he jump table?
Terrible!

Name: Anonymous 2010-12-16 9:16

use exit() :D

Name: Anonymous 2010-12-16 12:40

:D

Name: Anonymous 2010-12-16 13:47

Why wont 'x' exit pooper?

Name: Anonymous 2010-12-16 14:18

>>15
Fuck off and die.

Name: Anonymous 2010-12-16 15:59

☻☻☻☻

Welcome to Detroit!

Name: Anonymous 2010-12-16 18:56

Test

Name: Anonymous 2010-12-16 20:36

>>21
Marshal Mathers iswas white.

Name: Anonymous 2011-02-04 12:45

Name: Anonymous 2011-06-03 23:15

>>14
#!/usr/bin/php -q
<?php

/**
 * Set of classes for handling advanced operations on strings,
 * as per application guidelines.
 *
 * @package AdvancedStringHandling
 * @author Anonymous
 */


interface ASH_IStringContainer
{
    public function __construct($string);
    public function __toString();
}

interface ASH_IFactory
{   
    public static function create($type);
}

class ASH_StandardStringFunctionAsMethodCallException extends InvalidArgumentException
{
    const UNSPECIFIED = 0;
    const UNRECOGNIZED = 1;
    const UNSUPPORTED = 2;
   
    public function __construct($code = self::UNSPECIFIED, $message = '')
    {
        $this->code = (int)$code;
        $this->message = (string)$message;
    }
}   

class ASH_FactoryException extends Exception { }
class ASH_StringContainerFactoryException extends ASH_FactoryException { }



/**
 * Base inheritance class for all string containers. Not to be used as a standard string container.
 *
 * @abstract
 * @see ASH_StandardString
 */
abstract class ASH_StringContainer implements Countable, ArrayAccess, ASH_IStringContainer
{
    protected $str = array();
   
    public function __construct($string = '')
    {
        $this->_set($string);
    }
   
    public function __toString()
    {
        return implode($this->str);
    }
   
    /**
     * Magic method for allowing chainable standard string function calls on the string container's internal string.
     *
     * @final
     * @param mixed ... Use whichever variables are appropriate for the function call you want to make
     * @return $this if the result of the call was null, or if the call modified the internal string/array, otherwise returns the value of the call
     */
    final public function __call($name, $arguments)
    {
        switch ($name)
        {
            case 'echo':
            case 'print':
                return call_user_func_array(array(__CLASS__, '_echo'), $arguments);
            break;
           
            default:
                try
                {
                    $rf = new ReflectionFunction($name);
                    $offset = -1;
                   
                    // determine where our stored string is supposed to go in the argument list
                   
                    foreach ($rf->getParameters() as $paramNum => $param)
                    {
                        switch ($param->getName())
                        {
                            case 'glue':
                            case 'subject':
                            case 'str':
                            case 'str1':
                            case 'string':
                            case 'hebrew_text':
                                $offset = $paramNum;
                            break 2;
                        }
                    }
                   
                    if ($offset === -1)
                    {
                        throw new ASH_StandardStringFunctionAsMethodCallException(
                            ASH_StandardStringFunctionAsMethodCallException::UNSUPPORTED,
                            "Function {$name} not a supported standard string function to use as a magic method with StringContainer."
                        );
                    }
                    else
                    {
                        // insert our stored string in the appropriate space in the argument list before we pass it to the function
                        array_splice($arguments, $offset, 0, (string)$this);
                                               
                        $retval = $rf->invokeArgs($arguments);
                       
                        // should we return a (chainable) reference to self, or keep the value given to use by the call?
                        if (is_string($retval) || is_array($retval))
                        {
                            // this function intends to modify the stored value
                            $this->_set($retval);
                            $retval =& $this;
                        }
                        elseif(is_null($retval))
                        {
                            $retval =& $this;
                        }
                        return $retval;
                    }
                }
                catch (ReflectionException $e)
                {
                    throw new ASH_StandardStringFunctionAsMethodCallException(
                        ASH_StandardStringFunctionAsMethodCallException::UNRECOGNIZED,
                        "Function {$name} is not a known function."
                    );
                }
            break;
        }
       
    }
   
   
    /**
     * Manages the intricacies of our internal "string array"
     * @return $this
     */
    private function _set($string)
    {
        $this->str = str_split(is_array($string) ? implode(' ', $string) : (string)$string);
    }
   
    /**
     * Chainable command for outputting current state of the string.
     * @return $this
     */
    private function _echo($prepend = '', $append = '')
    {
        print($prepend . (string)$this . $append);
        return $this;
    }
   
    function prepend($string)
    {
        $this->_set($string . (string)$this);
        return $this;
    }
   
    function append($string)
    {
        $this->_set((string)$this . $string);
        return $this;
    }
   
    function set($string)
    {
        $this->_set($string);
    }
   
/// Countable implementation
 
    public function count()
    {
        return sizeof($this->str);
    }

/// ArrayAccess implementation
   
    public function offsetSet($offset, $value)
    {
        $this->str[$offset] = (string)$value;
    }
   
    public function offsetExists($offset)
    {
        return isset($this->str[$offset]);
    }
   
    public function offsetUnset($offset)
    {
        $this->str = array_splice($this->str, $offset, 1);
    }
   
    public function offsetGet($offset)
    {
        return $this->str[$offset];
    }   
}

/**
 * Standard string container, default created by factory
 */
class ASH_StringContainer_Standard extends ASH_StringContainer { }

/**
 * Standard string container, default created by factory
 */
class ASH_StringContainer_Reverse extends ASH_StringContainer
{
    public function __construct($string)
    {
        parent::__construct($string);
        $this->str = array_reverse($this->str);
    }
}


abstract class ASH_Factory implements ASH_IFactory
{
    /**
     * used to cache classname bases (expensive string operations)
     * @internal
     */
    private static $classnameBaseLookup = array();
   
    /**
     * Private constructor prevents inherited classes from being able to instantiate
     * @private
     */
    private function __construct()
    {
    }
   
    /**
     * Default implementation of factory creation.
     * If the class to be created does not follow naming conventions,
     * or requires arguments in the constructor, you must override $create.
     *
     * @return object classname decided by naming convention
     * @see getClassName
     * @see getClassNameBase
     */   
    public static function create($type)
    {
        $className = self::getClassName($type);
        if (class_exists($className))
        {
            return new $className();
        }
        else
        {
            throw new ASH_FactoryException("Unknown class type {$containerType} for factory " . get_called_class());
        }
    }
   
    /**
     * @return string Full class name being called by the Factory (by convention)
     */   
    protected static function getClassName($type)
    {
        $classSubname = preg_replace('/[\s]+/', ' ', ucwords(preg_replace('/[^a-zA-Z ]/', '', (string)$type)));
        return self::getClassNameBase() . "_{$classSubname}";
    }
   
    /**
     * @return string Base class name prefix for which the inherited factory is associated (by convention)
     */
    protected static function getClassNameBase()
    {
        $classname = get_called_class();
        if (!isset(self::$classnameBaseLookup[$classname]))
        {
            $parts = explode('_', $classname);
            if ('Factory' === end($parts))
            {
                array_pop($parts);
            }
            self::$classnameBaseLookup[$classname] = implode('_', $parts);
        }
        return self::$classnameBaseLookup[$classname];
    }
}

abstract class ASH_StringContainer_Factory extends ASH_Factory
{
    const DEFAULT_CONTAINER_TYPE = 'Standard';
   
    public static function create($type = self::DEFAULT_CONTAINER_TYPE)
    {
        return self::createString('', $type);
    }
   
    public static function createString($string, $type = self::DEFAULT_CONTAINER_TYPE)
    {
        $className = self::getClassName($type);
        if (class_exists($className) && is_subclass_of($className, self::getClassNameBase()))
        {
            return new $className($string);
        }
        else
        {
            throw new ASH_StringContainerFactoryException("Unknown StringContainer type {$type} (expected class {$className})");
        }
    }
}


/**
 * Inline Unit Test (IUT)
 * @internal
 */
if (isset($argc) && strstr($argv[0], basename(__FILE__)))
{
    // environment setup
    $argumentString = implode(' ', array_slice($argv, 1));
    $argumentString = $argumentString ? $argumentString : 'Default Unit Test String';
   
    // test
    ASH_StringContainer_Factory::createString($argumentString, 'Reverse')
        ->print()
        ->strrev()
        ->print("\n")
        ->strtoupper()
        ->append(' (I am typing in all caps to indicate that I am shouting)')
        ->print("\n");
}


$ php -f "AdvancedStringHandling_Main.php"
gnirtS tseT tinU tluafeD
Default Unit Test String
DEFAULT UNIT TEST STRING (I am typing in all caps to indicate that I am shouting)

Don't change these.
Name: Email:
Entire Thread Thread List