1
Programação e Scripts / Re: C/C++ no Linux- Por onde comecar?
« Online: 13 de Setembro de 2007, 18:36 »
acho q pra começar tu podia usar o gedit ou outro editor e compilar via terminal pra tu pegar intimidade com o gcc,
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#include <iostream>
#include <map>
#include <vector>
#include <string>
int main(){
std::map< std::string, std::string > MyMap; //aqui criamos o map
/*aqui criamos o iterator do map para podermos navegar pelo mapa */
std::map< std::string, std::string >::iterator MyIterMap;
std::vector< std::string > MyVector; //criamos o vetor
/*aqui iniciamos o iterator pro vetor lembrando que sempre o iterator tem q ser igual o vetor entaum se seu vetor é std::vector<int> o iterator tbm tem q ser std::vector<int>::iterator de int*/
std::vector< std::string >::iterator MyIterVector;
MyVector.push_back("vetor1");
MyVector.push_back("vetor2");
MyVector.push_back("vetor3");/* aqui adicionas no final do vetor os valores*/
MyMap["Teste1"] = "map1";
MyMap["Teste2"] = "map2";
MyMap["Teste3"] = "map3";/*aqui adiconamos valores no mapa*/
/*agora vamos imprimir os conteudos do vetor e do mapa usando os iteratores */
MyIterVector = MyVector.begin(); //aqui colocas o iterator no começo do vetor
MyIterMap = MyMap.begin(); //aqui a mesma coisa
while( MyIterVector != MyVector.end() ) /* enquanto o iterator for diferente do final do vetor ele vai imprimir o resultado */
{
std::cout << *MyIterVector<<std::endl;
MyIterVector++; //aqui passamos o iterator pra proxima posição
}
while( MyIterMap != MyMap.end() ) //aqui a mesma coisa
{
/* aqui pegamos o primeiro valor do map ou seja o valor MyMap["teste1"] esse valor pra podermos imprimir seu valor que é "map1" */
std::string values = (*MyIterMap).first;
std::cout << MyMap[values] <<std::endl;
MyIterMap++;
}
return 0;
}
resultadovetor1
vetor2
vetor3
map1
map2
map3
#include <iostream>
#include <map>
#include <vector>
#include <string>
struct di
{
std::string query;
int length;
};
int main()
{
std::map< std::string, std::vector< di > > teste;
std::map< std::string, std::vector< di > >::iterator iter;
std::vector< di >::iterator viter;
di diego;
diego.query ="diego";
diego.length = strlen("diego");
teste["teste1"].push_back( diego );
std::cout << teste["teste1"][0].length <<std::endl;
diego.query = "rocha";
diego.length = strlen("rocha");
teste["teste2"].push_back( diego );
iter = teste.begin();
while( iter != teste.end())
{
std::string valor = (*iter).first;
viter = teste[valor].begin();
while( viter != teste[valor].end())
{
std::cout << (*viter).query <<" "<<(*viter).length<<std::endl;
viter++;
}
iter++;
}
return 0;
}
resultado5
diego 5
rocha 5
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>
class Roleta
{
private:
std::string roleta1, roleta2, roleta3, roleta4; //slots
std::string format;
bool win, exec;
int credits, games, rraposta, aposta, pay;
WINDOW *screen;
int y, x;
public:
Roleta()
{
exec = true;
credits = 51;
games = 0;
rraposta = aposta = 1;
win = false;
srand( time(NULL) );
init_curses();
_init_colors();
noecho();
curs_set(0);
getmaxyx( screen, y, x );
if ( x < 120 && y < 35 )
{
wattron( screen, COLOR_PAIR(1));
format = "Sorry try increase the resolution to play this game :)";
mvwaddstr( screen,(y/2),(x/2)-24, format.c_str() );
wattroff( screen, COLOR_PAIR(1));
refresh();
close();
}
}
~Roleta()
{
resetty();
endwin();
}
bool init_curses( )
{
if ( ( screen = initscr() ) == NULL )
{
std::cout << "Erro ao iniciar window!" << std::endl;
return false;
}
savetty();
refresh();
return true;
}
void _init_colors()
{
if (has_colors())
{
start_color();
if ( use_default_colors() != ERR )
{
init_pair( 1, COLOR_RED, -1 );
init_pair( 2, COLOR_CYAN, -1 );
init_pair( 3, COLOR_BLUE, -1 );
}
}
}
std::string sorted()
{
a: //goto
int myrandnumber = rand() % 20 + 1;
if ( myrandnumber < 5 )
return "apple";
else if( myrandnumber > 5 && myrandnumber < 9 )
return "**BONUS**";
else if ( myrandnumber > 9 && myrandnumber < 13 )
return "lemon";
else if ( myrandnumber > 13 && myrandnumber < 20 )
return "oranje";
else
goto a;
}
void update()
{
char tt[5];
sprintf( tt, "%d", games );
wattron( screen, COLOR_PAIR(2) );
format = "Games: ";
mvwaddstr( screen, 2, 4, format.c_str() );
mvwaddstr( screen, 2, 12, tt );
sprintf( tt, "%d", credits );
format = "Credits: ";
mvwaddstr( screen, 3, 4, format.c_str() );
mvwaddstr( screen, 3, 14, tt );
wattroff( screen, COLOR_PAIR(2) );
refresh();
}
void make_game()
{
char c;
int i = 0;
char tt[5];
while ( c != 'q' )
{
switch( c )
{
case 65:
if ( rraposta < credits )
rraposta++;
break;
case 66:
if ( rraposta > 1 )
rraposta--;
break;
case 10:
run();
break;
}
if ( i == 0 )
run();
aposta = credits < rraposta ? credits : rraposta;
rraposta = aposta;
wattron( screen, COLOR_PAIR(2));
sprintf( tt, "%d ", aposta );
mvwaddstr( screen, (y/2)-6, 4, "BET = multiplies");
mvwaddstr( screen, (y/2)-6, 22, tt );
wattroff( screen, COLOR_PAIR(2));
refresh();
c = getch();
i++;
}
}
void run( )
{
clear();
int i = 0, j = -4;
bool printwin;
char tt[5];
std::string option;
win = false;
wattron( screen, COLOR_PAIR(2) );
mvwaddstr( screen, (y/2)-10, (x/2)-23,"**BONUS** = 50 credits");
mvwaddstr( screen, (y/2)-9, (x/2)-23, "orange = 2 credits");
mvwaddstr( screen, (y/2)-9, (x/2)+6,"apple = 5 credits");
mvwaddstr( screen, (y/2)-10, (x/2)+6, "lemon = 10 credits");
wattroff( screen, COLOR_PAIR(2) );
mvwaddstr( screen, (y/2)-7, (x/2)-23, "+---------------------------------------------+");
mvwaddstr( screen, (y/2)-6, (x/2)-23, "| Slot 1 | Slot 2 | Slot 3 | Slot 4 |");
mvwaddstr( screen, (y/2)-5, (x/2)-23, "+---------------------------------------------+");
while ( i < 5 )
{
roleta1 = sorted();
roleta2 = sorted();
roleta3 = sorted();
roleta4 = sorted();
printwin = false;
if ( roleta1 == roleta2 && roleta2 == roleta3 && roleta3 == roleta4 )
{
if ( roleta1 == "**BONUS**" )
{
credits += 50 * aposta;
pay = 50 * aposta;
}
else if ( roleta1 == "apple" )
{
credits += 5 * aposta;
pay = 5 * aposta;
}
else if ( roleta1 == "lemon" )
{
credits += 10 * aposta;
pay = 10 * aposta;
}
else
{
credits += 2 * aposta;
pay = 2 * aposta;
}
win = true;
printwin = true;
}
sprintf( tt, "%d", pay );
option = tt;
if ( printwin )
wattron( screen, COLOR_PAIR(1));
format ="| "+roleta1;
mvwaddstr( screen, (y/2)+j, (x/2)-23, format.c_str() );
format ="| "+roleta2;
mvwaddstr( screen, (y/2)+j, (x/2)-12, format.c_str() );
format ="| "+roleta3;
mvwaddstr( screen, (y/2)+j, (x/2)-1, format.c_str() );
format ="| "+roleta4;
mvwaddstr( screen, (y/2)+j, (x/2)+11, format.c_str() );
if ( printwin )
{
if ( roleta1 == "**BONUS**" )
format = "| **** BONUS +50";
else
format = "| *** +"+option+" credit";
}
else
format = "|";
mvwaddstr( screen, (y/2)+j, (x/2)+23, format.c_str());
if ( printwin )
wattroff( screen, COLOR_PAIR(1));
mvwaddstr( screen,(y/2)+(++j),(x/2)-23, "+---------------------------------------------+");
refresh();
i++;
j++;
}
refresh();
games++;
if ( credits == 0 )
{
clear();
wattron( screen, COLOR_PAIR(1));
mvwaddstr( screen, (y/2), (x/2)-strlen("GAME OVER"), "GAME OVER");
wattroff( screen, COLOR_PAIR(1));
refresh();
close();
}
if ( !win )
credits -= aposta;
mvwaddstr( screen,(y/2)+(++j),(x/2)-11, "Type 'UP or DOWN' to increase or diminish bet");
mvwaddstr( screen,(y/2)+(++j),(x/2)-11, "Type 'enter' to spin");
mvwaddstr( screen,(y/2)+(++j),(x/2)-11, "Type 'q' to quit game");
update();
}
void close()
{
resetty();
endwin();
exit(0);
}
};
int main()
{
Roleta rol;
rol.make_game();
return 0;
}
/*compilar
#g++ roleta.cpp -o roleta -lncurses
#./roleta
*/