int main() { int z; int c; c=getchar(); isdigit1(c); scanf("%d",&z); }
main(){ char post[20]; int i; float x,f,opr1,opr2; printf("Enter the postfix expression: "); gets(post); i=0; while(post[i]!='\0'){ if(isdigit1(post[i])==1){ f=(float)(post[i]-'0'); push(f); } else{ opr2=pop(); opr1=pop(); x=eval(opr1,opr2,post[i]); push(x); } i++; } x=pop(); printf("The value is %f.",x); }
void splitTextOnPrimitives(TEXTNODE *node) { TEXTNODE *newNode = NULL; int i; char ch, lastCh = '\0'; int hasDecimal = 0; PrimitiveType ptype = NONE; for (i = 0; i < strlen(node->text); i++) { ch = node->text[i]; if (isalphaupper(ch)) { if (ptype == NONE || ptype == ID) ptype = ID; else goto SPLIT_PRIMITIVE; } if (isdigit1(ch)) { if (ptype == NONE || ptype == NEGATIVE || ptype == NUM) ptype = NUM; else if (ptype == ID) ptype = ID; else goto SPLIT_PRIMITIVE; } switch (ch) { case '.': if ((ptype == NONE || ptype == NUM) && !hasDecimal) hasDecimal = 1; else goto SPLIT_PRIMITIVE; break; case '[': if (ptype == NONE) ptype = LBR; else goto SPLIT_PRIMITIVE; break; case ']': if (ptype == NONE) ptype = RBR; else goto SPLIT_PRIMITIVE; break; case '-': if (ptype == NONE) ptype = NEGATIVE; else goto SPLIT_PRIMITIVE; break; case '*': case '+': case '/': case '<': case '>': if (ptype == NONE || ptype == OP) ptype = OP; else goto SPLIT_PRIMITIVE; break; case '=': if (ptype == NONE || ptype == OP || ptype == NEGATIVE) ptype = OP; else goto SPLIT_PRIMITIVE; break; } lastCh = ch; } if (ptype == NEGATIVE) ptype = OP; if (ptype == ID && strcmp(node->text, "AND") == 0) ptype = OP; if (ptype == ID && strcmp(node->text, "OR") == 0) ptype = OP; if (ptype == ID && strcmp(node->text, "NOT") == 0) ptype = OP; node->type = ptype; return; //place chars starting from node->text[i] into new linked node SPLIT_PRIMITIVE: newNode = getNewTextNode(node->text + i); node->text[i] = '\0'; if (ptype == NEGATIVE) ptype = OP; if (ptype == ID && strcmp(node->text, "AND") == 0) ptype = OP; if (ptype == ID && strcmp(node->text, "OR") == 0) ptype = OP; if (ptype == ID && strcmp(node->text, "NOT") == 0) ptype = OP; node->type = ptype; newNode->prev = node; newNode->next = node->next; node->next = newNode; splitTextOnPrimitives(newNode); }
/* Description: This function reads a Rogo program from a serial buffer. It reads and filters-out characters that are not whitelisted. */ TEXTNODE *serialReadCommands(void) { int good, i, start, end; char linebuf[MAXSTR + 1]; char *chPtr; TEXTNODE *list = NULL; TEXTNODE *current = NULL; while (1) { for (i = 0; i < MAXSTR; i++) linebuf[i] = '\0'; i = 0; delay(10); while (Serial.available() > 0) { if (Serial.peek() == '\7') return list; linebuf[i++] = Serial.read(); if (i == MAXSTR) break; delay(10); } if (!i) break; //ignore lines starting with ; if (linebuf[0] != ';') { //convert all non-whitelisted characters into '\0' chPtr = linebuf; end = 0; while (*chPtr) { good = 0; if (isalphalower(*chPtr)) *chPtr ^= ' '; //convert to upper case (ascii trick) good |= isalphaupper(*chPtr); good |= isdigit1(*chPtr); switch (*chPtr) { case '.': case '+': case '-': case '*': case '/': case '=': case '<': case '>': case '[': case ']': good = 1; break; } if (!good) *chPtr = '\0'; chPtr++; end++; } //read each string from buffer and add to text list for (i = 0, start = -1; i <= end; i++) { if (linebuf[i] != '\0' && start == -1) start = i; if (linebuf[i] == '\0' && start != -1) { //printf("%s\n", linebuf + start); if (list == NULL) { list = current = getNewTextNode((const char *)(linebuf + start)); } else { current->next = getNewTextNode(linebuf + start); current->next->prev = current; current = current->next; } splitTextOnPrimitives(current); while (current->next) current = current->next; start = -1; } } } } return list; }
TNODE *newTreeNode(const TEXTNODE *node) { PRIMTYPE *pos = NULL; PRIMTYPE *search = NULL; NodeType ntype = NT_UNKNOWN; DataType dtype = DT_INT; int primIdx = -1, i, isVar, isSensor, nInt = 0; int varIdx = -1; double nDbl = 0.0; switch (node->type) { case OP: case ID: search = (PRIMTYPE *)malloc(sizeof(PRIMTYPE)); search->name = (char *)malloc(strlen(node->text) + 1); strcpy(search->name, node->text); pos = (PRIMTYPE *) bsearch(search, prims, sizeof(prims)/sizeof(prims[0]), sizeof(PRIMTYPE), cmpPrim); free(search->name); free(search); //cmd? if (pos != NULL) { //printf("\tFound: %s\n", pos->name); primIdx = (int)(pos - prims); switch (node->type) { case OP: ntype = NT_OP; break; case ID: ntype = NT_CONTROL; break; default: break; } } else { isVar = 0; isSensor = 0; if (strlen(node->text) == 2) { isVar = (node->text[0] == 'V' && isdigit1(node->text[1])); isSensor = (node->text[0] == 'S' && isdigit1(node->text[1])); } else if (strlen(node->text) == 3) { isVar = (node->text[0] == 'V' && isdigit1(node->text[1]) && isdigit1(node->text[2])); isSensor = (node->text[0] == 'S' && isdigit1(node->text[1]) && isdigit1(node->text[2])); } if (isVar) { varIdx = atoi(node->text + 1); //printf("\tVariable v%d found.\n", varIdx); ntype = NT_VAR; } if (isSensor) { varIdx = atoi(node->text + 1); ntype = NT_SENSOR; } //else //printf("\tNot found: %s\n", node->text); } break; case NUM: // convert to numeric ntype = NT_NUM; dtype = DT_INT; nInt = atoi(node->text); if (nInt == INT_MAX || nInt == INT_MIN) dtype = DT_DBL; else for (i = 0; i < strlen(node->text); i++) if (node->text[i] == '.') dtype = DT_DBL; nDbl = atof(node->text); break; case LBR: ntype = NT_LIST_START; break; case RBR: ntype = NT_LIST_END; break; default: return NIL; } TNODE *newNode = (TNODE *)malloc(sizeof(TNODE)); newNode->ntype = ntype; newNode->dtype = dtype; newNode->varIdx = varIdx; newNode->nInteger = nInt; newNode->nRational = nDbl; newNode->left = NULL; newNode->right = NULL; newNode->prev = NULL; newNode->next = NULL; newNode->primIdx = primIdx; /* "numargs" and "priority" now stored in primitives array */ //newNode->numargs = 0; //newNode->priority = 0; //if (pos != NULL) //{ // newNode->numargs = pos->numargs; // newNode->priority = pos->priority; //} return newNode; }