#include <stdio.h> int main(int argc, char *argv[]) { FILE *arquivo = fopen("inteiros.txt", "r"); while (!feof(arquivo)) { int i; fscanf(arquivo, "%d", &i); if (i % 2 == 0) printf("%d ", i); } return 0; }
#include <stdio.h> int main(int argc, char *argv[]) { FILE *entrada = fopen("palavras.txt", "r"), *saida = fopen("bin.dat", "wb"); char palavra[50]; while (!feof(entrada)) { int p; fscanf(entrada, "%s ", palavra); if (('A' <= palavra[0]) && ('Z' >= palavra[0])) { p = 1; } else { p = 0; } fwrite(&p,sizeof(int), 1, saida); } fclose(saida); return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> char *copia_digitos(char *a) { int ndigitos = 0; char *nova; int i; for (i = 0; a[i] != '\0'; ++i) { if (('0' <= a[i]) && ('9' >= a[i])) { ndigitos += 1; } } nova = malloc(sizeof(char)*(ndigitos+1)); ndigitos = 0; for (i = 0; a[i] != '\0'; ++i) { if (('0' <= a[i]) && ('9' >= a[i])) { nova[ndigitos] = a[i]; ndigitos += 1; } } nova[ndigitos] = '\0'; return nova; } int main(int argc, char *argv[]) { char s[] = "sfdstg234tysdfg45tw 0 rsdfg57y6678ssdf34"; char *novo = copia_digitos(s); printf("%s\n", novo); return 0; }
#include <stdio.h> #include <stdlib.h> struct no { int i; struct no *prox; }; struct no *novo(int i, struct no *prox) { struct no *n = malloc(sizeof(struct no)); n->i = i; n->prox = prox; return n; } struct no *impar_par(struct no *lista) { struct no *prev; if (lista == NULL) { return NULL; } else if (lista->prox == NULL) { return NULL; } else { prev = lista; lista = lista->prox; for (; lista != NULL; lista = lista->prox) { if ((prev->i %2 == 1) && (lista->i % 2 == 0)) { return lista; } prev = lista; } } return NULL; } struct no *impar_par2(struct no *lista) { int prev = 0; for (; lista != NULL; lista = lista->prox) { if ((prev%2==1) && (lista->i%2==0)) return lista; prev = lista->i; } return NULL; } int main(int argc, char *argv[]) { struct no *l = novo(2, novo(3, novo(5, novo(7, novo(9, novo(6, novo(1, novo(2, novo(3, NULL))))))))); struct no *r = impar_par(l); printf("%d\n", r->i); return 0; }
Date: 2010/11/23 14:45:44