У меня есть код, препод сказал надо сделать так чтоб считал отрицательный факториал, например -5. Подскажите что надо изменить в коде?вот код:
| Код | // Лаба2.cpp: определяет точку входа для консольного приложения. //
#include "stdafx.h" #include "stdio.h" #include "math.h" #include "conio.h" #include "windows.h"
long factorial (long x) { if (x==0) return 1; if (x==1) return 1; else return x*factorial(x-1); }
void fQE() { system("cls"); float a=0, b=0,c=0; printf ("Quadratic equations: \n a*(x^2)+b*x+c = 0\n\n"); printf ("a,b,c :"); scanf_s ("%f,%f,%f",&a,&b,&c);
printf ("a=%f, b=%f, c=%f\n\n",a,b,c); if(a==0) {printf("not quadratic equations"); _getch(); return;} float d = b*2-4*a*c; if (d<0) {printf("no roots"); _getch(); return;} printf("x1 = %f\nx2 = %f\n",(-b +sqrt (d)/2*a),(-b -sqrt (d)/2*a)); _getch();
}
void main() {
int nfac=0; long ifac=1;
do { system ("cls"); printf ("(1) - Quadratic equations\n (2) - Factorial\n (3) - Factorial (recursively)\n \n Please select: "); char iSelect = _getch(); switch (iSelect) { case '1': fQE(); break;
case '2': system("cls"); printf ("Factorial.\n \n Please enter n: "); ifac=1; scanf_s("%i",&nfac); if (nfac >=0 ) { for (int i=2;i<=nfac;i++) { ifac =ifac*i; } printf ("%i! = %i",nfac,ifac); } else printf("n can't be less that 0!\n"); _getch(); _getch(); break; case '3': system("cls"); printf ("Factorial (recursively).\n \n Please enter n: "); scanf_s("%i",&nfac); if (nfac >=0 ) { printf ("%i! = %i",nfac,factorial(nfac)); } else printf("n can't be less that 0!\n"); _getch(); _getch();
break;
default: printf ("\n\nBad select, try again"); _getch(); _getch(); break; } } while (true); } |
Модератор: не забываем пользоваться кнопочкой "Код" |