//returns new lint with lint a to the power b, assumed trimmed and +ve lint *_powerI(lint *a,lint *b) { lint *one=convertI("1"),*two,*mul,**div,*pow,*tmp; switch(_compareI(b,one)) { case GREATER: two=convertI("2"); div=_divisionI(b,two); deleteI(two); switch(_compareI(*(div+1),one)) { case LESS: deleteI(one); pow=_powerI(a,*div); destroyI(div,2); mul=_kmultiplicationI(pow,pow); deleteI(pow); return mul; case EQUAL: deleteI(one); pow=_powerI(a,*div); destroyI(div,2); mul=_kmultiplicationI(pow,pow); deleteI(pow); tmp=_kmultiplicationI(mul,a); deleteI(mul); return tmp; } case EQUAL: deleteI(one); return cloneI(a); case LESS: return one; } }
double get_decimal(char u[]) { double decimal=0; int i,tamanho = strtam(u); for(i=0;i<tamanho && u[i]!='.';i++); // posiciona o indice i em cima do ponto decimal=convertI(u+i+1);///(pot10(tamanho-i-1)); decimal=(double)convertI(u+i+1)/(double)(pot10(tamanho-i-1)); return decimal; }
// Função que guarda os tokens reconhecidos pelo yacc/lex void getToken(char * token, int type){ w_token *novo; novo = (w_token *)malloc(sizeof(w_token)); //printf("token: %s tipo=%d\n", token, type); novo->tipo = type; novo->next = NULL; if(type == WT_ABRE_P){ char *c = (char *)malloc(sizeof(char)); *c = '('; novo->valor=c; } else if(type == WT_FECHA_P){ char *c = (char *)malloc(sizeof(char)); *c = ')'; novo->valor=c; } else if(type == WT_NUMERO){ int *c = malloc(sizeof(int)); *c = convertI(token); novo->valor = c; } else if(type == WT_VALOR){ double *c = malloc(sizeof(double)); *c = convertD(token); novo->valor = c; } else{ novo->valor=(void *)malloc(strlen(token)); strcpy(novo->valor,token); } insert_token_list(novo); novo = NULL; }
//returns new lint with lint a to the power b, assumed trimmed lint *powerI(lint *a,lint *b) { if(b->sign==MINUS) return createI(1); if(a->sign==PLUS) return _powerI(a,b); lint *one=convertI("1"),*two=convertI("2"),**div=_divisionI(b,two); switch(_compareI(*(div+1),one)) { case LESS: destroyI(div,2); deleteI(one); deleteI(two); return _powerI(a,b); case EQUAL: destroyI(div,2); deleteI(one); deleteI(two); return negateI(_powerI(a,b)); } }
double get_inteiro(char v[]) { double inteiro=0; int i,tamanho = strtam(v); char u[10]; strcpy(u,v); //copia o vetor para uma vaiável auxiliar para preservar sua originalidade for(i=0;i<tamanho && u[i]!='.';i++); // posiciona o indice i em cima do ponto u[i]=0; // separa a parte inteira da parte decimal, inserindo um null no lugar do ponto inteiro=convertI(u); return inteiro; }
void convertIWithMatchTable(int* result1, int* result2) { int length = result1[0]; /*convert two arrays according to the MATCHTABLE*/ int turn = 0; for(turn = START_POINT; turn <=length; turn ++) { result2[turn] = convertI(result1[turn]); } }
lint *_fibonacciI(lint *n) { lint *i=convertI("2"),*a=convertI("1"),*b=convertI("1"),*c,*one=convertI("1"),*tmp; while(_compareI(i,n)==LESS) { c=_additionI(b,a); tmp=shrinkI(c); deleteI(c); c=tmp; deleteI(a); a=b; b=c; tmp=_additionI(i,one); deleteI(i); i=tmp; tmp=shrinkI(i); deleteI(i); i=tmp; } deleteI(i); deleteI(a); deleteI(one); return b; }
int main(int argc,char **argv) { if(argc!=2) { printf("[USAGE] %s [lint]\n",*argv); exit(-1); } lint *n=convertI(*(argv+1)),*fibo=_fibonacciI(n); printI(n); printf("F="); printI(fibo); printf("\n"); deleteI(n); deleteI(fibo); return 0; }
int finalizaInsert(char *nome, column *c) { column *auxC; int i = 0, x = 0, t; FILE *dados; struct fs_objects dicio = leObjeto(nome); // Le dicionario tp_table *auxT = leSchema(dicio); // Le esquema if((dados = fopen(dicio.nArquivo,"a+b")) == NULL) return ERRO_ABRIR_ARQUIVO; for(auxC = c, t = 0; auxC != NULL; auxC = auxC->next, t++) { if(t >= dicio.qtdCampos) t = 0; if(auxT[t].tipo == 'S'){ // Grava um dado do tipo string. if(sizeof(auxC->valorCampo) > auxT[t].tam){ return ERRO_NO_TAMANHO_STRING; } if(strcmp(auxC->nomeCampo, auxT[t].nome) != 0){ return ERRO_NOME_CAMPO; } char valorCampo[auxT[t].tam]; strcpy(valorCampo, auxC->valorCampo); strcat(valorCampo, "\0"); fwrite(&valorCampo,sizeof(valorCampo),1,dados); } else if(auxT[t].tipo == 'I'){ // Grava um dado do tipo inteiro. i = 0; while (i < strlen(auxC->valorCampo)) { if(auxC->valorCampo[i] < 48 || auxC->valorCampo[i] > 57){ return ERRO_NO_TIPO_INTEIRO; } i++; } int valorInteiro = convertI(auxC->valorCampo); fwrite(&valorInteiro,sizeof(valorInteiro),1,dados); } else if(auxT[t].tipo == 'D'){ // Grava um dado do tipo double. x = 0; while (x < strlen(auxC->valorCampo)) { if((auxC->valorCampo[x] < 48 || auxC->valorCampo[x] > 57) && (auxC->valorCampo[x] != 46)){ return ERRO_NO_TIPO_DOUBLE; } x++; } double valorDouble = convertD(auxC->valorCampo); fwrite(&valorDouble,sizeof(valorDouble),1,dados); } else if(auxT[t].tipo == 'C'){ // Grava um dado do tipo char. if(strlen(auxC->valorCampo) > (sizeof(char))) { return ERRO_NO_TIPO_CHAR; } char valorChar = auxC->valorCampo[0]; fwrite(&valorChar,sizeof(valorChar),1,dados); } } fclose(dados); free(c); // Libera a memoria da estrutura. free(auxT); // Libera a memoria da estrutura. return SUCCESS; }
int convertI(char u[]) { if(strtam(u) == 0) return 0; return (u[0]-48)*pot10(strtam(u)-1) + convertI(u+1); }
int finalizaInsert(char *nome, column *c) { column *auxC; int i = 0, x = 0, t; FILE *dados; if(!verificaNomeTabela(nome)){ printf("\nA Tabela informada nao existe!\n"); return 0; } struct fs_objects dicio = leObjeto(nome); // Le dicionario tp_table *auxT = leSchema(dicio); // Le esquema //se estiver inserindo na tabela de chaves, verifica regras de PK e FK if (strcmp (nome, "BD_Chaves") == 0){ column *auxP; char *tabela_fk=(char *)malloc(sizeof(char)*TAMANHO_NOME_TABELA); char *campo_fk=(char *)malloc(sizeof(char)*TAMANHO_NOME_TABELA); int p, fk; if(existe_arquivo("BD_Chaves.dat")){ int pk; for(auxP = c, p = 0; auxP != NULL; auxP = auxP->next, p++) { if(p >= dicio.qtdCampos) p = 0; if (pk != 1 && auxT[p].tipo == 'C'){ //Se está tentando inserir PK, seta pk = 1 para verificar posteriormente se já existe PK if (strcmp (auxP->valorCampo, "P") == 0){ pk = 1; p=0; auxP = c; } } //Caso esteja inserindo PK, verifica se tabela já possui PK if (pk == 1 && strcmp (auxP->nomeCampo, "TabelaOrigem") == 0){ printf("\nInserindo chave na tabela %s",auxP->valorCampo); if(TabelaPossuiPk(auxP->valorCampo)) return ERRO_VIOLACAO_PK; } if (fk != 1 && auxT[p].tipo == 'C'){ //Se está tentando inserir FK, seta Fk = 1 para verificar posteriormente se tabela da FK existe if (strcmp (auxP->valorCampo, "F") == 0){ fk = 1; p=0; auxP = c; } } //Caso esteja inserindo FK, verifica se tabela da FK existe if (fk == 1 && strcmp (auxP->nomeCampo, "TabelaDestino") == 0 && tabela_fk == NULL) strcpy(tabela_fk, auxP->valorCampo); if (fk == 1 && strcmp (auxP->nomeCampo, "CampoDestino") == 0 && campo_fk == NULL) strcpy(campo_fk, auxP->valorCampo); } }else{ for(auxP = c, p = 0; auxP != NULL; auxP = auxP->next, p++) { if(p >= dicio.qtdCampos) p = 0; if (fk != 1 && auxT[p].tipo == 'C'){ //Se está tentando inserir FK, seta Fk = 1 para verificar posteriormente se tabela da FK existe if (strcmp (auxP->valorCampo, "F") == 0){ fk = 1; p=0; auxP = c; } } //Caso esteja inserindo FK, verifica se tabela da FK existe if (fk == 1 && strcmp (auxP->nomeCampo, "TabelaDestino") == 0 && strcmp(tabela_fk, "\0") == 0) strcpy(tabela_fk, auxP->valorCampo); if (fk == 1 && strcmp (auxP->nomeCampo, "CampoDestino") == 0 && strcmp(campo_fk, "\0") == 0) strcpy(campo_fk, auxP->valorCampo); } } // Verifica se tabela destino da FK existe e se campo destino da Fk existe na tabela destino if(strcmp(tabela_fk, "\0") != 0 && strcmp(campo_fk, "\0") != 0){ //Verifica se tabela destino da fk existe if(!verificaNomeTabela(tabela_fk)){ printf("Erro: Tabela Destino da FK nao existe!\n"); return 0; } // Verifica se campo da fk existe na tabela destino da fk if(campo_existe_na_tabela(tabela_fk,campo_fk) == 0){ printf("Campo %s nao existe na tabela %s!\n",campo_fk, tabela_fk); return 0; } } } if((dados = fopen(dicio.nArquivo,"a+b")) == NULL) return ERRO_ABRIR_ARQUIVO; char *tabela_origem=(char *)malloc(sizeof(char)*TAMANHO_NOME_TABELA); for(auxC = c, t = 0; auxC != NULL; auxC = auxC->next, t++) { if(t >= dicio.qtdCampos) t = 0; if(auxT[t].tipo == 'S'){ // Grava um dado do tipo string. if(sizeof(auxC->valorCampo) > auxT[t].tam){ return ERRO_NO_TAMANHO_STRING; } if(strcmp(auxC->nomeCampo, auxT[t].nome) != 0){ return ERRO_NOME_CAMPO; } char valorCampo[auxT[t].tam]; strcpy(valorCampo, auxC->valorCampo); strcat(valorCampo, "\0"); fwrite(&valorCampo,sizeof(valorCampo),1,dados); } else if(auxT[t].tipo == 'I'){ // Grava um dado do tipo inteiro. i = 0; while (i < strlen(auxC->valorCampo)) { if(auxC->valorCampo[i] < 48 || auxC->valorCampo[i] > 57){ return ERRO_NO_TIPO_INTEIRO; } i++; } int valorInteiro = convertI(auxC->valorCampo); fwrite(&valorInteiro,sizeof(valorInteiro),1,dados); } else if(auxT[t].tipo == 'D'){ // Grava um dado do tipo double. x = 0; while (x < strlen(auxC->valorCampo)) { if((auxC->valorCampo[x] < 48 || auxC->valorCampo[x] > 57) && (auxC->valorCampo[x] != 46)){ return ERRO_NO_TIPO_DOUBLE; } x++; } double valorDouble = convertD(auxC->valorCampo); fwrite(&valorDouble,sizeof(valorDouble),1,dados); } else if(auxT[t].tipo == 'C'){ // Grava um dado do tipo char. if(strlen(auxC->valorCampo) > (sizeof(char))) { return ERRO_NO_TIPO_CHAR; } char valorChar = auxC->valorCampo[0]; fwrite(&valorChar,sizeof(valorChar),1,dados); } //Caso tenha criado chaves if (strcmp (auxC->nomeCampo, "TabelaOrigem") == 0) strcpy(tabela_origem, auxC->valorCampo); if (strcmp (auxC->nomeCampo, "TabelaDestino") == 0){ if (strcmp (auxC->valorCampo, "\0") == 0) printf("\nChave primaria criada na tabela %s",tabela_origem); else printf("\nChave estrangeira criada na tabela %s",tabela_origem); } } fclose(dados); free(c); // Libera a memoria da estrutura. free(auxT); // Libera a memoria da estrutura. return SUCCESS; }