Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdlib.h"
- #include "stdio.h"
- #include "list.h"
- #include "string.h"
- #include "stdint.h"
- void stringToNum(element_t * tp, element_t t){
- char* thingToBeParsed = (char*) t;
- intptr_t* storingThing = (intptr_t*) tp;
- char* endChar;
- *storingThing = strtol(thingToBeParsed, &endChar, 10);
- if (endChar == thingToBeParsed){
- *storingThing = -1;
- }
- }
- void numToString(element_t * tp, element_t t, element_t num){
- char* thingToBeParsed = (char*) t;
- char** storingThing = (char**) tp;
- intptr_t number = (intptr_t) num;
- if (number<0){
- *storingThing = thingToBeParsed;
- } else{
- *storingThing = NULL;
- }
- }
- int isPositive(element_t t){
- intptr_t tp = (intptr_t) t;
- if (tp<0){
- return 0;
- } else {
- return 1;
- }
- }
- int isNotNull(element_t t){
- char* tp = (char*) t;
- if (tp == NULL){
- return 0;
- } else {
- return 1;
- }
- }
- void truncStr(element_t* out, element_t str, element_t i){
- char** strOut = (char**) out;
- char* strIn = str;
- intptr_t intIn = (intptr_t) i;
- if (strlen(strIn) <= intIn){
- *strOut = strIn;
- return;
- }
- strIn[(int)intIn] = 0;
- *strOut = strIn;
- }
- // void print(element_t str){
- // char* strp = (char*) str;
- // if (strlen(strp)==0){
- // printf("");
- // return;
- // }
- // printf("%s ", strp);
- // }
- void print1(element_t str){
- char* strp = (char*) str;
- printf("%s\n", strp);
- }
- void findMax(element_t * outPtr, element_t outVal, element_t inVal){
- intptr_t inV = (intptr_t) inVal;
- intptr_t outV = (intptr_t) outVal;
- intptr_t* outP = (intptr_t*) outPtr;
- if (inV>outV){
- *outP = inV;
- } else {
- *outP = outV;
- }
- }
- void attach(element_t * outPtr, element_t outVal, element_t inVal){
- char** outP = (char**) outPtr;
- char* outV = (char*) outVal;
- char* inV = (char*) inVal;
- if (strlen(*outP)==0){
- *outP = realloc(*outP, strlen(inV)+1);
- strcpy(*outP, inV);
- return;
- }
- char space[] = " ";
- const int newLen = strlen(*outP) + strlen(inV) +3;
- *outP = realloc(*outP, newLen);
- strcat(*outP, space);
- strcat(*outP, inV);
- }
- int main(int argc, char* argv[]){
- struct list* l0 = list_create();
- for (int i=1; i<argc; i++){
- list_append(l0, (element_t) argv[i]);
- }
- struct list* l1 = list_create();
- list_map1(stringToNum, l1, l0);
- struct list* l2 = list_create();
- list_map2(numToString, l2, l0, l1);
- struct list* l3 = list_create();
- list_filter(isPositive, l3, l1);
- struct list* l4 = list_create();
- list_filter(isNotNull, l4, l2);
- struct list* l5 = list_create();
- list_map2(truncStr, l5, l4, l3);
- list_foreach(print1, l5);
- char* result = malloc(sizeof(*result));
- //result = (char*)'\0';
- list_foldl(attach, (element_t*)&result, l5);
- printf("%s", result);
- printf("\n");
- element_t max = 0;
- list_foldl(findMax, &max, l3);
- printf("%ld\n", (long) max);
- free(result);
- list_destroy(l0);
- list_destroy(l1);
- list_destroy(l2);
- list_destroy(l3);
- list_destroy(l4);
- list_destroy(l5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement