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

Let's make some simple programs!

Name: Anonymous 2010-05-08 4:03

Because I know that half of /prog/ can't program, let's make simple ones!

---------------------- MySavings.java ----------------------

/**
 * @(#)MySavings.java
 *
 * MySavings application
 *
 * @author Tim H
 * @version 1.00 2010/5/7
 */
 
import java.util.Scanner;

public class MySavings {
   
    public static void printInterface() {
        System.out.print(
            "1. Show total in bank.\n" +
            "2. Add a penny.\n" +
            "3. Add a nickel.\n" +
            "4. Add a dime.\n" +
            "5. Add a quarter.\n" +
            "6. Take money of out bank.\n" +
            "Enter 0 to quit.\n" +
            "Enter your choice: "
            );
    }
   
    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        int choice = 1;
        int moneyChoice = 0;
        double amount = 0;
       
        PiggyBank bank = new PiggyBank();
       
        while(choice > 0){
            printInterface();
            choice = input.nextInt();
           
            switch(choice){
                case 0: System.out.println("(c)Tim H 2010\nHave a nice day!"); break;
                case 1: System.out.println("Your current total is: " + bank.GetTotal()); break;
                case 2:
                case 3:
                case 4:
                case 5: moneyChoice = choice - 2; bank.AddMoney(moneyChoice); break;
                case 6:
                    System.out.print("Enter the amount you wish to withdraw: ");
                    amount = input.nextDouble();
                    bank.SubtractTotal(amount);
                    break;
                default:
                    System.out.println("Please enter a choice between 1-6!");
                    break;
            }
               
        }

---------------------- PiggyBank.java ----------------------


class PiggyBank {
    double total;
   
   
    public PiggyBank(){
        total = 100;
    }
   
    public void AddMoney(int choice){
        switch (choice) {
            case 0:
                total += 0.01;
                System.out.println("One penny added to total!\nYour total is now: " + total);
                break;
            case 1:
                total += 0.05;
                System.out.println("One nickel added to total!\nYour total is now: " + total);
                break;
            case 2:
                total += 0.10;
                System.out.println("One dime added to total!\nYour total is now: " + total);
                break;
            case 3:
                total += 0.25;
                System.out.println("One quarter added to total!\nYour total is now: " + total);
                break;
           
        }
    }
   
    public double GetTotal(){
        return total;
    }
   
    public void SubtractTotal(double amount){
        total -= amount;
        System.out.println(amount + " has been subtracted from your total!\nYour total is now: " + total);
    }
}

    }
   
}

Name: Anonymous 2010-05-09 6:12

toy programs? look out, here comes some FOIC!
this code is actually faster than the factorial function in the math module (which is written in c!)... proof that guido can't code for shit.
from itertools import takewhile, dropwhile

def eratosthenes():
        D = {}
        q = 2
        while 1:
                if q not in D:
                        yield q
                        D[q*q] = [q]
                else:
                        for p in D[q]:
                                D.setdefault(p+q,[]).append(p)
                        del D[q]
                q += 1

def bitcount(n):
        r = 0;
        while n > 0:
                r += n & 1
                n >>=1
        return r

def take(n, g):
        for i in range(n): yield g.next()

def swing(n):
        primes = list(takewhile(lambda x: x <= n, eratosthenes()))
        smalloddswing = [1,1,1,3,3,15,5,35,35,315,63,693,231,3003,429,6435,6435,109395,12155,230945,46189,969969,88179,2028117,676039,16900975,1300075,35102025,5014575,145422675,9694845,300540195,300540195]
        if n < 33: return smalloddswing[n]
        primelist = []
        primesa = takewhile(lambda x: x * x <= n, dropwhile(lambda x: x < 3, primes))
        primesb = takewhile(lambda x: x * 3 <= n, dropwhile(lambda x: x * x <= n, primes))
        for prime in primesa:
                q = n // prime
                p = 1
                while q > 0:
                        if q & 1 == 1: p *= prime
                        q //= prime
                if p > 1: primelist.append(p)
        return reduce(lambda x, y: x * y, list(takewhile(lambda x: x <= n, dropwhile(lambda x: x << 1 <= n, primes))) + primelist + filter(lambda x: n // x & 1 == 1, primesb), 1)

def recfactorial(n):
        if n < 2: return 1
        return recfactorial(n >> 1) ** 2 * swing(n)

def factorial(x):
        if x < 0: raise ValueError('factorial() not defined for negative values')
        n = int(x)
        if n != x: raise ValueError('factorial() only accepts integral values')
        if n < 20: return reduce(lambda x, y: x * y, range(2,n + 1), 1)
        return recfactorial(n) << (n - bitcount(n))

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