Rabu, 23 Disember 2009

C

its been a long time since I did programming. Back then I used to learn and program Java. As now, currently, I decided not to waste my time watching to much movie and do nothing so I decided that maybe I should learn C. Here's my first program:

/*******************************
this is a simple calculator
________________________________
created by Ma.D iftitah
********************************/

#include <stdio.h>
#include <stdbool.h>

int main()
{

float xFirstValue, ySecondValue;
char xOperation;
bool xCondition;

//when xCondition is a boolean, 1 = True, 0 = False

xCondition = 0;

//the while-loop

while (xCondition != 1) {

//program will ask user to enter the value of x

printf("please enter first value: \n");
scanf("%f", &xFirstValue);
fpurge(stdin);

//program will ask user to select the operation here ( +, -, x, / )

printf("\nplease select an Operation: \n");
scanf("%c", &xOperation);
fpurge(stdin);

//program will ask user to enter the value of y

printf("please enter second value: \n");
scanf("%f", &ySecondValue);
fpurge(stdin);

//program will check and calculate based on operation that user selected
//for example x + y = z

if (xOperation == '+') {
printf("\n%f + %f = %f\n", xFirstValue, ySecondValue, xFirstValue + ySecondValue);
}

else if (xOperation =='-'){
printf("\n%f - %f = %f\n", xFirstValue, ySecondValue, xFirstValue - ySecondValue);
}
else if (xOperation =='*'){
printf("\n%f * %f = %f\n", xFirstValue, ySecondValue, xFirstValue * ySecondValue);
}
else if (xOperation == '/'){
printf("\n%f / %f = %f\n", xFirstValue, ySecondValue, xFirstValue / ySecondValue);
}

else {
xCondition = 1;
}


}

//tell the user that the operation selected not valid and program will terminate
printf("\n\nOPERATION NOT VALID. PROGRAM TERMINATED\n\n");

//end of program
return 0;

}



well that is it. simple calculator. please do comment on the codes as this is my first time programmed in C.


p/s: help yourself if you need to steal my codes for your college assignment. lol. 

2 ulasan: