Как мне улучшить свой проект, который я назвал универсальным калькулятором?
Итак, у меня появилась идея создать проект на языке Си, который я назвал универсальным калькулятором. В основном, это код, который помогает решать общие задачи и вычисления в математике. Она состоит из main.cpp и метод.c. Он работает без каких-либо ошибок. Не могли бы вы подсказать мне, как я могу улучшить свой проект, потому что я хочу разместить его где-нибудь (например, на github), или использовать этот код для создания программного обеспечения, или использовать его в качестве плагина на веб-сайте.
Что я уже пробовал:
main.cpp:
//Emil Ahmadov //Program language C //Universal Calculator #include <stdio.h> #include "method.c" int main() { double a; double b; int check; int check2; int check3; double c; const double pi=3.14159265359; printf("Welcome to the Universal Calculator\n"); printf("Instructions: \n"); printf("1. Choose the command\n"); printf("2. Enter the number or numbers\n"); printf("3. Here is your answer\n\n"); printf("Operations with integers, decimals, radicals (press 1)\n" "Trigonometric operations (press 2 )\n" "Solve quadratic equation (press 3)\n" "Math formulas (press 4)\n"); printf("\nYour choice: "); scanf("%d", &check); while(check>4||check<1) { printf("\nWrong Command!\n"); printf("Your choice: "); scanf("%d", &check); } if(check==1) { printf("\nChoose the operation:\n"); printf("\nAddition (press 1)\nSubtraction (press 2)\nMultiplication (press 3)\nDivision (press 4)\n" "Raise into Power (press 5)\nExponent (press 6)\nLogarithm-base 10 (press 7)\nNatural logarithm (press 8)\n" "Square root (press 9)\nRound the number (press 10)\n"); printf("\nYour choice: "); scanf("%d", &check3); while(check3>10||check3<1) { printf("\nWrong Command!\n"); printf("Your choice: "); scanf("%d", &check3); } printf("\nNote: The digits entered should be either integers or decimals\n"); if(check3==1 || check3==2 || check3==3 || check3==5) { printf("\nEnter the first number: "); scanf("%lf", &a); printf("Enter the second number: "); scanf("%lf", &b); if(check3==1) { printf("\naddition: %lf\n", add(a,b)); } else if(check3==2) { printf("\nsubtraction: %lf\n", subtract(a,b)); } else if(check3==3) { printf("\nmultiplication: %lf\n", multiply(a,b)); } else { printf("\npower: %lf\n", power(a, b)); } } else if(check3==4) { printf("\nEnter the first number: "); scanf("%lf", &a); printf("Enter the second number: "); scanf("%lf", &b); while(b==0) { printf("\nError! divider shouldn't be equal to zero\n"); printf("Enter the second number again: "); scanf("%lf", &b); } printf("\ndivision: %lf\n", divide(a,b)); } else if(check3==6 || check3==7 || check3==8 || check3==9 || check3==10) { if(check3==6) { printf("\nEnter the number: "); scanf("%lf", &a); printf("\nexponent (e^x) : %lf\n", exponent(a)); } else if(check3==7) { printf("\nEnter the base of the logarithm: "); scanf("%lf", &a); while(a<=0) { printf("\nError! Base of the logarithm should be positive\n"); printf("Enter the base again: "); scanf("%lf", &a); } printf("\nlogarithm (base 10) : %lf\n", logarithm(a)); } else if(check3==8) { printf("\nEnter the base of a natural logarithm: "); scanf("%lf", &a); while(a<=0) { printf("\nError! Base of the logarithm should be positive\n"); printf("Enter the base of a natural logarithm again: "); scanf("%lf", &a); } printf("\nnatural logarithm (base e) : %lf\n", nat_log(a)); } else if(check3==9) { printf("\nEnter the number: "); scanf("%lf", &a); while(a<0) { printf("\nError! Cannot take the square root from a negative number\n"); printf("Enter the number again: "); scanf("%lf", &a); } printf("\nsquare root: %lf\n", sq_root(a)); } else { printf("\nEnter the number: "); scanf("%lf", &a); printf("\nrounded : %lf\n", round(a)); } } } else if(check==2) { printf("\nChoose the operation:\n"); printf("\nsin: press 1\ncos: press 2\ntan: press 3\nctg: press 4\nsinh: press 5\ncosh: press 6\ntanh: press 7\nctgh: press 8\n"); printf("\nYour choice: "); scanf("%d", &check2); while(check2>8||check2<1) { printf("\nWrong Command!\n"); printf("Your choice: "); scanf("%d", &check2); } if(check2==1||check2==2||check2==3||check2==4||check2==5||check2==6||check2==7||check2==8) { printf("\nEnter the angle in degrees: "); scanf("%lf", &c); double res; res = trig(c, check2); printf("\nresult is: %lf\n", res); } } else if(check==3) { float c1, c2, c3; float r1, r2; printf("\nNote: The digits entered should be either integers or decimals\n"); printf("\nEnter the first coefficient: "); scanf("%f", &c1); printf("Enter the second coefficient: "); scanf("%f", &c2); printf("Enter the third coefficient: "); scanf("%f", &c3); float disc; disc=pow(c2, 2)-4*c1*c3; if(disc>0) { printf("\nRoots are real and different!\n\n"); r1=(-c2+sqrt(disc))/(2*c1); r2=(-c2-sqrt(disc))/(2*c1); printf("first root = %f\n", r1); printf("second root = %f\n", r2); } else if(disc==0) { printf("\nRoots are real and same!\n\n"); r1= (-c2+sqrt(disc))/(2*c1); printf("first root = second root = %f\n", r1); } else { printf("\nRoots are complex and different!\n\n"); float realPart = (-c2)/(2*c1); float imagPart = sqrt(-disc)/(2*c1); printf("first root = %f + %fi\n",realPart, imagPart); printf("second root = %f - %fi\n",realPart, imagPart); } } else if(check==4) { int check4; printf("\nCalculate the average of numbers (press 1)"); printf("\nCalculate the percent of a number (press 2)"); printf("\nCalculate the factorial (press 3)"); printf("\nPythagorean theorem (press 4)"); printf("\nCalculate the perimeter (press 5)"); printf("\nCalculate the area (press 6)"); printf("\nCalculate the volume (press 7)\n"); printf("\nYour choice: "); scanf("%d", &check4); while(check4>7||check4<1) { printf("\nWrong Command!\n"); printf("Your choice: "); scanf("%d", &check4); } if(check4==1) { int i, n; double sum=0; double a[100]; double avg; printf("\nEnter the number of digits for which you want to find the average: "); scanf("%d", &n); while (n > 100 || n <= 0) { printf("\nError! number should in range of (1 to 100).\n"); printf("Enter the number again: "); scanf("%d", &n); } for(i=0; i<n; i++) { printf("Enter the digit %d: ", i+1); scanf("%lf", &a[i]); sum=sum+a[i]; } avg=sum/n; printf("\nThe average is %lf\n", avg); } else if(check4==2) { double a; double b; double res; printf("\nEnter the digit: "); scanf("%lf", &a); printf("\nEnter the percentage: "); scanf("%lf", &b); res=a*b/100; printf("\nThe result is %lf", res); } else if(check4==3) { int k; long fac=1; printf("\nEnter a number to calculate its factorial: "); scanf("%d", &k); fac=factorial(k); printf("\nFactorial of %d = %ld", k, fac); } else if(check4==4) { int check5; double cath1; double cath2; double hyp; printf("\nWhich side of a right angle triangle do you want to find?\n"); printf("hypotenuse (press 1)\n"); printf("cathetus (press 2)\n"); printf("\nYour choice: "); scanf("%d",&check5); while(check5>2||check5<1) { printf("\nWrong Command!\n"); printf("\nYour choice: "); scanf("%d", &check5); } if(check5==1) { printf("\nEnter the first cathetus: "); scanf("%lf", &cath1); printf("Enter the second cathetus: "); scanf("%lf", &cath2); hyp=sqrt(pow(cath1,2)+pow(cath2, 2)); printf("\nThe calculated hypotenuse: %lf", hyp); } else if(check5==2) { printf("\nEnter the hypotenuse: "); scanf("%lf", &hyp); printf("Enter the known cathetus: "); scanf("%lf", &cath1); cath2=sqrt(pow(hyp, 2)-pow(cath1,2)); printf("\nThe calculated cathetus: %lf", cath2); } } else if(check4==5) { int check6; double sq; double rect1, rect2; double tria1, tria2, tria3; double rad; printf("\nPerimeter of which shape do you want to calculate? "); printf("\nsquare (press 1)"); printf("\nrectangle (press 2)"); printf("\ntriangle (press 3)"); printf("\ncircle (press 4)\n"); printf("\nYour choice: "); scanf("%d", &check6); while(check6>4||check6<1) { printf("\nWrong Command!\n"); printf("\nYour choice: "); scanf("%d", &check6); } if(check6==1) { printf("\nEnter the side of a square: "); scanf("%lf", &sq); printf("\nPerimeter = %lf", 4*sq); } else if(check6==2) { printf("\nEnter the length of a rectangle: "); scanf("%lf", &rect1); printf("Enter the width of a rectangle: "); scanf("%lf", &rect2); printf("\nPerimeter = %lf", 2*(rect1+rect2)); } else if(check6==3) { printf("\nEnter the first side of a triangle: "); scanf("%lf", &tria1); printf("Enter the second side of a triangle: "); scanf("%lf", &tria2); printf("Enter the third side of a triangle: "); scanf("%lf", &tria3); printf("\nPerimeter = %lf", tria1+tria2+tria3); } else if(check6==4) { printf("\nEnter the radius of a circle: "); scanf("%lf", &rad); printf("\nPerimeter = %lf", 2*pi*rad); } } else if(check4==6) { int check7; double rect1, rect2; double tria1, tria2, tria3; double sq, rad; double s,s1; double triang, constant; double side, h; double r1, r2; double b1, b2, h1; printf("\nArea of which shape do you want to calculate? "); printf("\nsquare (press 1)"); printf("\nrectangle (press 2)"); printf("\ntriangle using Heron's formula (press 3)"); printf("\nequilateral triangle (press 4)"); printf("\ncircle (press 5)"); printf("\nparallelogram (press 6)"); printf("\ntrapezoid (press 7)"); printf("\nellipse (press 8)\n"); printf("\nYour choice: "); scanf("%d", &check7); while(check7>8||check7<1) { printf("\nWrong Command!\n"); printf("\nYour choice: "); scanf("%d", &check7); } if(check7==1) { printf("\nEnter the side of a square: "); scanf("%lf", &sq); printf("\nArea = %lf", pow(sq,2)); } if(check7==2) { printf("\nEnter the length of a rectangle: "); scanf("%lf", &rect1); printf("Enter the width of a rectangle: "); scanf("%lf", &rect2); printf("\nArea = %lf", rect1*rect2); } if(check7==3) { printf("\nEnter the first side of a triangle: "); scanf("%lf", &tria1); printf("Enter the second side of a triangle: "); scanf("%lf", &tria2); printf("Enter the third side of a triangle: "); scanf("%lf", &tria3); s=(tria1+tria2+tria3)/2; s1=s*(s-tria1)*(s-tria2)*(s-tria3); printf("\nArea = %lf", sqrt(s1)); } if(check7==4) { printf("\nEnter the side of an equilateral triangle: "); scanf("%lf", &triang); constant=sqrt(3)/4; printf("\nArea = %lf", constant*pow(triang, 2)); } if(check7==5) { printf("\nEnter the radius of a circle: "); scanf("%lf", &rad); printf("\nArea = %lf", pi*pow(rad,2)); } if(check7==6) { printf("\nEnter the side of a parallelogram: "); scanf("%lf", &side); printf("Enter the height of a parallelogram: "); scanf("%lf", &h); printf("\nArea = %lf", side*h); } if(check7==7) { printf("\nEnter the first base of a trapezoid: "); scanf("%lf", &b1); printf("Enter the second base of a trapezoid: "); scanf("%lf", &b2); printf("Enter the height of a trapezoid: "); scanf("%lf", &h1); printf("\nArea = %lf", (b1+b2)*h1/2); } if(check7==8) { printf("\nEnter the first radius of an ellipse: "); scanf("%lf", &r1); printf("Enter the second radius of an ellipse: "); scanf("%lf", &r2); printf("\nArea = %lf", pi*r1*r2); } } else if(check4==7) { int check8; double sq; double rect1, rect2, rect3; double r,h; double b; double e1, e2, e3; double v; printf("\nVolume of which shape do you want to calculate? "); printf("\ncube (press 1)"); printf("\nrectangular prism (press 2)"); printf("\ncylinder (press 3)"); printf("\npyramid (press 4)"); printf("\ncone (press 5)"); printf("\nsphere (press 6)"); printf("\nellipsoid (press 7)\n"); printf("\nYour choice: "); scanf("%d", &check8); while(check8>7||check8<1) { printf("\nWrong Command!\n"); printf("\nYour choice: "); scanf("%d", &check8); } if(check8==1) { printf("\nEnter the side of a cube: "); scanf("%lf", &sq); printf("\nVolume = %lf", pow(sq,3)); } if(check8==2) { printf("\nEnter the length of a rectangular prism: "); scanf("%lf", &rect1); printf("Enter the width of a rectangular prism: "); scanf("%lf", &rect2); printf("Enter the height of a rectangular prism: "); scanf("%lf", &rect3); printf("\nVolume = %lf", rect1*rect2*rect3); } if(check8==3) { printf("\nEnter the radius of a cylinder: "); scanf("%lf", &r); printf("Enter the height of a cylinder: "); scanf("%lf", &h); printf("\nVolume = %lf", pi*pow(r,2)*h); } if(check8==4) { printf("\nEnter the side of the base of a pyramid: "); scanf("%lf", &b); printf("Enter the height of a pyramid: "); scanf("%lf", &h); printf("\nVolume = %lf", b*h/3); } if(check8==5) { printf("\nEnter the radius of the base of a cone: "); scanf("%lf", &r); printf("Enter the height of a cone: "); scanf("%lf", &h); printf("\nVolume = %lf", pi*pow(r,2)*h/3); } if(check8==6) { printf("\nEnter the radius of a cone: "); scanf("%lf", &r); printf("\nVolume = %lf", 4/3*pi*pow(r,3)); } if(check8==7) { printf("\nEnter the first radius of an ellipsoid: "); scanf("%lf", &e1); printf("Enter the second radius of an ellipsoid: "); scanf("%lf", &e2); printf("Enter the third radius of an ellipsoid: "); scanf("%lf", &e3); printf("\nVolume = %lf", 4/3*pi*e1*e2*e3); } } } printf("\n\nThanks for using the Universal Calculator!\n"); return 0; }
метод.с:
#include <stdio.h> #define _USE_MATH_DEFINES #include <math.h> double add(double a, double b) { return a+b; } double subtract(double a, double b) { return a-b; } double multiply(double a, double b) { return a*b; } double divide(double a, double b) { return a/b; } double power(double a, double b) { if(a==0) { return 0; } else if(b==0) { return 1; } else if(b<0) { long double c; int sign; sign= (-1); b*=sign; c = 1/(pow(a,b)); return c; } else { long double d=pow(a, b); return d; } } double exponent(double a) { return exp(a); } double nat_log(double a) { return log(a); } double logarithm(double a) { return log10(a); } double sq_root(double a) { return sqrt(a); } double trig(double c, int check2) { const double pi=3.14159265359; c=c*pi/180; if(check2==1) { return sin(c); } else if(check2==2) { return cos(c); } else if(check2==3) { return tan(c); } else if(check2==4) { return (1/tan(c)); } else if(check2==5) { return sinh(c); } else if(check2==6) { return cosh(c); } else if(check2==7) { return tanh(c); } else if(check2==8) { return (1/tanh(c)); } else { printf("Wrong Command!"); } } double percentage(double a, double p) { return a*p/100; } long factorial(int a) { long fact=1; int i; for(i=1; i<=a; i++) { fact=fact*i; } return fact; }
Dave Kreskowiak
Я понимаю, что ты гордишься своими достижениями. Но не размещайте этот код нигде. Вы не собираетесь повторно использовать ни одну строку в "реальном" приложении или даже в лучшем приложении для калькулятора.
По мере накопления опыта вы будете оглядываться на этот код и смеяться над тем, что вы его написали, а затем выбрасывать его в мусорное ведро и писать что-то лучше. Затем пройдет еще несколько лет, и вы будете делать это снова, и снова, и снова.
Мы все так делаем.
BillWoodruff
Поздравляю, и я надеюсь, что это личное лучшее мотивирует вас продолжать учиться-экспериментировать-наблюдать-анализировать-переписывать-тестировать-повторять ... навсегда.
KarstenK
Вы также можете использовать ввод символов, и поэтому пользователь может непосредственно использовать операторы"+ -/*". Используйте константы, а не целые числа. Проверьте, что вы никогда не делите с нулем!!!