Skip to content

Instantly share code, notes, and snippets.

@JaHIY
Last active October 11, 2015 04:37
Show Gist options
  • Save JaHIY/3804124 to your computer and use it in GitHub Desktop.
Save JaHIY/3804124 to your computer and use it in GitHub Desktop.
C:calculate your cup size
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* * * * * * * * * * * * * * * * * * * * *
*
* ./bra [your_underbust] [your_bust]
* unit: cm
*
* for example:
* ./bra 75.3 93.2
*
* then you will get:
* Underbust is 75.3 cm, bust is 93.2 cm, bra is C75.
*
* * * * * * * * * * * * * * * * * * * * */
int get_cup_size(double underbust, double bust) {
int delta;
underbust = round(underbust);
delta = (int)round(bust-underbust);
if (delta % 2 == 0) {
delta -= 1;
}
return (delta-11)/2;
}
int get_underbust_size(double underbust) {
return (((int)underbust)+2)/5*5;
}
int main(int argc, char *argv[]) {
char *csize_str = NULL, *endptr = NULL;
double ub, b;
int csize, ubsize;
if (argc < 3) {
fprintf(stderr, "Usage: %s your_underbust your_bust\n", argv[0]);
return 5;
}
errno = 0;
ub = strtod(argv[1], &endptr);
if ((errno == ERANGE && (ub == DBL_MAX || ub == DBL_MIN)) || (errno != 0 && ub == 0)) {
perror("strtod");
return 3;
}
errno = 0;
b = strtod(argv[2], &endptr);
if ((errno == ERANGE && (b == DBL_MAX || b == DBL_MIN)) || (errno != 0 && b == 0)) {
perror("strtod");
return 4;
}
csize = get_cup_size(ub, b), ubsize = get_underbust_size(ub);
if (ub <= b) {
if (csize >= 0 && csize <= 26) {
if (csize == 0) {
csize_str = (char *)calloc(3, sizeof(char));
strcpy(csize_str, "AA");
} else {
csize_str = (char *)calloc(2, sizeof(char));
*csize_str = toascii(csize + 64);
*(csize_str + 1) = '\0';
}
printf("Underbust is %f cm, bust is %f cm, bra is %s%d.\n", ub, b, csize_str, ubsize);
free(csize_str);
} else if (csize > 26) {
fprintf(stderr, "Error: it\'s TOO BIG!!\n");
return 1;
} else {
fprintf(stderr, "Error: it\'s TOO FLAT!!\n");
return 3;
}
} else {
fprintf(stderr, "Error: underbust is BIGGER than bust! Are you kidding me?!\n");
return 2;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment