Skip to content

sylhare/Cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++

C++ is an evolution of the C language but with more modern features for a more diverse use than the C.

Introduction

C++ is a low level programming language, meaning that it is closer to the computer internal mechanism (close to the metal). This language strength are:

  • Fast - for high performance software
  • Multiplatform - can be executed in Windows, MacOS, Linux without code transoformation
  • It has imperative, object-oriented and generic programming features
  • Famous - it is used for multiple applications and has a lot of librairies available

Getting Started

To start coding in c++ you would need three things: - A text editor - A compiler - A debugger

Thanksfully there are options where all three are integrated in an IDE (Integrated Development Environment). A standard and famous IDE to use would be code::blocks. If you are on windows, choose the version with [MinGW] which is a c++ compiler. If you are on Linux (debian, ubuntu):

apt-get install build-essential codeblocks

You can open the IDE and start writing your programms in a .cpp file.

Tips

Don't try to optimize blindly

Check what takes time first. Most of the programming optimization is done via the compiler.

Don't use std::endl if \n will do

Using std::endl emits a \n and flushes the stream. Unless you really need the stream flushed. std::endl is potentially more computationally costly.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit. Consider this: you are using two libraries called Foo and Bar:

using namespace foo;
using namespace bar;

Everything works fine, you can call Blah() from Foo and Quux() from Bar without problems. But one day you upgrade to a new version of Foo, which now offers a function called Quux(). Now you've got a conflict: Both Foo 2.0 and Bar import Quux() into your global namespace. This is going to take some effort to fix, especially if the function parameters happen to match.

If you had used foo::Blah() and bar::Quux(), then the introduction of foo::Quux() would have been a non-event.

The "return 0;" is optionnal for main

For C++, since the first standard in 1998; see ISO/IEC 14882:1998 section 3.6.1:

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

Calculator

calculator!

Simple Calculator, that does simple calculations, don't expect too much though.

It implements:

  • (っ◔◡◔)っ ♥ Love
  • Standard operation default in c++
  • <iostream> for input and output
  • <cmath> for more complex math operation

Sources

Minesweeper

   	___  ___  _____   _   _   _____   _____   _    _   _____   _____  ______   _____  ______ 
	|  \/  | |_   _| | \ | | |  ___| /  ___| | |  | | |  ___| |  ___| | ___ \ |  ___| | ___ \
	| .  . |   | |   |  \| | | |__   \ `--.  | |  | | | |__   | |__   | |_/ / | |__   | |_/ /
	| |\/| |   | |   | . ` | |  __|   `--. \ | |/\| | |  __|  |  __|  |  __/  |  __|  |    / 
	| |  | |  _| |_  | |\  | | |___  /\__/ / \  /\  / | |___  | |___  | |     | |___  | |\ \ 
	\_|  |_/  \___/  \_| \_/ \____/  \____/   \/  \/  \____/  \____/  \_|     \____/  \_| \_|

Challenge from google, to recreate a minesweeper. There's the challenge and the solution.

I have made one minesweeper in javascript that is fully functionnal and better looking.

Source

Conway's game of life

life!

The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970.

Rules

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated".

Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

  • Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by overpopulation.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Implementation

A cell C is represented by a 1 when alive, or 0 when dead, in an m×m square array of cells.

We calculate N (the sum of live cells in C's eight-location neighbourhood), the state of the cell C is then dead or alive in the next generation based on the following table:

   C   N                 new C
   1   0,1             ->  0  # Lonely
   1   4,5,6,7,8       ->  0  # Overcrowded
   1   2,3             ->  1  # Lives
   0   3               ->  1  # It takes three to give birth!
   0   0,1,2,4,5,6,7,8 ->  0  # Barren

Cells near or beyond the border are considered to be dead.

Sources