Guest User

Untitled

a guest
Dec 5th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #define DEBUG
  6. #define PI 3.14159
  7. #define string_size 100
  8.  
  9. char input_string[string_size];
  10. void RemoveSpaces(char *source);
  11. double solve(char *equation_s);
  12.  
  13. int main (void)
  14. {
  15. int option, choice, selection;
  16. double first_Num, sec_Num, third_Num, area, rad1, rad2, rad3, base, height, volume;
  17. double result;
  18.     do
  19.     {
  20.         printf("Michael's Calculator\n");
  21.         printf("\nChoose what calculation to perform\n");
  22.         printf("\t1 - Standard Arithmetic Calculator\n");
  23.         printf("\t2 - Area of Shapes\n");
  24.         printf("\t3 - Volume of Shapes\n");
  25.         printf("(Enter a zero to exit) \n");
  26.         printf("\nMake a selection: ");
  27.         scanf("%d", &choice);
  28.  
  29.     switch (choice)
  30.     {
  31.     case 1:
  32.     {
  33.         do
  34.             {
  35.             system("cls");
  36.             printf("Standard Arithmetic Calculator\n");
  37.             printf("Enter an equation to solve\n");
  38.             fflush(stdin);
  39.             gets_s(input_string, string_size);
  40.             RemoveSpaces(input_string);
  41.             solve(input_string);
  42.             result = atof(input_string);
  43.             printf("\nAnswer: %.4f\n",result);
  44.             printf("\nDo you want to do another operation?\n1) Yes 2) No\n");
  45.             scanf("%d",&option);
  46.         }
  47.         while (option ==1);
  48.         system("cls");
  49.         break;
  50.     }
  51.     case 2:
  52.             {
  53.                 do
  54.                 {
  55.                     printf("\nFIND THE AREA OF A SHAPE\n\n");
  56.                     printf("Which shape?\n");
  57.                     printf("\t1 - Square\n");
  58.                     printf("\t2 - Rectangle\n");
  59.                     printf("\t3 - Parallelogram\n");
  60.                     printf("\t4 - Trapezoid\n");
  61.                     printf("\t5 - Circle\n");
  62.                     printf("\t6 - Ellipse\n");
  63.                     printf("\t7 - Triangle\n");
  64.                     printf("\t8 - Go back to previous menu\n");
  65.                     printf("\nMake a selection: ");
  66.                     scanf("%d", &selection);
  67.                        
  68.                         switch (selection)
  69.                         {
  70.                             case 1:
  71.                             {
  72.                                 printf("\nArea Of A Square\n");
  73.                                 printf("\nEnter length of a side of the square: ");
  74.                                 scanf("%lf", &first_Num);
  75.                                 while (first_Num <= 0)
  76.                                 {
  77.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  78.                                     printf("Enter length of a side of the square: ");
  79.                                     scanf("%lf", &first_Num);
  80.                                 }
  81.                                 area = first_Num * first_Num;
  82.                                 printf("\nThe area of a square with a side of %.4f is \n%.4f\n\n", first_Num, area);
  83.                                 break;
  84.                             }
  85.                             case 2:
  86.                             {
  87.                                 printf("\nArea Of A Rectangle\n");
  88.                                 printf("\nEnter width: ");
  89.                                 scanf("%lf", &first_Num);
  90.                                 while (first_Num <=0)
  91.                                 {
  92.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  93.                                     printf("Enter width: ");
  94.                                     scanf("%lf", &first_Num);
  95.                                 }
  96.                                 printf("Enter height: ");
  97.                                 scanf("%lf", &sec_Num);
  98.                                 while(sec_Num <= 0)
  99.                                 {
  100.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  101.                                     printf("Enter height: ");
  102.                                     scanf("%lf", &sec_Num);
  103.                                 }
  104.                                 area = first_Num * sec_Num;
  105.                                 printf("\nThe area of a rectagle with sides of %.4f and %.4f is \n%.4f\n\n", first_Num, sec_Num, area);
  106.                                 break;
  107.                             }
  108.                             case 3:
  109.                             {
  110.                                 printf("\nArea Of A Parallelogram\n\n");
  111.                                 printf("Enter base: ");
  112.                                 scanf("%lf", &first_Num);
  113.                                 while (first_Num <=0)
  114.                                 {
  115.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  116.                                     printf("Enter base: ");
  117.                                     scanf("%lf", &first_Num);
  118.                                 }
  119.                                 printf("Enter height: ");
  120.                                 scanf("%lf", &sec_Num);
  121.                                 while(sec_Num <= 0)
  122.                                 {
  123.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  124.                                     printf("Enter height: ");
  125.                                     scanf("%lf", &sec_Num);
  126.                                 }
  127.                                 area = first_Num * sec_Num;
  128.                                 printf("\nThe area of a parallelogram with sides of %.4f and %.4f is \n%.4f\n\n", first_Num, sec_Num, area);
  129.                                 break;
  130.                             }
  131.                             case 4:
  132.                             {
  133.                                 printf("\nArea Of A Trapezoid\n\n");
  134.                                 printf("Enter base-1: ");
  135.                                 scanf("%lf", &first_Num);
  136.                                 while (first_Num <=0)
  137.                                 {
  138.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  139.                                     printf("Enter base-1: ");
  140.                                     scanf("%lf", &first_Num);
  141.                                 }
  142.                                 printf("Enter base-2: ");
  143.                                 scanf("%lf", &sec_Num);
  144.                                 while(sec_Num <= 0)
  145.                                 {
  146.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  147.                                     printf("Enter base-2: ");
  148.                                     scanf("%lf", &sec_Num);
  149.                                 }
  150.                                 printf("Enter height: ");
  151.                                 scanf("%lf", &third_Num);
  152.                                 while(third_Num <= 0)
  153.                                 {
  154.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  155.                                     printf("Enter height: ");
  156.                                     scanf("%lf", &third_Num);
  157.                                 }
  158.                                 area = (third_Num/2) * (first_Num + sec_Num);
  159.                                 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);
  160.                                 break;
  161.                             }
  162.                             case 5:
  163.                             {
  164.                                 printf("\nArea Of A Circle\n\n");
  165.                                 printf("Enter the radius: ");
  166.                                 scanf("%lf", &rad1);
  167.                                 while(rad1 <= 0)
  168.                                 {
  169.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  170.                                     printf("Enter the radius: ");
  171.                                     scanf("%lf", &rad1);
  172.                                 }
  173.                                 area = (rad1 * rad1) * PI;
  174.                                 printf("The area of a circle with a radius of %.4f is \n%.4f\n\n", rad1, area);
  175.                                 break;
  176.                             }
  177.                             case 6:
  178.                             {
  179.                                 printf("\nArea Of An Ellipse\n\n");
  180.                                 printf("Enter radius-1: ");
  181.                                 scanf("%lf", &rad1);
  182.                                 while(rad1 <= 0)
  183.                                 {
  184.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  185.                                     printf("Enter radius-1: ");
  186.                                     scanf("%lf", &rad1);
  187.                                 }
  188.                                 printf("Enter radius-2: ");
  189.                                 scanf("%lf", &rad2);
  190.                                 while(rad2 <= 0)
  191.                                 {
  192.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  193.                                     printf("Enter radius-2: ");
  194.                                     scanf("%lf", &rad2);
  195.                                 }
  196.                                 area = (rad1 * rad2) * PI;
  197.                                 printf("The area of an ellipse with a radius-1 of %.4f and a radius-2 of %.4f is \n%.4f", rad1, rad2, area);
  198.                                 break;
  199.                             }
  200.                             case 7:
  201.                             {
  202.                                 printf("\nArea Of A Triangle\n\n");
  203.                                 printf("Enter base: ");
  204.                                 scanf("%lf", &base);
  205.                                 while(base<=0)
  206.                                 {
  207.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  208.                                     printf("Enter base: ");
  209.                                     scanf("%lf", &base);
  210.                                 }
  211.                                 printf("Enter height: ");
  212.                                 scanf("%lf", &height);
  213.                                 while(height<=0)
  214.                                 {
  215.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  216.                                     printf("Enter height: ");
  217.                                     scanf("%lf", &height);
  218.                                 }
  219.                                 area = (base * height) / 2;
  220.                                 printf("The area of a triangle with a base of %.4f and a height of %.4f is \n%.4f", base, height, area);
  221.                                 break;
  222.                             }
  223.                             default:
  224.                             {
  225.                                 if (selection !=8)
  226.                                 {
  227.                                     printf("\nYour selection was not from the menu\n");
  228.                                     printf("Please try again.\n\n");
  229.                                 }
  230.                                
  231.                             }break;
  232.                         }
  233.                 }
  234.                 while (selection != 8);
  235.                 break;
  236.             }
  237.     case 3:
  238.             {
  239.                 do
  240.                 {
  241.                     printf("\nFIND THE VOLUME OF A SHAPE\n\n");
  242.                     printf("Which shape?\n");
  243.                     printf("\t1 - Cube\n");
  244.                     printf("\t2 - Rectangular Prism\n");
  245.                     printf("\t3 - Cone\n");
  246.                     printf("\t4 - Cylinder\n");
  247.                     printf("\t5 - Pyramid\n");
  248.                     printf("\t6 - Sphere\n");
  249.                     printf("\t7 - Ellipsoid\n");
  250.                     printf("\t8 - Go back to previous menu\n");
  251.                    
  252.                     printf("\nMake a selection: ");
  253.                     scanf("%d", &selection);
  254.                        
  255.                         switch (selection)
  256.                         {
  257.                             case 1:
  258.                             {
  259.                                 printf("\nVolume Of A Cube\n\n");
  260.                                 printf("Enter length of a side of the cube: ");
  261.                                 scanf("%lf", &first_Num);
  262.                                 while (first_Num <= 0)
  263.                                 {
  264.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  265.                                     printf("Enter length of a side of the square: ");
  266.                                     scanf("%lf", &first_Num);
  267.                                 }
  268.                                 volume = first_Num * first_Num * first_Num;
  269.                                 printf("\nThe volume of a cube with a side of %.4f is \n%.4f\n\n", first_Num, volume);
  270.                                 break;
  271.                             }
  272.                             case 2:
  273.                             {
  274.                                 printf("\nVolume Of A Rectangular Prism\n\n");
  275.                                 printf("Enter width: ");
  276.                                 scanf("%lf", &first_Num);
  277.                                 while (first_Num <=0)
  278.                                 {
  279.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  280.                                     printf("Enter width: ");
  281.                                     scanf("%lf", &first_Num);
  282.                                 }
  283.                                 printf("Enter height: ");
  284.                                 scanf("%lf", &sec_Num);
  285.                                 while(sec_Num <= 0)
  286.                                 {
  287.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  288.                                     printf("Enter height: ");
  289.                                     scanf("%lf", &sec_Num);
  290.                                 }
  291.                                 printf("Enter depth: ");
  292.                                 scanf("%lf", &third_Num);
  293.                                 while (first_Num <=0)
  294.                                 {
  295.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  296.                                     printf("Enter depth: ");
  297.                                     scanf("%lf", &third_Num);
  298.                                 }
  299.                                 volume = first_Num * sec_Num * third_Num;
  300.                                 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",
  301.                                 first_Num, sec_Num, third_Num, volume);
  302.                                 break;
  303.                             }
  304.                             case 3:
  305.                             {
  306.                                 printf("\nVolume Of A Cone\n\n");
  307.                                 printf("Enter radius: ");
  308.                                 scanf("%lf", &first_Num);
  309.                                 while (first_Num <=0)
  310.                                 {
  311.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  312.                                     printf("Enter radius: ");
  313.                                     scanf("%lf", &first_Num);
  314.                                 }
  315.                                 printf("Enter height: ");
  316.                                 scanf("%lf", &sec_Num);
  317.                                 while(sec_Num <= 0)
  318.                                 {
  319.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  320.                                     printf("Enter height: ");
  321.                                     scanf("%lf", &sec_Num);
  322.                                 }
  323.                                 volume = (PI * (first_Num * first_Num) * sec_Num)/3;
  324.                                 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);
  325.                                 break;
  326.                             }
  327.                             case 4:
  328.                             {
  329.                                 printf("\nVolume Of A Cylinder\n\n");
  330.                                 printf("Enter radius: ");
  331.                                 scanf("%lf", &first_Num);
  332.                                 while (first_Num <=0)
  333.                                 {
  334.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  335.                                     printf("Enter radius: ");
  336.                                     scanf("%lf", &first_Num);
  337.                                 }
  338.                                 printf("Enter height: ");
  339.                                 scanf("%lf", &sec_Num);
  340.                                 while(sec_Num <= 0)
  341.                                 {
  342.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  343.                                     printf("Enter height: ");
  344.                                     scanf("%lf", &sec_Num);
  345.                                 }
  346.                                 volume = ((first_Num * first_Num) * sec_Num * PI);
  347.                                 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);
  348.                                 break;
  349.                             }
  350.                             case 5:
  351.                             {
  352.                                 printf("\nVolume Of A Pyramid\n\n");
  353.                                 printf("Enter the area of the base: ");
  354.                                 scanf("%lf", &first_Num);
  355.                                 while(first_Num <= 0)
  356.                                 {
  357.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  358.                                     printf("Enter the area of the base: ");
  359.                                     scanf("%lf", &first_Num);
  360.                                 }
  361.                                 printf("Enter the height: ");
  362.                                 scanf("%lf", &sec_Num);
  363.                                 volume = (first_Num * sec_Num) /3;
  364.                                 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);
  365.                                 break;
  366.                             }
  367.                             case 6:
  368.                             {
  369.                                 printf("\nVolume Of A Sphere\n\n");
  370.                                 printf("Enter radius: ");
  371.                                 scanf("%lf", &rad1);
  372.                                 while(rad1 <= 0)
  373.                                 {
  374.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  375.                                     printf("Enter radius: ");
  376.                                     scanf("%lf", &rad1);
  377.                                 }
  378.                                 volume =  ((rad1 * rad1 * rad1) * PI) / 0.75;
  379.                                 printf("\nThe volume of a sphere with a radius of %.4f is \n%.4f", rad1, volume);
  380.                                 break;
  381.                             }
  382.                             case 7:
  383.                             {
  384.                                 printf("\nVolume Of An Ellipsoid\n\n");
  385.                                 printf("Enter radius-1: ");
  386.                                 scanf("%lf", &rad1);
  387.                                 while(rad1<=0)
  388.                                 {
  389.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  390.                                     printf("Enter radius-1: ");
  391.                                     scanf("%lf", &rad1);
  392.                                 }
  393.                                 printf("Enter radius-2: ");
  394.                                 scanf("%lf", &rad2);
  395.                                 while(rad2<=0)
  396.                                 {
  397.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  398.                                     printf("Enter radius-2: ");
  399.                                     scanf("%lf", &rad2);
  400.                                 }
  401.                                 printf("Enter radius-3: ");
  402.                                 scanf("%lf", &rad3);
  403.                                 while(rad3<=0)
  404.                                 {
  405.                                     printf("\nERROR!! Distance must be a positive number\n\n");
  406.                                     printf("Enter radius-3: ");
  407.                                     scanf("%lf", &rad3);
  408.                                 }
  409.                                 volume = ((rad1 * rad2 * rad3) * PI) / 0.75;
  410.                                 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",
  411.                                 rad1, rad2, rad3, volume);
  412.                                 break;
  413.                             }
  414.                             default:
  415.                             {
  416.                                 if (selection !=8)
  417.                                 {
  418.                                     printf("\nYour selection was not from the menu\n");
  419.                                     printf("Please try again.\n\n");
  420.                                 }
  421.                                
  422.                             }break;
  423.                         }
  424.                        
  425.  
  426.                     }while (selection != 8);
  427.                 break;
  428.             }
  429.  
  430.             default:
  431.             {  
  432.                 if (choice != 0)
  433.                 {
  434.                     printf("\nYour selection was not from the menu\n");
  435.                     printf("Please try again.\n\n");
  436.                 }
  437.                 break;
  438.             }
  439.         }
  440.         while (option !=2);
  441.     }
  442.     while (choice !=0);
  443. return (0);
  444. }
  445.  
  446. void RemoveSpaces(char *source)//disregards any spaces
  447. {
  448.     char temp_string[string_size];
  449.     int i,j,size;
  450.     size = strnlen_s(source,string_size);
  451.     temp_string[0] = source[0];
  452.     j = 1;
  453.     for (i=1;i<=size;i++)
  454.     {
  455.         // if subtraction and preseading charitor is an operation, then change to adding a negitive number.
  456.         if ((source[i] == '-') &&! ((temp_string[j-1] == '*') || (temp_string[j-1] == '/') || (temp_string[j-1] == '+') || (temp_string[j-1] == '^')))
  457.         {
  458.             temp_string[j] = '+';
  459.             j++;
  460.         }
  461.         // remove all spaces
  462.         if (source[i] == ' ')
  463.         {
  464.         }
  465.         else
  466.         {
  467.             temp_string[j] = source[i];
  468.             j++;
  469.         }
  470.     };
  471.     strcpy_s(source,string_size,temp_string);
  472. }
  473.  
  474. int exist_paren(char *equation_s) //check for parentheses
  475. {
  476.     int length,i, rc=0;
  477.     length=strnlen_s(equation_s,string_size);
  478.     for(i=0; i<length; i++) // checks for parentheses and returns a 1. if no parentheses found, returns a 0
  479.     {
  480.         if((equation_s[i] == '(' )||(equation_s[i]== ')' ))
  481.         {
  482.         rc=1;
  483.         break;
  484.         }
  485.     }
  486.     return (rc);
  487. }
  488.  
  489. int exist_expon(char *equation_s) //check for exponents
  490. {
  491.     int length,i, rc=0;
  492.     length=strnlen_s(equation_s,string_size);  
  493.     for(i=0; i<length; i++) // checks for carrot
  494.     {
  495.         if(equation_s[i] == '^' )
  496.         {
  497.         rc=1;
  498.         break;
  499.         }
  500.     }
  501.     return (rc); //if found, returns a 1. if not found returns a 0
  502. }
  503.  
  504.  
  505. int exist_mult(char *equation_s) //check for multiplication and division
  506. {
  507.     int length,i, rc=0;
  508.     length=strnlen_s(equation_s,string_size);  
  509.     for(i=0; i<length; i++) // checks for carrot
  510.     {
  511.         if((equation_s[i] == '*' )||(equation_s[i] == '/' ))
  512.         {
  513.         rc=1;
  514.         break;
  515.         }
  516.     }
  517.     return (rc); //if found, returns a 1. if not found returns a 0
  518. }
  519.  
  520.  
  521. int exist_add(char *equation_s) //check for addition and subraction
  522. {
  523.     int length,i, rc=0;
  524.     length=strnlen_s(equation_s,string_size);
  525.     for(i=0; i<length; i++)
  526.     {
  527.         if(equation_s[i] == '+' )
  528.         {
  529.         rc=1;
  530.         break;
  531.         }
  532.     }
  533.     return (rc); //if found, returns a 1. if not found returns a 0
  534. }
  535.  
  536. double solve_paren(char *equation_s)
  537. {
  538. int open_paren=0, close_paren=0, i;
  539. double result;
  540. char paren_string[string_size], temp_string1[string_size],temp_string2[string_size];
  541.     for (i=0; i<strnlen_s(equation_s,string_size); i++) //continues until i = the length of the string entered
  542.     {
  543.         if(equation_s[i] == '(' )
  544.         {
  545.             open_paren = i;  //searches for open parentheses, and stores position in open_paren
  546.         }
  547.         if(equation_s[i] == ')' )
  548.         {
  549.             close_paren = i; //searches for close parentheses, and stores position in closes_paren
  550.             break;
  551.         }
  552.     }
  553.     strncpy_s(paren_string, string_size, &equation_s[open_paren+1], close_paren- open_paren-1);        
  554.     result=solve(paren_string);
  555.     //changes a double negative to a positive  
  556.     if(result<0)  //checks to see if number inside parentheses is negative
  557.     {
  558.         if(equation_s[open_paren-1] =='-') //checks if result is to be negated
  559.         {
  560.             result = result *-1;   // changes to a positive
  561.             open_paren -=1;   // erases negative sign
  562.         }
  563.     }                  
  564.     sprintf(temp_string1,"%lf", result); // converts results (double) into an array and stores it in temp_string1  
  565.     strcpy(temp_string2,&equation_s[close_paren+1]); //copies remainder of string after closing parenthese
  566.     strcpy(&equation_s[open_paren],temp_string1);
  567.     i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
  568.     strcpy(&equation_s[i],temp_string2);
  569.     return result;
  570. }
  571.  
  572. double solve_expon(char *equation_s)
  573. {
  574. int last_op= strnlen_s(equation_s,string_size), first_op=0, carrot=0, i;
  575. double result, r1, r2;
  576. char base[string_size],exponent[string_size], temp_string1[string_size], temp_string2[string_size];
  577.     // Find left operand for exponent
  578.     // Will be between first_op and carrot
  579.     for (i=0; i<strnlen_s(equation_s,string_size); i++)
  580.     {
  581.         if((equation_s[i]=='+')||(equation_s[i]=='*')||(equation_s[i]=='/'))
  582.         {
  583.             first_op = i+1;
  584.         }
  585.         if (equation_s[i]=='^')
  586.         {
  587.             carrot = i;
  588.             break;
  589.         }
  590.  
  591.     }  
  592.     // Find right operand for exponent
  593.     // will be between carrot and last_op
  594.     for (i=carrot+1; i < strnlen_s(equation_s,string_size); i++)
  595.     {
  596.         if((equation_s[i]=='+')||(equation_s[i]=='*')||(equation_s[i]=='/'))
  597.         {
  598.             last_op=i-1;
  599.             break;
  600.         }
  601.     }
  602.     strncpy_s(base, string_size, equation_s+first_op, carrot - first_op);
  603.     r1= atof(base);
  604.     strncpy_s(exponent, string_size, equation_s+carrot+1, last_op - carrot);
  605.     r2= atof(exponent);
  606.     result = pow(r1,r2);   
  607.     sprintf(temp_string1,"%f", result); // converts results (double) into an array and stores it in temp_string1
  608.     strcpy_s(&temp_string2,string_size,&equation_s[last_op+1]); //copies string in equation_s and stores it in temp_string2
  609.     strcpy_s(&equation_s[first_op],string_size, &temp_string1);
  610.     i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
  611.     strcpy_s(&equation_s[i], string_size, temp_string2);
  612.     return result;
  613. }  
  614.  
  615. double solve_mult(char *equation_s)
  616. {
  617. int last_op= strnlen_s(equation_s,string_size), first_op=0, operation=0, i;
  618. double result, r1, r2;
  619. char operand1[string_size],operand2[string_size], temp_string1[string_size], temp_string2[string_size];
  620.  
  621.     for (i=0; i<strnlen_s(equation_s,string_size); i++)
  622.     {
  623.         if(equation_s[i]=='+') //looks for + sign and stores position
  624.         {
  625.             first_op = i+1;
  626.         }
  627.         if ((equation_s[i]=='*')||(equation_s[i]=='/'))
  628.         {
  629.             operation = i;
  630.             break;
  631.         }
  632.  
  633.     }
  634.     for (i=operation+1; i < strnlen_s(equation_s,string_size); i++)
  635.     {
  636.         if((equation_s[i]=='+')||(equation_s[i]=='*')||(equation_s[i]=='/'))
  637.         {
  638.             last_op=i-1;
  639.             break;
  640.         }
  641.     }
  642.     strncpy_s(operand1, string_size, equation_s+first_op, operation - first_op);
  643.     r1= atof(operand1);
  644.     strncpy_s(operand2, string_size, equation_s+operation+1, last_op - operation);
  645.     r2= atof(operand2);
  646.     if(equation_s[operation]=='*')
  647.     {
  648.         result = r1*r2;
  649.     }
  650.     else
  651.     {
  652.         result = r1/r2;
  653.     }
  654.     sprintf(temp_string1,"%f", result); // converts results (double) into an array and stores it in temp_string1
  655.     strcpy_s(&temp_string2,string_size,&equation_s[last_op+1]); //copies string in equation_s and stores it in temp_string2
  656.     strcpy_s(&equation_s[first_op],string_size, &temp_string1);
  657.     i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
  658.     strcpy_s(&equation_s[i], string_size, temp_string2);
  659.     return result;
  660. }  
  661.  
  662. double solve_add(char *equation_s)
  663. {
  664. int last_op= strnlen_s(equation_s,string_size), first_op=0, operation=0, i;
  665. double result, r1, r2;
  666. char operand1[string_size],operand2[string_size], temp_string1[string_size], temp_string2[string_size];
  667.  
  668.     for (i=0; i<strnlen_s(equation_s,string_size); i++)
  669.     {
  670.         if(equation_s[i]=='+')
  671.         {
  672.             operation = i;
  673.             break;
  674.         }
  675.     }
  676.     for (i=operation+1; i < strnlen_s(equation_s,string_size); i++)
  677.     {
  678.         if(equation_s[i]=='+') //finding next + sign
  679.         {
  680.             last_op=i-1;
  681.             break;
  682.         }
  683.     }
  684.     strncpy_s(operand1, string_size, equation_s+first_op, operation - first_op);
  685.     r1= atof(operand1);
  686.     strncpy_s(operand2, string_size, equation_s+operation+1, last_op - operation);
  687.     r2= atof(operand2);
  688.     if(equation_s[operation]=='+')
  689.     {
  690.         result = r1+r2;
  691.     }
  692.     else
  693.     {
  694.         result = r1-r2;
  695.     }
  696.     sprintf(temp_string1,"%f", result); // converts results (double) into an array and stores it in temp_string1
  697.     strcpy_s(&temp_string2,string_size,&equation_s[last_op+1]); //copies string in equation_s and stores it in temp_string2
  698.     strcpy_s(&equation_s[first_op],string_size, &temp_string1);
  699.     i = strnlen_s(equation_s, string_size); // i is length of string size in equation_s
  700.     strcpy_s(&equation_s[i], string_size, temp_string2);
  701.     return result;
  702. }  
  703.  
  704. double solve(char *equation_s)
  705. {
  706. double result=0;
  707.     while(exist_paren(equation_s)) //check for parentheses
  708.     {  
  709.         result= solve_paren(equation_s); //if parentheses are in equation, goes to solve_paren function
  710.     }
  711.     while(exist_expon(equation_s)) // checks for carrot
  712.     {
  713.         result=solve_expon(equation_s); // if carrot is in equation, goes to slove_expon
  714.     }
  715.     while(exist_mult(equation_s))
  716.     {
  717.         result = solve_mult(equation_s);
  718.     }
  719.     while(exist_add(equation_s))
  720.     {
  721.         result = solve_add(equation_s);
  722.     }
  723.     return result;
  724. }
Advertisement
Add Comment
Please, Sign In to add comment