Advertisement
Dario95

Matematica.c

Mar 11th, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "CUnit/Basic.h"
  4. int min(int a, int b);
  5. int max(int a, int b);
  6. int succ(int a); // incrementa a di uno
  7. int pred(int a); // decrementa a di uno
  8. int sum(int a, int b);
  9. int subtract(int a, int b);
  10. int product(int a, int b);
  11. int factorial(int n, int fatt); // 0 <= n <= 10
  12.  
  13. int min(int a, int b) {
  14. if (a < b) {
  15. return a;
  16. }
  17. if (a > b) {
  18. return b;
  19. }
  20.  
  21. if (a == b) {
  22.  
  23. return "Impossibile calcolare il minimo tra questi due numeri";
  24. }
  25.  
  26. }
  27.  
  28. int max(int a, int b) {
  29. if (a > b) {
  30. return a;
  31. }
  32. if (b > a) {
  33. return b;
  34. }
  35. if (a == b) {
  36. return "Impossibile calcolare il massimo tra questi due numeri";
  37. }
  38. }
  39.  
  40. int succ(int a) {
  41.  
  42. a = a + 1;
  43. return a;
  44. }
  45.  
  46. int pred(int a) {
  47. a = a - 1;
  48. return a;
  49. }
  50.  
  51. int sum(int a, int b) {
  52. int i, s = a;
  53.  
  54. if (b > 0 && a > 0) {
  55. for (i = 0; i < b; i++) {
  56. s = succ(s);
  57.  
  58. }
  59. }
  60.  
  61. if (b < 0 && a > 0) {
  62. for (i = b; i < 0; i++) {
  63. s = pred(s);
  64. }
  65. }
  66. if (b < 0 && a < 0) {
  67. for (i = b; i < 0; i++) {
  68. s = pred(s);
  69. }
  70. }
  71. if (b > 0 && a < 0) {
  72. for (i = 0; i < b; i++) {
  73. s = succ(s);
  74. }
  75. }
  76. if (b != 0 && a == 0) {
  77. return b;
  78. }
  79. return s;
  80. }
  81.  
  82. int subtract(int a, int b) {
  83. int i, s = a;
  84.  
  85. if (b != 0 && a == 0) {
  86. return -b;
  87. }
  88. if (b == 0 && a != 0) {
  89. return s;
  90. }
  91. if (a > 0 && b > 0) {
  92. for (i = 0; i < b; i++) {
  93. s = pred(s);
  94. }
  95. return s;
  96. }
  97. if (a < 0 && b < 0) {
  98. for (i = b; i < 0; i++) {
  99. s = succ(s);
  100. }
  101. return s;
  102. }
  103. if (a < 0 && b > 0) {
  104. for (i = 0; i < b; i++) {
  105. s = pred(s);
  106. }
  107. return s;
  108. }
  109. if (a > 0 && b < 0) {
  110. for (i = b; i < 0; i++) {
  111. s = succ(s);
  112. }
  113. return s;
  114. }
  115. }
  116.  
  117. int product(int a, int b) {
  118. int i, s = 0;
  119. if (a > 0 && b > 0) {
  120. for (i = 0; i < b; i++) {
  121. s = sum(a, s);
  122. }
  123. return s;
  124. }
  125.  
  126. if (a > 0 && b < 0) {
  127. for (i = b; i < 0; i++) {
  128. s = sum(a, s);
  129. }
  130. return -s;
  131. }
  132. if (a < 0 && b < 0) {
  133. for (i = b; i < 0; i++) {
  134. s = sum(a, s);
  135. }
  136. return s *-1;
  137. }
  138.  
  139. if (a < 0 && b > 0) {
  140. for (i = 0; i < b; i++) {
  141. s = sum(a, s);
  142. }
  143. return s;
  144. }
  145. if (a == 0 || b == 0) {
  146. return 0;
  147. }
  148.  
  149. }
  150.  
  151. int factorial(int n, int fatt){
  152. int i;
  153.  
  154. if(n < 0){
  155. return "Impossibile calcolare il fattoriale di un numero negativo";
  156. }
  157. if(n == 0){
  158. return 0;
  159. }
  160. if(n > 0){
  161. //for(i = 1; i <= n; i++){
  162. //fatt = fatt * i;
  163.  
  164. //}
  165. if(n == 1){
  166.  
  167. return fatt;
  168. }
  169. if(n > 1){
  170. fatt = product(fatt, n);
  171. n--;
  172. return factorial(n, fatt);
  173.  
  174. }
  175. }
  176. }
  177. /*
  178. * Aggiungere tutti i metodi di test per le funzioni da testare
  179. */
  180. void test_min(void) {
  181.  
  182.  
  183. CU_ASSERT_EQUAL( min(4, 10), 4);
  184. CU_ASSERT_STRING_EQUAL(min(2,2), "Impossibile calcolare il minimo tra questi due numeri" );
  185. }
  186.  
  187. void test_max(void) {
  188.  
  189. CU_ASSERT_EQUAL(max(10, 11), 11);
  190. CU_ASSERT_EQUAL(max(10, 10), "Impossibile calcolare il massimo tra questi due numeri");
  191. CU_ASSERT_EQUAL(max(0, 0), "Impossibile calcolare il massimo tra questi due numeri");
  192. }
  193.  
  194. void test_sum(void) {
  195.  
  196. CU_ASSERT_EQUAL(sum(5, 6), 11);
  197. CU_ASSERT_EQUAL(sum(4, -3), 1);
  198. CU_ASSERT_EQUAL(sum(-4, -3), -7);
  199. CU_ASSERT_EQUAL(sum(-4, 3), -1);
  200. CU_ASSERT_EQUAL(sum(4, 0), 4);
  201. CU_ASSERT_EQUAL(sum(0, -3), -3);
  202. }
  203.  
  204. void test_pred(void) {
  205. CU_ASSERT_EQUAL(succ(2), 3);
  206. }
  207.  
  208. void test_succ(void) {
  209. CU_ASSERT_EQUAL(pred(2), 1);
  210. }
  211.  
  212. void test_subtract(void) {
  213. CU_ASSERT_EQUAL(subtract(5, 6), -1);
  214. CU_ASSERT_EQUAL(subtract(4, -3), 7);
  215. CU_ASSERT_EQUAL(subtract(-4, -3), -1);
  216. CU_ASSERT_EQUAL(subtract(-4, 3), -7);
  217. CU_ASSERT_EQUAL(subtract(4, 0), 4);
  218. CU_ASSERT_EQUAL(subtract(0, 3), -3);
  219. }
  220.  
  221. void test_product(void) {
  222. CU_ASSERT_EQUAL(product(2, 3), 6);
  223. CU_ASSERT_EQUAL(product(2, -3), -6);
  224. CU_ASSERT_EQUAL(product(-2, -3), 6);
  225. CU_ASSERT_EQUAL(product(-2, 3), -6);
  226. CU_ASSERT_EQUAL(product(0, 3), 0);
  227. CU_ASSERT_EQUAL(product(-2, 0), 0);
  228. }
  229. void test_factorial(void){
  230. CU_ASSERT_EQUAL(factorial(3, 1), 6);
  231. CU_ASSERT_STRING_EQUAL(factorial(-3, 1), "Impossibile calcolare il fattoriale di un numero negativo");
  232. CU_ASSERT_EQUAL(factorial(0, 1), 0);
  233. CU_ASSERT_EQUAL(factorial(10, 1), 3628800);
  234. }
  235. /*
  236. * Funzioni di inizializzazione e pulizia delle suite.
  237. * Di default sono funzioni vuote.
  238. */
  239. int init_suite_default(void) {
  240. return 0;
  241. }
  242.  
  243. int clean_suite_default(void) {
  244. return 0;
  245. }
  246.  
  247. /* **************************************************
  248. * TEST di UNITA'
  249. */
  250.  
  251. int main() {
  252. /* inizializza registro - e' la prima istruzione */
  253. CU_initialize_registry();
  254.  
  255. /* Aggiungi le suite al test registry */
  256. CU_pSuite pSuite_A = CU_add_suite("Suite_A", init_suite_default,
  257. clean_suite_default);
  258. CU_pSuite pSuite_B = CU_add_suite("Suite_B", init_suite_default,
  259. clean_suite_default);
  260. CU_pSuite pSuite_C = CU_add_suite("Suite_C", init_suite_default,
  261. clean_suite_default);
  262. CU_pSuite pSuite_D = CU_add_suite("Suite_D", init_suite_default,
  263. clean_suite_default);
  264. CU_pSuite pSuite_E = CU_add_suite("Suite_E", init_suite_default,
  265. clean_suite_default);
  266. /* Aggiungi i test alle suite
  267. * NOTA - L'ORDINE DI INSERIMENTO E' IMPORTANTE
  268. */
  269. CU_add_test(pSuite_A, "test of min()", test_min);
  270. CU_add_test(pSuite_A, "test of max()", test_max);
  271.  
  272. CU_add_test(pSuite_B, "test of sum()", test_sum);
  273.  
  274. CU_add_test(pSuite_C, "test of pred()", test_pred);
  275. CU_add_test(pSuite_C, "test of succ()", test_succ);
  276.  
  277. CU_add_test(pSuite_D, "test of subtract()", test_subtract);
  278. CU_add_test(pSuite_D, "test of product()", test_product);
  279.  
  280. CU_add_test(pSuite_E, "test of factorial()", test_factorial);
  281.  
  282.  
  283. /* Esegue tutti i casi di test con output sulla console */
  284. CU_basic_set_mode(CU_BRM_VERBOSE);
  285. CU_basic_run_tests();
  286.  
  287. /* Pulisce il registro e termina lo unit test */
  288. CU_cleanup_registry();
  289. system("PAUSE");
  290. return CU_get_error();
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement