stitchcounter

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

stitchcounter

Unread post by GekkoP » Wed Feb 26, 2014 5:18 pm

I coded this simple tool to help the missus in her knitting and crocheting. I know that rhowaldt has got you all about the manliness of this stuff, so I figured maybe this could be helpful for you too.

Code: Select all

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

/* stitchcounter: useful tool to help knitting and crocheting
 * usage:
 * - first option: specify original gauge
 *                 specify your gauge
 *                 specify how many values you need
 *                 calculate: x = (my_rows * value) / org_rows
 *                 calculate: x = (my_sts * value) / org_sts
 * - second option: specify your gauge: rows and stitches
 *                  specify how many values you need
 *                  specify cm for each value
 *                  calculate the number of stitches/rows needed for the value */

#define CM_PER_SQUARE 10.0
#define MAX_LINELENGTH 4096
#define MAX_ATTEMPTS 10

static float get_number(const char *prompt)
{
    float value;
    char line[MAX_LINELENGTH];
    int count = 0;
    while (fputs(prompt, stdout) != EOF &&
           fgets(line, sizeof(line), stdin) != 0) {
        if (sscanf(line, "%f", &value) == 1)
            return value;
        if (count++ > MAX_ATTEMPTS) {
            printf("I don't understand what you're typing\n");
            exit(1);
        }
	printf("value must be a number (es.: 1, 0.1, 20.3, etc.)\n");
    }

    printf("I got EOF or an error.\n");
    exit(1);
}

static char get_char(const char *prompt)
{
    char value;
    char line[MAX_LINELENGTH];
    int count = 0;
    while (fputs(prompt, stdout) != EOF &&
           fgets(line, sizeof(line), stdin) != 0) {
        if (sscanf(line, "%s", &value) == 1) {
	    if (!isalpha(value)) {
		printf("value must be either [r] or [s]\n");
	    } else {
		return value;
	    }
	}
        if (count++ > MAX_ATTEMPTS) {
            printf("I don't understand what you're typing\n");
            exit(1);
        }
	printf("value must be either [r] or [s]\n");
    }

    printf("I got EOF or an error.\n");
    exit(1);
}

int opt1()
{
    float rows, sts, org_rows, org_sts, org_val;
    int values;

    printf("original gauge\n");
    org_rows = get_number("\tplease enter the number of rows: ");
    org_sts = get_number("\tplease enter the number of stitches: ");

    printf("your gauge\n");
    rows = get_number("\tplease enter the number of your rows: ");
    sts = get_number("\tplease enter the number of your stitches: ");

    values = get_number("how many values do you need? ");

    int i;
    for (i = 1; i <= values; i++) {
	char sel = get_char("is value rows [r] or stitches [s]? ");
	switch(sel) {
	    case 'r':
   	        org_val = get_number("please enter the rows of the original project: ");
	        printf("your final result is: %3.1f\n", (rows * org_val) / org_rows);
	        break;
	    case 's':
		org_val = get_number("please enter the stitches of the original project: ");
	        printf("your final result is: %3.1f\n", (sts * org_val) / org_sts);
		break;
  	    default:
  	        printf("i don't understand your input\n");
	        break;
	}
    }
    return 0;
}

int opt2()
{
    float rows, sts, rows1cm, sts1cm, cm;
    int values;

    printf("your gauge\n");
    rows = get_number("\tplease enter the number of your rows: ");
    sts = get_number("\tplease enter the number of your stitches: ");

    rows1cm = rows / CM_PER_SQUARE;
    sts1cm = sts / CM_PER_SQUARE;

    values = get_number("how many values do you need? ");

    int i;
    for (i = 1; i <= values; i++) {
	char sel = get_char("is value rows [r] or stitches [s]? ");
	switch(sel) {
	    case 'r':
		cm = get_number("please enter the cms of your project: ");
	        printf("your final result is: %3.1f\n", (rows1cm * cm));
	        break;
	    case 's':
		cm = get_number("please enter the cms of your project: ");
	        printf("your final result is: %3.1f\n", (sts1cm * cm));
		break;
  	    default:
  	        printf("i don't understand your input\n");
	        break;
	}
    }
    return 0;
}

main()
{
    int opt;

    printf("\nstitchcounter: useful tool to help knitting and crocheting\n\n");
    printf("options:\n");
    printf("\t1) calculate stitches from original gauge\n");
    printf("\t2) calculate stitches from dimensions\n");
    opt = get_number("pick your option: ");

    switch(opt) {
        case 1:
            opt1();
	    break;
        case 2:
            opt2();
	    break;
        default:
            printf("option must be 1 or 2\n");
	    break;
    }

    return 0;
}

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: stitchcounter

Unread post by wuxmedia » Wed Feb 26, 2014 7:00 pm

cool, nice coding!
"Seek, and Ye shall find"
"Github | Chooons | Site"

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: stitchcounter

Unread post by machinebacon » Wed Feb 26, 2014 7:01 pm

yeah, nice coding - just no idea what it is for :D
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: stitchcounter

Unread post by GekkoP » Wed Feb 26, 2014 7:07 pm

I'm no knitter, but wife's used it to knit a skirt. She usually starts from a pattern somebody else did, but has to make some math to adjust it to her needs. This tool makes it simpler, does nothing more actually. (and I suppose it can be coded better, I'm no C expert)

User avatar
rust collector
Motörhead
Posts: 535
Joined: Mon Jan 13, 2014 3:56 pm
Location: no_nb

Re: stitchcounter

Unread post by rust collector » Wed Feb 26, 2014 7:14 pm

WOW!

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: stitchcounter

Unread post by machinebacon » Wed Feb 26, 2014 7:30 pm

Oh I see, so you can use it to "convert" the sizes or something like this. Nice then :D
..gnutella..

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: stitchcounter

Unread post by GekkoP » Wed Feb 26, 2014 7:33 pm

^ yes. She uses it to calculate the right amount of stiches for different sizes. :)

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: stitchcounter

Unread post by machinebacon » Wed Feb 26, 2014 7:34 pm

I hope she always will have to calculate towards a smaller size :D
..gnutella..

User avatar
paolo
Head Banger
Posts: 275
Joined: Mon Nov 12, 2012 11:08 am
Location: Milano,Italy

Re: stitchcounter

Unread post by paolo » Wed Feb 26, 2014 10:21 pm

Wow ! Interesting thing.
Dell Latitude C640 - CPU P4 2GHz - RAM 1,5 GB - made in 2002 - I'm in the Manjaro-i3 land now :)

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: stitchcounter

Unread post by rhowaldt » Thu Feb 27, 2014 11:24 am

cool shit Gekko! i will check it out and let you know if i understand it, or maybe my GF does :D (i am no expert)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: stitchcounter

Unread post by GekkoP » Mon May 11, 2015 6:33 pm

Just for fun, I ported this little tool in Clojure: https://github.com/manuel-uberti/stitchcounter-clj

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: stitchcounter

Unread post by GekkoP » Wed Aug 19, 2015 2:35 pm

Just for fun, I ported this little tool in Chicken Scheme: https://github.com/manuel-uberti/stitchcounter-scm

(BTW, my favorite implementation so far)

User avatar
simgin
Meme Fodder
Posts: 1167
Joined: Sun Jan 06, 2013 12:07 am
Location: Bradford-on-Avon, UK

Re: stitchcounter

Unread post by simgin » Wed Aug 19, 2015 8:52 pm

Hmm, it is a lot shorter in Lisp dialects, than in C. o.O Nice work Manuel!

cheers
simon
Someone told me that I am delusional, I almost fell off my unicorn.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: stitchcounter

Unread post by GekkoP » Thu Aug 20, 2015 7:38 am

^ Thank you.

Yes, Lisp versions are a lot shorter. But Clojure isn't a good choice for CLI tools, because of all the Java/JVM madness. The executable I get from my Clojure version of stichcounter is over 15MB (!). Only good for learning's sake.

That's where Scheme, and Chicken Scheme in particular, comes in. I got the Lisp I love but I can compile it to C and obtain small and light executables (~88Kb). It still feels like magic.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: stitchcounter

Unread post by GekkoP » Sat Aug 22, 2015 2:53 pm

MWE in JavaScript: https://github.com/manuel-uberti/stitchcounter-js

Ok, enough. :D

Post Reply