本文整理汇总了C++中ctr_build_number_from_float函数的典型用法代码示例。如果您正苦于以下问题:C++ ctr_build_number_from_float函数的具体用法?C++ ctr_build_number_from_float怎么用?C++ ctr_build_number_from_float使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ctr_build_number_from_float函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ctr_array_add
/**
* [List] + [List]
*
* Returns a new list, containing elements of itself and the other
* list.
*
* In other languages:
* Dutch: [Reeks] + [Reeks] | Geeft de reeks die bestaat uit de samenvoeging van gegeven reeksen.
*/
ctr_object* ctr_array_add(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherArray = argumentList->object;
ctr_object* newArray = ctr_array_new(CtrStdArray, NULL);
int i;
for(i = myself->value.avalue->tail; i<myself->value.avalue->head; i++) {
ctr_argument* pushArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* elnumArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_object* elnum = ctr_build_number_from_float((ctr_number) i);
elnumArg->object = elnum;
pushArg->object = ctr_array_get(myself, elnumArg);
ctr_array_push(newArray, pushArg);
ctr_heap_free( elnumArg );
ctr_heap_free( pushArg );
}
if (otherArray->info.type == CTR_OBJECT_TYPE_OTARRAY) {
for(i = otherArray->value.avalue->tail; i<otherArray->value.avalue->head; i++) {
ctr_argument* pushArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* elnumArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_object* elnum = ctr_build_number_from_float((ctr_number) i);
elnumArg->object = elnum;
pushArg->object = ctr_array_get(otherArray, elnumArg);
ctr_array_push(newArray, pushArg);
ctr_heap_free( elnumArg );
ctr_heap_free( pushArg );
}
}
return newArray;
}
示例2: ctr_json_create_object
ctr_object* ctr_json_create_object(json_t* root, ctr_object* gt) {
switch(json_typeof(root)) {
case JSON_OBJECT: {
ctr_object* sub = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
ctr_set_link_all(sub, gt);
// size_t size;
const char *key;
json_t *value;
ctr_argument* argl = ctr_heap_allocate(sizeof(*argl));
argl->next = ctr_heap_allocate(sizeof(*argl));
// size = json_object_size(root);
json_object_foreach(root, key, value) {
char* k = (char*)key;
ctr_object* ko = ctr_build_string_from_cstring(k);
ctr_object* vo = ctr_json_create_object(value, gt);
argl->object = vo;
argl->next->object = ko;
sub = ctr_map_put(sub, argl);
}
ctr_heap_free(argl->next);
ctr_heap_free(argl);
return sub;
}
case JSON_ARRAY: {
ctr_object* arr = ctr_array_new(CtrStdArray, NULL);
ctr_argument* arg = ctr_heap_allocate(sizeof(ctr_argument));
size_t i;
size_t size = json_array_size(root);
for (i = 0; i < size; i++) {
arg->object = ctr_json_create_object(json_array_get(root, i), gt);
ctr_array_push(arr, arg);
}
ctr_heap_free(arg);
return arr;
}
case JSON_STRING: {
ctr_object* str = ctr_build_string((char*)json_string_value(root), json_string_length(root));
return str;
}
case JSON_INTEGER: {
return ctr_build_number_from_float(json_integer_value(root));
}
case JSON_REAL: {
return ctr_build_number_from_float(json_real_value(root));
}
case JSON_FALSE: {
return ctr_build_bool(0);
}
case JSON_TRUE: {
return ctr_build_bool(1);
}
case JSON_NULL: {
return ctr_build_nil();
}
default: {
CtrStdFlow = ctr_build_string_from_cstring("Unrecognized JSON type");
return CtrStdNil;
}
}
示例3: ctr_string_last_index_of
/**
* StringLastIndexOf
*
* Returns the index (character number, not the byte!) of the
* needle in the haystack.
*
* Usage:
* 'find the needle' lastIndexOf: 'needle'. #9
*/
ctr_object* ctr_string_last_index_of(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* sub = ctr_internal_cast2string(argumentList->object);
long hlen = myself->value.svalue->vlen;
long nlen = sub->value.svalue->vlen;
ctr_size uchar_index;
ctr_size byte_index;
char* p = ctr_internal_memmem(myself->value.svalue->value, hlen, sub->value.svalue->value, nlen, 1);
if (p == NULL) return ctr_build_number_from_float((float)-1);
byte_index = (ctr_size) ( (uintptr_t) p - (uintptr_t) (myself->value.svalue->value) );
uchar_index = ctr_getutf8len(myself->value.svalue->value, byte_index);
return ctr_build_number_from_float((float) uchar_index);
}
示例4: ctr_internal_cast2number
/**
* InternalNumberCast
*
* Casts an object to a number object.
*/
ctr_object* ctr_internal_cast2number(ctr_object* o) {
if (o->info.type == CTR_OBJECT_TYPE_OTNUMBER) return o;
if (o->info.type == CTR_OBJECT_TYPE_OTSTRING) {
return ctr_build_number_from_string(o->value.svalue->value, o->value.svalue->vlen);
}
return ctr_build_number_from_float((ctr_number)0);
}
示例5: ctr_array_map
/**
* [List] map: [Block].
*
* Iterates over the array. Passing each element as a key-value pair to the
* specified block.
* The map message will pass the following arguments to the block, the key,
* the value and a reference to the array itself. The last argument might seem
* redundant but allows for a more functional programming style. Instead of map,
* you can also use each:.
*
* Usage:
*
* files map: showName.
* files map: {
* :key :filename :files
* ✎ write: filename.
* }.
*
* files each: {
* :key :filename :files
* ✎ write: filename.
* }.
*
* In other languages:
* Dutch: [Reeks] lijst: [Codeblok] | Maakt van een reeks
*/
ctr_object* ctr_array_map(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* block = argumentList->object;
int i = 0;
if (block->info.type != CTR_OBJECT_TYPE_OTBLOCK) {
CtrStdFlow = ctr_build_string_from_cstring( CTR_ERR_EXP_BLK );
CtrStdFlow->info.sticky = 1;
}
for(i = myself->value.avalue->tail; i < myself->value.avalue->head; i++) {
ctr_argument* arguments = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* argument2 = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
ctr_argument* argument3 = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
arguments->object = ctr_build_number_from_float((double) i);
argument2->object = *(myself->value.avalue->elements + i);
argument3->object = myself;
arguments->next = argument2;
argument2->next = argument3;
/* keep receiver in block object otherwise, GC will destroy it */
ctr_gc_internal_pin(block);
ctr_gc_internal_pin(myself);
ctr_gc_internal_pin(argument2->object);
ctr_block_run(block, arguments, myself);
ctr_heap_free( arguments );
ctr_heap_free( argument2 );
ctr_heap_free( argument3 );
if (CtrStdFlow == CtrStdContinue) CtrStdFlow = NULL;
if (CtrStdFlow) break;
}
if (CtrStdFlow == CtrStdBreak) CtrStdFlow = NULL; /* consume break */
return myself;
}
示例6: ctr_number_factorial
/**
* Factorial
*
* Calculates the factorial of a number.
*/
ctr_object* ctr_number_factorial(ctr_object* myself, ctr_argument* argumentList) {
ctr_number t = myself->value.nvalue;
int i;
ctr_number a = 1;
for(i = (int) t; i > 0; i--) {
a = a * i;
}
return ctr_build_number_from_float(a);
}
示例7: ctr_array_sum
/**
* [Array] sum
*
* Takes the sum of an array. This message will calculate the
* sum of the numerical elements in the array.
*
* Usage:
*
* a := Array <- 1 ; 2 ; 3.
* s := a sum. #6
*
* In the example above, the sum of array will be stored in s and
* it's value will be 6.
*/
ctr_object* ctr_array_sum(ctr_object* myself, ctr_argument* argumentList) {
double sum = 0;
ctr_object* el;
size_t i = 0;
for(i = 0; i < myself->value.avalue->head; i++) {
el = *(myself->value.avalue->elements + i);
sum += ctr_internal_cast2number(el)->value.nvalue;
}
return ctr_build_number_from_float(sum);
}
示例8: ctr_shell_call
/**
* [Shell] call: [String]
*
* Performs a Shell operation. The Shell object uses a fluid API, so you can
* mix shell code with programming logic. For instance to list the contents
* of a directory use:
*
* Shell ls
*
* This will output the contents of the current working directly, you
* can also pass keyword messages like so:
*
* Shell echo: 'Hello from the Shell!'.
*
* The example above will output the specified message to the console.
* Every message you send will be turned into a string and dispatched to
* the 'call:' message.
*/
ctr_object* ctr_shell_call(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* arg = ctr_internal_cast2string(argumentList->object);
long vlen = arg->value.svalue->vlen;
char* comString = malloc(vlen + 1);
int r;
memcpy(comString, arg->value.svalue->value, vlen);
memcpy(comString+vlen,"\0",1);
r = system(comString);
return ctr_build_number_from_float( (ctr_number) r );
}
示例9: ctr_string_byte_at
/**
* StringByteAt
*
* Returns the byte at the specified position (in bytes).
*
* Usage:
* ('abc' byteAt: 1). #98
*/
ctr_object* ctr_string_byte_at(ctr_object* myself, ctr_argument* argumentList) {
char x;
ctr_object* fromPos = ctr_internal_cast2number(argumentList->object);
long a = (fromPos->value.nvalue);
long len = myself->value.svalue->vlen;
if (a > len) return CtrStdNil;
if (a < 0) return CtrStdNil;
x = (char) *(myself->value.svalue->value + a);
return ctr_build_number_from_float((double)x);
}
示例10: ctr_number_modulo
ctr_object* ctr_number_modulo(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum = ctr_internal_cast2number(argumentList->object);
ctr_number a = myself->value.nvalue;
ctr_number b = otherNum->value.nvalue;
if (b == 0) {
CtrStdError = ctr_build_string_from_cstring("Division by zero.");
return myself;
}
return ctr_build_number_from_float(fmod(a,b));
}
示例11: ctr_percolator_new
/**
* Percolator new
*
* Creates a new instance of the percolator object.
*
* Usage:
*
* myPercolator := Percolator new.
* cupOfCoffee := myPercolator coffee: 1 water: 2, brew.
*
*/
ctr_object* ctr_percolator_new(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* percolatorInstance = ctr_internal_create_object(CTR_OBJECT_TYPE_OTOBJECT);
percolatorInstance->link = myself;
ctr_internal_object_set_property(
percolatorInstance,
ctr_build_string_from_cstring( "coffee" ),
ctr_build_number_from_float(0),
CTR_CATEGORY_PRIVATE_PROPERTY
);
ctr_internal_object_set_property(
percolatorInstance,
ctr_build_string_from_cstring( "water" ),
ctr_build_number_from_float(0),
CTR_CATEGORY_PRIVATE_PROPERTY
);
return percolatorInstance;
}
示例12: ctr_number_multiply
ctr_object* ctr_number_multiply(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* otherNum;
ctr_number a;
ctr_number b;
CTR_MIRROR_CALL(CTR_OBJECT_TYPE_OTBLOCK, ctr_block_times, mirror1);
otherNum = ctr_internal_cast2number(argumentList->object);
a = myself->value.nvalue;
b = otherNum->value.nvalue;
return ctr_build_number_from_float(a*b);
}
示例13: ctr_string_skip
ctr_object* ctr_string_skip(ctr_object* myself, ctr_argument* argumentList) {
ctr_argument* argument1;
ctr_argument* argument2;
if (myself->value.svalue->vlen < argumentList->object->value.nvalue) return ctr_build_string("",0);
argument1 = CTR_CREATE_ARGUMENT();
argument2 = CTR_CREATE_ARGUMENT();
argument1->object = argumentList->object;
argument1->next = argument2;
argument2->object = ctr_build_number_from_float(myself->value.svalue->vlen - argumentList->object->value.nvalue);
return ctr_string_from_length(myself, argument1);
}
示例14: ctr_array_splice
/**
* [List] replace: [Number] length: [Number] with: [List].
*
* Returns a copy of the list with the specified elements replaced.
* The first argument indicates the start index to begin the replacement.
* Here, 0 means the beginning of the list.
* The second argument (length)
* must indicate the number of elements to delete in the copy, counting
* from the starting point. Finally, one has to provide the replacement
* list as the third argument.
* If the replacement list is empty, the specified elements will only be
* removed from the copy.
* If the replacement is not an array an error will be thrown.
*
* Usage:
*
* ☞ buy := cakes
* replace: 1
* length: 2
* with: ( List ← 'cinnamon' ; 'pineapple' ).
*
* In other languages:
* Dutch: [Reeks] vervang: [Getal] lengte: [Getal] door: [Reeks]
* Vervangt een deel van de reeks door een andere reeks.
*/
ctr_object* ctr_array_splice(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* newArray = ctr_array_new(CtrStdArray, NULL);
ctr_object* start = ctr_internal_cast2number(argumentList->object);
ctr_object* deleteCount = ctr_internal_cast2number(argumentList->next->object);
ctr_object* replacement = argumentList->next->next->object;
ctr_object* remainder;
ctr_argument* sliceFromArg;
ctr_argument* sliceLengthArg;
ctr_argument* replacementArg;
ctr_argument* remainderArg;
ctr_size n;
if ( replacement->info.type != CTR_OBJECT_TYPE_OTARRAY ) {
CtrStdFlow = ctr_error_text( CTR_ERR_EXP_ARR );
return myself;
}
n = ( start->value.nvalue + deleteCount->value.nvalue );
sliceFromArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
sliceLengthArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
replacementArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
remainderArg = (ctr_argument*) ctr_heap_allocate( sizeof( ctr_argument ) );
sliceFromArg->object = ctr_build_number_from_float(0);
sliceLengthArg->object = start;
sliceFromArg->next = sliceLengthArg;
newArray = ctr_array_from_length( myself, sliceFromArg );
replacementArg->object = replacement;
newArray = ctr_array_add(newArray, replacementArg);
sliceFromArg->object = ctr_build_number_from_float( n );
if ( n < (myself->value.avalue->head - myself->value.avalue->tail) ) {
sliceLengthArg->object = ctr_build_number_from_float( (myself->value.avalue->head - myself->value.avalue->tail) - n );
sliceFromArg->next = sliceLengthArg;
remainder = ctr_array_from_length( myself, sliceFromArg );
remainderArg->object = remainder;
newArray = ctr_array_add( newArray, remainderArg );
}
ctr_heap_free( sliceFromArg );
ctr_heap_free( sliceLengthArg );
ctr_heap_free( replacementArg );
ctr_heap_free( remainderArg );
return newArray;
}
示例15: ctr_file_write_bytes
/**
* [File] writeBytes: [String].
*
* Takes a string and writes the bytes in the string to the file
* object. Returns the number of bytes actually written.
*
* Usage:
*
* f := File new: '/path/to/file.txt'.
* f open: 'r+'.
* n := f writeBytes: 'Hello World'.
* f close.
*
* The example above writes 'Hello World' to the specified file as bytes.
* The number of bytes written is returned in variable n.
*/
ctr_object* ctr_file_write_bytes(ctr_object* myself, ctr_argument* argumentList) {
int bytes, written;
ctr_object* string2write;
char* buffer;
if (myself->value.rvalue == NULL) return myself;
if (myself->value.rvalue->type != 1) return myself;
string2write = ctr_internal_cast2string(argumentList->object);
buffer = ctr_heap_allocate_cstring( string2write );
bytes = string2write->value.svalue->vlen;
written = fwrite(buffer, sizeof(char), (int)bytes, (FILE*)myself->value.rvalue->ptr);
ctr_heap_free( buffer );
return ctr_build_number_from_float((double_t) written);
}