exercicios_resolvidos
Diferenças
Aqui você vê as diferenças entre duas revisões dessa página.
— | exercicios_resolvidos [2023/09/12 16:14] (atual) – criada - edição externa 127.0.0.1 | ||
---|---|---|---|
Linha 1: | Linha 1: | ||
+ | ====== Exercícios Resolvidos ====== | ||
+ | Esta página irá conter exercícios de entrada e saída, estruturas condicionais, | ||
+ | Programas que utilizam as bibliotecas matemáticas devem ser compiladas com a opção **-lm**. | ||
+ | |||
+ | Exemplo: **'' | ||
+ | |||
+ | Este comando compila o programa **lista_002.c** e cria o executável **lista_002**. | ||
+ | |||
+ | ===== Entrada e Saída ===== | ||
+ | Dado um número inteiro de segundos, mostrar a quantas horas, minutos e segundos ele corresponde. | ||
+ | |||
+ | <code c> | ||
+ | /* lista_es001.c */ | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int iQtdSegundos, | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | /* divisao de inteiros */ | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | O governo acaba de liberar 10 milhões de dólares para a construção de casas populares, a qual contratou a Construtora Pica-Pau S/A. Cada casa custa o equivalente a 150 salários mínimos. Faça um algoritmo que leia o valor do salário mínimo, o valor do dólar e calcule a quantidade de casas possíveis de se construir. | ||
+ | |||
+ | <code c> | ||
+ | /* lista_es002.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | float fTotal, fDolar, fSalario, fCustoCasa, fCasas, fSobras; | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Estruturas Condicionais ===== | ||
+ | Receber 3 valores numéricos: **X**, **Y** e **Z**, e verificar se esses valores podem corresponder aos lados de um triângulo. Em caso afirmativo, informar ao usuário se o triângulo é equilátero, | ||
+ | |||
+ | <code c> | ||
+ | /* lista_condicional001.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | float fX, fY, fZ; | ||
+ | | ||
+ | | ||
+ | |||
+ | /* condição de existência de um triângulo */ | ||
+ | if( (fabsf(fY-fZ) < fX) && (fX < (fY+fZ)) ) | ||
+ | { | ||
+ | if( (fX == fY) && (fX == fZ) ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | if( (fX != fY) && (fX != fZ) ) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Faça um programa que lê a operação (+,-,/,*) e dois números e exibe o resultado. | ||
+ | <code c> | ||
+ | /* lista_condicional002.c */ | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | float fValor_a, fValor_b, fResultado; | ||
+ | char cOperacao; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | { | ||
+ | case ' | ||
+ | break; | ||
+ | case ' | ||
+ | break; | ||
+ | case '/': | ||
+ | break; | ||
+ | case ' | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Estruturas de Repetição ===== | ||
+ | Um método para cálculo de raizes quadradas de um número **N** já era conhecido pelos babilônios em... bom, há muito tempo atrás. Este método também é conhecido como método de Heron, um matemático grego que o descreveu 20 séculos depois, perto do ano 50 DC. Começando de um valor inicial **k** (geralmente valendo um) os babilônios geravam um novo valor de **k** de acordo com a regra **k=(k+N/ | ||
+ | | ||
+ | |||
+ | < | ||
+ | algoritmo " | ||
+ | var | ||
+ | |||
+ | K, N: real | ||
+ | I: inteiro | ||
+ | |||
+ | inicio | ||
+ | | ||
+ | | ||
+ | K <- 1 | ||
+ | para I de 1 ate 12 faca | ||
+ | K <- (K + (N/K) )/2 | ||
+ | escreval(" | ||
+ | | ||
+ | fimalgoritmo | ||
+ | </ | ||
+ | |||
+ | <code c> | ||
+ | /* lista_repeticao001.c */ | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | float fK, fN; | ||
+ | int iI; | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | fK = 1; | ||
+ | for( iI = 1; iI <= 12; iI++) | ||
+ | { | ||
+ | fK = (fK + (fN/fK))/2; | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Calcular o valor aproximado de **p**. Dada a fórmula **p = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 ......)**, repetir até que o erro seja menor que **0.000001** (**p = 3, | ||
+ | |||
+ | <code c> | ||
+ | /* lista_repeticao002.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | | ||
+ | char cSinal; | ||
+ | int iContador; | ||
+ | |||
+ | dP = 0; | ||
+ | dS = 0; | ||
+ | | ||
+ | | ||
+ | dErro = 1; | ||
+ | |||
+ | | ||
+ | { | ||
+ | if( cSinal == ' | ||
+ | { | ||
+ | dS = dS + (double)(1.0/ | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | dS = dS - (double)(1.0/ | ||
+ | | ||
+ | } | ||
+ | |||
+ | dP = 4.0 * dS; | ||
+ | dErro = fabs(3.141592654-dP); | ||
+ | iContador+=2; | ||
+ | } | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Desenvolva um programa que apresente os 100 primeiros números primos. | ||
+ | |||
+ | <code c> | ||
+ | /* lista_repeticao003.c */ | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int iQtdPrimos, iPrimo, i, iNatural; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | { | ||
+ | | ||
+ | for( i = 2; i < iNatural; i++) | ||
+ | { | ||
+ | if( iNatural % i == 0 ) /* divisao exata */ | ||
+ | { | ||
+ | iPrimo = 0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if( iPrimo ) | ||
+ | { | ||
+ | printf(" | ||
+ | iQtdPrimos++; | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Faça um programa que leia um número e apresente todos os divisores (resto da divisão igual a 0) desse número. | ||
+ | <code c> | ||
+ | /* lista_repeticao004.c */ | ||
+ | #include < | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int iValor, iDivisor; | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | for( iDivisor = 1; iDivisor <= iValor/2; iDivisor++) | ||
+ | { | ||
+ | if( iValor % iDivisor == 0) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Funções ===== | ||
+ | Crie uma função que retorne o próximo termo da sequência de Fibonacci. | ||
+ | |||
+ | <code c> | ||
+ | /* lista_funcao_001.c */ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | int fibonacci(void) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | int iP3; | ||
+ | |||
+ | iP3 = iP1 + iP2; // calcula o próximo termo | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int i; | ||
+ | | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Vetores e Matrizes ===== | ||
+ | |||
+ | Dadas duas matrizes numéricas A e B de dimensão 4x3, fazer um algoritmo que gere uma matriz | ||
+ | | ||
+ | | ||
+ | |||
+ | EXEMPLO: | ||
+ | |||
+ | |||
+ | ^ Vetor A ^ Vetor B ^ Vetor C ^ | ||
+ | | 2 4 6 | 2 5 8 | verdadeiro falso falso | | ||
+ | | 1 5 9 | 1 9 7 | verdadeiro falso falso | | ||
+ | | 3 7 2 | 3 7 1 | verdadeiro verdadeiro falso | | ||
+ | | 4 6 8 | 4 5 8 | verdadeiro falso verdadeiro | | ||
+ | |||
+ | <code c> | ||
+ | /* lista_mat001.c */ | ||
+ | |||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define QTD_L 4 | ||
+ | #define QTD_C 3 | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int mA[QTD_L][QTD_C], | ||
+ | |||
+ | int iL, iC; | ||
+ | |||
+ | for( iL = 0; iL < QTD_L; iL++ ) | ||
+ | { | ||
+ | for( iC = 0; iC < QTD_C; iC++) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | for( iL = 0; iL < QTD_L; iL++ ) | ||
+ | { | ||
+ | for( iC = 0; iC < QTD_C; iC++) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | for( iL = 0; iL < QTD_L; iL++ ) | ||
+ | { | ||
+ | for( iC = 0; iC < QTD_C; iC++) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | for( iL = 0; iL < QTD_L; iL++ ) | ||
+ | { | ||
+ | for( iC = 0; iC < QTD_C; iC++) | ||
+ | { | ||
+ | printf(" | ||
+ | printf(" | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Criar um jogo da velha para 2 jogadores. O jogo deve informar quando houver jogadas inválidas, considerar 3x3 o tamanho do tabuleiro. | ||
+ | |||
+ | <code c> | ||
+ | /* lista_mat002.c */ | ||
+ | #include < | ||
+ | #define _X_ ' | ||
+ | #define _O_ ' | ||
+ | |||
+ | #define D_VALIDACAO | ||
+ | |||
+ | void desenha( char as_velha[3][3] ); | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int i,j; | ||
+ | int ijogada; | ||
+ | char as_velha[3][3]; | ||
+ | char ss_nome_jogador_01[40]; | ||
+ | char ss_nome_jogador_02[40]; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | for( i=0; | ||
+ | { | ||
+ | for( j=0; | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | { | ||
+ | |||
+ | #ifdef D_VALIDACAO | ||
+ | do | ||
+ | { | ||
+ | #endif | ||
+ | if( ijogada == _X_ ) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | #ifdef D_VALIDACAO | ||
+ | if( as_velha[i][j] != ' ' ) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | } | ||
+ | while( as_velha[i][j] != ' ' ); | ||
+ | #endif | ||
+ | |||
+ | as_velha[i][j]=ijogada; | ||
+ | desenha(as_velha); | ||
+ | |||
+ | /* inverte jogadores */ | ||
+ | if( ijogada == _X_ ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | void desenha( char as_velha[3][3] ) | ||
+ | { | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Criar um jogo da velha que aceite n-dimensões. | ||
+ | <code c> | ||
+ | /* lista_mat003.c */ | ||
+ | #include < | ||
+ | |||
+ | #define _X_ ' | ||
+ | #define _O_ ' | ||
+ | |||
+ | #define D_VALIDACAO | ||
+ | |||
+ | #define D_DIMENSAO 7 | ||
+ | |||
+ | void desenha( char as_velha[D_DIMENSAO][D_DIMENSAO] ); | ||
+ | int verifica( char as_velha[D_DIMENSAO][D_DIMENSAO], | ||
+ | |||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int i,j; | ||
+ | int ijogada; | ||
+ | char as_velha[D_DIMENSAO][D_DIMENSAO]; | ||
+ | char ss_nome_jogador_01[40]; | ||
+ | char ss_nome_jogador_02[40]; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | for( i=0; | ||
+ | { | ||
+ | for( j=0; | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | { | ||
+ | |||
+ | #ifdef D_VALIDACAO | ||
+ | do | ||
+ | { | ||
+ | #endif | ||
+ | if( ijogada == _X_ ) | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | #ifdef D_VALIDACAO | ||
+ | if( as_velha[i][j] != ' ' ) | ||
+ | printf(" | ||
+ | } | ||
+ | while( as_velha[i][j] != ' ' ); | ||
+ | #endif | ||
+ | |||
+ | as_velha[i][j]=ijogada; | ||
+ | desenha(as_velha); | ||
+ | |||
+ | if( verifica(as_velha, | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | if( ijogada == _X_ ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | void desenha( char as_velha[D_DIMENSAO][D_DIMENSAO] ) | ||
+ | { | ||
+ | int i,j; | ||
+ | |||
+ | for( i=0; i< | ||
+ | { | ||
+ | printf(" | ||
+ | for( j=0; j< | ||
+ | { | ||
+ | | ||
+ | if( j < D_DIMENSAO-1 ) //não deve imprimir | na última coluna | ||
+ | { | ||
+ | printf( " | ||
+ | } | ||
+ | } | ||
+ | printf(" | ||
+ | |||
+ | if( i < D_DIMENSAO - 1) //não deve imprimir ---|--- na última linha | ||
+ | { | ||
+ | | ||
+ | { | ||
+ | printf(" | ||
+ | if( j < D_DIMENSAO-1 ) //não deve imprimir | na última coluna | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | int verifica( char as_velha[D_DIMENSAO][D_DIMENSAO], | ||
+ | { | ||
+ | int i, j; | ||
+ | int iVenceu = 1; // 1 é verdadeiro | ||
+ | // verifica todas as linhas horizontalmente | ||
+ | for( i=0; i< | ||
+ | { | ||
+ | iVenceu=1; | ||
+ | for( j=0; j< | ||
+ | { | ||
+ | if( as_velha[i][j] != cSimbol ) | ||
+ | { | ||
+ | iVenceu = 0; | ||
+ | } | ||
+ | } | ||
+ | if(iVenceu) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if( ! iVenceu ) | ||
+ | { | ||
+ | iVenceu = 1; | ||
+ | // verifica todas as linhas verticalmente | ||
+ | |||
+ | for( j=0; j< | ||
+ | { | ||
+ | | ||
+ | for( i=0; i< | ||
+ | { | ||
+ | if( as_velha[i][j] != cSimbol ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Ponteiros ===== | ||
+ | Desenvolva uma função que faça a troca dos valores de duas variáveis passadas como parâmetros. | ||
+ | |||
+ | <code c> | ||
+ | /* lista_ponteiros0001.c */ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | void troca(int * piValor_a, int * piValor_b) | ||
+ | { | ||
+ | int iAux; | ||
+ | iAux = *piValor_a; | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | |||
+ | int iValor_a, iValor_b; | ||
+ | | ||
+ | | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Exercícios com strings ===== | ||
+ | Faça um programa para verificar se uma palavra ou frase são palíndromas. | ||
+ | <code c> | ||
+ | /* lista_string001.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int palindrome( char * sFrase ) | ||
+ | { | ||
+ | int iInicio, iFim; | ||
+ | | ||
+ | iFim = strlen(sFrase)-1; | ||
+ | | ||
+ | { | ||
+ | if( sFrase[iFim] != sFrase[iInicio] ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | iFim--; | ||
+ | iInicio++; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | char sFrase[50]; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Faça um programa para verificar se uma palavra ou frase são palíndromas, | ||
+ | <code c> | ||
+ | /* lista_string002.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | int palindrome( char * sFrase ) | ||
+ | { | ||
+ | int iInicio, iFim; | ||
+ | | ||
+ | iFim = strlen(sFrase)-1; | ||
+ | | ||
+ | { | ||
+ | while( sFrase[iFim] == ' ' ) iFim--; | ||
+ | while( sFrase[iInicio] == ' ' ) iInicio++; | ||
+ | if( tolower(sFrase[iFim]) != tolower(sFrase[iInicio]) ) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | iFim--; | ||
+ | iInicio++; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | char sFrase[50]; | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Exercícios com Sockets de rede ===== | ||
+ | * Criar 2 programas (um cliente e outro servidor). | ||
+ | * Basicamente ocorrerá trocas de mensagens no formato COMANDO: | ||
+ | * O servidor precisa aceitar a conexão de apenas 1 cliente por vez. | ||
+ | * O cliente recebe os comandos e/ou mensagens sempre via linha de comando. | ||
+ | * O Cliente pode enviar mensagens para o servidor; retirar 1 mensagem do servidor; retirar várias mensagens do servidor. | ||
+ | * Para colocar 1 mensagem no servidor, o cliente deve enviar o comando: '' | ||
+ | * Para retirar 1 mensagem do servidor, o cliente deve enviar o comando: '' | ||
+ | * Para retirar várias mensagens do servidor, o cliente deve enviar o comando '' | ||
+ | |||
+ | |||
+ | **Lado do servidor** | ||
+ | <code c> | ||
+ | /* lista_redess001.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define MAX_LEN 50 | ||
+ | |||
+ | |||
+ | /* estrutura para a fila */ | ||
+ | struct queue | ||
+ | { | ||
+ | char itens[MAX_LEN][MAX_LEN]; | ||
+ | int front,rear; | ||
+ | }; | ||
+ | |||
+ | /* funcoes retiradas do livro ESTRUTURA DE DADOS COM ALGORITMOS E C */ | ||
+ | char * dequeue(struct queue * pq); | ||
+ | char * front(struct queue * pq); | ||
+ | int size(struct queue * pq); | ||
+ | void enqueue(struct queue * pq, char * szMensagem ); | ||
+ | int empty(struct queue * pq); | ||
+ | |||
+ | |||
+ | void envia( int iSock, char * szMensagem ); | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | |||
+ | |||
+ | int iSock; | ||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | |||
+ | iSock = socket(AF_INET, | ||
+ | if( iSock == -1) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | if( bind(iSock, (struct sockaddr *)& | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | if( listen( iSock, 10 ) < 0) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | { | ||
+ | int iFd; | ||
+ | ssize_t iBytes; | ||
+ | struct sockaddr_in client_addr; | ||
+ | socklen_t sin_size; | ||
+ | char szMensagem[MAX_LEN]; | ||
+ | |||
+ | fflush(stdout); | ||
+ | |||
+ | sin_size = sizeof(struct sockaddr_in); | ||
+ | |||
+ | if( (iFd = accept(iSock, | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | memset(szMensagem, | ||
+ | |||
+ | if ((iBytes=recv(iFd, | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | szMensagem[iBytes] = ' | ||
+ | printf(" | ||
+ | |||
+ | /* colocar na fila */ | ||
+ | if( strncmp( szMensagem, " | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | /* retira da fila */ | ||
+ | if( strncmp( szMensagem, " | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | if( strncmp( szMensagem, " | ||
+ | { | ||
+ | char szNumero[MAX_LEN]; | ||
+ | |||
+ | | ||
+ | if( size(& | ||
+ | { | ||
+ | envia(iFd," | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | int i; | ||
+ | for( i=0; i< | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | envia(iFd, " | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | void envia( int iSock, char * szMensagem ) | ||
+ | { | ||
+ | int iBytes; | ||
+ | | ||
+ | |||
+ | if ((iBytes=send(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | |||
+ | int empty(struct queue * pq) | ||
+ | { | ||
+ | /* se o inicio fila for igual ao final da fila, a fila esta vazia */ | ||
+ | if( pq-> | ||
+ | { | ||
+ | return 1; | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | void enqueue(struct queue * pq, char * szMensagem ) | ||
+ | { | ||
+ | /* se front estiver no inicio e foi alcancado o fim da fila */ | ||
+ | if( pq->rear + 1 >= MAX_LEN && pq-> | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | /* fila cheia, mas tem espaco no inicio */ | ||
+ | if( pq->rear + 1 >= MAX_LEN ) | ||
+ | { | ||
+ | /* desloco de front para o inicio */ | ||
+ | int i, element; | ||
+ | element = pq-> | ||
+ | for( i=0; i < pq-> | ||
+ | { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | pq->rear = pq->rear - pq-> | ||
+ | pq-> | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | |||
+ | int size(struct queue * pq) | ||
+ | { | ||
+ | | ||
+ | } | ||
+ | |||
+ | char * front(struct queue * pq) | ||
+ | { | ||
+ | /* o primeiro elemento sempre esta no inicio do vetor */ | ||
+ | | ||
+ | } | ||
+ | |||
+ | char * dequeue(struct queue * pq) | ||
+ | { | ||
+ | if( empty(pq) ) | ||
+ | { | ||
+ | printf(" | ||
+ | exit(1); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Lado do cliente** | ||
+ | <code c> | ||
+ | /* lista_redesc001.c */ | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define MAX_LEN 50 | ||
+ | |||
+ | void envia( int iSock, char * szMensagem ); | ||
+ | void recebe( int iSock, char * szMensagem ); | ||
+ | |||
+ | |||
+ | int main(int argc, char ** argv ) | ||
+ | { | ||
+ | int iSock; | ||
+ | |||
+ | | ||
+ | char szMensagem[MAX_LEN]; | ||
+ | |||
+ | iSock = socket(AF_INET, | ||
+ | |||
+ | if( iSock == -1) | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | if( connect(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | if( strcmp(argv[1]," | ||
+ | { | ||
+ | sprintf( szMensagem, " | ||
+ | envia(iSock, | ||
+ | } | ||
+ | |||
+ | if( strcmp(argv[1]," | ||
+ | { | ||
+ | sprintf( szMensagem, " | ||
+ | envia(iSock, | ||
+ | recebe(iSock, | ||
+ | } | ||
+ | |||
+ | if( strcmp(argv[1], | ||
+ | { | ||
+ | sprintf( szMensagem, " | ||
+ | envia(iSock, | ||
+ | do | ||
+ | { | ||
+ | | ||
+ | if( strncmp(szMensagem, | ||
+ | { | ||
+ | break; | ||
+ | } | ||
+ | }while( strncmp(szMensagem, | ||
+ | } | ||
+ | |||
+ | | ||
+ | } | ||
+ | |||
+ | void envia( int iSock, char * szMensagem ) | ||
+ | { | ||
+ | int iBytes; | ||
+ | | ||
+ | |||
+ | if ((iBytes=send(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | |||
+ | void recebe( int iSock, char * szMensagem ) | ||
+ | { | ||
+ | int iBytes; | ||
+ | | ||
+ | if ((iBytes=recv(iSock, | ||
+ | { | ||
+ | perror(" | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | | ||
+ | | ||
+ | } | ||
+ | </ |