Bem esse é um dos programinhas que eu faço para criar pratica na linguagem C e C++ ///////////////////////////////////////////////////////////////////////////////////////////////////// /* Name: Rev Version: 2.5 Copyright: Soft Author: Alex Silva Torres Mail: zotweb@gmail.com MSN: xzot@hotmail.com Date: 25/01/05 21:15 Description: Software para reverter string */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.c> #include <ctype.h> #define NULL 0 #define BACKSPACE 8 #define UP 72 #define DOWN 80 #define ENTER 13 #define DIGIT 60 //variaveis globais int countfunc; int _menu(char*, const int,const int,const int); int countchar(const char*,char*,int,int,int); void create_dir(void); void fremove(void); void fsave(char*); char* input(const char*,int,int); void shbgn(int, int); void sherr(const char*); void fshow(int,int); void shtitle(int); void shby(int, int); void wrev(const char*, char*, int); void shline(int); char* strprp(char*); void shfield(int); /*********************************************************/ /* Função para exibir mensagens */ /*********************************************************/ void sherr(const char *msg) { int x, y; x = wherex(); y = wherey(); textbackground(7); textcolor(4); gotoxy(27,3); cprintf("[ %s ]" ,msg); gotoxy(x,y); textbackground(0); textcolor(15); } void shbgn(int col, int line) { int x, y; x = wherex(); y = wherey(); gotoxy(col,line); cprintf("********************************************************\n"); gotoxy(col,(line+1)); cprintf("* REV - 2.5 INICIADO *\n"); gotoxy(col,(line+2)); cprintf("********************************************************\n"); gotoxy(x,y); } void shtitle(int col) { int x, y; x = wherex(); y = wherey(); textbackground(1); gotoxy(1,1); cprintf(" Rev "); textbackground(0); } void shby(int col, int line) { int x, y; x = wherex(); y = wherey(); gotoxy(col,line); cprintf("Autor: Alex Silva Torres"); gotoxy(col,(line+1)); cprintf("E-mail: zotweb@gmail.com"); gotoxy(x,y); } void shline(int line) { int x, y; x = wherex(); y = wherey(); textcolor(7); gotoxy(1,line); cprintf("____Strings Salvas______________________________________________________________"); gotoxy(x,y); textcolor(15); } void shfield(int line) { int x, y; x = wherex(); y = wherey(); textcolor(7); gotoxy(1,line); cprintf("+------------------------------------------------------------------------------+"); gotoxy(1,line+1); cprintf("|"); gotoxy(80,line+1); cprintf("|"); gotoxy(1,line+2); cprintf("+------------------------------------------------------------------------------+"); gotoxy(x,y); textcolor(15); } /*********************************************************/ /* FIM Função para exibir mensagens */ /*********************************************************/ /*********************************************************/ /* Função para exibição input */ /*********************************************************/ //função para fazer contagem de caracteres int countchar(const char *prompt, char *string, int len, int col, int line) { int x, y, caracteres; caracteres = (len - (strlen(string))); x = wherex(); y = wherey(); gotoxy(col,line); textcolor(7); textbackground(1); cprintf(prompt); textcolor(10); cprintf("%d ",caracteres); textcolor(15); textbackground(0); gotoxy(x,y); } //função para exibição da caixa de edição char *input(const char *prompt, int len, int opc) { static char buffer[128]; int i = 0; int x = 55; int y = 1; char letra; char tecla; //apenas para começar do 60 normal for(i=0; i < (strlen(buffer)) ; i++) { buffer[i++] = ' '; } for(i=(strlen(buffer)); i >= 0 ; i--) { buffer = NULL; //apaga o * anterior putchar(BACKSPACE); putchar(' '); putchar(BACKSPACE); } i = 0; countchar("Digitos restantes: ",buffer,len,x,y); gotoxy(2,5); textcolor(7); printf(prompt); textcolor(15); while(letra != '\r') { here: if(i == len) { tecla = getch(); if(tecla == '\r') { break; } else { if(tecla == BACKSPACE) { buffer[--i] = NULL; //apaga o * anterior putchar(BACKSPACE); putchar(' '); putchar(BACKSPACE); countchar("Digitos restantes: ",buffer,len,x,y); } else { putchar(7); goto here; } } } digita: letra = getch(); switch(opc) { case 1: letra = toupper(letra); break; case 2: letra = tolower(letra); break; default: letra = letra; } if(letra == BACKSPACE) { if(i > 0) { buffer[--i] = NULL; //apaga o * anterior putchar(BACKSPACE); putchar(' '); putchar(BACKSPACE); countchar("Digitos restantes: ",buffer,len,x,y); } else { putchar(7); } } else if(letra != '\r') { buffer[i++] = letra; putchar(letra); countchar("Digitos restantes: ",buffer,len,x,y); } if((letra == '\r') && (i == 0)) { goto digita; } } buffer = NULL; return (buffer); } //mostra palavra ao contrario void wrev(const char *prompt, char *str, int line) { gotoxy(2,line); textcolor(7); cprintf(prompt); str = strrev(str); textcolor(15); cprintf(str); textcolor(15); } //mostra o numero de caracteres da string void shlen(const char *prompt, char *str, int line) { int len; len = strlen(str); gotoxy(2,line); textcolor(7); cprintf(prompt); textcolor(10); cprintf("%d ", len); textcolor(15); cprintf("Caractere(s)"); textcolor(15); } char *strprp(char *str) { char strsave[256]; char *inv; strcpy(strsave,str); inv = strrev(str); strcat(strsave," - "); strcat(strsave,inv); return(strsave); } /*********************************************************/ /* FIM Função para exibição input */ /*********************************************************/ /*********************************************************/ /* Função para exibição do menu */ /*********************************************************/ int _menu(char *cmenu[], const int col, const int line, const int qnt) { int mline, tecla; int size;// sizeof(cmenu)/sizeof(cmenu[0]); size = (qnt - 1); textcolor(0); textbackground(7); for(mline=1;mline<=size;mline++) { gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); } mline=0; textbackground(1); textcolor(15); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); textbackground(0); do { tecla=getch(); if((tecla == DOWN) && (mline <= size)) { textbackground(7); textcolor(0); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); mline++; textbackground(1); textcolor(15); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); } if((tecla == UP) && (mline > -1)) { textbackground(7); textcolor(0); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); mline--; textbackground(1); textcolor(15); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); } if((tecla == DOWN) && (mline == (size + 1))) { textbackground(0); textcolor(0); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); mline = 0; textbackground(1); textcolor(15); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); } if((mline == -1) && (tecla == UP)) { textbackground(0); textcolor(0); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); mline = (qnt - 1); textbackground(1); textcolor(15); gotoxy(col,mline+line); cprintf("%s", cmenu[mline]); } textbackground(0); } while(tecla != ENTER); return(mline); } /*********************************************************/ /* FIM Função para exibição do menu */ /*********************************************************/ /*********************************************************/ /* Função para manuzear arquivo */ /*********************************************************/ //funíão para criar diretorio void create_dir(void) { if(chdir("C:\\REV") == -1) { if(mkdir("C:\\REV") == -1) { clrscr(); sherr("OCORREU UM ERRO GRAVISSIMO.\n DIGITE ALGO PARA SAIR"); getch(); system("EXIT"); } } } //função para criar arquivo e salvar string void fsave(char *str) { FILE *fp; if(fp = fopen("C:\\REV\\rev.txt","a+")) { fputs(str,fp); sherr("STRING SALVA COM SUCESSO"); } else { sherr("ERRO AO SALVAR STRING"); } fclose(fp); } //funíão para abrir um arquivo chamado rev.txt void fshow(int col, int line) { char lines[1024]; FILE *fp; int letra; if(fp = fopen("C:\\REV\\rev.txt","r")) { clrscr();//limpa a tela while((letra = fgetc(fp)) != EOF) { if(letra == '\n') { col = 4; line++; } gotoxy(col,line); putch(letra); col++; } } else { sherr("ERRO AO ABRIR ARQUIVO"); } fclose(fp); } //funíão para remoVer arquivo void fremove(void) { if(remove("C:\\REV\\rev.txt") == -1) { sherr("ERRO AO EXCLUIR ARQUIVO"); } else { sherr("ARQUIVO EXCLUIDO"); } } /*********************************************************/ /* FIM Função para manuzear arquivo */ /*********************************************************/ /*********************************************************/ /* Função principal */ /*********************************************************/ int main() { int menuopc;//serve para todas as partes /*********************************************************/ /* variveis para uso primeira parte */ /*********************************************************/ int tecla; char *cmenuone[5] = { " > Digitar uma String < ", " > Abrir Arquivo de String < ", " > Deletar Arquivo de String < ", " > Sobre < ", " > Sair < ", }; /*********************************************************/ /* variveis para uso segunda parte */ /*********************************************************/ char *str = new char[512], *inv = new char[512]; char *strsv = new char[512]; char *cmenutwo[6] = { " > Digitar uma Nova String < ", " > Salvar String < ", " > Abrir Arquivo de String < ", " > Deletar Arquivo de String < ", " > Sobre < ", " > Sair < ", }; /*********************************************************/ /* variveis para uso parte de arquivo */ /*********************************************************/ char *cmenufile[4] = { " > Digitar uma Nova String < ", " > Deletar Arquivo de String < ", " > Sobre < ", " > Sair < ", }; /*********************************************************/ /* variveis para uso parte Sobre */ /*********************************************************/ char *cmenuabout[4] = { " > Digitar uma String < ", " > Abrir Arquivo de String < ", " > Deletar Arquivo de String < ", " > Sair < ", }; /*********************************************************/ /* Primeira parte do programa */ /*********************************************************/ clrscr(); //limpa a tela first: textcolor(15); shtitle(31);//exibe titulo shbgn(12,10); //exibe abertura shby(2,5); menuopc = _menu(cmenuone,20,17,5); switch(menuopc) { case 0: goto second; break; case 1: goto openfile; break; case 2: fremove(); goto first; break; case 3: goto about; break; case 4: exit(0); break; } /*********************************************************/ /* Segunda parte do programa */ /*********************************************************/ second: while(1 == 1)//faz um loop infinito { clrscr(); shtitle(31);//exibe titulo shfield(4); str = input("Palavra: ",DIGIT,0); strcpy(strsv,str); strcat(strsv," - "); inv = strrev(str); gotoxy(2,8); shfield(7); textcolor(7); cprintf("Palavra Inversa: "); textcolor(15); cprintf("%s",inv); strcat(strsv,inv); shlen("A Palavra Possui: ",str,11); strcat(strsv,"\n"); //rotulo para o menu menu: menuopc = _menu(cmenutwo,2,18,6); switch(menuopc) { case 0: goto second; break; case 1: fsave(strsv); goto menu; break; case 2: goto openfile; break; case 3: fremove(); goto menu; break; case 4: goto about; break; case 5: exit(0); break; } } /*********************************************************/ /* Parte para abrir arquivo */ /*********************************************************/ openfile: clrscr(); fshow(5,13); shtitle(31);//exibe titulo shline(11); menufile: menuopc = _menu(cmenufile,2,5,4); switch(menuopc) { case 0: goto second; break; case 1: fremove(); goto menufile; break; case 2: goto about; break; case 3: exit(0); break; } about: clrscr(); shtitle(31);//exibe titulo //escreve sobre o programa textcolor(15); gotoxy(2,10); cprintf("SOBRE O REV:\n"); shby(2,12); gotoxy(2,15); cprintf("Version: 2.5.0"); gotoxy(2,16); cprintf("Nome: Rev"); menuabout: menuopc = _menu(cmenuabout,2,5,4); switch(menuopc) { case 0: goto second; break; case 1: goto openfile; break; case 2: fremove(); goto menufile; break; case 3: exit(0); break; } return(0); } ///////////////////////////////////////////////////////////////////////////////////////////////// Bem para quem desejar pegar esse codigo para estudar, lembrando esse é um exemplo basico de programação c