Sie sind auf Seite 1von 3

main.

// the ex-s instruction page wasn't found


#include <stdio.h> #include <stdlib.h> //*************************functions interface*********************************// int isNatural(int num); void calculateEX(int x, int n); int isInRange(int num, int min, int max); int cBinary(int num); int isBinary(int num); int cDecimal(int num); float random_value(int max); float radiusSqr(float x, float y); int throwArrow(); void calculate_pi(void); //*************************main program*********************************// int main(void) { // input: no input // output: performs a unique task according to the user input int operation_code = -1, x , n ; //MAIN MENU while (operation_code != 5) { printf("1. Press 1 (1, Enter) for the Taylor series application\n" "2. Press 2 (2, Enter) for base conversion application (dec->bin)\n" "3. Press 3 (3, Enter) for base conversion application (bin->dec)\n" "4. Press 4 (4, Enter) for pi approximation\n" "5. Press 5 (5, Enter) to exit the application\n"); scanf("%i", &operation_code); //checking validity of the operation code input while (!isInRange(operation_code, 1, 5)) { printf("\nAn invalid input. Please enter an integer between 1 and 5:\n"); scanf("%i", &operation_code); } //PERFORMING A TASK ACCORDING TO THE INPUT switch (operation_code) { case 1: //CALCULATING e^x BY TAYLOR SERIES TO A GIVEN x AND WITH PRECISION OF n TERMS //ASKING FOR x AND n printf("\nENTER TWO INTEGERS X AND N RESPECTIVELY AND THE PROGRAM WILL CALCULATE e^x WITH THE APPROXIMITY OF N (BIGGER THE N BIGGER THE PRECISION):\n"); scanf("%i %i", &x, &n); while (!isNatural(x) || !isNatural(n)) { //INVALID INPUT printf("\nPLEASE ENTER INTEGERS FOR X AND N:\n"); scanf("%i %i", &x, &n); } calculateEX(x, n); // CALLING THE FUNCTION THAT CALCULATES AND PRINTS e^x break; case 2: // CONVERTS A GIVEN DECIMAL INTEGER TO A BINARY INTEGER // ASKING FOR A DECIMAL INTEGER printf("\nEnter an integer between -255 and 255 and the program will convert it to binary number):\n"); scanf("%i", &n); while (!isInRange(n, -255, 255)) { //INVALID INPUT printf("\nAn invalid input. Enter an integer between -255 and 255:\n"); scanf("%i", &n); } printf("\n%i (base 10) => %i (base 2)\n\n", n, cBinary(n)); // PRINTS THE RESULT OF THE CALCULATION break; case 3: //CONVERTS A GIVEN BINARY INTEGER TO A DECIMAL INTEGER // ASKING FOR A BINARY INTEGER printf("\nEnter a binary number between -11111111 and 11111111 and the program will calculate its decimal value: \n"); scanf("%i",&n ); while(!isBinary(n)){ //INVALID INPUT printf("\nAn invalid input. Enter a binary number (number having only zeros and ones digits) between -11111111 and 11111111: \n"); scanf("%i",&n ); } printf("\n%i (base 2) => %i (base 10)\n\n", n,cDecimal(n)); // PRINTS THE RESULT OF THE CALCULATION break; case 4: // CALCULATES PI ACCORDING TO MONTE KARLO METHOD calculate_pi(); break; } } return 1; } //*************************functions implematation*********************************//

int isNatural(int num) {

// input: an integer num // output: returns 1 if num is positive, 0 otherwise return num >= 0;

void calculateEX(int x, int n) { // input: two integers x and n // output: calculates e^x by Talyor series with precision level of n terms and prints the calculation on the screen float ex = 1.00, nth_term=1; int i; printf("\ne^%i=1.00", x); // the 0-th term for (i = 1; i <= n; i++) { nth_term*=(x/(float)i); ex += nth_term; printf("+%0.2f", nth_term); } printf("=%0.2f\n\n", ex); } int isInRange(int num, int min, int max) { // input: 3 integers num, min and max // output: returns 1 if the num is in the range between min and max so min<=num<=max, 0 otherwise return num >= min && num <= max; } int cBinary(int num) { // input: an integer num // output: returns the binary conversion of num int weight=1, converted = 0; while (num != 0) { converted += ((num % 2) * weight); //adding the next digit to the converted number num = num / 2;//decreaing num to get the next digit in the next loop weight *= 10; //digit weight increasing } return converted; } int isBinary(int num){ // input: an integer num // output: returns 1 whether num represents a binary number between -11111111 and 11111111, and 0 otherwise int num_digit; if(!isInRange(num,-11111111,11111111)) return 0; //checks whether num is in the required range while(num!=0){ //looping num digits and checks whether each digit equals 0 or 1 num_digit=num%10; //getting the next digit of num if (!isInRange(num_digit,-1,1)) return 0; //checks whether num_digit is a valid binary number digit num/=10; //decreasing num in order to get the next digit in he next loop } return 1; //returns 1 if no error was dedected } int cDecimal(int num) { // input: an integer num that represants a binary number // output: returns the decimal conversion of num int weight=1, converted = 0; while (num != 0) { converted += ((num % 10) * weight); //adding the next digit to the converted number num = num / 10;//decreaing num to get the next digit in the next loop weight *= 2; //digit weight increasing } return converted; } float random_value(int max){ // input: an integer max // output: returns a float between 0 and max return (float)max*rand()/RAND_MAX; } float radiusSqr(float x, float y){ // input: two floats x and y represanting a point position // output: returns the square of the distance of the point from the origin return x*x+y*y; } int throwArrow(void) { // input: no input // output: 1 if a random point inside a square of 5X5 is inside the circle blocked by the square, 0 otherwise. float x,y; x=random_value(5); y=random_value(5); return radiusSqr(x,y)<25; //returns whether the point distance from origin is less than 5 }

void calculate_pi(void){

// input: no input //output: calculates pi approximation by Monte karlo method and prints the calculation results as described in the exercise instructions const float PI=3.141592653589793; int i, in_circle_arrow=0; float pi_approximation; for(i=1;i<=2500;i++){ //throwing arrows in_circle_arrow+=throwArrow(); } pi_approximation=in_circle_arrow*0.0016; //=(in_circle_arrow/2500)*4 //printing the results to the screen printf("\nInside: %i\n",in_circle_arrow); printf("Total points: 2500"); printf("\nPI approximation: %.3f",pi_approximation); printf("\nDifference:3.141-%0.3f=%0.3f\n\n", pi_approximation, PI-pi_approximation); }

Das könnte Ihnen auch gefallen