Guest tsenart Posted January 12, 2007 at 10:56 PM Report #76013 Posted January 12, 2007 at 10:56 PM Olá pessoal. Aqui está um programa que o meu irmão me pediu para fazer(foi mais pelo desafio que pela necessidade...) Foi o mais complexo programa que desenvolvi até hoje 😁. Opiniões são bem-vindas. /*MADE BY S@M0T. *Started on 09/01/06 *Finished on 12/01/06 *You can only redistribute this source-code as it is(no changes!). *For improvements e-mail me to tsenart@gmail.com */ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio2.h> float saldo, valor,*ptrvalor = &valor; time_t tempo; FILE *user_saldo, *user_movs; void cls(); /*Limpa o buffer, espera que o user pressione alguma tecla e limpa o ecran.*/ float receber_saldo(); /*Lê saldo da stream user_saldo. Se nao encontrar o ficheiro, devolve 0.*/ void imprimir_movs(); /*Lê os movimentos da stream user_movs. Se nao encontrar o ficheiro, escreve Ficheiro nao encontrado.*/ void op_debitar_saldo();/*Debita um valor a introduzir pelo utilizador no saldo.*/ void op_creditar_saldo();/*Credita...*/ void registar_saldo();/*Regista o saldo na stream user_saldo.*/ void registar_mov(char tipo);/*Regista as operações efectuadas(Crédito e Débito), a data e o valor.*/ void cls() { fflush(stdin); getch(); clrscr(); } float receber_saldo() { float saldo_func = 0; if((user_saldo = fopen("fich_saldo.txt","r"))==NULL){textcolor(RED); printf("\nFicheiro nao encontrado.\n"); } else { while(fscanf(user_saldo,"%f", &saldo_func) != EOF); fclose(user_saldo);} return saldo_func; } void imprimir_movs() { char movc_func; if ((user_movs = fopen("fich_movs.txt","r")) == NULL) {textcolor(RED);printf("\nFicheiro nao encontrado.\n");} else { while(fscanf(user_movs,"%c",&movc_func) != EOF) printf("%c",movc_func); fclose(user_movs);} } void op_debitar_saldo() { saldo = receber_saldo(); textcolor(LIGHTBLUE); printf("\nSaldo actual-> %.3f euros\n",saldo); textcolor(YELLOW); printf("Quantia a debitar: "); scanf("%f",&valor); saldo = saldo -valor; textcolor(MAGENTA); printf("Novo saldo-> %.3f euros",saldo); registar_mov('d'); registar_saldo(); } void op_creditar_saldo() { saldo = receber_saldo(); textcolor(LIGHTBLUE); printf("\nSaldo actual-> %.3f euros\n",saldo); textcolor(YELLOW); printf("Quantia a creditar: "); scanf("%f",&valor); saldo = saldo + valor; textcolor(MAGENTA); printf("Novo saldo-> %.3f euros",saldo); registar_mov('c'); registar_saldo(); } void registar_saldo() { user_saldo = fopen("fich_saldo.txt","w"); fprintf(user_saldo,"%.3f",saldo); fclose(user_saldo); } void registar_mov(char tipo) { user_movs = fopen("fich_movs.txt","a+"); tempo = time(NULL); switch(tipo) { case 'd': fprintf(user_movs,"\nData-> %sTipo-> Debito\tValor-> %.3f euros\n\n",asctime(localtime(&tempo)),*ptrvalor); break; case 'c': fprintf(user_movs,"\nData-> %sTipo-> Credito\tValor-> %.3f euros\n\n",asctime(localtime(&tempo)),*ptrvalor); break; default : fprintf(user_movs,"Tipo de movimento invalido"); } fclose(user_movs); } int main() { textbackground(BLACK); textcolor(GREEN); const char *admin_password = "1souirlat"; short opcao; char *passwd; do { textcolor(LIGHTGRAY); gotoxy(30,12); printf("PASSWORD: "); gets(passwd); clrscr(); }while(strcmp(passwd,admin_password)!= 0 && (strcmp(passwd,"default"))!= 0); while (1) { textcolor(LIGHTGREEN); printf("\n\n\t\t\t GESTAO DE DINHEIRO\n\n"); printf("\t\t\t 1-Creditar saldo\n"); printf("\t\t\t 2-Debitar saldo\n"); printf("\t\t\t 3-Consultar saldo\n"); printf("\t\t\t 4-Consultar movimentos\n"); printf("\t\t\t 5-Limpar saldo\n"); printf("\t\t\t 6-Limpar movimentos\n"); printf("\t\t\t 0-Sair\n\n\n"); textcolor(WHITE); printf("Opcao ->"); scanf("%hd",&opcao); switch (opcao) { case 0: textcolor(MAGENTA);printf("\n\nOs inteligentes aprendem com os seus proprios erros.\nOs sabios aprendem com os erros dos inteligentes.",MAGENTA); getch();exit(0); case 1: op_creditar_saldo(); cls(); break; case 2: op_debitar_saldo(); cls(); break; case 3: textcolor(MAGENTA);printf("\n%.3f euros",receber_saldo()); cls(); break; case 4: textcolor(CYAN);imprimir_movs(); cls(); break; case 5: if(remove("fich_saldo.txt")==0) {textcolor(LIGHTRED); printf("\nFicheiro apagado."); cls(); break; } else {textcolor(RED);printf("\nNao foi possivel apagar o ficheiro."); cls(); break;} case 6: if(remove("fich_movs.txt")==0) {textcolor(LIGHTRED); printf("\nFicheiro apagado."); cls(); break; } else {textcolor(RED);printf("\nNao foi possivel apagar o ficheiro."); cls(); break;} default: textcolor(RED);printf("Opcao invalida"); cls(); } } fcloseall(); return 0; } O meu monitor é um 24.... Porque vcs podem nao conseguir ver todo o codigo anexo o ficheiro para o compilarem.
Rui Carlos Posted January 12, 2007 at 11:29 PM Report #76019 Posted January 12, 2007 at 11:29 PM ficam aqui algumas sugestões... -não devias usar o 'gets' e muito menos a biblioteca 'conio2'; -aparentemente está a usar a função 'exit' para sair devias usar um 'return'. a função 'exit' é para ser usada em situações de erro; -não estás a verificar se os 'scanf' são bem sucedidos; -esse 'while(1)' não me agrada muito... Rui Carlos Gonçalves
sandro_G Posted January 19, 2007 at 01:45 AM Report #77208 Posted January 19, 2007 at 01:45 AM Também no meio de umas linhas de código já significativamente boas nem tudo pode estar perfeito... ||I'm the devil25, I'm G and I'm sandro_G||
bertolo Posted February 18, 2007 at 08:08 PM Report #83785 Posted February 18, 2007 at 08:08 PM nem compila no meu. 23.c:11:20: error: conio2.h: No such file or directory 23.c: In function 'cls': 23.c:29: warning: implicit declaration of function 'getch' 23.c:30: warning: implicit declaration of function 'clrscr' 23.c: In function 'receber_saldo': 23.c:38: warning: implicit declaration of function 'textcolor' 23.c:38: error: 'RED' undeclared (first use in this function) 23.c:38: error: (Each undeclared identifier is reported only once 23.c:38: error: for each function it appears in.) 23.c: In function 'imprimir_movs': 23.c:51: error: 'RED' undeclared (first use in this function) 23.c: In function 'op_debitar_saldo': 23.c:64: error: 'LIGHTBLUE' undeclared (first use in this function) 23.c:66: error: 'YELLOW' undeclared (first use in this function) 23.c:69: error: 'MAGENTA' undeclared (first use in this function) 23.c: In function 'op_creditar_saldo': 23.c:81: error: 'LIGHTBLUE' undeclared (first use in this function) 23.c:83: error: 'YELLOW' undeclared (first use in this function) 23.c:86: error: 'MAGENTA' undeclared (first use in this function) 23.c: In function 'main': 23.c:127: warning: implicit declaration of function 'textbackground' 23.c:127: error: 'BLACK' undeclared (first use in this function) 23.c:128: error: 'GREEN' undeclared (first use in this function) 23.c:130: warning: ISO C90 forbids mixed declarations and code 23.c:134: error: 'LIGHTGRAY' undeclared (first use in this function) 23.c:135: warning: implicit declaration of function 'gotoxy' 23.c:138: warning: implicit declaration of function 'strcmp' 23.c:140: error: 'LIGHTGREEN' undeclared (first use in this function) 23.c:149: error: 'WHITE' undeclared (first use in this function) 23.c:155: error: 'MAGENTA' undeclared (first use in this function) 23.c:155: warning: too many arguments for format 23.c:163: error: 'CYAN' undeclared (first use in this function) 23.c:165: error: 'LIGHTRED' undeclared (first use in this function) 23.c:166: error: 'RED' undeclared (first use in this function) 23.c:175: warning: implicit declaration of function 'fcloseall'
sandro_G Posted February 18, 2007 at 09:35 PM Report #83817 Posted February 18, 2007 at 09:35 PM nem compila no meu. 23.c:11:20: error: conio2.h: No such file or directory 23.c: In function 'cls': 23.c:29: warning: implicit declaration of function 'getch' 23.c:30: warning: implicit declaration of function 'clrscr' 23.c: In function 'receber_saldo': 23.c:38: warning: implicit declaration of function 'textcolor' 23.c:38: error: 'RED' undeclared (first use in this function) 23.c:38: error: (Each undeclared identifier is reported only once 23.c:38: error: for each function it appears in.) 23.c: In function 'imprimir_movs': 23.c:51: error: 'RED' undeclared (first use in this function) 23.c: In function 'op_debitar_saldo': 23.c:64: error: 'LIGHTBLUE' undeclared (first use in this function) 23.c:66: error: 'YELLOW' undeclared (first use in this function) 23.c:69: error: 'MAGENTA' undeclared (first use in this function) 23.c: In function 'op_creditar_saldo': 23.c:81: error: 'LIGHTBLUE' undeclared (first use in this function) 23.c:83: error: 'YELLOW' undeclared (first use in this function) 23.c:86: error: 'MAGENTA' undeclared (first use in this function) 23.c: In function 'main': 23.c:127: warning: implicit declaration of function 'textbackground' 23.c:127: error: 'BLACK' undeclared (first use in this function) 23.c:128: error: 'GREEN' undeclared (first use in this function) 23.c:130: warning: ISO C90 forbids mixed declarations and code 23.c:134: error: 'LIGHTGRAY' undeclared (first use in this function) 23.c:135: warning: implicit declaration of function 'gotoxy' 23.c:138: warning: implicit declaration of function 'strcmp' 23.c:140: error: 'LIGHTGREEN' undeclared (first use in this function) 23.c:149: error: 'WHITE' undeclared (first use in this function) 23.c:155: error: 'MAGENTA' undeclared (first use in this function) 23.c:155: warning: too many arguments for format 23.c:163: error: 'CYAN' undeclared (first use in this function) 23.c:165: error: 'LIGHTRED' undeclared (first use in this function) 23.c:166: error: 'RED' undeclared (first use in this function) 23.c:175: warning: implicit declaration of function 'fcloseall' Poucos erros...lol..Já agr que compilador usas???? ||I'm the devil25, I'm G and I'm sandro_G||
bertolo Posted February 18, 2007 at 09:43 PM Report #83820 Posted February 18, 2007 at 09:43 PM usei linux knoppix 5.1 para compilar usei o comando gcc -Wall -pedantic -ansi doc.c -o doc
Rui Carlos Posted February 18, 2007 at 10:08 PM Report #83824 Posted February 18, 2007 at 10:08 PM o problema é que ele está a usar a biblioteca 'conio'. penso que todos os erros são derivados desse facto. Rui Carlos Gonçalves
Guest tsenart Posted February 21, 2007 at 05:13 PM Report #84410 Posted February 21, 2007 at 05:13 PM o problema é que ele está a usar a biblioteca 'conio'. penso que todos os erros são derivados desse facto. Exacto... O meu código não é ANSI.... Sorry. Compila se tiveres a biblioteca conio2.h. Em linux nunca experimentei.
Guest tsenart Posted May 6, 2007 at 10:35 PM Report #98720 Posted May 6, 2007 at 10:35 PM Decidi alterá-lo para ficar ANSI. /*MADE BY S@M0T. *Started on 09/01/06 *Finished on 12/01/06 *You can only redistribute this source-code as it is(no changes!). *For improvements e-mail me to tsenart@gmail.com */ #include <stdio.h> #include <stdlib.h> #include <time.h> float saldo, valor, *ptrvalor = &valor; time_t tempo; FILE *user_saldo, *user_movs; void cls(); /*Limpa o buffer, espera que o user pressione alguma tecla e limpa o ecran.*/ float receber_saldo(); /*Lê saldo da stream user_saldo. Se nao encontrar o ficheiro, devolve 0.*/ void imprimir_movs(); /*Lê os movimentos da stream user_movs. Se nao encontrar o ficheiro, escreve Ficheiro nao encontrado.*/ void op_debitar_saldo();/*Debita um valor a introduzir pelo utilizador no saldo.*/ void op_creditar_saldo();/*Credita...*/ void registar_saldo();/*Regista o saldo na stream user_saldo.*/ void registar_mov(char tipo);/*Regista as operações efectuadas(Crédito e Débito), a data e o valor.*/ void cls() { getchar(); printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); } float receber_saldo() { float saldo_func = 0; if((user_saldo = fopen("fich_saldo.txt","r")) == NULL) { printf("\nFicheiro nao encontrado.\n"); } else { while(fscanf(user_saldo,"%f", &saldo_func) != EOF) ; fclose(user_saldo); } return saldo_func; } void imprimir_movs() { char movc_func; if ((user_movs = fopen("fich_movs.txt","r")) == NULL) { printf("\nFicheiro nao encontrado.\n"); } else { while(fscanf(user_movs,"%c",&movc_func) != EOF) printf("%c",movc_func); fclose(user_movs); } } void op_debitar_saldo() { saldo = receber_saldo(); printf("\nSaldo actual-> %.3f euros\n",saldo); printf("Quantia a debitar: "); scanf("%f",&valor); saldo -= valor; printf("Novo saldo-> %.3f euros",saldo); registar_mov('d'); registar_saldo(); scanf("%*[^\n]"); scanf("%*c"); } void op_creditar_saldo() { saldo = receber_saldo(); printf("\nSaldo actual-> %.3f euros\n",saldo); printf("Quantia a creditar: "); scanf("%f",&valor); saldo += valor; printf("Novo saldo-> %.3f euros",saldo); registar_mov('c'); registar_saldo(); scanf("%*[^\n]"); scanf("%*c"); } void registar_saldo() { user_saldo = fopen("fich_saldo.txt","w"); fprintf(user_saldo,"%.3f",saldo); fclose(user_saldo); } void registar_mov(char tipo) { user_movs = fopen("fich_movs.txt","a+"); tempo = time(NULL); switch(tipo) { case 'd': fprintf(user_movs,"\nData-> %sTipo-> Debito\tValor-> %.3f euros\n\n",asctime(localtime(&tempo)), *ptrvalor); break; case 'c': fprintf(user_movs,"\nData-> %sTipo-> Credito\tValor-> %.3f euros\n\n",asctime(localtime(&tempo)), *ptrvalor); break; default : fprintf(user_movs,"Tipo de movimento invalido"); } fclose(user_movs); } int main() { short opcao; while (1) { printf("\n\n\t\t\t GESTAO DE DINHEIRO\n\n"); printf("\t\t\t 1-Creditar saldo\n"); printf("\t\t\t 2-Debitar saldo\n"); printf("\t\t\t 3-Consultar saldo\n"); printf("\t\t\t 4-Consultar movimentos\n"); printf("\t\t\t 5-Limpar saldo\n"); printf("\t\t\t 6-Limpar movimentos\n"); printf("\t\t\t 0-Sair\n\n\n"); printf("Opcao ->"); scanf("%hd",&opcao); scanf("%*[^\n]"); scanf("%*c"); switch (opcao) { case 0: return 0; case 1: op_creditar_saldo(); cls(); break; case 2: op_debitar_saldo(); cls(); break; case 3: { printf("\n%.3f euros",receber_saldo()); cls(); } break; case 4: { imprimir_movs(); cls(); } break; case 5: { if(remove("fich_saldo.txt") == 0) { printf("\nFicheiro apagado."); cls(); break; } else { printf("\nNao foi possivel apagar o ficheiro."); cls(); break; } } case 6: { if(remove("fich_movs.txt")==0) { printf("\nFicheiro apagado."); cls(); break; } else { printf("\nNao foi possivel apagar o ficheiro."); cls(); break; } } default: { printf("Opcao invalida"); cls(); } } } fcloseall(); return 0; }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now