Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define DEBUG
- #define PI 3.14159
- #define string_size 100
- char input_string[string_size];
- void RemoveSpaces(char *source);
- double solve(char *equation_s);
- int main (void)
- {
- int option, choice, selection;
- double first_Num, sec_Num, third_Num, area, rad1, rad2, rad3, base, height, volume;
- double result;
- do
- {
- printf("Michael's Calculator\n");
- printf("\nChoose what calculation to perform\n");
- printf("\t1 - Standard Arithmetic Calculator\n");
- printf("\t2 - Area of Shapes\n");
- printf("\t3 - Volume of Shapes\n");
- printf("(Enter a zero to exit) \n");
- printf("\nMake a selection: ");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- {
- do
- {
- system("cls");
- printf("Standard Arithmetic Calculator\n");
- printf("Enter an equation to solve\n");
- fflush(stdin);
- gets_s(input_string, string_size);
- RemoveSpaces(input_string);
- solve(input_string);
- result = atof(input_string);
- printf("\nAnswer: %.4f\n",result);
- printf("\nDo you want to do another operation?\n1) Yes 2) No\n");
- scanf("%d",&option);
- }
- while (option ==1);
- system("cls");
- break;
- }
- case 2:
- {
- do
- {
- printf("\nFIND THE AREA OF A SHAPE\n\n");
- printf("Which shape?\n");
- printf("\t1 - Square\n");
- printf("\t2 - Rectangle\n");
- printf("\t3 - Parallelogram\n");
- printf("\t4 - Trapezoid\n");
- printf("\t5 - Circle\n");
- printf("\t6 - Ellipse\n");
- printf("\t7 - Triangle\n");
- printf("\t8 - Go back to previous menu\n");
- printf("\nMake a selection: ");
- scanf("%d", &selection);
- switch (selection)
- {
- case 1:
- {
- printf("\nArea Of A Square\n");
- printf("\nEnter length of a side of the square: ");
- scanf("%lf", &first_Num);
- while (first_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter length of a side of the square: ");
- scanf("%lf", &first_Num);
- }
- area = first_Num * first_Num;
- printf("\nThe area of a square with a side of %.4f is \n%.4f\n\n", first_Num, area);
- break;
- }
- case 2:
- {
- printf("\nArea Of A Rectangle\n");
- printf("\nEnter width: ");
- scanf("%lf", &first_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter width: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- while(sec_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- }
- area = first_Num * sec_Num;
- printf("\nThe area of a rectagle with sides of %.4f and %.4f is \n%.4f\n\n", first_Num, sec_Num, area);
- break;
- }
- case 3:
- {
- printf("\nArea Of A Parallelogram\n\n");
- printf("Enter base: ");
- scanf("%lf", &first_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter base: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- while(sec_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- }
- area = first_Num * sec_Num;
- printf("\nThe area of a parallelogram with sides of %.4f and %.4f is \n%.4f\n\n", first_Num, sec_Num, area);
- break;
- }
- case 4:
- {
- printf("\nArea Of A Trapezoid\n\n");
- printf("Enter base-1: ");
- scanf("%lf", &first_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter base-1: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter base-2: ");
- scanf("%lf", &sec_Num);
- while(sec_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter base-2: ");
- scanf("%lf", &sec_Num);
- }
- printf("Enter height: ");
- scanf("%lf", &third_Num);
- while(third_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &third_Num);
- }
- area = (third_Num/2) * (first_Num + sec_Num);
- printf("\nThe area of a trapezoid with a base-1 of %.4f, a base-2 of %.4f, and a height of %.4f is \n%.4f\n\n", first_Num, sec_Num, third_Num, area);
- break;
- }
- case 5:
- {
- printf("\nArea Of A Circle\n\n");
- printf("Enter the radius: ");
- scanf("%lf", &rad1);
- while(rad1 <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter the radius: ");
- scanf("%lf", &rad1);
- }
- area = (rad1 * rad1) * PI;
- printf("The area of a circle with a radius of %.4f is \n%.4f\n\n", rad1, area);
- break;
- }
- case 6:
- {
- printf("\nArea Of An Ellipse\n\n");
- printf("Enter radius-1: ");
- scanf("%lf", &rad1);
- while(rad1 <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius-1: ");
- scanf("%lf", &rad1);
- }
- printf("Enter radius-2: ");
- scanf("%lf", &rad2);
- while(rad2 <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius-2: ");
- scanf("%lf", &rad2);
- }
- area = (rad1 * rad2) * PI;
- printf("The area of an ellipse with a radius-1 of %.4f and a radius-2 of %.4f is \n%.4f", rad1, rad2, area);
- break;
- }
- case 7:
- {
- printf("\nArea Of A Triangle\n\n");
- printf("Enter base: ");
- scanf("%lf", &base);
- while(base<=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter base: ");
- scanf("%lf", &base);
- }
- printf("Enter height: ");
- scanf("%lf", &height);
- while(height<=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &height);
- }
- area = (base * height) / 2;
- printf("The area of a triangle with a base of %.4f and a height of %.4f is \n%.4f", base, height, area);
- break;
- }
- default:
- {
- if (selection !=8)
- {
- printf("\nYour selection was not from the menu\n");
- printf("Please try again.\n\n");
- }
- }break;
- }
- }
- while (selection != 8);
- break;
- }
- case 3:
- {
- do
- {
- printf("\nFIND THE VOLUME OF A SHAPE\n\n");
- printf("Which shape?\n");
- printf("\t1 - Cube\n");
- printf("\t2 - Rectangular Prism\n");
- printf("\t3 - Cone\n");
- printf("\t4 - Cylinder\n");
- printf("\t5 - Pyramid\n");
- printf("\t6 - Sphere\n");
- printf("\t7 - Ellipsoid\n");
- printf("\t8 - Go back to previous menu\n");
- printf("\nMake a selection: ");
- scanf("%d", &selection);
- switch (selection)
- {
- case 1:
- {
- printf("\nVolume Of A Cube\n\n");
- printf("Enter length of a side of the cube: ");
- scanf("%lf", &first_Num);
- while (first_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter length of a side of the square: ");
- scanf("%lf", &first_Num);
- }
- volume = first_Num * first_Num * first_Num;
- printf("\nThe volume of a cube with a side of %.4f is \n%.4f\n\n", first_Num, volume);
- break;
- }
- case 2:
- {
- printf("\nVolume Of A Rectangular Prism\n\n");
- printf("Enter width: ");
- scanf("%lf", &first_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter width: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- while(sec_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- }
- printf("Enter depth: ");
- scanf("%lf", &third_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter depth: ");
- scanf("%lf", &third_Num);
- }
- volume = first_Num * sec_Num * third_Num;
- printf("\nThe volume of a rectagular prism with a width of %.4f, a height of %.4f and a depth of %.4f is \n%.4f\n\n",
- first_Num, sec_Num, third_Num, volume);
- break;
- }
- case 3:
- {
- printf("\nVolume Of A Cone\n\n");
- printf("Enter radius: ");
- scanf("%lf", &first_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- while(sec_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- }
- volume = (PI * (first_Num * first_Num) * sec_Num)/3;
- printf("\nThe volume of a cone with a radius of %.4f and a height %.4f is \n%.4f\n\n", first_Num, sec_Num, volume);
- break;
- }
- case 4:
- {
- printf("\nVolume Of A Cylinder\n\n");
- printf("Enter radius: ");
- scanf("%lf", &first_Num);
- while (first_Num <=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- while(sec_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter height: ");
- scanf("%lf", &sec_Num);
- }
- volume = ((first_Num * first_Num) * sec_Num * PI);
- printf("\nThe volume of a cylinder with a radius of %.4f and a height of %.4f is \n%.4f\n\n", first_Num, sec_Num, volume);
- break;
- }
- case 5:
- {
- printf("\nVolume Of A Pyramid\n\n");
- printf("Enter the area of the base: ");
- scanf("%lf", &first_Num);
- while(first_Num <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter the area of the base: ");
- scanf("%lf", &first_Num);
- }
- printf("Enter the height: ");
- scanf("%lf", &sec_Num);
- volume = (first_Num * sec_Num) /3;
- printf("\nThe volume of a pyramid with a base area of %.4f and a height of %.4f is \n%.4f\n\n", first_Num, sec_Num, volume);
- break;
- }
- case 6:
- {
- printf("\nVolume Of A Sphere\n\n");
- printf("Enter radius: ");
- scanf("%lf", &rad1);
- while(rad1 <= 0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius: ");
- scanf("%lf", &rad1);
- }
- volume = ((rad1 * rad1 * rad1) * PI) / 0.75;
- printf("\nThe volume of a sphere with a radius of %.4f is \n%.4f", rad1, volume);
- break;
- }
- case 7:
- {
- printf("\nVolume Of An Ellipsoid\n\n");
- printf("Enter radius-1: ");
- scanf("%lf", &rad1);
- while(rad1<=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius-1: ");
- scanf("%lf", &rad1);
- }
- printf("Enter radius-2: ");
- scanf("%lf", &rad2);
- while(rad2<=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius-2: ");
- scanf("%lf", &rad2);
- }
- printf("Enter radius-3: ");
- scanf("%lf", &rad3);
- while(rad3<=0)
- {
- printf("\nERROR!! Distance must be a positive number\n\n");
- printf("Enter radius-3: ");
- scanf("%lf", &rad3);
- }
- volume = ((rad1 * rad2 * rad3) * PI) / 0.75;
- printf("\nThe volume of an ellipsoid with a radius-1 of %.4f, a radius-2 of %.4f and a radius-3 of %.4f is \n%.4f",
- rad1, rad2, rad3, volume);
- break;
- }
- default:
- {
- if (selection !=8)
- {
- printf("\nYour selection was not from the menu\n");
- printf("Please try again.\n\n");
- }
- }break;
- }
- }while (selection != 8);
- break;
- }
- default:
- {
- if (choice != 0)
- {
- printf("\nYour selection was not from the menu\n");
- printf("Please try again.\n\n");
- }
- break;
- }
- }
- while (option !=2);
- }
- while (choice !=0);
- return (0);
- }
- void RemoveSpaces(char *source)//disregards any spaces
- {
- char temp_string[string_size];
- int i,j,size;
- size = strnlen_s(source,string_size);
- temp_string[0] = source[0];
- j = 1;
- for (i=1;i<=size;i++)
- {
- // if subtraction and preseading charitor is an operation, then change to adding a negitive number.
- if ((source[i] == '-') &&! ((temp_string[j-1] == '*') || (temp_string[j-1] == '/') || (temp_string[j-1] == '+') || (temp_string[j-1] == '^')))
- {
- temp_string[j] = '+';
- j++;
- }
- // remove all spaces
- if (source[i] == ' ')
- {
- }
- else
- {
- temp_string[j] = source[i];
- j++;
- }
- };
- strcpy_s(source,string_size,temp_string);
- }
- int exist_paren(char *equation_s) //check for parentheses
- {
- int length,i, rc=0;
- length=strnlen_s(equation_s,string_size);
- for(i=0; i<length; i++) // checks for parentheses and returns a 1. if no parentheses found, returns a 0
- {
- if((equation_s[i] == '(' )||(equation_s[i]== ')' ))
- {
- rc=1;
- break;
- }
- }
- return (rc);
- }
- int exist_expon(char *equation_s) //check for exponents
- {
- int length,i, rc=0;
- length=strnlen_s(equation_s,string_size);
- for(i=0; i<length; i++) // checks for carrot
- {
- if(equation_s[i] == '^' )
- {
- rc=1;
- break;
- }
- }
- return (rc); //if found, returns a 1. if not found returns a 0
- }
- int exist_mult(char *equation_s) //check for multiplication and division
- {
- int length,i, rc=0;
- length=strnlen_s(equation_s,string_size);
- for(i=0; i<length; i++) // checks for carrot
- {
- if((equation_s[i] == '*' )||(equation_s[i] == '/' ))
- {
- rc=1;
- break;
- }
- }
- return (rc); //if found, returns a 1. if not found returns a 0
- }
- int exist_add(char *equation_s) //check for addition and subraction
- {
- int length,i, rc=0;
- length=strnlen_s(equation_s,string_size);
- for(i=0; i<length; i++)
- {
- if(equation_s[i] == '+' )
- {
- rc=1;
- break;
- }
- }
- return (rc); //if found, returns a 1. if not found returns a 0
- }
- double solve_paren(char *equation_s)
- {
- int open_paren=0, close_paren=0, i;
- double result;
- char paren_string[string_size], temp_string1[string_size],temp_string2[string_size];
- for (i=0; i<strnlen_s(equation_s,string_size); i++) //continues until i = the length of the string entered
- {
- if(equation_s[i] == '(' )
- {
- open_paren = i; //searches for open parentheses, and stores position in open_paren
- }
- if(equation_s[i] == ')' )
- {
- close_paren = i; //searches for close parentheses, and stores position in closes_paren
- break;
- }
- }
- strncpy_s(paren_string, string_size, &equation_s[open_paren+1], close_paren- open_paren-1);
- result=solve(paren_string);
- //changes a double negative to a positive
- if(result<0) //checks to see if number inside parentheses is negative
- {
- if(equation_s[open_paren-1] =='-') //checks if result is to be negated
- {
- result = result *-1; // changes to a positive
- open_paren -=1; // erases negative sign
- }
- }
- sprintf(temp_string1,"%lf", result); // converts results (double) into an array and stores it in temp_string1
- strcpy(temp_string2,&equation_s[close_paren+1]); //copies remainder of string after closing parenthese
- strcpy(&equation_s[open_paren],temp_string1);
- i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
- strcpy(&equation_s[i],temp_string2);
- return result;
- }
- double solve_expon(char *equation_s)
- {
- int last_op= strnlen_s(equation_s,string_size), first_op=0, carrot=0, i;
- double result, r1, r2;
- char base[string_size],exponent[string_size], temp_string1[string_size], temp_string2[string_size];
- // Find left operand for exponent
- // Will be between first_op and carrot
- for (i=0; i<strnlen_s(equation_s,string_size); i++)
- {
- if((equation_s[i]=='+')||(equation_s[i]=='*')||(equation_s[i]=='/'))
- {
- first_op = i+1;
- }
- if (equation_s[i]=='^')
- {
- carrot = i;
- break;
- }
- }
- // Find right operand for exponent
- // will be between carrot and last_op
- for (i=carrot+1; i < strnlen_s(equation_s,string_size); i++)
- {
- if((equation_s[i]=='+')||(equation_s[i]=='*')||(equation_s[i]=='/'))
- {
- last_op=i-1;
- break;
- }
- }
- strncpy_s(base, string_size, equation_s+first_op, carrot - first_op);
- r1= atof(base);
- strncpy_s(exponent, string_size, equation_s+carrot+1, last_op - carrot);
- r2= atof(exponent);
- result = pow(r1,r2);
- sprintf(temp_string1,"%f", result); // converts results (double) into an array and stores it in temp_string1
- strcpy_s(&temp_string2,string_size,&equation_s[last_op+1]); //copies string in equation_s and stores it in temp_string2
- strcpy_s(&equation_s[first_op],string_size, &temp_string1);
- i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
- strcpy_s(&equation_s[i], string_size, temp_string2);
- return result;
- }
- double solve_mult(char *equation_s)
- {
- int last_op= strnlen_s(equation_s,string_size), first_op=0, operation=0, i;
- double result, r1, r2;
- char operand1[string_size],operand2[string_size], temp_string1[string_size], temp_string2[string_size];
- for (i=0; i<strnlen_s(equation_s,string_size); i++)
- {
- if(equation_s[i]=='+') //looks for + sign and stores position
- {
- first_op = i+1;
- }
- if ((equation_s[i]=='*')||(equation_s[i]=='/'))
- {
- operation = i;
- break;
- }
- }
- for (i=operation+1; i < strnlen_s(equation_s,string_size); i++)
- {
- if((equation_s[i]=='+')||(equation_s[i]=='*')||(equation_s[i]=='/'))
- {
- last_op=i-1;
- break;
- }
- }
- strncpy_s(operand1, string_size, equation_s+first_op, operation - first_op);
- r1= atof(operand1);
- strncpy_s(operand2, string_size, equation_s+operation+1, last_op - operation);
- r2= atof(operand2);
- if(equation_s[operation]=='*')
- {
- result = r1*r2;
- }
- else
- {
- result = r1/r2;
- }
- sprintf(temp_string1,"%f", result); // converts results (double) into an array and stores it in temp_string1
- strcpy_s(&temp_string2,string_size,&equation_s[last_op+1]); //copies string in equation_s and stores it in temp_string2
- strcpy_s(&equation_s[first_op],string_size, &temp_string1);
- i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
- strcpy_s(&equation_s[i], string_size, temp_string2);
- return result;
- }
- double solve_add(char *equation_s)
- {
- int last_op= strnlen_s(equation_s,string_size), first_op=0, operation=0, i;
- double result, r1, r2;
- char operand1[string_size],operand2[string_size], temp_string1[string_size], temp_string2[string_size];
- for (i=0; i<strnlen_s(equation_s,string_size); i++)
- {
- if(equation_s[i]=='+')
- {
- operation = i;
- break;
- }
- }
- for (i=operation+1; i < strnlen_s(equation_s,string_size); i++)
- {
- if(equation_s[i]=='+') //finding next + sign
- {
- last_op=i-1;
- break;
- }
- }
- strncpy_s(operand1, string_size, equation_s+first_op, operation - first_op);
- r1= atof(operand1);
- strncpy_s(operand2, string_size, equation_s+operation+1, last_op - operation);
- r2= atof(operand2);
- if(equation_s[operation]=='+')
- {
- result = r1+r2;
- }
- else
- {
- result = r1-r2;
- }
- sprintf(temp_string1,"%f", result); // converts results (double) into an array and stores it in temp_string1
- strcpy_s(&temp_string2,string_size,&equation_s[last_op+1]); //copies string in equation_s and stores it in temp_string2
- strcpy_s(&equation_s[first_op],string_size, &temp_string1);
- i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
- strcpy_s(&equation_s[i], string_size, temp_string2);
- return result;
- }
- double solve(char *equation_s)
- {
- double result=0;
- while(exist_paren(equation_s)) //check for parentheses
- {
- result= solve_paren(equation_s); //if parentheses are in equation, goes to solve_paren function
- }
- while(exist_expon(equation_s)) // checks for carrot
- {
- result=solve_expon(equation_s); // if carrot is in equation, goes to slove_expon
- }
- while(exist_mult(equation_s))
- {
- result = solve_mult(equation_s);
- }
- while(exist_add(equation_s))
- {
- result = solve_add(equation_s);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment