Created
October 19, 2011 16:58
-
-
Save Jessidhia/1298939 to your computer and use it in GitHub Desktop.
EE2 Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.o | |
main | |
main.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <string.h> | |
#include "lib.h" | |
static void q1() { | |
/* No original, o acumulador foi definido no escopo errado (dentro do 'for' em vez de fora) */ | |
int s = 0; | |
for (int i = 1; i < 10; i++) | |
s += i*i; | |
printf("%d\n", s); | |
} | |
static void q2() { | |
srand((unsigned) time(0)); | |
int face[6]; | |
memset(&face[0], 0, 6 * sizeof(int)); | |
for (int i = 0; i < 1200; i++) | |
++face[rand()%6]; | |
char *face_names[] = { | |
" Um ", | |
" Dois ", | |
" Tres ", | |
" Quatro ", | |
" Cinco ", | |
" Seis " }; | |
printf(" Face Observacoes\n"); | |
for(int i = 0; i < 6; i++) { | |
printf("%s %d\n", face_names[i], face[i]); | |
} | |
} | |
void lista_03() { | |
char *names[] = { "Corrigido", "Dados" }; | |
func_t funcs[] = { &q1, &q2 }; | |
int choice = menu("Lista 03.2011.2", 2, names); | |
funcs[choice](); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <string.h> | |
#include "lib.h" | |
void lista_04() { | |
int opt = menu("Usar srand(time(NULL))", 2, (char*[]) { "Sim", "Nao" }); | |
srand(opt == 1 ? 0 : time(NULL)); | |
for(int i = 0; i < 26; i++) | |
printf("%c%s", rand()%26 + 'a', i == 25 ? "\n" : " "); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include "lib.h" | |
static void q1() { | |
for (int i = 0x20; i <= 0xff; i++) { | |
printf("dec: %02d -- hex: %02x -- char: %c\n", i, i, (char)i); | |
} | |
} | |
static void q2() { | |
int chr; | |
int alloc_mult = 1; | |
char *buf = malloc(alloc_mult * 255); | |
char *pos = buf; | |
while ((chr = fgetc(stdin)) != 0x1B) { | |
*pos++ = (char) chr; | |
if (pos - buf > alloc_mult * 255 - 2) { | |
int distance = pos - buf; | |
buf = realloc(buf, ++alloc_mult * 255); | |
pos = buf + distance; | |
} | |
} | |
*pos = 0; | |
for (char *c = buf; c != pos; c++) { | |
printf("%c", toupper((int) *c)); | |
} | |
printf("\n"); | |
fflush(stdout); | |
} | |
static void q3() { | |
long term; | |
ask("Numero do termo da seq. de Fibonacci", ASK_LONG, 0, &term); | |
printf("%ld\n", fib(term)); | |
} | |
static void q4() { | |
int mode = menu("", 2, (char*[]){ "Combinacao", "Permutacao" }); | |
long n, r; | |
ask("Numero total de objetos", ASK_LONG, 0, &n); | |
ask("Quantos por vez", ASK_LONG, 0, &r); | |
if (n < 0 || r < 0) { | |
fprintf(stderr, "Nao e possivel formar um conjunto negativo.\n"); | |
return; | |
} | |
if (r > n) { | |
fprintf(stderr, "Nao e possivel remover mais objetos do que existem.\n"); | |
return; | |
} | |
printf("%ld\n", mode == 1 ? combine(n, r) : permute(n, r)); | |
} | |
void lista_05() { | |
char *names[] = { "ASCII", "toupper", "Fibonacci Iterativo", "Combinacao / Permutacao" }; | |
func_t funcs[] = { &q1, &q2, &q3, &q4 }; | |
int choice = menu("Lista 05.2011.2", 4, names); | |
funcs[choice](); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <math.h> | |
#include "lib.h" | |
static void q1() { | |
long n; | |
ask("Numero de termos", ASK_LONG, 0, &n); | |
if (n > 0) | |
printf("1"); | |
long acc = 1, prev = 0; | |
for (int i = 1; i < n; i++) { | |
acc += prev; | |
prev = acc - prev; | |
printf(" %ld", acc); | |
} | |
printf("\n"); | |
} | |
static void q2() { | |
int opt = menu("Triangulo de Pascal", 2, (char*[]) { "Imprimir triangulo", "Calcular soma de linha especifica" }); | |
long n; | |
ask(opt ? "Linha do triangulo" : "Ordem to triangulo", ASK_LONG, 0, &n); | |
if (opt) { | |
printf("Soma da linha %ld: %ld\n", n, (long) pow(2, n-1)); | |
} else { | |
for (int i = 0; i <= n; i++) { | |
for (int j = 0; j <= i; j++) { | |
printf("%5ld%s", combine(i, j), i == j ? "\n" : " "); | |
} | |
} | |
} | |
} | |
void lista_06() { | |
char *names[] = { "Fibonacci", "Pascal" }; | |
func_t funcs[] = { &q1, &q2 }; | |
int choice = menu("Lista 06.2011.2", 2, names); | |
funcs[choice](); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include <math.h> | |
#include "lib.h" | |
void lista_07() { | |
char in_str[512], in_ch_str[2]; | |
ask("Digite uma sentenca", ASK_STRING, 511, &in_str[0]); | |
do { | |
ask("Digite um caractere", ASK_STRING, 1, &in_ch_str[0]); | |
} while (strlen(in_ch_str) < 1); | |
in_str[511] = 0; | |
int l = strlen(in_str); | |
if (in_str[l-1] == '\n') | |
in_str[l-1] = 0; // remove o \n | |
in_ch_str[1] = 0; | |
int count = 0; | |
char *first = NULL; | |
char *ptr = &in_str[0]; | |
while ((ptr = strstr(ptr, in_ch_str))) { | |
if (!first) first = ptr; | |
++count; | |
++ptr; | |
} | |
if (count) { | |
printf("O caractere '%s' pertence a sentenca.\n", in_ch_str); | |
printf("O caractere aparece '%d' vezes.\n", count); | |
printf("Sua primeira ocorrencia e na posicao '%d'.\n", (int)(first - &in_str[0])); | |
printf("A sentenca a partir da primeira ocorrencia e: '%s'.\n", first); | |
} else { | |
printf("O caractere '%s' nao ocorre na sentenca.\n", in_ch_str); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#include "lib.h" | |
int ask(char *prompt, AskType ask_type, int maxlen, void *out) | |
{ | |
char temp_buf[256]; | |
char *bool_prompt = ""; | |
if (ask_type == ASK_BOOL) | |
bool_prompt = " [s/n]"; | |
while (1) { | |
fprintf(stderr, "%s%s: ", prompt, bool_prompt); | |
fflush(stderr); | |
if (!fgets(temp_buf, 256, stdin)) { | |
return -1; | |
} | |
switch (ask_type) { | |
case ASK_STRING: { | |
int len, total_len = 0; | |
char *out_pos = out; | |
while (maxlen > total_len && (len = strlen(temp_buf)) == 255) { | |
strncpy(out_pos, &temp_buf[0], maxlen - total_len); | |
total_len += len; | |
if (!fgets(temp_buf, 256, stdin)) { | |
return -2; | |
} | |
} | |
if (maxlen > total_len) | |
strncpy(out_pos, &temp_buf[0], maxlen - total_len); | |
return total_len + len - 1; | |
} | |
case ASK_LONG: { | |
long res; | |
char *temp_buf_end; | |
res = strtol(temp_buf, &temp_buf_end, 0); | |
if (*temp_buf_end != '\n') { | |
if (*temp_buf_end == '\0') { | |
fprintf(stderr, "Numero muito longo\n"); | |
while (fgetc(stdin) != '\n') {} // discard rest of input line | |
} | |
else | |
fprintf(stderr, "Numero invalido\n"); | |
continue; | |
} | |
*(long*)out = res; | |
return 0; | |
} | |
case ASK_FLOAT: { | |
float res; | |
char *temp_buf_end; | |
res = (float) strtod(temp_buf, &temp_buf_end); | |
if (*temp_buf_end != '\n') { | |
fprintf(stderr, "Numero invalido\n"); | |
continue; | |
} | |
*(float*)out = res; | |
return 0; | |
} | |
case ASK_BOOL: { | |
char ans = tolower((int) temp_buf[0]); | |
if (strlen(temp_buf) != 1 || (ans != 's' && ans != 'n')) { | |
fprintf(stderr, "Resposta invalida\n"); | |
continue; | |
} | |
if (out) | |
*(int*)out = ans == 's'; | |
return ans; | |
} | |
default: | |
fprintf(stderr, "Internal error: invalid AskType\n"); | |
exit(50); | |
} | |
} | |
} | |
long menu(char *title, int optcount, char **options) { | |
long o = -1; | |
fprintf(stderr, "Menu: %s\n", title); | |
for (int i = 0; i < optcount; i++) { | |
fprintf(stderr, " %d. %s\n", i+1, options[i]); | |
} | |
int res; | |
while (!(res = ask("Escolha", ASK_LONG, 0, &o)) && (o < 1 || o > optcount)) { | |
fprintf(stderr, "Numero invalido\n"); | |
} | |
if (res < 0) | |
exit(-res); | |
return o - 1; | |
} | |
long fib(int n) { | |
long acc = 1, prev = 1; | |
for(int i = 2; i < n; i++) { | |
acc += prev; | |
prev = acc - prev; | |
} | |
return acc; | |
} | |
long fac(int n) { | |
long acc = 1; | |
for(long next = n; next > 1; next--) | |
acc *= next; | |
return acc; | |
} | |
long combine(int n, int k) { | |
return fac(n) / fac(k) / fac(n - k); | |
} | |
long permute(int n, int k) { | |
return fac(n) / fac(n - k); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _LIB_H_ | |
#define _LIB_H_ | |
typedef void (*func_t)(); | |
typedef enum { | |
ASK_STRING, | |
ASK_LONG, | |
ASK_FLOAT, | |
ASK_BOOL | |
} AskType; | |
int ask(char *prompt, AskType ask_type, int maxlen, void *out); | |
/* Exibe menu com titulo "title", numero "optcount" de opcoes | |
e texto das opcoes em "options". Retorna o indice de "options" | |
que foi selecionado. */ | |
long menu(char *title, int optcount, char **options); | |
long fib(int n); | |
long fac(int n); | |
long combine(int n, int k); | |
long permute(int n, int k); | |
#endif // _LIB_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "lib.h" | |
extern void lista_03(); | |
extern void lista_04(); | |
extern void lista_05(); | |
extern void lista_06(); | |
extern void lista_07(); | |
extern void lista_08(); | |
static void main_exit() { | |
exit(0); | |
} | |
int main() { | |
char *names[] = { "03.2011.2", "04.2011.2", "05.2011.2", "06.2011.2", "07.2011.2", "08.2011.2", "Sair" }; | |
func_t funcs[] = { &lista_03, &lista_04, &lista_05, &lista_06, &lista_07, &lista_08, &main_exit }; | |
while (1) { | |
int choice = menu("Listas de Exercicios", 7, names); | |
funcs[choice](); | |
printf("\n\n"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CC = gcc | |
CFLAGS = -std=c99 | |
OBJS = l0320112.o \ | |
l0420112.o \ | |
l0520112.o \ | |
l0620112.o \ | |
l0720112.o \ | |
l0820112.o \ | |
lib.o | |
all: main lista_08_2 | |
main: $(OBJS) | |
lista_08_2: lista_08_2.o lib.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment