#include <stdio.h> 
#include <stdlib.h>
 
int gcd(int a, int b){
	if(a==0){
		return b;
	}
	return gcd(b%a, a);
}
 
int max(int a, int b, int c, int d){
	if(a>=b && a>=c && a>=d){
		return a;
	}
	if(b>=a && b>=c && b>=d){
		return b;
	}
	if(c>=a && c>=b && c>=d){
		return c;
	}
	return d;
}

int min(int a, int b, int c, int d){
	if(a<=b && a<=c && a<=d){
		return a;
	}
	if(b<=a && b<=c && b<=d){
		return b;
	}
	if(c<=a && c<=a && c<=d){
		return c;
	}
	return d;
}

 
void show(int espresso, int milk, int chocolate, int froth){
	printf("Espresso: %d\n", espresso);
	printf("Milk: %d\n", milk);
	printf("Chocolate: %d\n", chocolate);
	printf("Froth: %d\n", froth);
}

void add(int *espresso, int *milk, int *chocolate, int *froth, int supply_espresso, int supply_milk, int supply_chocolate, int supply_froth){
	*espresso+=supply_espresso;
	*milk+=supply_milk;
	*chocolate+=supply_chocolate;
	*froth+=supply_froth;
}

bool make(int *espresso, int *milk, int *chocolate, int *froth, int r0 ,int r1 ,int r2 ,int r3 ,int *output){
	int hcm = gcd(r0,gcd(r1,gcd(r2,r3)));
	r0 = (r0==0?r0:r0/hcm);
	r1 = (r1==0?r1:r1/hcm);
	r2 = (r2==0?r2:r2/hcm);
	r3 = (r3==0?r3:r3/hcm);
	int maxR = max(r0, r1, r2, r3);
	int a,b,c,d;
	a = (*espresso/maxR==0?9999:*espresso/maxR);
	b = (*milk/maxR==0?9999:*milk/maxR);
	c = (*chocolate/maxR==0?9999:*chocolate/maxR);
	d = (*froth/maxR==0?9999:*froth/maxR);
	int minI = min(a,b,c,d);
	if(*espresso-minI*r0<0 || *milk-minI*r1<0 || *chocolate-minI*r2<0 || *froth-minI*r3<0){
		return false;
	}
	int dif = minI*r0 + minI*r1 + minI*r2 + minI*r3;
	*espresso-=minI*r0;
	*milk-=minI*r1;
	*chocolate-=minI*r2;
	*froth-=minI*r3;
	*output = dif;
	return true;
}
 
int main(){
	int espresso=0, milk=0, chocolate=0, froth=0, option, opt, out;
	int re, rm, rc, rf;
	int addE, addM, addC, addF;
	char cnt = 'y';
	do{
		system("cls");
		show(espresso, milk, chocolate, froth);
		printf("1. Add ingredient\n");
		printf("2. Make coffee\n");
		printf("Choose option: ");
		scanf("%d", &option);
		switch(option){
			case 1:
				printf("Input increased volume(in order of espresso, milk, chocolate, froth)\n");
				scanf("%d %d %d %d", &addE, &addM, &addC, &addF);
				add(&espresso, &milk, &chocolate, &froth, addE, addM, addC, addF);
				break;
			case 2:
				printf("1. Latte\n");
				printf("2. Cappuccino\n");
				printf("3. Mocha\n");
				printf("4. User define\n");
				scanf("%d", &opt);
				switch(opt){
					case 1:
						if(make(&espresso, &milk, &chocolate, &froth, 3, 3, 0, 4, &out)){
							printf("Congratulations! you make %d ml Latte\n", out);
						}else{
							printf("Ingredients are not enough\n");
						}
						break;
					case 2:
						if(make(&espresso, &milk, &chocolate, &froth, 7, 3, 0, 5, &out)){
							printf("Congratulations! you make %d ml Cappuccino\n", out);
						}else{
							printf("Ingredients are not enough\n");
						}
						break;
					case 3:
						if(make(&espresso, &milk, &chocolate, &froth, 3, 1, 1, 0, &out)){
							printf("Congratulations! you make %d ml Mocha\n", out);
						}else{
							printf("Ingredients are not enough\n");
						}
						break;
					case 4:
						printf("Input ratio<in order of espresso, milk, chocolate, froth\n");
						scanf("%d %d %d %d", &re, &rm, &rc, &rf);
						if(make(&espresso, &milk, &chocolate, &froth, re, rm, rc, rf, &out)){
							printf("Congratulations! you make %d ml defined coffee\n", out);
						}else{
							printf("Ingredients are not enough\n");
						}
						break;
				}		
				break;
		}
		printf("Continue?(y/n): ");
		scanf(" %c", &cnt);
	}while(cnt!='n');
	printf("Thank you for using coffee maker!");
	return 0; 
}