// Performs function calls and handles user input. int main() { char **square; int size=0, numInts=0; print10Square(); printf("\nProblem 2:\n"); while (numInts != 1 || size < 2 || size > 10) { printf("Please enter the size of the square [2-10]: "); numInts = scanf("%d", &size); clearInputBuffer(); } allocateNumberSquare( (const int) size, &square); initializeNumberSquare( (const int) size, square); printNumberSquare( (const int) size, square); printf("\nProblem 3:\n"); printNumberDiamond( (const int) size, square); deallocateNumberSquare( (const int) size, square); return 0; }
int getMenuOption() { int option; scanf("%d", &option); // read integer into option clearInputBuffer(); return option; }
void waitForEnter() { fflush(stdout); clearInputBuffer(); char c = getc(stdin); while (c != '\n') { c = getc(stdin); } }
int getIntegerLesserThan(int max) { int number = 0; do{ scanf("%d", &number); if(number > max || number <= 0) printf("Please enter a number between 1 & %d\n", max); clearInputBuffer(); }while(number > max || number <= 0); return number; }
void results(PLAYER (*ppp)[]) { int i; for (i = 0; i < numOfPlayers; i++) { PLAYER *p = &((*ppp)[i]); clock_t start = (*p).mmm[0].time; clock_t end = (*p).mmm[(*p).lastMove-1].time; double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("\033[%d;%dH%c: %f\n", 10+i, 30, (*p).obj.c, cpu_time_used); } sleep(2); clearInputBuffer(); setMenuMode(); }
void countdown() { int x = 10, y = 0; fflush(stdout); eraseScoreboard(); printMatrixOnBoardXY(three, x, y); deepSleep1(); printMatrixOnBoardXY(two, x, y); deepSleep1(); printMatrixOnBoardXY(one, x, y); deepSleep1(); clearInputBuffer(); printMatrixOnBoardXY(go, 3, y); }
unsigned int readUInt(){ char c; unsigned int res=0; c=getchar(); switch(c){ case '+': c=getchar(); break; } while( c != '\n' && res != INT_MAX){ if( c >= '0' && c <= '9' ){ res = res*10 + c - '0'; c = getchar(); }else{ res = INT_MAX; clearInputBuffer(); } } return res; }
int lerStr(char **ptr){ char strBuffer[1024]; int i=0, excedeu=1; do{ strBuffer[i] = getchar(); i++; }while( strBuffer[i-1] != '\0' && strBuffer[i-1] != '\n' && i < 1023 ); strBuffer[i-1] = '\0'; if( i == 1023 ){ clearInputBuffer(); excedeu=-1; } if( (*ptr = allocStr(strBuffer)) == NULL ) return 0; else return excedeu; }
int readInt(){ char c; int sinal=1, res=0; c=getchar(); switch(c){ case '-': sinal = -1; case '+': c=getchar(); break; } while( c != '\n' && res != INT_MAX){ if( c >= '0' && c <= '9' ){ res = res*10 + c - '0'; c = getchar(); }else{ res = INT_MAX; clearInputBuffer(); } } if( res != INT_MAX ) res *= sinal; return res; }
int main() { int keepLooping = TRUE; char text[MAX_INPUT_CHARS + 1]; // +1 for \0 character text[0] = '\0'; // print menu printf("1. Input Text\n2. Text Stats\n3. Find Substring\n4. Reverse Text\n5. Quit\n\n\n\n"); // infinite loop to accept user input while (keepLooping) { // get user option printf("===============Input Option:"); int option; if (isValidOption(option = getMenuOption())) { switch (option) { case 1: { int keepPrompting = TRUE; while (keepPrompting) { char c; int i = 0; keepPrompting = FALSE; while ((c = getchar()) != '\n') { text[i++] = c; if (i > MAX_INPUT_CHARS) { printf("Text no more than 1000 characters. Please enter again.\n"); clearInputBuffer(); keepPrompting = TRUE; break; } } if (!keepPrompting) if (text[i-1] == '\n') text[i-1] = '\0'; else text[i] = '\0'; } printf("\n\n\n"); break; } case 2: { // Text Stats int i, inWord = FALSE, wordCount = 0, sentenceCount = 0; // index is ascii char; index value is frequency int freqChars[NUM_ASCII_CHARS]; // initialize freqChars to 0's for (i = 0; i < NUM_ASCII_CHARS; i++) freqChars[i] = 0; // loop over each character in input for (i = 0; i < strlen(text); i++) { // check if word if (isalpha(text[i]) && !inWord) { inWord = TRUE; wordCount++; } else if (!isalpha(text[i]) && inWord) { inWord = FALSE; } // check for sentence if (i < strlen(text) && (text[i] == '.' || text[i] == '!' || text[i] == '?') && (text[i+1] == ' ' || text[i+1] == '\0' || text[i+1] == '\'' || text[i+1] == '"')) sentenceCount++; // increment character's frequency freqChars[text[i]]++; } printf("Character Number: %d\nWord Number: %d\nSentence Number: %d\nMost Frequent Characters:\n", strlen(text), wordCount, sentenceCount); int counter = 0, largest = 0; do { largest = 0; // find largest for (i = 0; i < NUM_ASCII_CHARS; i++) { if (freqChars[i] > largest) largest = freqChars[i]; } if (largest != 0) { // find and print ties for (i = 0; i < NUM_ASCII_CHARS; i++) { if (freqChars[i] == largest) { // print value. e.g. 'c':4 printf("'%c':%d\n", i, freqChars[i]); // delete largest values for next iteration freqChars[i] = 0; } } } else break; } while (++counter < 3); printf("\n\n\n"); break; } case 3: { // Find Substring printf("Please Input Substring\n"); char substring[MAX_INPUT_CHARS+1], c; int i = 0; while ((c = getchar()) != '\n') { substring[i++] = c; if (i > MAX_INPUT_CHARS) { printf("Text no more than 1000 characters. Substring not found.\n"); clearInputBuffer(); break; } } if (substring[i-1] == '\n') substring[i-1] = '\0'; else substring[i] = '\0'; if (substring[0] != '\0') { char *pos = text; int counter = 0; // cut off the portion we have already visited by advancing the pos pointer while ((pos = strstr(pos, substring)) > 0) { counter++; pos++; } printf("found %d occurrence(s)\n", counter); } printf("\n\n\n"); break; } case 4: { // Reverse Text printf("Reversed Text:\n"); // reverse whole string character by character int i, textLength = strlen(text); for (i = 0; i < textLength-i-1; i++) { char temp = text[i]; text[i] = text[textLength-1-i]; text[textLength-1-i] = temp; } // reverse characters in each word int inWord = FALSE, j, k; for (i = 0; i < textLength; i++) { if (text[i] != '\t' && text[i] != ' ' && !inWord) { inWord = TRUE; int wordLength = 0; // find word length for (j = i; text[j] != '\t' && text[j] != ' ' && text[j] != '\0'; j++, wordLength++); // reverse word for (k = 0; k < wordLength-k-1; k++) { char temp = text[k+i]; text[k+i] = text[wordLength+i-k-1]; text[wordLength+i-k-1] = temp; } } else if (!(text[i] != '\t' && text[i] != ' ')) { inWord = FALSE; } else { continue; } } printf("%s\n", text); printf("\n\n\n"); break; } case 5: // Quit keepLooping = FALSE; break; } // end switch } else { printf("Invalid Input.\n"); } } return 0; }
void UIChat_Clear(uiwidget_t* obj) { clearInputBuffer(obj); }
void LSCPTest::tearDown() { clearInputBuffer(); // to avoid that the next test reads an answer from a previous test }
/* Function: main Purpose: main program performs UI controls and forwards cmd through function calls Input: void Returns: o if clean termnation */ int main (void) { char cmd; message msgOut; message msgIn; int i; int strobeHandle; int adcValue; float voltage; int dataPath[DATA_PATH_SIZE] = {GP_4, GP_5, GP_6, GP_7}; pthread_t displayThread; pthread_mutex_init (&cGmutex, NULL); initiateGPIO(Strobe); initiateGPIOArray(dataPath, DATA_PATH_SIZE); strobeHandle = openGPIO(Strobe, GPIO_DIRECTION_OUT); writeGPIO(strobeHandle,1); close(strobeHandle); clearPAGE(); while (1) { printMenu(); cmd = getchar(); clearInputBuffer(); clearBelowLine(MSG_Y); saveCursor(); switch (cmd) { case 'L': { msgOut.data = 0xD; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t LED Always On"); } else if (msgIn.data == 0xC) { printf("[\033[0;32m OK \033[m]\t LED Triggered By Sensor"); } else { printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed"); } break; } case 'P' : { msgOut.data = 0x1; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t Ping Successful"); } else { printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed"); } break; } case 'R' : { msgOut.data = 0x0; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t Reset Successful"); } else { printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed"); } break; } case 'G' : { adcValue = 0; voltage = 0; msgOut.data = 0x2; sendMessage(msgOut, dataPath); for (i = 2; i >= 0; i--) { msgIn = receiveMessage(dataPath); adcValue |= (msgIn.data << (i * DATA_PATH_SIZE)); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); } msgIn = receiveMessage(dataPath); if (msgIn.data == 0xE) { adcValue &= 0x3FF; voltage = (float) ((adcValue/1024.0) * 5.0); setColor(YELLOW); gotoXY(MSG_X,MSG_Y + 1); printf("ADC Value: 0x%X", adcValue); gotoXY(MSG_X,MSG_Y + 2); printf("Voltage: %lf \033[u", voltage); gotoXY(STATUS_X,STATUS_Y); setColor(RESET); printf("[\033[0;32m OK \033[m]\t ADC Read Successful \033[u"); } else { gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); printf("[\033[0;31m Error \033[m]\t Message Acknowledgment Failed \033[u"); } break; } case 'T' : { msgOut.data = 0x3; sendMessage(msgOut, dataPath); msgIn = receiveMessage(dataPath); gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("Message Received: %X", msgIn.data); gotoXY(STATUS_X,STATUS_Y); clearLine(STATUS_Y); setColor(RESET); if (msgIn.data == 0xE) { printf("[\033[0;32m OK \033[m]\t ADC Toggled On"); } else if (msgIn.data == 0xC) { printf("[\033[0;32m OK \033[m]\t ADC Toggled Off"); } break; } case 'D': { pthread_mutex_lock (&cGmutex); continueGraphing = true; pthread_mutex_unlock (&cGmutex); pthread_create(&displayThread, NULL, graphVoltage, &dataPath); getchar(); pthread_mutex_lock (&cGmutex); continueGraphing = false; pthread_mutex_unlock (&cGmutex); pthread_join(displayThread, NULL); clearPAGE(); break; } case 'Q': { gotoXY(MSG_X,MSG_Y); clearLine(MSG_Y); printf("[\033[0;32m OK \033[m]\t ThankYou For UsingLightSensor"); unexport(Strobe); unexportArray(dataPath, DATA_PATH_SIZE); printf("\033[2J\033[0;0H\033[m"); exit(0); } } recallCursor(); } return (0); }