Advertisement
Guest User

Trabalho PC

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.10 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <time.h>
  6.    
  7.  
  8. typedef struct
  9. {
  10.     int numero;
  11.     char nome[10];
  12.     char apelido[10];
  13. }num_nom_apelid;
  14. char sel, ord, op, dis[20], nome[15];
  15. int numero_de_alunos, i, j, k, w, esco,esc,con,l1,l2,c1,c2,cop, des,ver,des1,des2;
  16. tm t;
  17. FILE *fp, *d;
  18. num_nom_apelid *numero_nome_apelido;
  19. float **matriz1;
  20. float **matriz2;
  21. float **matriz3;
  22. int ordemnum(int a, int b)
  23. {
  24.     int z;
  25.     z = a - b;
  26.     return z;
  27. }
  28.  
  29. int ordemalf(char*a, char*b,char*d,char*e)
  30. {
  31.     int c,f;
  32.     c = strcmp(a, b);
  33.     if (c != 0)
  34.     {
  35.         return c;
  36.     }
  37.     if (c == 0)
  38.     {
  39.         f = strcmp(d, e);
  40.         return f;
  41.     }
  42. }
  43. int data_hora(tm* a)
  44. {
  45.  
  46.     const time_t timer = time(NULL);
  47.     a = gmtime(&timer);
  48.     printf("\n Data: %02d/%02d/%4d ", a->tm_mday, a->tm_mon + 1, 1900 + (a->tm_year));
  49.     printf("    Hora: %02d:%02d:%02d ", a->tm_hour + 1, a->tm_min, a->tm_sec);
  50.     printf("\n");
  51.     return timer;
  52.  
  53. }
  54. int verificacao(char a,int b,int c, int d, int e)
  55. {
  56.     if (a == 'a' || a == 'A' || a == 's' || a == 'S')
  57.     {
  58.         if (b = d && c == e)
  59.         {
  60.             return 1;
  61.         }
  62.         else
  63.         {
  64.             return 0;
  65.         }
  66.  
  67.     }
  68.     if (a == 'm' || a == 'M')
  69.     {
  70.         if (c == d)
  71.         {
  72.             return 1;
  73.  
  74.         }
  75.         else
  76.         {
  77.             return 0;
  78.         }
  79.     }
  80. }
  81.  
  82. float **cmatriz( int a,int b )
  83. {
  84.     float **Matriz;
  85.     Matriz = (float **)malloc(a * sizeof(float *));            
  86.     for (i = 0; i <= b; i++)
  87.     {
  88.         Matriz[i] = (float *)malloc(b * sizeof(float));            
  89.     }
  90.     return Matriz;
  91. }
  92. int main()
  93. {
  94.    
  95.     des = 0;
  96.     des1 = 0;
  97.     des2 = 0;
  98.     con = 0;
  99.     num_nom_apelid x;
  100.  
  101.     sel = 'a';
  102.     //d = fopen( "C:\\operacoesdematrizes.txt", "w");
  103.     /*if (d == NULL)
  104.     {
  105.         puts("Erro ao criar ficheiro");
  106.     }*/
  107.    
  108.    
  109.     while (con != 1)
  110.     {
  111.     printf("Menu Principal\n");
  112.     printf("==============\n");
  113.     printf("Como quer fornecer os dados ao PROGRAMA? [T ou F ou S]\n");
  114.     printf("======================================================\n");
  115.     printf("   Por Teclado               (Responda 'T')\n");
  116.     printf("   Por Ficheiro              (Responda 'F')\n");
  117.     printf("   Desejo sair do programa   (Responda 'S')\n");
  118.     scanf(" %c", &sel);
  119.     while (sel != 'f' && sel != 'F' && sel != 't' && sel != 'T' && sel != 's' && sel != 'S')
  120.     {
  121.         printf("Essa escolha nao existe, insira novamente\n");
  122.         scanf(" %c", &sel);
  123.     }
  124.        
  125.     if (sel == 't' || sel == 'T')
  126.     {
  127.         printf("\n Qual o n%cmero de alunos a considerar ?\n", 163);
  128.         scanf(" %d", &numero_de_alunos);
  129.         numero_nome_apelido = (num_nom_apelid *) malloc(numero_de_alunos * sizeof(num_nom_apelid));
  130.         if (numero_nome_apelido == NULL)
  131.         {
  132.             printf("\n N%co foi poss%cvel alocar mem%cria para o vetor da estrutura 'numero_nome_apelido'!",198,161,162);
  133.             exit(1);
  134.         }
  135.            
  136.  
  137.         for (i = 0; i < numero_de_alunos; i++)
  138.         {
  139.            
  140.             numero_nome_apelido[i].nome[i] = '0';
  141.             numero_nome_apelido[i].apelido[i] = '0';
  142.             do
  143.             {
  144.                 printf("\n Qual o n%cmero do %d%c aluno ? [30000-46000]\n", 163, i+1, 167);
  145.                 scanf(" %d", &numero_nome_apelido[i].numero);
  146.             } while ((numero_nome_apelido[i].numero <30000) || (numero_nome_apelido[i].numero >46000));
  147.                 printf("\n Qual o nome do %d%c aluno ?\n", i+1, 167);
  148.                 scanf("%s", numero_nome_apelido[i].nome);
  149.                 printf("\n Qual o apelido do %d%c aluno ?\n", i+1, 167);
  150.                 scanf("%s", numero_nome_apelido[i].apelido);
  151.                    
  152.         }
  153.         if (numero_de_alunos != 1)
  154.         {              
  155.             printf("Qual a ordem pela qual deseja organizar os alunos ('N'umerica/'A'lfabetica)\n");
  156.             scanf(" %c", &ord);
  157.             while (ord != 'n' && ord != 'N' && ord != 'a' && ord != 'A')
  158.             {
  159.                 printf("Essa escolha nao existe, insira novamente\n");
  160.                 scanf(" %c", &ord);
  161.             }
  162.  
  163.             if (ord == 'n' || ord == 'N' )
  164.             {
  165.                 w = ordemnum(numero_nome_apelido[0].numero, numero_nome_apelido[1].numero);
  166.                 if (w > 0)
  167.                 {
  168.                     x = numero_nome_apelido[0];
  169.                     numero_nome_apelido[0] = numero_nome_apelido[1];
  170.                     numero_nome_apelido[1] = x;
  171.                 }
  172.  
  173.             }
  174.             if (ord == 'a' || ord == 'A')
  175.             {
  176.                 k = ordemalf(numero_nome_apelido[0].nome, numero_nome_apelido[1].nome,numero_nome_apelido[0].apelido,numero_nome_apelido[1].apelido);
  177.                 if (k > 0)
  178.                 {
  179.                     x = numero_nome_apelido[0];
  180.                     numero_nome_apelido[0] = numero_nome_apelido[1];
  181.                     numero_nome_apelido[1] = x;
  182.                    
  183.                 }
  184.             }
  185.         }
  186.         system("cls");
  187.         printf("========================================\n");
  188.         printf("PROGRAMA DE OPERACOES COM MATRIZES REAIS\n");
  189.         printf("   ADICAO,SUBTRACAO E MULTIPLICACAO\n");
  190.         for (i = 0; i < numero_de_alunos; i++)
  191.         {
  192.             printf("\n Aluno N%c %5d - ", 167, numero_nome_apelido[i].numero);
  193.             printf("Nome: ");
  194.             printf("%s ", numero_nome_apelido[i].nome);
  195.             printf("%s ", numero_nome_apelido[i].apelido);
  196.         }
  197.         data_hora(&t);
  198.         printf("========================================\n");
  199.        
  200.         system("pause");
  201.         free(numero_nome_apelido);
  202.         system("cls");
  203.        
  204.     }
  205.     if (sel == 'f' || sel == 'F')
  206.     {
  207.         printf("Em que diretorio esta o ficheiro?\n");
  208.         scanf(" %c", dis);
  209.         printf("Qual o nome do ficheiro?\n");
  210.         scanf(" %s", nome);
  211.         strcat(dis, ":\\");
  212.         strcat(dis, nome);
  213.         strcat(dis, ".txt");
  214.         fp = fopen(dis, "r");
  215.         if (fp == NULL)
  216.         {
  217.             printf("O ficheiro nao existe\n");
  218.             system("pause");
  219.         }
  220.         fscanf(fp, "%d ", &numero_de_alunos);
  221.         for ( i = 0; i < numero_de_alunos; i++)
  222.         {
  223.             fscanf(fp, "%d ", &numero_nome_apelido[i].numero);
  224.             fscanf(fp, "%s ", &numero_nome_apelido[i].nome);
  225.             fscanf(fp, "%s ", &numero_nome_apelido[i].apelido);
  226.         }
  227.         printf("========================================\n");
  228.         //fprintf(d, "========================================\n");
  229.         printf("PROGRAMA DE OPERACOES COM MATRIZES REAIS\n");
  230.         //fprintf(d, "PROGRAMA DE OPERACOES COM MATRIZES REAIS\n");
  231.         printf("   ADICAO,SUBTRACAO E MULTIPLICACAO\n");
  232.         //fprintf(d, "   ADICAO,SUBTRACAO E MULTIPLICACAO\n");
  233.         for (i = 0; i < numero_de_alunos; i++)
  234.         {
  235.             printf("\n Aluno N%c %5d - ", 167, numero_nome_apelido[i].numero);
  236.             //fprintf(d, "\n Aluno Nº %5d - ", numero_nome_apelido[i].numero);
  237.             printf("Nome: ");
  238.             printf("%s ", numero_nome_apelido[i].nome);
  239.             //fprintf(d, "%s ", numero_nome_apelido[i].nome);
  240.             printf("%s ", numero_nome_apelido[i].apelido);
  241.             //fprintf(d, "%s ", numero_nome_apelido[i].apelido);
  242.         }
  243.         data_hora(&t);
  244.         printf("========================================\n");
  245.  
  246.     }
  247.     if (sel == 's' || sel == 'S')
  248.     {
  249.         system("pause");
  250.         return 0;
  251.     }
  252.     while (esc != 8)
  253.     {
  254.     system("cls");
  255.     printf("MENU OPERATIVO\n");
  256.     printf("==============\n\n");
  257.     printf("1- Qual a operacao a executar? [A-Adicao S-Subtracao M-Multiplicacao]\n");
  258.     printf("2- Quais as dimensoes da 1 matriz?\n");
  259.     printf("3- Quais as dimensoes da 2 matriz?\n");
  260.     printf("4- Verificacao da possibilidade da operacao pedida em 1-\n");
  261.     printf("5- Quais os valores dos elementos da 1 matriz\n");
  262.     printf("6- Quais os valores dos elementos da 2 matriz\n"); 
  263.     printf("7- Proceder a execucao da operacao de matrizes indicada em 1\n");
  264.     printf("8- Voltar ao menu principal\n");
  265.     scanf("%d", &esc);
  266.         if (esc == 1)
  267.         {
  268.             system("cls");
  269.             printf("Qual e a operacao?(A-S-M)\n");
  270.             scanf(" %c", &op);
  271.             des = des + 1;
  272.  
  273.         }
  274.         if (esc == 2)
  275.         {
  276.             system("cls");
  277.             printf("Qual e o numero de linhas da 1 matriz?\n");
  278.             scanf("%d", &l1);
  279.             printf("Qual e o numero de colunas da 1 matriz?\n");
  280.             scanf("%d", &c1);
  281.             des1 = des1 + 1;
  282.         }
  283.         if (esc == 3)
  284.         {
  285.             system("cls");
  286.             printf("Qual e o numero de linhas da 2 matriz?\n");
  287.             scanf("%d", &l2);
  288.             printf("Qual e o numero de colunas da 2 matriz?\n");
  289.             scanf("%d", &c2);
  290.             des2 = des2 + 1;
  291.         }
  292.         if ((esc == 4 || esc == 7) && des == 0)
  293.         {
  294.             system("cls");
  295.             printf("Ainda nao selecionou a operacao a realizar\n");
  296.         }
  297.         if (esc == 4 && des != 0)
  298.         {
  299.             system("cls");
  300.             ver = verificacao(op, l1, c1, l2, c2);
  301.             if (ver == 1)
  302.             {
  303.                 printf("E possivel realizar a operacao desejada\n");
  304.             }
  305.             if (ver == 0)
  306.             {
  307.                 printf("Nao e possivel realizar a operacao desejada\n");
  308.             }
  309.             system("pause");
  310.         }
  311.         if (esc == 5 && des1 == 0)
  312.         {
  313.             system("cls");
  314.             printf("Ainda nao determinou as dimensoes da matriz");
  315.         }
  316.         if (esc == 5 && des1 != 0)
  317.         {
  318.             system("cls");
  319.             matriz1 = cmatriz(l1, c1);
  320.             for ( i = 0; i < l1; i++)
  321.             {
  322.                 for (j = 0; j < c1; j++)
  323.                 {
  324.                     printf("Insira o elemento da %d linha e %d coluna\n", i+1, j+1);
  325.                     scanf("%f", &matriz1[i][j]);
  326.                 }
  327.                
  328.             }
  329.         }
  330.         if (esc == 6 && des2 == 0)
  331.         {
  332.             system("cls");
  333.             printf("Ainda nao determinou as dimensoes da matriz");
  334.         }
  335.         if (esc == 6 && des2 != 0)
  336.         {
  337.             system("cls");
  338.             matriz2 = cmatriz(l2, c2);
  339.             for (i = 0; i < l2; i++)
  340.             {
  341.                 for (j = 0; j < c2; j++)
  342.                 {
  343.                     printf("Insira o elemento da %d linha e %d coluna\n", i+1, j+1);
  344.                     scanf("%f", &matriz2[i][j]);
  345.                 }
  346.  
  347.             }
  348.         }
  349.         if (esc == 7 && des != 0 && (op == 'a' || op == 'A') && ver == 1)
  350.         {
  351.             printf("A matriz resultante da adicao das duas e:\n");
  352.             matriz3 = cmatriz(l2, c2);
  353.            
  354.             for (i = 0; i < l1; i++)
  355.             {
  356.                 for (j = 0; j < c1; j++)
  357.                 {
  358.                     matriz3[i][j] = matriz1[i][j] + matriz2[i][j];
  359.                     printf(" %f ", matriz3[i][j]);
  360.                    
  361.                 }
  362.                 printf("\n");
  363.             }
  364.             system("pause");
  365.         }
  366.         if (esc == 7 && des != 0 && (op == 's' || op == 'S') && ver == 1)
  367.         {
  368.             printf("A matriz resultante da subtracao das duas e:\n");
  369.             matriz3 = cmatriz(l2, c2);
  370.  
  371.             for (i = 0; i < l1; i++)
  372.             {
  373.                 for (j = 0; j < c1; j++)
  374.                 {
  375.                     matriz3[i][j] = matriz1[i][j] - matriz2[i][j];
  376.                     printf(" %f ", matriz3[i][j]);
  377.  
  378.                 }
  379.                 printf("\n");
  380.             }
  381.             system("pause");
  382.         }
  383.         if (esc == 7 && des != 0 && (op == 'm' || op == 'M') && ver == 1)
  384.         {
  385.             printf("A matriz resultante da multiplicacao das duas e:\n");
  386.             matriz3 = cmatriz(l1, c2);
  387.  
  388.             for (i = 0; i<c1; i++)
  389.             {
  390.                 for (j = 0; j<c2 ; j++)
  391.                 {
  392.                     matriz3[i][j] = 0.0;
  393.                     for (k = 0; k<c1; k++)
  394.                     {
  395.                         matriz3[i][j] += matriz1[i][k] * matriz2[k][j];
  396.                     }
  397.                 }
  398.             }
  399.             for (i = 0; i < l1; i++)
  400.             {
  401.                 for (j = 0; j < c2; j++)
  402.                 {
  403.                     printf(" %f ", matriz3[i][j]);
  404.  
  405.                 }
  406.                 printf("\n");
  407.             }
  408.             system("pause");
  409.         }
  410.  
  411.     }
  412.  
  413.    
  414.     }
  415.    
  416.    
  417.    
  418.    
  419.    
  420.     fclose(d);
  421.     fclose(fp);
  422.     printf("\n\n\n");
  423.     system("pause");
  424.     return 0;
  425.    
  426.    
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement