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

Happy Numbers

Name: Tynach 2009-09-03 19:34

Hey, I just found this board on 4chan (didn't know about it before), and I wanted to know what you guys thought of my program that checks to see if a number is happy. It was originally written in Python, but as a means to learn C, I re-wrote it in C a while back. Recently, I decided I wanted to find out if I really understood pointers as well as I thought, so I re-wrote the C version from scratch, though implemented some of the same code (though heavily modified to work with my new format) to use pointers so it uses less RAM.

Here's the source:

#include <stdio.h>

void check(char *);
char checkh(long long int, long long int *);
void usragain(char *, char *);
void getnum(long long int *);
void flushin();

int main()
{
    char run = 1;
    char state = 0;
    printf("Happy Number Checker v 2.0\n\n");
    while(run == 1) {
        switch (state) {
        case 0:
            check(&state);
            break;
        case 1:
            usragain(&run, &state);
            break;
        }
    }
    return 0;
}

void check(char *ret)
{
    long long int orig;
    getnum(&orig);
    *ret = checkh(orig, &orig);
    if (*ret == 0) {
        printf("%lld is not happy. Try again.\n\n", orig);
    } else if (*ret == 1) {
        printf("%lld is happy!\n\n", orig);
    }
}

char checkh(long long int guess, long long int *orig)
{
    long long int tmp = guess;
    long long int sum;
    char digit;
    char loop = 1;
   
    while (loop == 1) {
        sum = 0;
        while (tmp != 0) {
            digit = tmp % 10;
            sum += digit * digit;
            tmp /= 10;
        }
        if (sum == 1) {
            loop = 0;
            return 1;http://www.qso.com/carlautta/colin/Beta/happy.c
        } else if (sum == 42 || sum == *orig) {
            loop = 0;
            return 0;
        } else {
            loop = 1;
            tmp = sum;
        }
    }
}

void usragain(char *yn, char *state)
{
    *yn = 2;
    printf("Would you like to check another number (y/n)? ");
    while (*yn == 2) {
        flushin();
        scanf("%c", yn);
        printf("\n");
        if (*yn == 'y') {
            *yn = 1;
            *state = 0;
        } else if (*yn == 'n') {
            *yn = 0;
        } else {
            printf("Invalid input, type y or n.\n");
            *yn = 2;
        }
    }
}

void getnum(long long int *guess)
{
    int test = 0;
    while (test == 0) {
        printf("Please insert a number: ");
        test = scanf("%lld", guess);
        if (test == 0) {
            flushin();
            printf("\n\nInvalid input. Try again.\n\n");
        }
    }
}

void flushin()
{
    int ch = 0;
    while ((ch = getc(stdin)) != EOF && ch != '\n') {
        continue;
    }
}

I'm a newb to text boards here on 4chan, so I may be missing a way to insert code properly (in some forums it's and). Please tell me if I fail at this and should become an hero.

Name: Anonymous 2009-09-04 2:37

>>21
I'm aware of how to do it using LOOP, however I chose to use a tail-recursive version since /prog/ has many Scheme fans who seem to hate its usage.
/prog/ has a mighty assembly of morons and a few people who can code, and even those are not credible enough to voice their own opiniond be taken seriously. Support for TCO, unlike scheme, is optional in an implementation. Support for all cases where TCO might apply is optional in an implementation (although this one is trivial and one would better change implementation instead of code). You're not required to use LOOP of course; DO and DO* are available (if you're interested, take a look at MVDO and MVDO* from PGs On Lisp, which provide additional features). What's more absurd is that while you seem to protest against the use of LOOP (or at least sympathize with those who do), IS-HAPPY-NUMBER makes use thereof to control flow. You can write this instead:

(defun happy-number-p (n)                 
  (do ((n n (square-and-sum-digits n)))
      ((member n '(1 4)) (= n 1)))


The CL naming conventions that I've read about
Conventions by whom? Are they normative or guidelines? I'd like to read them too. I have never read any naming conventions.

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