<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Código C++</title>
	
	<link>http://codigoc.org</link>
	<description>Ayuda para tu tarea en C++</description>
	<lastBuildDate>Thu, 22 Sep 2011 02:27:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codigoc" /><feedburner:info uri="codigoc" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>codigoc</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Diferencias entre C y C++</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/B7yRNYki8y0/708-diferencias-entre-c-y-c</link>
		<comments>http://codigoc.org/708-diferencias-entre-c-y-c#comments</comments>
		<pubDate>Thu, 22 Sep 2011 02:27:39 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Artículos]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=708</guid>
		<description><![CDATA[Me han estado llegando comentarios y correos con dudas, correcciones y todo tipo de insultos (no, eso último no) y decidí responder todos ellos con este post en el que aclararé lo que es C y lo que es C++ y sus diferencias. C fue creado en 1972 por Dennis M. Ritchie en los Laboratorios [...]]]></description>
			<content:encoded><![CDATA[<p>Me han estado llegando comentarios y correos con dudas, correcciones y todo tipo de insultos (<em>no, eso último no</em>) y decidí responder todos ellos con este post en el que aclararé lo que es <strong>C</strong> y lo que es <strong>C++</strong> y sus <strong>diferencias</strong>.</p>
<p><a href="http://es.wikipedia.org/wiki/C_(lenguaje_de_programaci%C3%B3n)">C</a> fue creado en 1972 por <em>Dennis M. Ritchie</em> en los Laboratorios Bell como evolución del anterior lenguaje B, a su vez basado en BCPL.</p>
<p><a href="http://es.wikipedia.org/wiki/C%2B%2B">C++</a>, por su parte, fue creado a mediados de los años 1980 por <em>Bjarne Stroustrup</em>. La intención de su creación fue el extender al exitoso lenguaje de programación C con mecanismos que permitan la manipulación de objetos.</p>
<p>Así que <strong>C es el lenguaje original</strong>, mientras que <strong>C++ es una ampliación de C</strong>, por eso el ++.</p>
<p>A mi cuando me enseñaron a programar me dijeron que iba a aprender a programar en C++, pero en verdad me enseñaron únicamente C, así que muchos profesores en realidad no saben lo que es programar en C++. Veamos un hola mundo en los dos programas:</p>
<p>En C</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;  </span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span> <span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hola Mundo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>En C++</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hola mundo&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> 
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Para el tipo de programas que se estarán mostrando en este blog la diferencia más importante es la entrada y salida de datos. Así que veamos un ejemplo de entrada y salida de datos de cada programa:</p>
<p>En C</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> radio<span style="color: #339933;">;</span>
    <span style="color: #993333;">float</span> area<span style="color: #339933;">,</span> perimetro<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// SALIDA: mensaje un pantalla</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Introduce el radio del circulo: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//ENTRADA: recibir dato desde teclado</span>
    scanf<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>radio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// calculos</span>
    area <span style="color: #339933;">=</span> <span style="color:#800080;">3.1416</span> <span style="color: #339933;">*</span> radio <span style="color: #339933;">*</span> radio<span style="color: #339933;">;</span>
    perimetro <span style="color: #339933;">=</span> <span style="color:#800080;">3.1416</span> <span style="color: #339933;">*</span> radio <span style="color: #339933;">*</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//SALIDA: resultado en pantalla</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;El area es %.2f y el perimetro %.2f&quot;</span><span style="color: #339933;">,</span> area<span style="color: #339933;">,</span> perimetro<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    getch<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>En C++</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> radio<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">float</span> area, perimetro<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// SALIDA: mensaje un pantalla</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Introduce el radio del circulo: &quot;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//ENTRADA: recibir dato desde teclado</span>
    <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> radio<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// calculos</span>
    area <span style="color: #000080;">=</span> <span style="color:#800080;">3.1416</span> <span style="color: #000040;">*</span> radio <span style="color: #000040;">*</span> radio<span style="color: #008080;">;</span>
    perimetro <span style="color: #000080;">=</span> <span style="color:#800080;">3.1416</span> <span style="color: #000040;">*</span> radio <span style="color: #000040;">*</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">//SALIDA: resultado en pantalla</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;El area es &quot;</span> <span style="color: #000080;">&lt;&lt;</span> area <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; y el perimetro &quot;</span> <span style="color: #000080;">&lt;&lt;</span> perimetro<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Lo que noté al hacer estos dos programitas es que pedir un dato en C++ es mucho mucho más simple que en C. Sin embargo, el asunto de los &lt;&lt; y &gt;&gt; puede llegar a &#8216;asustar&#8217; a los recién iniciados en la programación.</p>
<p><strong>Pedir un dato en C</strong></p>
<pre>scanf("<em>modificador</em>", &#038;<em>nombre de la variable</em>);</pre>
<p><strong>Pedir un dato en C++</strong></p>
<pre>cin >> <em>nombre de la variable</em>;</pre>
<p><strong>Mostrar un dato en C</strong></p>
<pre>printf("Dato: <em>modificador</em>", <em>nombre de la variable</em>);</pre>
<p><strong>Mostrar un dato en C++</strong></p>
<pre>cout << "Dato: " << <em>nombre de la variable</em>;</pre>
<p>Los modificadores son los siguientes: %d para int, %f para float, %s para string, %c para char.</p>
<h4>Librerías en C++</h4>
<p>Por simple convención las librerías en C terminan en &#8216;.h&#8217; (<em>punto hache</em>). Todas las librerías de C sirven para C++, sin embargo, también por convención, se elimina la terminación &#8216;.h&#8217; y mejor se agrega &#8216;c&#8217; al principio.</p>
<p><strong>Libreria en C      Librería en C++</strong></p>
<p>math.h                  cmath</p>
<p>string.h                 cstring</p>
<p>time.h                   ctime</p>
<p>etcetera.</p>
<h4>El &#8216;namespace&#8217;</h4>
<p>C como lenguaje tiene un conjunto de palabras reservadas, como por ejemplo: if, for, while, int, float, &#8230; C++ es una ampliación, por lo tanto tiene que agregar nuevas palabras reservadas. Éstas palabras reservadas están en un &#8216;namespace&#8217; (<em>espacio de nombres</em>). En específico cout y cin están el namespace std (<em>standard</em>).</p>
<p>Si no declararamos que vamos a usar el namespace std (<em>using namespace std;</em>), cada vez que quisieramos usar <strong>cout</strong>, tendríamos que escribir <strong>std::cout</strong>.</p>
<p>Espero que a partir de ahora pueden identificar si un programa está en C o en C++. También espero estar publicando mis programas en ambos lenguajes a partir de ahora.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/708-diferencias-entre-c-y-c">Permalink</a> |
<a href="http://codigoc.org/708-diferencias-entre-c-y-c#comments">5 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/B7yRNYki8y0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/708-diferencias-entre-c-y-c/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://codigoc.org/708-diferencias-entre-c-y-c</feedburner:origLink></item>
		<item>
		<title>#define en C++, cómo sí y cómo no</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/HMDyziUGRgs/700-define-en-c-como-si-y-como-no</link>
		<comments>http://codigoc.org/700-define-en-c-como-si-y-como-no#comments</comments>
		<pubDate>Sun, 28 Aug 2011 06:40:45 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Curso]]></category>
		<category><![CDATA[básico]]></category>
		<category><![CDATA[define]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=700</guid>
		<description><![CDATA[El mayor objetivo de este post es darles a conocer una de las cosas que C++ permite hacer pero que nadie en su sano juicio debería de hacer. ¿Y para que se los enseño entonces? Pues para que si algún día lo ven en un código ajeno sepan qué pinch_ está pasando. Bueno, antes que [...]]]></description>
			<content:encoded><![CDATA[<p>El mayor objetivo de este post es darles a conocer una de las cosas que C++ permite hacer pero que nadie en su sano juicio debería de hacer. ¿Y para que se los enseño entonces? Pues para que si algún día lo ven en un código ajeno sepan qué pinch_ está pasando.</p>
<p>Bueno, antes que nada, <strong>#define</strong> sirve para dos cosas: <strong>definir una constante</strong> o crear un macro. Los macros pueden llegar a ser algo difícil de entender, ahora sólo veremos el asunto de las constantes. El asunto aquí es que si yo pongo (<em>fuera del main</em>) una línea así:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define PI 3.14159265</span></pre></div></div>

<p>&#8230;significa que cada vez que escribamos <em>PI</em> en nuestro programa, C++ lo interpretará como <em>3.14159265</em>. Es una simple sustitución.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define &lt;aquí va el nombre&gt; &lt;después de un espacio va el valor&gt;</span></pre></div></div>

<p>Pero el asunto es que se puedes hacer algunas cosas bastantes feas con esta propiedad. Vean este programa:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#define PI 3.14159265</span>
<span style="color: #339900;">#define NOF_CHAR 50</span>
<span style="color: #339900;">#define pause cin.get();cin.get();</span>
<span style="color: #339900;">#define ct cout</span>
<span style="color: #339900;">#define cn cin</span>
<span style="color: #339900;">#define false true</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">char</span> name<span style="color: #008000;">&#91;</span>NOF_CHAR<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
    ct <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter your name: &quot;</span><span style="color: #008080;">;</span>
    cn <span style="color: #000080;">&gt;&gt;</span> name<span style="color: #008080;">;</span>
    ct <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Bye &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span>
        ct <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;P.S. Pi = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> PI<span style="color: #008080;">;</span>
    pause
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Tenemos 6 definiciones, ordenadas en su nivel de aceptabilidad:</p>
<ul>
<li><strong>#define PI 3.14159265</strong> y <strong>#define NOF_CHAR 50</strong>, éstos ejemplifican el uso primario de #define: guardan un valor que es constante durante toda la ejecución del programa.</li>
<li><strong>#define pause cin.get();cin.get();</strong>, ésta ya empieza a ser no aceptable pero podemos darla por buena porque nos ayuda a leer mejor nuestro código. Ya sabemos que al escribir <em>cin.get();cin.get(); </em>nuestro programa se pausa al igual que con un <em>getch()</em>, pero al usar este #define escribiremos en lugar de eso simplemente <strong>pause</strong>.</li>
<li><strong>#define ct cout</strong> y <strong>#define cn cin</strong>, totalmente inaceptables. Algunas personas usan este tipo de #define&#8217;s para escribir menos, pero de verdad que (<em>en la gran mayoría de los casos</em>) no vale la pena el ahorro de caracteres, sólo estamos haciendo nuestro código menos entendible para otras personas.</li>
<li><strong>#define false true</strong>, este es mas bien como una broma (<em>y es increíble que funcione en verdad</em>), pero si se usa en serio es como una rayada de madre. Si ejecutan el código verán que sí se muestra la línea donde se imprime <em>PI</em> aún que está dentro de un <em>if</em> al que nunca se debería de entrar ¿y porqué sí entra? porque <em>false</em> es <em>true</em>.</li>
</ul>
<div>Así que ya saben <strong>cómo sí y cómo no usar un #define en C++</strong>, y ya no están &#8216;indefensos&#8217; ante programadores con malos hábitos.</div>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/700-define-en-c-como-si-y-como-no">Permalink</a> |
<a href="http://codigoc.org/700-define-en-c-como-si-y-como-no#comments">5 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/HMDyziUGRgs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/700-define-en-c-como-si-y-como-no/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://codigoc.org/700-define-en-c-como-si-y-como-no</feedburner:origLink></item>
		<item>
		<title>Nuevo: Curso de C++</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/UfsS9NGKdF0/693-nuevo-curso-de-c</link>
		<comments>http://codigoc.org/693-nuevo-curso-de-c#comments</comments>
		<pubDate>Fri, 19 Aug 2011 01:44:19 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=693</guid>
		<description><![CDATA[Según Google Analytics están regresando las visitas, que raro que coincida con el inicio de clases ¿verdad? :D pero bueno, afortunadamente yo sí me preocupe por este blog en vacaciones. Como ya repetí dos veces, esto es un blog, lo que significa que los posts mas recientes son los que aparecen al principio. Además, cuando [...]]]></description>
			<content:encoded><![CDATA[<p>Según Google Analytics están regresando las visitas, que raro que coincida con el inicio de clases ¿verdad? :D pero bueno, afortunadamente yo sí me preocupe por este blog en vacaciones.</p>
<p>Como ya repetí dos veces, esto es un blog, lo que significa que los posts mas recientes son los que aparecen al principio. Además, cuando inicié este blog mi propósito era ir avanzando en dificultad conforme al tiempo, no lo he cumplido mucho pero aún así los posts sobre los temas más básicos están &#8216;enterrados&#8217; debajo de los más recientes. Así que me tomé un tiempo para echarme un clavado de panza en el <a href="http://codex.wordpress.org/">Codex de WordPress</a> y crear una forma diferente de navegar en el blog.</p>
<p><a href="http://codigoc.org/wp-content/uploads/2011/08/codigoc.jpg"><img class="aligncenter size-full wp-image-695" title="codigo c" src="http://codigoc.org/wp-content/uploads/2011/08/codigoc.jpg" alt="codigo c" width="425" height="184" /></a></p>
<p>Entonces así es como inauguro (<em>siendo las 19:30 del dia&#8230; ah no verdad?</em>) el <strong>Curso de C++</strong>. Todo lo que tienen que hacer es ir a este índice y comenzar a leer: <a href="http://codigoc.org/662-curso-de-c">Curso de C++, Índice</a>. Luego pueden simplemente dar clic en el link &#8216;Siguiente&#8217; que aparecerá en la barra lateral (-&gt;).</p>
<p>Así que espero que les sea de utilidad, si les gusta presúmanlo, si tienen un amigo que siempre les anda preguntando pues mándenlo aquí y líbrense de él.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/693-nuevo-curso-de-c">Permalink</a> |
<a href="http://codigoc.org/693-nuevo-curso-de-c#comments">3 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/UfsS9NGKdF0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/693-nuevo-curso-de-c/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://codigoc.org/693-nuevo-curso-de-c</feedburner:origLink></item>
		<item>
		<title>Code::Blocks, un IDE moderno para C++</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/I4SXpYvqEco/645-codeblocks-un-ide-moderno-para-c</link>
		<comments>http://codigoc.org/645-codeblocks-un-ide-moderno-para-c#comments</comments>
		<pubDate>Wed, 13 Jul 2011 01:28:54 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[ide]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=645</guid>
		<description><![CDATA[Yo hace un buen rato que no uso Dev-C++ para programar, y sin embargo vi que aquí en la barra lateral aún está el link hacia el post donde se los recomiendo. Así que ahora les voy a recomendar el IDE que uso actualmente Code::Blocks. Code::Blocks tiene bastante ventajas respecto a otros IDES: Aspecto moderno. [...]]]></description>
			<content:encoded><![CDATA[<p>Yo hace un buen rato que no uso Dev-C++ para programar, y sin embargo vi que aquí en la barra lateral aún está el link hacia el post donde se los recomiendo. Así que ahora les voy a recomendar el IDE que uso actualmente <a href="http://www.codeblocks.org">Code::Blocks</a>.</p>
<p style="text-align: center;"><a href="http://codigoc.org/wp-content/uploads/2011/07/codeblocks.jpg"><img class="size-medium wp-image-646 aligncenter" title="codeblocks" src="http://codigoc.org/wp-content/uploads/2011/07/codeblocks-300x155.jpg" alt="codeblocks" width="300" height="155" /></a></p>
<p>Code::Blocks tiene bastante ventajas respecto a otros IDES:</p>
<ul>
<li><strong>Aspecto moderno</strong>. ¿A quién le gusta programar a pantalla completa en consola con fuente amarilla sobre fondo azul hoy en día?</li>
<li><strong>Multiplataforma</strong>. Windows XP/Vista/7, Mac OS X, Linux.</li>
<li><strong>Rápido</strong>. Está escrito en C++.</li>
<li><strong>Open Source</strong>. Eso significa muchas cosas, pero lo importante es que es gratis.</li>
<li><strong>Pestañas</strong>. Puedes tener muchos archivos abiertos.</li>
</ul>
<div>Lo pueden descargar aquí: <a href="http://www.codeblocks.org/downloads/26">Download Code::Blocks</a>. Asegúrense de descargar la versión que trae el compilador incluido, el que dice <strong>mingw</strong>.</div>
<div>Si de plano nunca han manejado este tipo de software, tal vez les sean útiles estos consejos:</div>
<div>
<ul>
<li>Para hacer un programa pequeño (<em>lo que sea que no necesite mas de dos archivos</em>) no se compliquen creando proyectos. Simplemente vayan a: <em>File/New/Empy File</em>. Luego guarden el archivo con extensión <strong>.cpp, </strong>preferentemente antes de empezar a programar (<em>si no lo guardan no tendrán diferentes colores en su código (Sintax Highlighting)</em>).</li>
<li>Hay tres botones relacionados con &#8216;correr&#8217; el programa: <em>Build</em>, <em>Run</em> y <em>Build and Run</em>. El primero solo construye el programa pero no lo corre, el segundo solo lo corre y el tercero los construye y los corre. Así que cuando queramos probar nuestro programa el botón indicado es <em>Build and Run</em>, pero si no hemos hecho ningún cambio y queremos volver a probarlo es más rápido <em>Run</em>.</li>
</ul>
</div>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/645-codeblocks-un-ide-moderno-para-c">Permalink</a> |
<a href="http://codigoc.org/645-codeblocks-un-ide-moderno-para-c#comments">9 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/I4SXpYvqEco" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/645-codeblocks-un-ide-moderno-para-c/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://codigoc.org/645-codeblocks-un-ide-moderno-para-c</feedburner:origLink></item>
		<item>
		<title>Aprender inglés, ¿¡en C++!?</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/hLCi68AU4Fo/633-aprender-ingles-%c2%bf%c2%a1en-c</link>
		<comments>http://codigoc.org/633-aprender-ingles-%c2%bf%c2%a1en-c#comments</comments>
		<pubDate>Sat, 21 May 2011 04:08:30 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[matrices]]></category>
		<category><![CDATA[rand]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=633</guid>
		<description><![CDATA[Me contó SetRoot a través de un comentario que se le ocurrió hacer un programa en C++ que nos ayudará a aprender palabras en inglés, en sus propias palabras: Se me ocurrio hacer un programa para aprender ingles y creo que los tiros tambien van por ahi corrígeme: 1 llenar 1000 palabras 2 agruparlas por alguna [...]]]></description>
			<content:encoded><![CDATA[<p>Me contó <strong>SetRoot </strong>a través de un comentario que se le ocurrió hacer un programa en C++ que nos ayudará a aprender palabras en inglés, en sus propias palabras:<br />
<code>Se me ocurrio hacer un programa para aprender ingles y creo que los tiros tambien van por ahi corrígeme:<br />
1 llenar 1000 palabras<br />
2 agruparlas por alguna relacion como emociones articulos y otros(para esto quisiera usar una matriz para acceder a ella mas facil aunque 2 matrices tambien creo q me van :s con punteros y eso ,s)<br />
3 ejecucion: con un par de cases hacer un menu que me permita lanzar 20 palabras al azar(con sran o ran creo + time) con su significado<br />
y despues que me permita hacer un tipo de examen o test con esas mismas palabras desordenadas que cada acierto valga algo para ganar puntos y asi involucrar a los amigos y familiares</code><br />
Me gustó la idea de hacer un pequeño &#8216;bosquejo&#8217; de su idea porque precisamente se necesita usar el código de mi último post: <a href="http://codigoc.org/627-numeros-aleatorios-sin-repetir-en-c">Números aleatorios sin repetir en C++</a>. Así que mi algoritmo es algo así:</p>
<ol>
<li>Crear una matriz tipo string de N filas y 2 columnas, la primera columna son las palabras en inglés; la segunda, en español.</li>
<li>Pedir el número de palabras que el usuario quiera memorizar.</li>
<li>Obtener ese número de palabras aleatoriamente y mostrarlas.</li>
<li>Borrar las palabras y empezar a preguntar el significado de cada una.</li>
<li>Comparar la respuesta del usuario con la respuesta correcta.</li>
<li>Calcular su calificación y mostrarla.</li>
</ol>
<p>El código es este:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;cstdlib&gt;</span>
<span style="color: #339900;">#include&lt;ctime&gt;</span>
<span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> nofw<span style="color: #008080;">;</span>
<span style="color: #339900;">#define N 8 // numero de pares de palabras en la matriz</span>
&nbsp;
<span style="color: #0000ff;">bool</span> checkrep<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n, <span style="color: #0000ff;">int</span> num<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>nofw<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>n <span style="color: #000080;">==</span> num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    string ans, words<span style="color: #008000;">&#91;</span>N<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;hi&quot;</span>, <span style="color: #FF0000;">&quot;hola&quot;</span><span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;house&quot;</span>, <span style="color: #FF0000;">&quot;casa&quot;</span><span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;water&quot;</span>, <span style="color: #FF0000;">&quot;agua&quot;</span><span style="color: #008000;">&#125;</span>, 
      <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;roof&quot;</span>, <span style="color: #FF0000;">&quot;techo&quot;</span><span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;chair&quot;</span>, <span style="color: #FF0000;">&quot;silla&quot;</span><span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;loudspeaker&quot;</span>, <span style="color: #FF0000;">&quot;bocina&quot;</span><span style="color: #008000;">&#125;</span>, 
      <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;screen&quot;</span>, <span style="color: #FF0000;">&quot;pantalla&quot;</span><span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #FF0000;">&quot;money&quot;</span>, <span style="color: #FF0000;">&quot;dinero&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> n, correct<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>, wrong<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Numero de palabras?: &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> nofw<span style="color: #008080;">;</span>
    <span style="color: #666666;">// obtener 'nofw' palabras aleatoriamente de nuestra matriz</span>
    <span style="color: #0000ff;">int</span> num<span style="color: #008000;">&#91;</span>nofw<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>nofw<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">do</span>
            n <span style="color: #000080;">=</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> N<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>checkrep<span style="color: #008000;">&#40;</span>n, num<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> n<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> words<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; -&gt; &quot;</span> <span style="color: #000080;">&lt;&lt;</span> words<span style="color: #008000;">&#91;</span>n<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">system</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;cls&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #666666;">// preguntar el significado de cada palabra</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span>nofw<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> words<span style="color: #008000;">&#91;</span>num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; = &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> ans<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>ans <span style="color: #000080;">==</span> words<span style="color: #008000;">&#91;</span>num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            correct<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&gt;&gt; Bien :)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span>
        <span style="color: #008000;">&#123;</span>
            wrong<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;&gt;&gt; Mal :(<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Tuviste un &quot;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span>correct <span style="color: #000040;">/</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">float</span><span style="color: #008000;">&#41;</span>nofw <span style="color: #000040;">*</span> <span style="color: #0000dd;">100</span> <span style="color: #000080;">&lt;&lt;</span> 
                <span style="color: #FF0000;">&quot;% de respuestas correctas.&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Como dije antes, es sólo un bosquejo, pero tal vez sea el punto de partida para un buen proyecto. Para optimizarlo creo que se deberían agregar algunas cosas:</p>
<ul>
<li>Obtener los pares de palabras desde una base de datos</li>
<li>Ordenarlas por categorías</li>
<li>Que no importe si la respuesta está en mayúsculas o minúsculas</li>
<li>Mejorar la interacción con el usuario</li>
</ul>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/633-aprender-ingles-%c2%bf%c2%a1en-c">Permalink</a> |
<a href="http://codigoc.org/633-aprender-ingles-%c2%bf%c2%a1en-c#comments">11 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/hLCi68AU4Fo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/633-aprender-ingles-%c2%bf%c2%a1en-c/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://codigoc.org/633-aprender-ingles-%c2%bf%c2%a1en-c</feedburner:origLink></item>
		<item>
		<title>Números aleatorios sin repetir en C++</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/Lx-g4KFlwpY/627-numeros-aleatorios-sin-repetir-en-c</link>
		<comments>http://codigoc.org/627-numeros-aleatorios-sin-repetir-en-c#comments</comments>
		<pubDate>Sat, 02 Apr 2011 02:45:25 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[bool]]></category>
		<category><![CDATA[rand]]></category>
		<category><![CDATA[while]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=627</guid>
		<description><![CDATA[Ya hemos visto cómo obtener número aleatorios en c++, pero tarde o temprano surge la necesidad de generar números aleatorios pero sin que se repitan. Podría ser útil para varias cosas como por ejemplo, ordenar de manera aleatoria elementos de una lista, formas grupos aleatorios, barajear cartas, etc. Así que en algoritmo quedaría algo así: [...]]]></description>
			<content:encoded><![CDATA[<p>Ya hemos visto cómo obtener <a href="http://codigoc.org/354-obtener-numeros-aleatorios-en-c-rand-srand">número aleatorios en c++</a>, pero tarde o temprano surge la necesidad de <strong>generar números aleatorios pero sin que se repitan</strong>. Podría ser útil para varias cosas como por ejemplo, ordenar de manera aleatoria elementos de una lista, formas grupos aleatorios, barajear cartas, etc. Así que en algoritmo quedaría algo así:</p>
<ol>
<li>Crear una matriz y dejarla vacía.</li>
<li>Obtener un número aleatorio.</li>
<li>Checar si ese número existe en la matriz. Si si, regresar al paso 2. Si no, guardar el numero en la matriz.</li>
</ol>
<p>Así que el programa es este:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;cstdlib&gt;</span>
<span style="color: #339900;">#include&lt;ctime&gt;</span>
<span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> checkrep<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> n, <span style="color: #0000ff;">int</span> num<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>n <span style="color: #000080;">==</span> num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> n, num<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">do</span>
            n <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>checkrep<span style="color: #008000;">&#40;</span>n, num<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> n<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> num<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;  &quot;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Primero que nada, &lt;cstdlib&gt; es la forma correcta de incluir la librería &lt;stdlib.h&gt; en c++, igual con &lt;ctime&gt;.</p>
<p>Como ven, usamos una <strong>función bool</strong> (<em>que retorna falso o verdadero</em>) para checar si el número aleatorio ya existe en nuestra matriz. Si cualquiera de los números es igual a nuestro número aleatorio <strong>n</strong>, la función retorna <em>true</em>, por lo que el ciclo <em>do-while</em> desde el cual llamamos la función tendrá que repetirse una vez más y hasta que la función retorne <em>false</em>.</p>
<p>Cuando se logra salir del ciclo do-while, guardamos nuestra n en el lugar de la matriz que corresponda, lo mostramos en pantalla y continuámos con la siguiente posición de la matriz.</p>
<p>Otra cosa. Para decirnos si un número está repetido en la matriz, la función <strong>checkrep()</strong> necesita obviamente dos cosas, el número aleatorio y la matriz. Así que le <a href="http://codigoc.org/530-como-pasar-una-matriz-o-arreglo-como-parametro-a-una-funcion">enviámos como parámetros nuestra matriz</a> y nuestro número aleatorio.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/627-numeros-aleatorios-sin-repetir-en-c">Permalink</a> |
<a href="http://codigoc.org/627-numeros-aleatorios-sin-repetir-en-c#comments">5 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/Lx-g4KFlwpY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/627-numeros-aleatorios-sin-repetir-en-c/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://codigoc.org/627-numeros-aleatorios-sin-repetir-en-c</feedburner:origLink></item>
		<item>
		<title>Devolver cambio en C++</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/SxnsUZ-W3xE/611-devolver-cambio-en-c</link>
		<comments>http://codigoc.org/611-devolver-cambio-en-c#comments</comments>
		<pubDate>Fri, 11 Feb 2011 02:39:46 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[if else]]></category>
		<category><![CDATA[while]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=611</guid>
		<description><![CDATA[¿Han pagado algún recibo en una máquina automática y se han preguntado cómo será el programa que les devuelve el cambio? Lo más seguro es que no, pero pues resulta que el profesor de Miguel Ángel le encargó un programa que simulara eso, en sus propias palabras: &#8220;Supón que una maquina de monedas de 10,5,2,1 [...]]]></description>
			<content:encoded><![CDATA[<p>¿Han pagado algún recibo en una máquina automática y se han preguntado cómo será el programa que les devuelve el cambio? Lo más seguro es que no, pero pues resulta que el profesor de Miguel Ángel le encargó un programa que simulara eso, en sus propias palabras:</p>
<p>&#8220;<em>Supón que una maquina de monedas de 10,5,2,1 y 50 centavos. Debes escribir un programa que decida cuantas monedas dará de cambio, dando prioridad las de mayor denominación. Debe recibir como entrada la cantidad de dar a cambio. Ejemplo: para $40.50 será 4 de 10, 0 de 5, 0 de 2, 0 de 1 y 1 de 50 centavos.</em>&#8221;<br />
<a href="http://codigoc.org/wp-content/uploads/2011/02/cambio.jpg"><img src="http://codigoc.org/wp-content/uploads/2011/02/cambio.jpg" alt="Cambio" title="Cambio" width="440" height="209" class="aligncenter size-full wp-image-614" /></a><br />
Así que el primer problema se presenta con la entrada ¿cómo hacer que el usuario introduzca sólo cantidades como $50 o $43.50 y no cantidades como $23.45 o $9.70?</p>
<p>Lo que se me ocurrió es pedir la cantidad en una variable float (<em>cambio</em>), en otra variable convertir esa cantidad a tipo int (<em>cambioint</em>). En ese proceso se perderá la parte decimal del número, así que si la resta <em>cambio &#8211; cambioint</em> es igual a 0 o igual a 0.50, la cantidad que introdujo el usuario es correcta.</p>
<p>La otra parte es sencilla, sólo se necesita ver si la variable <em>cambio</em> es mayor que 10. Si si,  se le restan 10 a la variable y se aumenta un contador. Luego se hace lo mismo con las demás monedas.</p>
<p>Pero bueno, el programa es este:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">float</span> cambio<span style="color: #008080;">;</span> <span style="color: #0000ff;">int</span> cambioint, m10<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>, m5<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>, m2<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>, m1<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>, m50c<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">do</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Cambio?: &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> cambio<span style="color: #008080;">;</span>
        cambioint <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span>cambio<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>cambio <span style="color: #000040;">-</span> cambioint<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #008000;">&#40;</span>cambio <span style="color: #000040;">-</span> cambioint<span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color:#800080;">0.50</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>cambio <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cambio<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m10<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            cambio<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cambio<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">5</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m5<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            cambio<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cambio<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m2<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            cambio<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cambio<span style="color: #000080;">&gt;=</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m1<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            cambio<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>cambio<span style="color: #000080;">&gt;=</span><span style="color:#800080;">0.5</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            m50c<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
            cambio<span style="color: #000040;">-</span><span style="color: #000080;">=</span><span style="color:#800080;">0.5</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> m10 <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> m5 <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> m2 <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> m1 <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;, &quot;</span> <span style="color: #000080;">&lt;&lt;</span> m50c<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Solo le faltaría darle un poco de formato a la salida, pero pues ya es cuestión de gustos y ya saben, cualquier duda aquí están los comentarios abajo.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/611-devolver-cambio-en-c">Permalink</a> |
<a href="http://codigoc.org/611-devolver-cambio-en-c#comments">11 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/SxnsUZ-W3xE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/611-devolver-cambio-en-c/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://codigoc.org/611-devolver-cambio-en-c</feedburner:origLink></item>
		<item>
		<title>Cuando inicializar las variables en cero y cuando no</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/oNiqwPJ-XEE/602-cuando-inicializar-las-variables-en-cero-y-cuando-no</link>
		<comments>http://codigoc.org/602-cuando-inicializar-las-variables-en-cero-y-cuando-no#comments</comments>
		<pubDate>Sat, 15 Jan 2011 04:33:52 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Curso]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=602</guid>
		<description><![CDATA[Cuando empecé a aprender a programar en c++ (creo que uno nunca acaba) me enseñaron que siempre hay que inicializar en cero o en algún otro valor las variables, porque si no, pueden tomar valores raros que podrían arruinar nuestro programa. Vamos a ver si la leyenda es cierta: 1 2 3 4 5 6 [...]]]></description>
			<content:encoded><![CDATA[<p>Cuando empecé a aprender a <strong>programar en c++</strong> (<em>creo que uno nunca acaba</em>) me enseñaron que siempre hay que inicializar en cero o en algún otro valor las variables, porque si no, pueden tomar valores raros que podrían arruinar nuestro programa.</p>
<p>Vamos a ver si la leyenda es cierta:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> var1<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> var1<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Pues con mi compilador sí lo es, una variable sin inicializar toma valores raros. Pero si se han fijado en mis programas, yo no siempre inicializo mis variables, ¿por qué? Pues porque no siempre es necesario.</p>
<p>La regla es bastante simple: &#8220;<em>Si la primera instrucción en la que usamos nuestra variable es de asignación, no es necesario inicializarla.</em>&#8221; Por ejemplo:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> var1<span style="color: #008080;">;</span> <span style="color: #666666;">// no es necesario inicializarla</span>
var2 <span style="color: #000080;">=</span> <span style="color: #0000dd;">14</span><span style="color: #008080;">;</span> <span style="color: #666666;">// no es necesario inicializarla</span>
<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> var3<span style="color: #008080;">;</span> <span style="color: #666666;">// es necesario inicializarla</span>
var4 <span style="color: #000080;">=</span> <span style="color: #0000dd;">cos</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">45</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// no es necesario inicializarla</span>
var5<span style="color: #000040;">++</span><span style="color: #008080;">;</span> <span style="color: #666666;">// es necesario inicializarla</span>
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>var6 <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// es necesario inicializarla</span>
<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">getline</span><span style="color: #008000;">&#40;</span>var7<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// no es necesario inicializarla</span></pre></div></div>

<p>Podemos seguir inicializando todas las variables que usemos, pero si únicamente inicializamos las necesarias vamos a dar la impresión de que sabemos lo que estamos haciendo XD</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/602-cuando-inicializar-las-variables-en-cero-y-cuando-no">Permalink</a> |
<a href="http://codigoc.org/602-cuando-inicializar-las-variables-en-cero-y-cuando-no#comments">16 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/oNiqwPJ-XEE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/602-cuando-inicializar-las-variables-en-cero-y-cuando-no/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<feedburner:origLink>http://codigoc.org/602-cuando-inicializar-las-variables-en-cero-y-cuando-no</feedburner:origLink></item>
		<item>
		<title>Simular una Progress Bar en C++</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/4xtE_io5VyI/596-simular-una-progress-bar-en-c</link>
		<comments>http://codigoc.org/596-simular-una-progress-bar-en-c#comments</comments>
		<pubDate>Thu, 13 Jan 2011 08:29:36 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[time]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=596</guid>
		<description><![CDATA[Una progress bar es la barrita, generalmente verde, que aparece en sistemas operativos e infinidad de programas cuando algo se está cargando. Hoy no nos vamos a meter en asuntos de cargar archivos o algo, únicamente vamos a ver el principio básico sobre el que operan estas barras. Nuestro programa sólo pedirá una cantidad en [...]]]></description>
			<content:encoded><![CDATA[<p>Una <strong>progress bar</strong> es la barrita, generalmente verde, que aparece en sistemas operativos e infinidad de programas cuando algo se está cargando. Hoy no nos vamos a meter en asuntos de cargar archivos o algo, únicamente vamos a ver el principio básico sobre el que operan estas barras.</p>
<p>Nuestro programa sólo pedirá una cantidad en segundos, que será el tiempo que durará en llenarse nuestra progress bar. A la ventana donde se ejecutan nuestros programas le caben 80 caracteres de largo, así que vamos a decir que nuestra progress bar tiene una resolución de 80. El único problema es saber cuánto durará cada unidad de esas 80 para que en total se acumule el tiempo indicado.</p>
<h3>La función delay() o Sleep()</h3>
<p>Si en su IDE pueden usar la librería <em>conio.h</em>, entonces van a usar la función <strong>delay()</strong>. Si no, vamos a tener que agregar la librería <em>windows.h</em> y usar la función <strong>Sleep()</strong> (<em>así con mayúscula</em>).</p>
<p>Ambas funciones hacen exactamente lo mismo: paran el programa durante una cierta cantidad de tiempo en milisegundos. Por ejemplo:<br />
<code>cout << "Hola" << endl;<br />
Sleep(2000);<br />
cout << "Hace dos segundos te saludé!";</code><br />
Se muestra 'Hola', se pausa 2 segundos y se muestra 'Hace dos segundos te saludé!'</p>
<p>Para los que usan conio.h, sería algo así:<br />
<code>printf("Hola\n");<br />
delay(2000);<br />
printf("Hace dos segundos te saludé!");</code></p>
<p>Ahora sí, para saber cuanto tiempo durará cada unidad de nuestra barra tenemos primero que convertir los segundos a milisegundos (<em>s*1000</em>), luego dividirlos entre la resolución de la barra (<em>s*1000/80</em>). Así que nuestro programa queda así:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;windows.h&gt;</span>
<span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> s<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Segundos?: &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> s<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">79</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;_&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">79</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;=&quot;</span><span style="color: #008080;">;</span>
        Sleep<span style="color: #008000;">&#40;</span>s<span style="color: #000040;">*</span><span style="color: #0000dd;">1000</span><span style="color: #000040;">/</span><span style="color: #0000dd;">80</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>El primer ciclo es solo por estética, para ver hasta donde va a llenar la barra. El segundo es el que imprime la barra con sus respectivas pausas.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/596-simular-una-progress-bar-en-c">Permalink</a> |
<a href="http://codigoc.org/596-simular-una-progress-bar-en-c#comments">10 comentarios</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/4xtE_io5VyI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/596-simular-una-progress-bar-en-c/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		<feedburner:origLink>http://codigoc.org/596-simular-una-progress-bar-en-c</feedburner:origLink></item>
		<item>
		<title>Saber si es palíndromo o no, de forma recursiva 2da parte</title>
		<link>http://feedproxy.google.com/~r/codigoc/~3/7UjJ-h7E7u8/590-saber-si-es-palindromo-o-no-de-forma-recursiva-2da-parte</link>
		<comments>http://codigoc.org/590-saber-si-es-palindromo-o-no-de-forma-recursiva-2da-parte#comments</comments>
		<pubDate>Tue, 11 Jan 2011 00:38:10 +0000</pubDate>
		<dc:creator>THEbatzuk</dc:creator>
				<category><![CDATA[Programas]]></category>
		<category><![CDATA[cadenas]]></category>
		<category><![CDATA[string.h]]></category>

		<guid isPermaLink="false">http://codigoc.org/?p=590</guid>
		<description><![CDATA[Pues la idea que se me ocurrió en el otro post resultó ser más sencilla de lo que me imaginé, y ahora sí la función recursiva se ve mucho mejor, ya es mucho más que un ciclo disfrazado. Comenté extensivamente el código, así que aquí está: 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>Pues la idea que se me ocurrió en el otro post resultó ser más sencilla de lo que me imaginé, y ahora sí la función recursiva se ve mucho mejor, ya es mucho más que un ciclo disfrazado.</p>
<p>Comenté extensivamente el código, así que aquí está:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;string.h&gt;</span>
<span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> len, n<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
string chk4palindrosity<span style="color: #008000;">&#40;</span>string thestr<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>thestr<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> thestr<span style="color: #008000;">&#91;</span>thestr.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// comparar primer caracter con ultimo</span>
    <span style="color: #008000;">&#123;</span>
        n<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>n <span style="color: #000080;">==</span> len <span style="color: #000040;">/</span> <span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">// si el numero de veces que la comparación ha sido cierta es...</span>
            <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;Si es palindromo!&quot;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// igual a la mitad de los caracters, es palindromo</span>
        thestr.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// borramos primer caracter</span>
        thestr.<span style="color: #007788;">erase</span><span style="color: #008000;">&#40;</span>thestr.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// borramos ultimo</span>
        <span style="color: #0000ff;">return</span> chk4palindrosity<span style="color: #008000;">&#40;</span>thestr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// llamamos a la función con el string recortado</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">else</span> <span style="color: #666666;">// si una de las comparaciones no es cierta, no es palíndromo</span>
        <span style="color: #0000ff;">return</span> <span style="color: #FF0000;">&quot;No es palindromo&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">char</span> inputf<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">50</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span><span style="color: #008000;">&#123;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#125;</span>, input<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">50</span><span style="color: #008000;">&#93;</span>, <span style="color: #000040;">*</span>parte<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Introduce un palindromo: &quot;</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">getline</span><span style="color: #008000;">&#40;</span>input, <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    parte <span style="color: #000080;">=</span> <span style="color: #0000dd;">strtok</span><span style="color: #008000;">&#40;</span>input, <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>                 <span style="color: #666666;">//</span>
    <span style="color: #0000dd;">strcat</span><span style="color: #008000;">&#40;</span>inputf, parte<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>                     <span style="color: #666666;">//</span>
    <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>parte <span style="color: #000080;">=</span> <span style="color: #0000dd;">strtok</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span>, <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span> <span style="color: #666666;">//</span>
        <span style="color: #0000dd;">strcat</span><span style="color: #008000;">&#40;</span>inputf, parte<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>               <span style="color: #666666;">// quitar espacios del array</span>
&nbsp;
    string thestr<span style="color: #008000;">&#40;</span>inputf<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// convertir array en string para facilitar operaciones</span>
    len <span style="color: #000080;">=</span> thestr.<span style="color: #007788;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// obtener longuitud del string</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>len <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Si es palindromo!&quot;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">else</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> chk4palindrosity<span style="color: #008000;">&#40;</span>thestr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// llamar a la funcion e imprimir lo que retorne</span>
    <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">get</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Lo que hace la función recursiva es comparar el primer caracter con el último, si son iguales recortarlos y llamarse; si no, no es palíndromo. Cualquier duda ya saben que los comentarios están abiertos.</p>
<hr />
<p><small>Post escrito en <a href="http://codigoc.org">Código C++</a> © 2011. |
<a href="http://codigoc.org/590-saber-si-es-palindromo-o-no-de-forma-recursiva-2da-parte">Permalink</a> |
<a href="http://codigoc.org/590-saber-si-es-palindromo-o-no-de-forma-recursiva-2da-parte#comments">Un comentario</a>
</small></p><img src="http://feeds.feedburner.com/~r/codigoc/~4/7UjJ-h7E7u8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://codigoc.org/590-saber-si-es-palindromo-o-no-de-forma-recursiva-2da-parte/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://codigoc.org/590-saber-si-es-palindromo-o-no-de-forma-recursiva-2da-parte</feedburner:origLink></item>
	</channel>
</rss>

