Finished Topic-11
This commit is contained in:
parent
6bb9ef9b20
commit
0dc2c63492
@ -19,7 +19,7 @@ void handle_arguments(int argc, char *argv[]) {
|
||||
|
||||
struct TokenizedQuerry tokenize(const char *querry) {
|
||||
struct TokenizedQuerry tokenized_querry;
|
||||
printf("QUERRY: %s\n", querry);
|
||||
// printf("QUERRY: %s\n", querry);
|
||||
char left[50], *operand_ptr, right[50];
|
||||
char valid_operands[4] = {'+', '-', '/', '*'};
|
||||
for (int i = 0; i < sizeof(valid_operands); ++i) {
|
||||
@ -34,13 +34,29 @@ struct TokenizedQuerry tokenize(const char *querry) {
|
||||
|
||||
strncpy(left, querry, left_length);
|
||||
strcpy(right, querry + left_length + 1);
|
||||
printf("Tokenized: %s %c %s\n", left, *operand_ptr, right);
|
||||
// printf("Tokenized: %s %c %s\n", left, *operand_ptr, right);
|
||||
tokenized_querry.left = strtol(left, NULL, 10);
|
||||
tokenized_querry.operand = *operand_ptr;
|
||||
tokenized_querry.right = strtol(right, NULL, 10);
|
||||
|
||||
return tokenized_querry;
|
||||
}
|
||||
|
||||
int calculate_TokenizedQuerry(struct TokenizedQuerry expression) {
|
||||
switch (expression.operand) {
|
||||
case '+':
|
||||
return expression.left + expression.right;
|
||||
case '-':
|
||||
return expression.left - expression.right;
|
||||
case '*':
|
||||
return expression.left * expression.right;
|
||||
case '/':
|
||||
return expression.left / expression.right;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void run_server(int port) {
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
@ -61,16 +77,19 @@ void run_server(int port) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char buffer[100];
|
||||
memset(&buffer, 0, sizeof(buffer));
|
||||
size_t buffer_size = 100;
|
||||
char buffer[buffer_size];
|
||||
memset(&buffer, 0, buffer_size);
|
||||
socklen_t client_addr_len;
|
||||
struct TokenizedQuerry tokenized_querry;
|
||||
|
||||
while (1) {
|
||||
rv = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)&client_addr, &client_addr_len);
|
||||
rv = recvfrom(sockfd, buffer, buffer_size, 0, (struct sockaddr *)&client_addr, &client_addr_len);
|
||||
tokenized_querry = tokenize(buffer);
|
||||
|
||||
//rv = sendto(sockfd, response, sizeof(response), 0, (const struct sockaddr *)&client_addr, sizeof(client_addr));
|
||||
int result = calculate_TokenizedQuerry(tokenized_querry);
|
||||
memset(&buffer, 0, buffer_size);
|
||||
snprintf(buffer, buffer_size, "%d", result);
|
||||
rv = sendto(sockfd, buffer, sizeof(buffer), 0, (const struct sockaddr *)&client_addr, sizeof(client_addr));
|
||||
memset(&buffer, 0, sizeof(buffer));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user