Advertisement
Jvsierra

Com conio2

Aug 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <windows.h>
  4. #include <conio2.h>
  5.  
  6. //máximo de linhas
  7. #define TFL 25
  8. //máximo de colunas
  9. #define TFC 80
  10.  
  11. //frequências das notas
  12. #define FREQ_DO 261
  13. #define FREQ_RE 293
  14. #define FREQ_MI 329
  15. #define FREQ_FA 349
  16. #define FREQ_SOL 392
  17. #define FREQ_LA 440
  18. #define FREQ_SI 493
  19. //tempo (em milisegundos) em que as notas serão tocadas
  20. #define TEMPO 1000
  21.  
  22. void desenha(int x1, int y1, int x2, int y2)
  23. {
  24. int x;
  25.  
  26. textcolor(4);
  27.  
  28. gotoxy(x1, y1);
  29. printf("*");
  30. gotoxy(x2, y1);
  31. printf("*");
  32. gotoxy(x1, y2);
  33. printf("*");
  34. gotoxy(x2, y2);
  35. printf("*");
  36.  
  37. textcolor(10);
  38.  
  39. for(x = y1 + 1; x < y2; x++)
  40. {
  41. gotoxy(x1, x);
  42. printf("|");
  43. gotoxy(x2, x);
  44. printf("|");
  45. }
  46.  
  47. for(x = x1 + 1; x < x2; x++)
  48. {
  49. gotoxy(x, y1);
  50. printf("-");
  51. gotoxy(x, y2);
  52. printf("-");
  53. }
  54.  
  55. textcolor(15);
  56. }
  57.  
  58. void desenhaCabecalho(char msg[100])
  59. {
  60. desenha(2, 2, 79, 5);
  61.  
  62. gotoxy(20, 3);
  63.  
  64. textcolor(4);
  65.  
  66. printf("%s ", msg);
  67.  
  68. textcolor(15);
  69. }
  70.  
  71. int decToBin(int dec)
  72. {
  73. int ret = 0, cont = 0;
  74.  
  75. while(dec >= 1)
  76. {
  77. if(dec % 2 == 1)
  78. ret += pow(10, cont);
  79.  
  80. cont++;
  81. dec /= 2;
  82. }
  83.  
  84. return ret;
  85. }
  86.  
  87. int binToDec(int bin)
  88. {
  89. int ret = 0, cont = 0;
  90.  
  91. while(bin >= 1)
  92. {
  93. if(bin % 10 == 1)
  94. ret += pow(2, cont);
  95.  
  96. cont++;
  97. bin /= 10;
  98. }
  99.  
  100. return ret;
  101. }
  102.  
  103. int numPerf(int n)
  104. {
  105. int ret, i, soma = 0;
  106.  
  107. for(i = 1; i < n; i++)
  108. if(n % i == 0)
  109. soma += i;
  110.  
  111. if(soma == n)
  112. ret = 1;
  113. else
  114. ret = 0;
  115.  
  116. return ret;
  117. }
  118.  
  119. int difHora(int h1, int m1, int s1, int h2, int m2, int s2)
  120. {
  121. return (h2 * 3600 + m2 * 60 + s2) - (h1 * 3600 + m1 * 60 + s1);
  122. }
  123.  
  124. int fatorial(int n)
  125. {
  126. int fat = 1, i;
  127.  
  128. for(i = n; i >= 1; i--)
  129. fat *= i;
  130.  
  131. return fat;
  132. }
  133.  
  134. int menu(void)
  135. {
  136. int i;
  137.  
  138. desenha(2, 6, 35, 20);
  139.  
  140. gotoxy(15, 7);
  141.  
  142. printf("MENU");
  143.  
  144. do
  145. {
  146. gotoxy(5, 8);
  147.  
  148. printf("1 - Decimal para binario");
  149.  
  150. gotoxy(5, 9);
  151.  
  152. printf("2 - Binario para decimal");
  153.  
  154. gotoxy(5, 10);
  155.  
  156. printf("3 - Numero perfeito");
  157.  
  158. gotoxy(5, 11);
  159.  
  160. printf("4 - Diferenca entre duas horas");
  161.  
  162. gotoxy(5, 12);
  163.  
  164. printf("5 - Desenhar moldura");
  165.  
  166. gotoxy(5, 13);
  167.  
  168. printf("6 - Calcular fatorial");
  169.  
  170. gotoxy(5, 14);
  171.  
  172. printf("7 - Usar piano");
  173.  
  174. gotoxy(5, 15);
  175.  
  176. printf("8 - Sair ");
  177.  
  178. gotoxy(5, 16);
  179.  
  180. scanf("%d", &i);
  181. }while(i < 1 || i > 8);
  182.  
  183. return i;
  184. }
  185.  
  186. void piano(int op)
  187. {
  188. switch(op)
  189. {
  190. case 97:
  191. Beep(FREQ_DO, TEMPO);
  192. break;
  193. case 115:
  194. Beep(FREQ_RE, TEMPO);
  195. break;
  196. case 100:
  197. Beep(FREQ_MI, TEMPO);
  198. break;
  199. case 102:
  200. Beep(FREQ_FA, TEMPO);
  201. break;
  202. case 103:
  203. Beep(FREQ_SOL, TEMPO);
  204. break;
  205. case 104:
  206. Beep(FREQ_LA, TEMPO);
  207. break;
  208. case 106:
  209. Beep(FREQ_SI, TEMPO);
  210. break;
  211. default:
  212. printf("");
  213. }
  214. }
  215.  
  216. int menuPiano(void)
  217. {
  218. int i;
  219.  
  220. gotoxy(39, 7);
  221. printf("a - do");
  222. gotoxy(39, 8);
  223. printf("s - re");
  224. gotoxy(39, 9);
  225. printf("d - mi");
  226. gotoxy(39, 10);
  227. printf("f - fa");
  228. gotoxy(39, 11);
  229. printf("g - sol");
  230. gotoxy(39, 12);
  231. printf("h - la");
  232. gotoxy(39, 13);
  233. printf("j - si");
  234. gotoxy(39, 14);
  235.  
  236. i = getch();
  237. return i;
  238. }
  239.  
  240. void desenhaRodape(char msg[100])
  241. {
  242. desenha(2, 21, 79, 24);
  243.  
  244. gotoxy(25, 22);
  245.  
  246. textcolor(11);
  247.  
  248. printf("%s", msg);
  249. }
  250.  
  251. void usoFuncoes(int op)
  252. {
  253. int n, x1, y1, x2, y2, h1, m1, s1, h2, m2, s2;
  254.  
  255. switch(op)
  256. {
  257. case 1:
  258. gotoxy(39, 7);
  259. printf("Digite o numero em decimal:\n");
  260. gotoxy(39, 8);
  261. scanf("%d", &n);
  262. gotoxy(39, 9);
  263. printf("%d = %d em binario\n", n, decToBin(n));
  264. gotoxy(39, 10);
  265. getch();
  266. break;
  267. case 2:
  268. gotoxy(39, 7);
  269. printf("Digite o numero em binario:\n");
  270. gotoxy(39, 8);
  271. scanf("%d", &n);
  272. gotoxy(39, 9);
  273. printf("%d = %d em decimal\n", n, binToDec(n));
  274.  
  275. gotoxy(39, 10);
  276. getch();
  277. break;
  278. case 3:
  279. gotoxy(39, 7);
  280. printf("Digite o numero:\n");
  281. gotoxy(39, 8);
  282. scanf("%d", &n);
  283.  
  284. gotoxy(39, 9);
  285.  
  286. if(numPerf(n) == 1)
  287. printf("O numero e perfeito\n");
  288. else
  289. printf("O numero nao e perfeito\n");
  290.  
  291. gotoxy(39, 10);
  292.  
  293. getch();
  294. break;
  295. case 4:
  296. gotoxy(39, 7);
  297. printf("Digite a primeira hora (h, m, s):");
  298. gotoxy(39, 8);
  299. scanf("%d %d %d", &h1, &m1, &s1);
  300. gotoxy(39, 9);
  301. printf("Digite a segunda hora (h, m, s):");
  302. gotoxy(39, 10);
  303. scanf("%d %d %d", &h2, &m2, &s2);
  304.  
  305. gotoxy(39, 11);
  306.  
  307. printf("Diferenca = %d segundos", difHora(h1, m2, s1, h2, m2, s2));
  308.  
  309. gotoxy(39, 12);
  310.  
  311. getch();
  312. break;
  313. case 5:
  314. do
  315. {
  316. gotoxy(39, 7);
  317. printf("Digite a linha inicial:");
  318.  
  319. gotoxy(39, 8);
  320.  
  321. scanf("%d", &y1);
  322. }while(y1 < 1 || y1 > TFL);
  323.  
  324. do
  325. {
  326. gotoxy(39, 9);
  327. printf("Digite a coluna inicial:");
  328.  
  329. gotoxy(39, 10);
  330. scanf("%d", &x1);
  331. }while(x1 < 1 || x1 > TFC);
  332.  
  333. do
  334. {
  335. gotoxy(39, 11);
  336. printf("Digite a linha final:");
  337.  
  338. gotoxy(39, 12);
  339.  
  340. scanf("%d", &y2);
  341. }while(y2 < 1 || y2 > TFL);
  342.  
  343. do
  344. {
  345. gotoxy(39, 13);
  346. printf("Digite a coluna final:");
  347.  
  348. gotoxy(39, 14);
  349. scanf("%d", &x2);
  350. }while(x2 < 1 || x2 > TFC);
  351.  
  352.  
  353. clrscr();
  354.  
  355. desenha(x1, y1, x2, y2);
  356.  
  357. getch();
  358. break;
  359. case 6:
  360. gotoxy(39, 7);
  361. printf("Digite o numero:\n");
  362. gotoxy(39, 8);
  363. scanf("%d", &n);
  364.  
  365. gotoxy(39, 9);
  366. printf("Fatorial de %d = %d\n", n, fatorial(n));
  367.  
  368. gotoxy(39, 10);
  369. getch();
  370. break;
  371. case 7:
  372. n = menuPiano();
  373.  
  374. while(n != 27)
  375. {
  376. piano(n);
  377.  
  378. n = menuPiano();
  379. }
  380. break;
  381. default:
  382. printf("Opcao invalida\n");
  383. }
  384. }
  385.  
  386. void formulario(void)
  387. {
  388. int i;
  389.  
  390. do
  391. {
  392. clrscr();
  393.  
  394. desenha(1, 1, 80, 25);
  395.  
  396. desenhaCabecalho("COMPILADOR DOS EXERCICIOS DE ATP II");
  397.  
  398. desenha(37, 6, 79, 20);
  399.  
  400. desenhaRodape("***Versao do programa: 1.0***");
  401.  
  402. i = menu();
  403.  
  404. if(i != 8)
  405. usoFuncoes(i);
  406. else
  407. gotoxy(1, 25);
  408. }while(i != 8);
  409.  
  410. textcolor(15);
  411. }
  412.  
  413. int main(void)
  414. {
  415. formulario();
  416.  
  417. printf("\n");
  418.  
  419. getch();
  420.  
  421. return 0;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement