int main() { srand(time(NULL)); int guess = 1; int num = rrange(2, 500); float root = to1place(sqrt(num)); printf("Welcome to the guessing game!\n"); printf("In this game, you have 12 guesses to try and figure out the quare root of"); printf("a given number.\n"); printf("If you enter a square root that's correct to at least one decimal place,\n"); printf("you'll win.\n"); printf("====================================================\n"); printf("What is the square root of %i?\n", num); do { float sr = 0; do { printf("Please enter guess %i: ", guess); if(scanf("%f", &sr) != EOF) break; printf("Please enter a number.\n"); } while(1); float temp = to1place(sr); if(sr < root) { printf("You'll have to guess higher!\n"); } else if(sr > root) { printf("You'll have to guess lower!\n"); } else if(root == sr) { printf("You got it!\n"); return 0; } guess++; } while(guess <= 12); printf("You lost.\n"); }
int *rrange(int start, int end) { int *range; int i; if(start>end) return rrange(end,start); //so that start is always <end n=end-start+1; // n globally declared range=malloc(n*sizeof(int)); //casting is not required if(range==NULL) printf("fail"); for(i=0;i<n;i++,start++) { range[i]=start; } return range; }
int main() { int numb_range; int i; int start; int end; int *numbers; i = 0; start = 2147483647; end = 2147483640; if (end > start) numb_range = end - start + 1; else numb_range = start - end; // numbers = (int *)malloc(sizeof(int) * numb_range); numbers = rrange(start, end); while (i <= numb_range) printf("%d |", numbers[i++]); return 0; }
int main(int argc, char** argv) { srand(time(NULL)); float stack[32768]; float* stackPointer = stack; char* temp = (char*)malloc(256); for(int i = 0; i < strlen(argv[1]); i++) { if(argv[1][i] == ':') { printf("%f", *(stackPointer)); stackPointer--; } else if(argv[1][i] == ';') { printf("%c", (int)*(stackPointer)); stackPointer--; } else if(argv[1][i] == '?') { scanf("%f", *(stackPointer)); stackPointer++; } else if(argv[1][i] == ')') { scanf("%255s", temp); for(int i = 0; i < strlen(temp); i++) { *(stackPointer) = temp[i]; stackPointer++; } } else if(argv[1][i] == '~') { if(*(stackPointer) == 0) { i -= *(stackPointer); } } else if(argv[1][i] == '!') { if(*(stackPointer) != 0) { i -= *(stackPointer); } } else if(argv[1][i] == '`') { *(stackPointer++) = rrange(*(stackPointer), *(stackPointer - 1)); } else if(argv[1][i] == '+') { *(stackPointer + 1) = *(stackPointer) + *(stackPointer - 1); stackPointer++; } else if(argv[1][i] == '-') { *(stackPointer - 1) = *(stackPointer) - *(stackPointer - 1); } else if(argv[1][i] == '*') { *(stackPointer + 1) = *(stackPointer) * *(stackPointer - 1); stackPointer++; } else if(argv[1][i] == '/') { *(stackPointer + 1) = *(stackPointer) / *(stackPointer - 1); stackPointer++; } else if(argv[1][i] == '^') { *(stackPointer + 1) = sqrt(*(stackPointer)); stackPointer++; } else { int tempc = 0; sscanf((argv[1] + i), "%255[0123456789@#$%&(_=qwertyuiop\\QWERTYUIOP{}|asdfghjkl'ASDFGHJKL\"zxcvbnm,.ZXCVBNM<>\20]%n", &temp, &tempc); printf("t0: %f", temp[0]); if(isdigit(temp[0]) || temp[0] == '-') { *(stackPointer) = (float)atof(temp); } else { *(stackPointer) = (float)temp[0]; } i += (tempc - 1); printf("added: --%f--", *(stackPointer)); } } for(int i = 0; i < 32768; i++) { putchar(stack[i]); } }
int main() { srand(time(NULL)); int pile = rrange(10, 100); int turn = rrange(0, 1); int allowedToTake = rrange(1, pile / 3); int firstTurn = 1; printf("Welcome to the game of Nim (version A).\n"); printf("In this game, two players alternatley take marbles from a pile.\n"); printf("In each move, a player chooses how many marbles to take.\n"); printf("The player must take at least one and at most %i marbles.\n", allowedToTake); printf("Then the other player takes a turn.\n"); printf("The player who takes the last marble wins.\n"); printf("====================================================\n"); printf("There are %i marbles on the pile.\n", pile); do { if(turn == 0) turn = 1; else turn = 0; if(turn == 0) { if(firstTurn == 1) printf("You have the first turn.\n"); else printf("It is your turn.\n"); do { printf("Please enter how many marbles you would like to remove from the pile: "); int howMany = 0; if(scanf("%i", &howMany) != EOF) { if(pile == 1) { if(howMany == 1) { pile = 0; printf("There are now %i marbles on the pile.\n", pile); break; } else { printf("Please enter a valid number.\n"); printf("You must take at least one but at most %i marbles.\n", min(allowedToTake, pile)); } } else if((howMany >= 1) && (howMany <= min(allowedToTake, pile))) { pile -= howMany; printf("There are now %i marbles on the pile.\n", pile); break; } else { printf("Please enter a valid number.\n"); printf("You must take at least one but at most %i marbles.\n", min(allowedToTake, pile)); } } else { printf("Please enter a number.\n"); } } while(1); } else { if(firstTurn == 1) printf("The computer has the first turn.\n"); else printf("It is the computers turn.\n"); int temp = rrange(1, allowedToTake); while(temp > pile) { temp = rrange(1, allowedToTake); } pile = pile - temp; printf("There are now %i marbles on the pile.\n", pile); } firstTurn = 0; } while(pile != 0); if(turn == 0) { printf("The computer has won.\n"); } else { printf("You have won.\n"); } }