Skip to main content

Linguagem C_Exercicios Propostos Solucao_10

Page 1

Linguagem C 25.ª Edição Atualizada e Aumentada FCA Editora (c) 2025

10

EXERCÍCIOS PROPOSTOS – SOLUÇÃO 10.1 1: #include <stdio.h> 2: #include <stdlib.h> 3: 4: #define MAX_STR 120 /* Dim. da maior string */ 5: 6: int main(int argc, char *argv[]) 7: { 8: FILE *fp; 9: char s[MAX_STR+1]; /* Linha lida no ficheiro */ 10: int i=0; /* Contador das linhas */ 11: 12: if (argc==1) 13: { 14: fprintf(stderr, "Sintaxe: Line Ficheiro\n\n"); 15: exit(1); 16: } 17: 18: if ((fp=fopen(argv[1], "r"))==NULL) 19: { 20: fprintf(stderr, "Impossível abrir o ficheiro %s\n\n", argv[1]); 21: exit(1); 22: } 23: 24: while (fgets(s, MAX_STR+1, fp)!=NULL) 25: printf("%3d: %s", ++i, s); 26: 27: fclose(fp); 28: exit(0); 29: }

LINE.C

10.2 1: #include <stdio.h> 2: #include <string.h> 3: #include <stdlib.h> 4: 5: int main(int argc, char *argv[]) 6: { 7: FILE *fp; 8: int so_total=0; /* Por Defeito, processa tudo */ 9: int n1, n2; /* Operandos */ 10: char op; /* Operador */ 11: long res, total=0L; /* Total acumulado */ 12: 13: if (argc==3) 14: so_total = (strcmp(argv[1], "-t")==0); /* ver se argv[1]=="-t" */ 15: 16: if (argc<2 || argc >3) 17: { 18: fprintf(stderr, "Sintaxe: proc [-t] ficheiro\n");

© FCA

1


Turn static files into dynamic content formats.

Create a flipbook
Linguagem C_Exercicios Propostos Solucao_10 by Grupo Lidel - Issuu