El programa describe la funcionalidad básica de un semáforo de dos tiempos solamente ejemplificando la lógica del mismo.
El programa que a continuación se ve se puede copiar y pegar en un compilador de Borlald C++ 3.0,
una ves copiado se procede a su compilación no debiendo tener ninguna complicación.
el compilador transforma el código fuente de extension .CPP a extension .EXE
jueves, 7 de noviembre de 2013
Ejemplo de un programa en fuente
Ejemplo programa de semáforo.
#include<CONIO.h>
#include<DOS.h>
#include<stdlib.h>
#include<stdio.h>
#define ESCAPE 0x1b
int tiempo1=2;
int tiempo2=500;
int tiempo3=3;
int ra2=1;
int rb3=1;
int ra1=0;
int rb5=0;
int no();
int led_rojo();
int led_verde();
int led_amarillo();
int semaforo();
int sema1verde();
int sema2amarillo();
int sema3rojo();
int sema4verde();
int sema5amarillo();
int sema6rojo();
int sema7verde();
int sema8amarillo();
int sema9rojo();
char opc;
void main(void)
{
clrscr();
textcolor(15);
textbackground(0);
gotoxy(10,10);
textcolor(15);
cprintf("Oprima pulsador");
gotoxy(10,14);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("A ");
textcolor(15);
cprintf("= enciende led verde");
gotoxy(10,16);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("B ");
textcolor(15);
cprintf("= enciende led amarillo");
gotoxy(10,18);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("C ");
textcolor(15);
cprintf("= enciende led rojo");
gotoxy(10,20);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("S ");
textcolor(15);
cprintf("= SEMAFORO");
gotoxy(10,24);
textcolor(12);
cprintf("EXIT = ESC");
opc=getch();
switch(opc)
{
case'a':
case'A':
led_verde();
break;
case'b':
case'B':
led_amarillo();
break;
case'c':
case'C':
led_rojo();
break;
case's':
case'S':
semaforo();
break;
default:
no();
break;
}
}
led_rojo()
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
getch();
}
main();
}
return(0);
}
led_verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema1verde()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(10);
clrscr();
sleep(tiempo1);
}
sema2amarillo();
}
exit(0);
}
return(0);
}
led_amarillo()
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
getch();
}
main();
}
return(0);
}
no()
{
if(opc==ESCAPE)
{
clrscr();
gotoxy(10,12);
cprintf("Gracias por utilizar el rograma");
getch();
exit(0);
}
clrscr();
gotoxy(10,10);
cprintf("USTED OPRIMIO UNA TECLA INCORECTA");
gotoxy(10,12);
cprintf("EL PROGRAMA SE CERRARA");
getch();
exit(0);
return(0);
}
semaforo()
{
clrscr();
window(20,10,40,11);
sema1verde();
return(0);
}
led_1verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema2amarillo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
delay(tiempo2);
}
sema3rojo();
}
exit(0);
}
return(0);
}
sema3rojo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
sleep(tiempo3);
}
sema4verde();
}
exit(0);
}
return(0);
}
led2_rojo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema4verde()
{
window(30,10,40,11);
clrscr();
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(10);
clrscr();
sleep(tiempo1);
}
sema5amarillo();
}
exit(0);
}
return(0);
}
led3_amarillo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema5amarillo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
delay(tiempo2);
}
sema6rojo();
}
exit(0);
}
return(0);
}
led5_verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema6rojo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
sleep(tiempo3);
}
sema7verde();
}
exit(0);
}
return(0);
}
led8_verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema7verde()
{
window(40,10,80,11);
clrscr();
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(10);
clrscr();
sleep(tiempo1);
}
sema8amarillo();
}
exit(0);
}
return(0);
}
led_3amarillo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema8amarillo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
delay(tiempo2);
}
sema9rojo();
}
exit(0);
}
return(0);
}
sema9rojo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
sleep(tiempo3);
}
clrscr();
sema1verde();
}
exit(0);
}
return(0);
}
led_6rojo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
#include<DOS.h>
#include<stdlib.h>
#include<stdio.h>
#define ESCAPE 0x1b
int tiempo1=2;
int tiempo2=500;
int tiempo3=3;
int ra2=1;
int rb3=1;
int ra1=0;
int rb5=0;
int no();
int led_rojo();
int led_verde();
int led_amarillo();
int semaforo();
int sema1verde();
int sema2amarillo();
int sema3rojo();
int sema4verde();
int sema5amarillo();
int sema6rojo();
int sema7verde();
int sema8amarillo();
int sema9rojo();
char opc;
void main(void)
{
clrscr();
textcolor(15);
textbackground(0);
gotoxy(10,10);
textcolor(15);
cprintf("Oprima pulsador");
gotoxy(10,14);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("A ");
textcolor(15);
cprintf("= enciende led verde");
gotoxy(10,16);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("B ");
textcolor(15);
cprintf("= enciende led amarillo");
gotoxy(10,18);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("C ");
textcolor(15);
cprintf("= enciende led rojo");
gotoxy(10,20);
textcolor(15);
cprintf("tecla ");
textcolor(12);
cprintf("S ");
textcolor(15);
cprintf("= SEMAFORO");
gotoxy(10,24);
textcolor(12);
cprintf("EXIT = ESC");
opc=getch();
switch(opc)
{
case'a':
case'A':
led_verde();
break;
case'b':
case'B':
led_amarillo();
break;
case'c':
case'C':
led_rojo();
break;
case's':
case'S':
semaforo();
break;
default:
no();
break;
}
}
led_rojo()
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
getch();
}
main();
}
return(0);
}
led_verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema1verde()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(10);
clrscr();
sleep(tiempo1);
}
sema2amarillo();
}
exit(0);
}
return(0);
}
led_amarillo()
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
getch();
}
main();
}
return(0);
}
no()
{
if(opc==ESCAPE)
{
clrscr();
gotoxy(10,12);
cprintf("Gracias por utilizar el rograma");
getch();
exit(0);
}
clrscr();
gotoxy(10,10);
cprintf("USTED OPRIMIO UNA TECLA INCORECTA");
gotoxy(10,12);
cprintf("EL PROGRAMA SE CERRARA");
getch();
exit(0);
return(0);
}
semaforo()
{
clrscr();
window(20,10,40,11);
sema1verde();
return(0);
}
led_1verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema2amarillo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
delay(tiempo2);
}
sema3rojo();
}
exit(0);
}
return(0);
}
sema3rojo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
sleep(tiempo3);
}
sema4verde();
}
exit(0);
}
return(0);
}
led2_rojo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema4verde()
{
window(30,10,40,11);
clrscr();
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(10);
clrscr();
sleep(tiempo1);
}
sema5amarillo();
}
exit(0);
}
return(0);
}
led3_amarillo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema5amarillo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
delay(tiempo2);
}
sema6rojo();
}
exit(0);
}
return(0);
}
led5_verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema6rojo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
sleep(tiempo3);
}
sema7verde();
}
exit(0);
}
return(0);
}
led8_verde()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema7verde()
{
window(40,10,80,11);
clrscr();
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(10);
clrscr();
sleep(tiempo1);
}
sema8amarillo();
}
exit(0);
}
return(0);
}
led_3amarillo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
sema8amarillo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(6);
clrscr();
delay(tiempo2);
}
sema9rojo();
}
exit(0);
}
return(0);
}
sema9rojo()
{
while(!kbhit())
{
ra2=0;
if(ra2==0)
{
ra1=1;
if(ra1==1)
{
clrscr();
textbackground(4);
clrscr();
sleep(tiempo3);
}
clrscr();
sema1verde();
}
exit(0);
}
return(0);
}
led_6rojo()
{
rb3=0;
if(rb3==0)
{
rb5=1;
if(rb5==1)
{
clrscr();
textbackground(10);
clrscr();
getch();
}
main();
}
return(0);
}
Suscribirse a:
Entradas (Atom)