Skip to content

Instantly share code, notes, and snippets.

View marcoscastro's full-sized avatar

Marcos Castro de Souza marcoscastro

View GitHub Profile
@marcoscastro
marcoscastro / exemplo_typedef.c
Created January 3, 2014 07:52
Exemplo da criação de apelidos em C - typedef
#include <stdio.h>
struct pessoa
{
int idade;
};
typedef float real;
typedef struct pessoa tipo_pessoa;
@marcoscastro
marcoscastro / alocacao_dinamica.c
Created January 3, 2014 08:15
Alocação dinâmica em C - malloc e realloc
#include <stdio.h>
#include <stdlib.h> // contém funções como malloc, realloc, free, exit
#define MAX 5 // quantidade para ser alocada
int main()
{
int *vet; // variável ponteiro do tipo inteiro
int i = 0, qte = 0;
// aloca um espaço para MAX números inteiros
@marcoscastro
marcoscastro / swap_c.c
Created January 4, 2014 03:44
Forma não muito comum de trocar o valor de duas variáveis em C ;-)
#include <stdio.h>
void trocar(int *a, int *b)
{
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
printf("%d %d\n", *a, *b);
}
@marcoscastro
marcoscastro / example_class_string.cpp
Created January 10, 2014 18:37
example using the string class from C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
// cria uma string
string str("Hello");
@marcoscastro
marcoscastro / russian_peasant_multiplication.c
Created January 10, 2014 20:14
Russian peasant algorithm Goal: to multiply two numbers
#include <stdio.h>
int russian_peasant(unsigned int x, unsigned int y)
{
int resultado = 0;
while(y > 0)
{
if(y % 2 != 0)
resultado += x;
@marcoscastro
marcoscastro / game_guess_number.cpp
Created January 11, 2014 05:15
Game Guess the Number
// Game Guess the Number
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
@marcoscastro
marcoscastro / funcao_js.htm
Created January 16, 2014 09:42
Exemplo de criação de função com JavaScript. Example of function with JavaScript.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
<script>
// função que retorna a soma de dois números
@marcoscastro
marcoscastro / fibonacci.js
Created January 16, 2014 09:59
JavaScript - Fibonacci
function fib(n) {
if(n == 1 || n == 2) {
return 1;
}
return fib(n-1) + fib(n-2);
}
console.log(fib(10));
@marcoscastro
marcoscastro / exemplo_arrays.html
Created January 17, 2014 05:30
Exemplo utilizando arrays (vetores) com JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
<script>
// criando um array
var vetor = [1, 2, 3, 4];
@marcoscastro
marcoscastro / exemplo_objetos.html
Created January 17, 2014 05:44
Exemplo da utilização de objetos com JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
<script>
// criando um objeto
var pessoa = new Object();