Advertisement
Jvsierra

Atp II

Aug 20th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.60 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.        
  182.         gotoxy(5, 17);
  183.     }while(i < 1 || i > 8);
  184.  
  185.     return i;
  186. }
  187.  
  188. void piano(int op)
  189. {
  190.     switch(op)
  191.         {
  192.             case 97:
  193.                 Beep(FREQ_DO, TEMPO);
  194.             break;
  195.             case 115:
  196.                 Beep(FREQ_RE, TEMPO);
  197.             break;
  198.             case 100:
  199.                 Beep(FREQ_MI, TEMPO);
  200.             break;
  201.             case 102:
  202.                 Beep(FREQ_FA, TEMPO);
  203.             break;
  204.             case 103:
  205.                 Beep(FREQ_SOL, TEMPO);
  206.             break;
  207.             case 104:
  208.                 Beep(FREQ_LA, TEMPO);
  209.             break;
  210.             case 106:
  211.                 Beep(FREQ_SI, TEMPO);
  212.             break;
  213.             default:
  214.                 printf("");
  215.         }
  216. }
  217.  
  218. int menuPiano(void)
  219. {
  220.     int i;
  221.    
  222.     gotoxy(39, 7);
  223.     printf("a - do");
  224.     gotoxy(39, 8);
  225.     printf("s - re");
  226.     gotoxy(39, 9);
  227.     printf("d - mi");
  228.     gotoxy(39, 10);
  229.     printf("f - fa");
  230.     gotoxy(39, 11);
  231.     printf("g - sol");
  232.     gotoxy(39, 12);
  233.     printf("h - la");
  234.     gotoxy(39, 13);
  235.     printf("j - si");
  236.     gotoxy(39, 14);
  237.    
  238.     i = getch();
  239.     return i;
  240. }
  241.  
  242. void desenhaRodape(char msg[100])
  243. {
  244.     desenha(2, 21, 79, 24);
  245.    
  246.     gotoxy(25, 22);
  247.    
  248.     textcolor(11);
  249.    
  250.     printf("%s", msg);
  251. }
  252.  
  253. void usoFuncoes(int op)
  254. {
  255.         int n, x1, y1, x2, y2, h1, m1, s1, h2, m2, s2;
  256.        
  257.         switch(op)
  258.         {
  259.             case 1:
  260.                 gotoxy(39, 7);
  261.                 printf("Digite o numero em decimal:\n");
  262.                 gotoxy(39, 8);
  263.                 scanf("%d", &n);
  264.                 gotoxy(39, 9);
  265.                 printf("%d = %d em binario\n", n, decToBin(n));
  266.                 gotoxy(39, 10);
  267.                 getch();
  268.             break;
  269.             case 2:
  270.                 gotoxy(39, 7);
  271.                 printf("Digite o numero em binario:\n");
  272.                 gotoxy(39, 8);
  273.                 scanf("%d", &n);
  274.                 gotoxy(39, 9);
  275.                 printf("%d = %d em decimal\n", n, binToDec(n));
  276.                
  277.                 gotoxy(39, 10);
  278.                 getch();
  279.             break;
  280.             case 3:
  281.                 gotoxy(39, 7);
  282.                 printf("Digite o numero:\n");
  283.                 gotoxy(39, 8);
  284.                 scanf("%d", &n);
  285.                
  286.                 gotoxy(39, 9);
  287.                
  288.                 if(numPerf(n) == 1)
  289.                     printf("O numero e perfeito\n");
  290.                 else
  291.                     printf("O numero nao e perfeito\n");
  292.                
  293.                 gotoxy(39, 10);
  294.                    
  295.                 getch();
  296.             break;
  297.             case 4:
  298.                 gotoxy(39, 7);
  299.                 printf("Digite a primeira hora (h, m, s):");
  300.                 gotoxy(39, 8);
  301.                 scanf("%d %d %d", &h1, &m1, &s1);
  302.                 gotoxy(39, 9);
  303.                 printf("Digite a segunda hora (h, m, s):");
  304.                 gotoxy(39, 10);
  305.                 scanf("%d %d %d", &h2, &m2, &s2);
  306.                
  307.                 gotoxy(39, 11);
  308.                
  309.                 printf("Diferenca = %d segundos", difHora(h1, m2, s1, h2, m2, s2));
  310.                
  311.                 gotoxy(39, 12);
  312.                
  313.                 getch();
  314.             break;
  315.             case 5:
  316.                  do
  317.                 {
  318.                     gotoxy(39, 7);
  319.                     printf("Digite a linha inicial:");
  320.                    
  321.                     gotoxy(39, 8);
  322.                    
  323.                     scanf("%d", &y1);
  324.                 }while(y1 < 1 || y1 > TFL);
  325.                    
  326.                 do
  327.                 {
  328.                     gotoxy(39, 9);
  329.                     printf("Digite a coluna inicial:");
  330.                    
  331.                     gotoxy(39, 10);
  332.                     scanf("%d", &x1);
  333.                 }while(x1 < 1 || x1 > TFC);
  334.                
  335.                  do
  336.                 {
  337.                     gotoxy(39, 11);
  338.                     printf("Digite a linha final:");
  339.                    
  340.                     gotoxy(39, 12);
  341.                    
  342.                     scanf("%d", &y2);
  343.                 }while(y2 < 1 || y2 > TFL);
  344.                    
  345.                 do
  346.                 {
  347.                     gotoxy(39, 13);
  348.                     printf("Digite a coluna final:");
  349.                    
  350.                     gotoxy(39, 14);
  351.                     scanf("%d", &x2);
  352.                 }while(x2 < 1 || x2 > TFC);
  353.                
  354.                
  355.                 clrscr();
  356.                
  357.                 desenha(x1, y1, x2, y2);
  358.                
  359.                 getch();
  360.             break;
  361.             case 6:
  362.                 gotoxy(39, 7);
  363.                 printf("Digite o numero:\n");
  364.                 gotoxy(39, 8);
  365.                 scanf("%d", &n);
  366.                
  367.                 gotoxy(39, 9);
  368.                 printf("Fatorial de %d = %d\n", n, fatorial(n));
  369.                
  370.                 gotoxy(39, 10);
  371.                 getch();
  372.             break;
  373.             case 7:
  374.                 n = menuPiano();
  375.                
  376.                 while(n != 27)
  377.                 {
  378.                     piano(n);
  379.                    
  380.                     n = menuPiano();
  381.                 }
  382.             break;
  383.             default:
  384.                 printf("Opcao invalida\n");
  385.         }
  386. }
  387.  
  388. void formulario(void)
  389. {
  390.     int i; 
  391.    
  392.     do
  393.     {
  394.         clrscr();
  395.        
  396.         //retângulo maior
  397.         desenha(1, 1, 80, 25);
  398.        
  399.         //cabeçalho
  400.         desenhaCabecalho("COMPILADOR DOS EXERCICIOS DE ATP II");
  401.        
  402.         // segundo retângulo do meio
  403.         desenha(37, 6, 79, 20);
  404.        
  405.         //rodapé
  406.         desenhaRodape("***Versao do programa: 1.0***");
  407.        
  408.         //menu
  409.         i = menu();
  410.        
  411.         if(i != 8)
  412.             usoFuncoes(i);
  413.         else
  414.             gotoxy(1, 25);
  415.     }while(i != 8);
  416.    
  417.     textcolor(15);
  418. }
  419.  
  420. int main(void)
  421. {
  422.     formulario();
  423.    
  424.     printf("\n");
  425.    
  426.     getch();
  427.    
  428.     return 0;
  429. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement