int main (void) { /* initialize suit array */ const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"}; /* initialize face array */ const char *face[NUM_FACES] = {"Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; /* initalize deck array */ int deck[SUITS][NUM_FACES] = {0}; /* initialize winning card arrays */ int winningCards[13] = {0}, winningCardsComp[13] = {0}; // Initialize suit and face arrays to store current hand info int numSuits[SUITS] = {0}; int numFaces[NUM_FACES] = {0}; int numSuitsComp[SUITS] = {0}; int numFacesComp[NUM_FACES] = {0}; int keepCards[HAND_SIZE] = {0}; int winner = -1, wins = 0, losses = 0, choice = 0; // initialize 2 hands Card hand1[HAND_SIZE] = {{0, 0, 0}}; Card hand2[HAND_SIZE] = {{0, 0, 0}}; srand ((unsigned) time (NULL)); /* seed random-number generator */ while(choice != 3){ displayMenu(); choice = getMenuChoice(); if(choice == 1){ system("cls"); winner = -1; // Shuffle deck resetDeck(deck); shuffle (deck); resetKeepCards(keepCards); // Deal player hand deal (deck, hand1, 1, keepCards); // Deal computer hand deal(deck, hand2, 0, keepCards); resetCardArrays(numSuits, numFaces); resetCardArrays(numSuitsComp, numFacesComp); setCardArrays(numSuitsComp, numFacesComp, hand2); // Print player and computer hands printf("Your hand:\n"); printf("-------------------------\n"); printHand(hand1, face, suit); /* **For testing** printf("\nComputer's hand:\n"); printf("-------------------------\n"); printHand(hand2, face, suit);*/ printSecretHand(); // Get cards player would like to keep getKeepCards(keepCards); // Deal replacement cards to player deal(deck, hand1, 0, keepCards); // Determine which cards computer will keep resetKeepCards(keepCards); determineCompKeepCards(keepCards, numSuitsComp, numFacesComp, hand2); // Deal replacement cards to computer deal(deck, hand2, 0, keepCards); // Print new hands system("cls"); printf("Your hand:\n"); printf("-------------------------\n"); printHand(hand1, face, suit); printf("\nComputer's hand:\n"); printf("-------------------------\n"); printHand(hand2, face, suit); // Determine winner resetCardArrays(numSuits, numFaces); resetCardArrays(numSuitsComp, numFacesComp); setCardArrays(numSuitsComp, numFacesComp, hand2); setCardArrays(numSuits, numFaces, hand1); setDetermineWinnerArray(numSuits, numFaces, winningCards); setDetermineWinnerArray(numSuitsComp, numFacesComp, winningCardsComp); winner = determineWinner(winningCards, winningCardsComp); // Display winner if(winner == 1){ printf("\nYou win!\n"); wins++; } else if(winner == 0){ printf("\nComputer wins\n"); losses++; } system("pause"); } if(choice == 2){ displayWinsLosses(wins, losses); } } return 0; }
int menu(userlist *users){ system("clear"); // Clears the terminal (aesthetics!) printf("Welcome.\n"); char * username = malloc(sizeof(char)*100); char * password = malloc(sizeof(char)*100); char * usertype = malloc(sizeof(char)*100); unsigned char c; do { c = getMenuChoice(); switch (c) { case 48: break; case '1': // Code to add a user using the menu. #ifdef DEBUG printf("Add.\n"); #endif fflush(stdin); username = getUsername(); password = getPassword(); usertype = getUsertype(); add(users, username, password, usertype); getc(stdin); // Junk collection! break; case '2': // Code to edit a user using the menu. #ifdef DEBUG printf("Edit.\n"); #endif printf("First, I'll ask for the credentials of the user you wish to edit.\n"); username = getUsername(); password = getPassword(); usertype = getUsertype(); printf("Next, I'll need the new information you wish to replace it with.\nNote: this data will overwrite the other data.\n"); char * username2 = malloc(sizeof(char)*100); char * password2 = malloc(sizeof(char)*100); char * usertype2 = malloc(sizeof(char)*100); username2 = getUsername(); password2 = getPassword(); usertype2 = getUsertype(); edit(users, username, password, usertype, username2, password2, usertype2); free(username2); free(password2); free(usertype2); getc(stdin); // Junk collection! break; case '3': // Code to delete a user using the menu. #ifdef DEBUG printf("Delete.\n"); #endif fflush(stdin); username = getUsername(); del(users, username); getc(stdin); // Junk collection! break; case '4': // Code to verify a user using the menu. #ifdef DEBUG printf("Verify.\n"); #endif username = getUsername(); password = getPassword(); #ifdef DEBUG printf("\n%s %s\n", username, password); #endif if(verify(users, username, password) == EXIT_SUCCESS){ printf("VALID.\n"); }else{ printf("INVALID.\n"); } getc(stdin); // Junk collection! break; #ifdef DEBUG case '5': // Code to print the list of users. printf("Printing them.\n"); int pu=0; for (pu=0; pu<users->length; pu++) { print_user(users->entries[pu]); } break; #endif default: printf("That is not an option. Please try again.\n"); break; } }while(c != '0'); system("clear"); free(username); free(password); free(usertype); return EXIT_SUCCESS; }
int main() { //*** ENTER YOUR OPENING OUTPUT STATEMENT(S) HERE printf("Hi all!\ncs.calvin.edu/books/c++/engr-sci/LabExercises/Lab7/exercise.html\nwriten by Ramadanov :)\n"); //cout << "Now enter your operands: "; char operation; char clculate; char MENU[] = { "\nPlease enter:\n" "\t+ - to perform addition;\n" "\t- - to perform subtraction;\n" "\t* - to perform multiplication;\n" "\t/ - to perform division;\n" "\t^ - to perform exponentiation;\n" "--> " }; do { do { operation = getMenuChoice(MENU); while (getchar() != '\n'); } while ((operation != '+') && (operation != '-') && (operation != '*') && (operation != '/') && (operation != '^')); //double op1, op2, result; char op1_str[20], op2_str[20]; double op1, op2, result; printf("Now enter your operands: \n"); //cout << "Now enter your operands: "; //cin >> op1 >> op2; do { op1_str[0] = '\0'; printf("Enter your first operand: \n"); fgets(op1_str, sizeof op1_str, stdin); //while (getchar() != '\n'); } while (!(validd(op1_str))); op1 = dd_converter(op1_str); // assert(cin.good()); //->bad inout - return error do { op2_str[0] = '\0'; printf("Enter your second operand: \n"); fgets(op2_str, sizeof op2_str, stdin); //while (getchar() != '\n'); } while (!(validd(op2_str))); op2 = dd_converter(op2_str); //assert(cin.good()); //->bad inout - return error printf("\n op1=%f op2=%f\n", op1, op2); result = apply(operation, op1, op2); printf("The result is %f\n", result); //cout << "The result is " << result << endl; do { printf("Do you want to continue: 'Y' 'N':\t"); clculate = getchar(); while (getchar() != '\n'); } while ((clculate != 'Y') && (clculate != 'N')); } while (clculate == 'Y'); printf("Press any key to continue.....\n"); getchar(); }
int main(int argc, char **argv) { printf("Running 'echo' file transfer client.\n"); if (DEBUG) printf("Debug mode on.\n"); int sockfd, sec_sockfd, n, cmd, count, recvbytes; char ipinput[46], usercmd[2], filename[80], recv_port[161], filebuffer[512]; struct sockaddr_in servaddr, sec_servaddr; // Open a stream (TCP) IPv4 socket, and check if successful if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Error opening socket!\n"); return -1; } printf("Give IP address of the file server: "); scanf("%s", ipinput); int server_port = 6666; memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(server_port); if (inet_pton(AF_INET, ipinput, &servaddr.sin_addr) <= 0) { printf("inet_pton error for %s!\n", argv[1]); return -1; } // Connect to IP address and port indicated by servaddr // Check if it was succesful if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { perror("TCP connect error!\n"); return -1; } /*printf( "1: FILELIST\n2: TCPSEND\n3: UDPSEND\n4: TCPGET\n5: UDPGET\nGive command number: "); scanf("%d", &usercmd);*/ usercmd[0] = getMenuChoice(); //write command to server write(sockfd, usercmd, sizeof(char)); if (DEBUG) printf("wrote cmd %d to server\n", usercmd[0]); cmd = (int) usercmd[0]; switch (cmd) { case 1: printf("\n==File listing==\n"); //sleep(1); //Not sure if these sleeps are necessary. while ((n = read(sockfd, recv_port, 160)) > 0) { printf("%s", recv_port); } break; case 2: printf("\n==TCP SEND==\n"); read(sockfd, recv_port, 160); if (DEBUG) printf("Server wants connection to port: %s\n", recv_port); sleep(1); if ((sec_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Error opening socket!\n"); return -1; } memset(&sec_servaddr, 0, sizeof(sec_servaddr)); sec_servaddr.sin_family = AF_INET; if (DEBUG) printf("port int is %d\n", atoi(recv_port)); sec_servaddr.sin_port = htons(atoi(recv_port)); if (inet_pton(AF_INET, ipinput, &sec_servaddr.sin_addr) <= 0) { printf( "Some known issues were encountered when trying to use IP address.\nThis error has previously occured when nwprog server was used for the client.\nPlease use other computer for the client!\n"); printf("inet_pton error\n"); return -1; } // Connect to IP address and port indicated by sec_servaddr // Check if it was succesful if (connect(sec_sockfd, (struct sockaddr *) &sec_servaddr, sizeof(sec_servaddr)) < 0) { perror("TCP connect error!\n"); return -1; } if (DEBUG) printf("Connected\n"); printf("Give filename to upload: "); scanf("%s", filename); //------------- //file transfer //------------- FILE *filetosend = fopen(filename, "rb"); if (filetosend == NULL) { printf("Couldn't open the file.\n"); printf("Closing client\n"); return -1; } if (DEBUG) printf("Sending filename to the server...\n"); write(sec_sockfd, filename, strlen(filename) + 1); sleep(1); count = 0; printf("Uploading file. Please wait...\n"); while (1) { //read data, 512 bytes at a time unsigned char filebuffer[512] = { 0 }; //printf("starting to read\n"); int bytes_read = fread(filebuffer, 1, 512, filetosend); //printf("read %d bytes\n", bytes_read); //sending the data if (bytes_read > 0) { //printf("writing chunk to server\n"); write(sec_sockfd, filebuffer, bytes_read); count = count + bytes_read; } //find eof if (bytes_read < 512) { if (feof(filetosend)) if (DEBUG) printf("End of file reached\n"); if (ferror(filetosend)) printf("Error reading the file\n"); break; } } sleep(1); printf("File uploaded successfully (%d bytes)\n", count); //printf("data transfer complete\n"); //close(sec_sockfd); break; case 3: printf("\n==UDP SEND==\n"); if (DEBUG) printf(">>>>>> TEMP: Calling udpsendcli(28000, %s).\n", ipinput); read(sockfd, recv_port, 160); if (DEBUG) printf("Server wants connection to port: %s\n", recv_port); int tempport = atoi(recv_port); char tempport_str[80]; sprintf(tempport_str, "%d", tempport); //char* tempport = "28000"; //udpsendcli(tempport, ipinput); udpsendcli(tempport_str, ipinput); break; case 4: printf("\n==TCP GET==\n"); read(sockfd, recv_port, 160); if (DEBUG) printf("Server wants connection to port: %s\n", recv_port); sleep(1); if ((sec_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Error opening socket!\n"); return -1; } memset(&sec_servaddr, 0, sizeof(sec_servaddr)); sec_servaddr.sin_family = AF_INET; if (DEBUG) printf("port int is %d\n", atoi(recv_port)); sec_servaddr.sin_port = htons(atoi(recv_port)); //sec_servaddr.sin_port = htons(server_port2); //TODO: inet_pton fails for some reason if using ipinput if (inet_pton(AF_INET, ipinput, &sec_servaddr.sin_addr) <= 0) { printf( "Some known issues (mainly when using nwprog) were found when trying to use IP address.\nPlease use other computer for the client!\n"); printf("inet_pton error\n"); return -1; } // Connect to IP address and port indicated by sec_servaddr // Check if it was succesful if (connect(sec_sockfd, (struct sockaddr *) &sec_servaddr, sizeof(sec_servaddr)) < 0) { perror("TCP connect error!\n"); return -1; } if (DEBUG) printf("Connected\n"); //------------- //file transfer //------------- printf("Give filename to download: "); scanf("%s", filename); //read(sec_sockfd, filebuffer, 512) if (DEBUG) printf("Opening file\n"); FILE *newfile; newfile = fopen(filename, "wb"); if (newfile == NULL) { printf("Error opening local file"); return -1; } write(sec_sockfd, filename, strlen(filename) + 1); //TODO If file isn't found from server, should return -1; // We need to implement some kind of OK/NOK check from server if (DEBUG) printf("Waiting for file name check from server\n"); memset(recv_port, 0, strlen(recv_port)); read(sec_sockfd, recv_port, 160); //printf("%s\n", recv_port); if (strcmp(recv_port, "1")) { printf("Server doesnt have file: %s\n", filename); return -1; } else { if (DEBUG) printf("Server accepted filename: %s\n", filename); } printf("Downloading file from server. Please wait...\n"); count = 0; //receive data, 512 bytes at a time while ((recvbytes = read(sec_sockfd, filebuffer, 512)) > 0) { //printf("bytes received: %d\n", recvbytes); fwrite(filebuffer, 1, recvbytes, newfile); count = count + recvbytes; } fclose(newfile); printf("File downloaded successfully (%d bytes)\n", count); break; case 5: printf("\n==UDP GET==\n"); read(sockfd, recv_port, 160); if (DEBUG) printf("Server wants connection to port: %s\n", recv_port); int tempport2 = atoi(recv_port); char tempport2_str[80]; sprintf(tempport2_str, "%d", tempport2); udpgetcli(tempport2_str, ipinput); break; default: printf("Got an unknown command, terminating!\n"); return -1; } printf("Closing client\n"); close(sockfd); return 0; }