Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- typedef struct
- {
- int numero;
- char nome[10];
- char apelido[10];
- }num_nom_apelid;
- char sel, ord, op, dis[20], nome[15];
- int numero_de_alunos, i, j, k, w, esco,esc,con,l1,l2,c1,c2,cop, des,ver,des1,des2;
- tm t;
- FILE *fp, *d;
- num_nom_apelid *numero_nome_apelido;
- float **matriz1;
- float **matriz2;
- float **matriz3;
- int ordemnum(int a, int b)
- {
- int z;
- z = a - b;
- return z;
- }
- int ordemalf(char*a, char*b,char*d,char*e)
- {
- int c,f;
- c = strcmp(a, b);
- if (c != 0)
- {
- return c;
- }
- if (c == 0)
- {
- f = strcmp(d, e);
- return f;
- }
- }
- int data_hora(tm* a)
- {
- const time_t timer = time(NULL);
- a = gmtime(&timer);
- printf("\n Data: %02d/%02d/%4d ", a->tm_mday, a->tm_mon + 1, 1900 + (a->tm_year));
- printf(" Hora: %02d:%02d:%02d ", a->tm_hour + 1, a->tm_min, a->tm_sec);
- printf("\n");
- return timer;
- }
- int verificacao(char a,int b,int c, int d, int e)
- {
- if (a == 'a' || a == 'A' || a == 's' || a == 'S')
- {
- if (b = d && c == e)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- if (a == 'm' || a == 'M')
- {
- if (c == d)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- float **cmatriz( int a,int b )
- {
- float **Matriz;
- Matriz = (float **)malloc(a * sizeof(float *));
- for (i = 0; i <= b; i++)
- {
- Matriz[i] = (float *)malloc(b * sizeof(float));
- }
- return Matriz;
- }
- int main()
- {
- des = 0;
- des1 = 0;
- des2 = 0;
- con = 0;
- num_nom_apelid x;
- sel = 'a';
- //d = fopen( "C:\\operacoesdematrizes.txt", "w");
- /*if (d == NULL)
- {
- puts("Erro ao criar ficheiro");
- }*/
- while (con != 1)
- {
- printf("Menu Principal\n");
- printf("==============\n");
- printf("Como quer fornecer os dados ao PROGRAMA? [T ou F ou S]\n");
- printf("======================================================\n");
- printf(" Por Teclado (Responda 'T')\n");
- printf(" Por Ficheiro (Responda 'F')\n");
- printf(" Desejo sair do programa (Responda 'S')\n");
- scanf(" %c", &sel);
- while (sel != 'f' && sel != 'F' && sel != 't' && sel != 'T' && sel != 's' && sel != 'S')
- {
- printf("Essa escolha nao existe, insira novamente\n");
- scanf(" %c", &sel);
- }
- if (sel == 't' || sel == 'T')
- {
- printf("\n Qual o n%cmero de alunos a considerar ?\n", 163);
- scanf(" %d", &numero_de_alunos);
- numero_nome_apelido = (num_nom_apelid *) malloc(numero_de_alunos * sizeof(num_nom_apelid));
- if (numero_nome_apelido == NULL)
- {
- printf("\n N%co foi poss%cvel alocar mem%cria para o vetor da estrutura 'numero_nome_apelido'!",198,161,162);
- exit(1);
- }
- for (i = 0; i < numero_de_alunos; i++)
- {
- numero_nome_apelido[i].nome[i] = '0';
- numero_nome_apelido[i].apelido[i] = '0';
- do
- {
- printf("\n Qual o n%cmero do %d%c aluno ? [30000-46000]\n", 163, i+1, 167);
- scanf(" %d", &numero_nome_apelido[i].numero);
- } while ((numero_nome_apelido[i].numero <30000) || (numero_nome_apelido[i].numero >46000));
- printf("\n Qual o nome do %d%c aluno ?\n", i+1, 167);
- scanf("%s", numero_nome_apelido[i].nome);
- printf("\n Qual o apelido do %d%c aluno ?\n", i+1, 167);
- scanf("%s", numero_nome_apelido[i].apelido);
- }
- if (numero_de_alunos != 1)
- {
- printf("Qual a ordem pela qual deseja organizar os alunos ('N'umerica/'A'lfabetica)\n");
- scanf(" %c", &ord);
- while (ord != 'n' && ord != 'N' && ord != 'a' && ord != 'A')
- {
- printf("Essa escolha nao existe, insira novamente\n");
- scanf(" %c", &ord);
- }
- if (ord == 'n' || ord == 'N' )
- {
- w = ordemnum(numero_nome_apelido[0].numero, numero_nome_apelido[1].numero);
- if (w > 0)
- {
- x = numero_nome_apelido[0];
- numero_nome_apelido[0] = numero_nome_apelido[1];
- numero_nome_apelido[1] = x;
- }
- }
- if (ord == 'a' || ord == 'A')
- {
- k = ordemalf(numero_nome_apelido[0].nome, numero_nome_apelido[1].nome,numero_nome_apelido[0].apelido,numero_nome_apelido[1].apelido);
- if (k > 0)
- {
- x = numero_nome_apelido[0];
- numero_nome_apelido[0] = numero_nome_apelido[1];
- numero_nome_apelido[1] = x;
- }
- }
- }
- system("cls");
- printf("========================================\n");
- printf("PROGRAMA DE OPERACOES COM MATRIZES REAIS\n");
- printf(" ADICAO,SUBTRACAO E MULTIPLICACAO\n");
- for (i = 0; i < numero_de_alunos; i++)
- {
- printf("\n Aluno N%c %5d - ", 167, numero_nome_apelido[i].numero);
- printf("Nome: ");
- printf("%s ", numero_nome_apelido[i].nome);
- printf("%s ", numero_nome_apelido[i].apelido);
- }
- data_hora(&t);
- printf("========================================\n");
- system("pause");
- free(numero_nome_apelido);
- system("cls");
- }
- if (sel == 'f' || sel == 'F')
- {
- printf("Em que diretorio esta o ficheiro?\n");
- scanf(" %c", dis);
- printf("Qual o nome do ficheiro?\n");
- scanf(" %s", nome);
- strcat(dis, ":\\");
- strcat(dis, nome);
- strcat(dis, ".txt");
- fp = fopen(dis, "r");
- if (fp == NULL)
- {
- printf("O ficheiro nao existe\n");
- system("pause");
- }
- fscanf(fp, "%d ", &numero_de_alunos);
- for ( i = 0; i < numero_de_alunos; i++)
- {
- fscanf(fp, "%d ", &numero_nome_apelido[i].numero);
- fscanf(fp, "%s ", &numero_nome_apelido[i].nome);
- fscanf(fp, "%s ", &numero_nome_apelido[i].apelido);
- }
- printf("========================================\n");
- //fprintf(d, "========================================\n");
- printf("PROGRAMA DE OPERACOES COM MATRIZES REAIS\n");
- //fprintf(d, "PROGRAMA DE OPERACOES COM MATRIZES REAIS\n");
- printf(" ADICAO,SUBTRACAO E MULTIPLICACAO\n");
- //fprintf(d, " ADICAO,SUBTRACAO E MULTIPLICACAO\n");
- for (i = 0; i < numero_de_alunos; i++)
- {
- printf("\n Aluno N%c %5d - ", 167, numero_nome_apelido[i].numero);
- //fprintf(d, "\n Aluno Nº %5d - ", numero_nome_apelido[i].numero);
- printf("Nome: ");
- printf("%s ", numero_nome_apelido[i].nome);
- //fprintf(d, "%s ", numero_nome_apelido[i].nome);
- printf("%s ", numero_nome_apelido[i].apelido);
- //fprintf(d, "%s ", numero_nome_apelido[i].apelido);
- }
- data_hora(&t);
- printf("========================================\n");
- }
- if (sel == 's' || sel == 'S')
- {
- system("pause");
- return 0;
- }
- while (esc != 8)
- {
- system("cls");
- printf("MENU OPERATIVO\n");
- printf("==============\n\n");
- printf("1- Qual a operacao a executar? [A-Adicao S-Subtracao M-Multiplicacao]\n");
- printf("2- Quais as dimensoes da 1 matriz?\n");
- printf("3- Quais as dimensoes da 2 matriz?\n");
- printf("4- Verificacao da possibilidade da operacao pedida em 1-\n");
- printf("5- Quais os valores dos elementos da 1 matriz\n");
- printf("6- Quais os valores dos elementos da 2 matriz\n");
- printf("7- Proceder a execucao da operacao de matrizes indicada em 1\n");
- printf("8- Voltar ao menu principal\n");
- scanf("%d", &esc);
- if (esc == 1)
- {
- system("cls");
- printf("Qual e a operacao?(A-S-M)\n");
- scanf(" %c", &op);
- des = des + 1;
- }
- if (esc == 2)
- {
- system("cls");
- printf("Qual e o numero de linhas da 1 matriz?\n");
- scanf("%d", &l1);
- printf("Qual e o numero de colunas da 1 matriz?\n");
- scanf("%d", &c1);
- des1 = des1 + 1;
- }
- if (esc == 3)
- {
- system("cls");
- printf("Qual e o numero de linhas da 2 matriz?\n");
- scanf("%d", &l2);
- printf("Qual e o numero de colunas da 2 matriz?\n");
- scanf("%d", &c2);
- des2 = des2 + 1;
- }
- if ((esc == 4 || esc == 7) && des == 0)
- {
- system("cls");
- printf("Ainda nao selecionou a operacao a realizar\n");
- }
- if (esc == 4 && des != 0)
- {
- system("cls");
- ver = verificacao(op, l1, c1, l2, c2);
- if (ver == 1)
- {
- printf("E possivel realizar a operacao desejada\n");
- }
- if (ver == 0)
- {
- printf("Nao e possivel realizar a operacao desejada\n");
- }
- system("pause");
- }
- if (esc == 5 && des1 == 0)
- {
- system("cls");
- printf("Ainda nao determinou as dimensoes da matriz");
- }
- if (esc == 5 && des1 != 0)
- {
- system("cls");
- matriz1 = cmatriz(l1, c1);
- for ( i = 0; i < l1; i++)
- {
- for (j = 0; j < c1; j++)
- {
- printf("Insira o elemento da %d linha e %d coluna\n", i+1, j+1);
- scanf("%f", &matriz1[i][j]);
- }
- }
- }
- if (esc == 6 && des2 == 0)
- {
- system("cls");
- printf("Ainda nao determinou as dimensoes da matriz");
- }
- if (esc == 6 && des2 != 0)
- {
- system("cls");
- matriz2 = cmatriz(l2, c2);
- for (i = 0; i < l2; i++)
- {
- for (j = 0; j < c2; j++)
- {
- printf("Insira o elemento da %d linha e %d coluna\n", i+1, j+1);
- scanf("%f", &matriz2[i][j]);
- }
- }
- }
- if (esc == 7 && des != 0 && (op == 'a' || op == 'A') && ver == 1)
- {
- printf("A matriz resultante da adicao das duas e:\n");
- matriz3 = cmatriz(l2, c2);
- for (i = 0; i < l1; i++)
- {
- for (j = 0; j < c1; j++)
- {
- matriz3[i][j] = matriz1[i][j] + matriz2[i][j];
- printf(" %f ", matriz3[i][j]);
- }
- printf("\n");
- }
- system("pause");
- }
- if (esc == 7 && des != 0 && (op == 's' || op == 'S') && ver == 1)
- {
- printf("A matriz resultante da subtracao das duas e:\n");
- matriz3 = cmatriz(l2, c2);
- for (i = 0; i < l1; i++)
- {
- for (j = 0; j < c1; j++)
- {
- matriz3[i][j] = matriz1[i][j] - matriz2[i][j];
- printf(" %f ", matriz3[i][j]);
- }
- printf("\n");
- }
- system("pause");
- }
- if (esc == 7 && des != 0 && (op == 'm' || op == 'M') && ver == 1)
- {
- printf("A matriz resultante da multiplicacao das duas e:\n");
- matriz3 = cmatriz(l1, c2);
- for (i = 0; i<c1; i++)
- {
- for (j = 0; j<c2 ; j++)
- {
- matriz3[i][j] = 0.0;
- for (k = 0; k<c1; k++)
- {
- matriz3[i][j] += matriz1[i][k] * matriz2[k][j];
- }
- }
- }
- for (i = 0; i < l1; i++)
- {
- for (j = 0; j < c2; j++)
- {
- printf(" %f ", matriz3[i][j]);
- }
- printf("\n");
- }
- system("pause");
- }
- }
- }
- fclose(d);
- fclose(fp);
- printf("\n\n\n");
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement