/************************************************* * Three Operand Addition, No Carry * *************************************************/ word bigint_add3_nc(word z[], const word x[], u32bit x_size, const word y[], u32bit y_size) { if(x_size < y_size) { return bigint_add3_nc(z, y, y_size, x, x_size); } word carry = 0; const u32bit blocks = y_size - (y_size % 8); for(u32bit j = 0; j != blocks; j += 8) carry = word8_add3(z + j, x + j, y + j, carry); for(u32bit j = blocks; j != y_size; ++j) z[j] = word_add(x[j], y[j], &carry); for(u32bit j = y_size; j != x_size; ++j) { word x_j = x[j] + carry; if(carry && x_j) carry = 0; z[j] = x_j; } return carry; }
void file_read (const char * txt, text_t * text, sentence_t * sentence) { FILE * file = fopen(txt, "r"); if(file == NULL) return; char c; int len = 0; char word[25] = ""; while((c = fgetc(file)) != EOF) { if(ispunct(c)) { if(c == '.' || c == '!' || c == '?') { if(len == 0) continue; word_add(sentence, word_t_new(word)); sentence_add(text, sentence); sentence = sentence_t_new(); memset(word, 0, 25); len = 0; } continue; } else if(isspace(c)) { if(len == 0) continue; word_add(sentence, word_t_new(word)); memset(word, 0, 25); len = 0; } else { word[len] = c; len++; } } fclose(file); }
/************************************************* * Two Operand Addition, No Carry * *************************************************/ word bigint_add2_nc(word x[], u32bit x_size, const word y[], u32bit y_size) { word carry = 0; const u32bit blocks = y_size - (y_size % 8); for(u32bit j = 0; j != blocks; j += 8) carry = word8_add2(x + j, y + j, carry); for(u32bit j = blocks; j != y_size; ++j) x[j] = word_add(x[j], y[j], &carry); if(!carry) return 0; for(u32bit j = y_size; j != x_size; ++j) if(++x[j]) return 0; return 1; }
/* Given a read buffer of length (ct), identify and save individual tokens. * * This (and word_hash) will need to be changed for i18n. * It should probably be made a config option. */ static int scanner(set *s, int ct, int *inword, int case_sensitive) { int i, j, last = 0; char c; /* current byte */ int alf, diff; /* isalpha(c) flag; diff */ /* Scan along and save each consecutive alphabetical region. */ for (i=0; i<ct; i++) { c = buf[i]; alf = isalpha(c) || c == '-' || c == '_'; if (*inword && !alf) { /* end of current token */ *inword = 0; diff = i - last; if (diff >= MIN_WORD_SZ && diff < MAX_WORD_SZ) { if (!case_sensitive) for (j=0; j<diff; j++) buf[last+j] = tolower(buf[last+j]); word_add(s, buf + last, diff); } } else if (!*inword && alf) { /* start of new token */ last = i; *inword = 1; } } return last; }
word_t * senToWords(char * str) { char ** words = NULL; int wordscount = 1; if (strlen(str) == 0) { printf("string is empty\n"); return NULL; } int flag = 0;// перший пробіл на рахує for (int i = 0; i < strlen(str); i++) { if (str[i] == ' ' && flag) { flag = 0;// перевірка на пробіли(убирає лишні пробіли(шоб не рахувало зайві пробіли як слово )) wordscount++; } if (str[i] != ' ') flag = 1;//не був пробіл if (str[i] == ' ' && str[i + 1] == '\0')// якщо останній символ був пробіл wordscount--;//видаляєм слово } printf("%i\n", wordscount); words = malloc(sizeof(char *) * (wordscount + 1));// память під масив слів char * word; // puts(str); const char tmpstr[2] = {' ', '\0'}; word = strtok(str, tmpstr);//фкнкція розбиття строки на підстроки // printf("%i\n", wordscount); for (int i = 0; i < wordscount; i++) { words[i] = malloc(sizeof(char) * strlen(word)); strcpy(words[i], word); // puts(words[i]); word = NULL; if (i != wordscount - 1 )// поки не останнє слово { word = strtok(NULL, " ");// лишній раз стрток не виконувався while(strlen(word) == 0) word = strtok(NULL, " "); if( word == NULL) break; } } words[wordscount] = NULL; if (words[0] == NULL) exit(1); word_t * self = word_new(); //puts("dsasfd"); for(int i = 0 ; i < wordscount; i++) { word_add(self , words[i]); } //puts("dafsdf"); return self; }
static void test_word_add() { char str[10] = {0}; char res[10]; int rem; for(int i=0; i<220; i++) { int rem = word_add(res, str, i*1000, 8, 9); printf("%d:\t",i); for(int k=0; res[k]!=0; k++) printf("%d",res[k]); printf("\trem=%d\n",rem); } for(int i=0; i<10; i++) str[i] = 0; //alphabet = "123456789" //"" + 1 = "1" rem = word_add(res, str, 1, 2, 9); assert(rem == 0); assert(res[0] == 1); assert(res[1] == 0); //"" + 10 = "9" rem = word_add(res, str, 9, 2, 9); assert(rem == 0); assert(res[0] == 9); assert(res[1] == 0); //"" + 12 = "21" rem = word_add(res, str, 10, 2, 9); assert(rem == 0); assert(res[0] == 1); assert(res[1] == 1); assert(res[2] == 0); //"" + 100 = "121" trucated rem = word_add(res, str, 100, 2, 9); assert(res[0] == 1); assert(res[1] == 2); assert(rem == 1); assert(res[2] == 0); //"9" + 1 = "11" str[0] = 9; str[1] = 0; rem = word_add(res, str, 1, 2, 9); assert(rem == 0); assert(res[0] == 1); assert(res[1] == 1); assert(res[2] == 0); //"11" + 1 = "21" str[0] = 1; str[1] = 1; str[2] = 0; rem = word_add(res, str, 1, 2, 9); assert(rem == 0); assert(res[0] == 2); assert(res[1] == 1); assert(res[2] == 0); //"22"(#20) + 10 = "33"(#30) str[0] = 2; str[1] = 2; str[2] = 0; rem = word_add(res, str, 10, 2, 9); assert(rem == 0); assert(res[0] == 3); assert(res[1] == 3); assert(res[2] == 0); str[0] = 9; str[1] = 9; str[2] = 0; rem = word_add(res, str, 1, 2, 9); assert(rem != 0); str[0] = 1; str[1] = 1; str[2] = 0; rem = word_add(res, str, 0, 2, 9); assert(rem == 0); assert(res[0] == 1); assert(res[1] == 1); assert(res[2] == 0); str[0] = 9; str[1] = 9; str[2] = 0; rem = word_add(res, str, 1, 2, 9); assert(rem == 1); }
static Const fold_op(Node node) /*;fold_op*/ { Node opn, arg1, arg2, oplist; Const result, op1, op2, tryc; Symbol sym, op_name; int *uint; int rm; Tuple tup; int res, overflow; opn = N_AST1(node); oplist = N_AST2(node); tup = N_LIST(oplist); arg1 = (Node) tup[1]; arg2 = (Node) tup[2]; op1 = const_fold(arg1); op2 = const_fold(arg2); op_name = N_UNQ(opn); /* If either operand raises and exception, so does the operation */ if (N_KIND(arg1) == as_raise) { copy_attributes(arg1, node); return const_new(CONST_OM); } if (N_KIND(arg2) == as_raise && op_name != symbol_andthen && op_name != symbol_orelse) { copy_attributes(arg2, node); return const_new(CONST_OM); } if (is_const_om(op1) || (is_const_om(op2) && (op_name != symbol_in || op_name != symbol_notin))) { return const_new(CONST_OM); } sym = op_name; if ( sym == symbol_addi || sym == symbol_addfl) { if (sym == symbol_addi) { res = word_add(INTV(op1), INTV(op2), &overflow); if (overflow) { create_raise(node, symbol_constraint_error); result = const_new(CONST_OM); } else result = int_const(res); } else result = real_const(REALV(op1) + REALV(op2)); } else if ( sym == symbol_addfx) { const_check(op1, CONST_RAT); const_check(op2, CONST_RAT); result= rat_const(rat_add(RATV(op1), RATV(op2))); } else if ( sym == symbol_subi) { if (is_const_int(op1)) { if (is_const_int(op2)) { res = word_sub(INTV(op1), INTV(op2), &overflow); if (overflow) { create_raise(node, symbol_constraint_error); result = const_new(CONST_OM); } else result = int_const(res); } else { chaos("fold_op: subi operand types"); } } } else if (sym == symbol_subfl) { result = real_const(REALV(op1) - REALV(op2)); } else if ( sym == symbol_subfx) { const_check(op1, CONST_RAT); const_check(op2, CONST_RAT); result= rat_const(rat_sub(RATV(op1), RATV(op2))); } else if ( sym == symbol_muli) { #ifdef TBSL -- need to check for overflow and convert result back to int if not -- note that low-level setl is missing calls to check_overflow that -- are present in high-level and should be in low-level as well result = int_mul(int_fri(op1), int_fri(op2)); #endif /* until overflow check in */ const_check(op1, CONST_INT); const_check(op2, CONST_INT); res = word_mul(INTV(op1), INTV(op2), &overflow); if (overflow) { create_raise(node, symbol_constraint_error); result = const_new(CONST_OM); } else result = int_const(res); }