Skip to content

Instantly share code, notes, and snippets.

@AjayKrP
Last active October 1, 2017 03:59
Show Gist options
  • Save AjayKrP/9d6270266c0b393ca15c8b36d85cc378 to your computer and use it in GitHub Desktop.
Save AjayKrP/9d6270266c0b393ca15c8b36d85cc378 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <zconf.h>
#include <memory.h>
#include <stdbool.h>
#include <ctype.h>
void error(const char* msg){
perror(msg);
exit(0);
}
float stof(const char* s){
float rez = 0, fact = 1;
if (*s == '-'){
s++;
fact = -1;
};
int point_seen;
for (point_seen = 0; *s; s++){
if (*s == '.'){
point_seen = 1;
continue;
};
int d = *s - '0';
if (d >= 0 && d <= 9){
if (point_seen) fact /= 10.0f;
rez = rez * 10.0f + (float)d;
};
};
return rez * fact;
};
float calculator(char calculation[256]) {
char *token;
char *val1 = NULL;
char *val2 = NULL;
float v1;
float v2;
bool validCal = true;
char tempCalculation[256];
strcpy(tempCalculation,calculation);
token = strtok(tempCalculation,"+-*//^");
int c = 0;
while(token != NULL) {
c++;
if(c == 1)
val1 = token;
if(c == 2)
val2 = token;
token = strtok(NULL,"+-*//^");
}
if(c == 2) {
int i1;
size_t lenVal1 = strlen(val1);
for(i1=0; i1<lenVal1 ; i1++) {
if(!isdigit(val1[i1]))
break;
}
if(i1 != lenVal1)
validCal = false;
int i2;
const int lenVal2 = (const int) (strlen(val2) - 1);
for(i2 = 0; i2 < lenVal2 ; i2++) {
if(!isdigit(val2[i2]))
break;
}
if(i2 != lenVal2)
validCal = false;
if(validCal){
char oper = calculation[lenVal1];
printf("Operator%c\n",oper);
v1 = stof(val1);
v2 = stof(val2);
if(oper == '+') {
return v1 + v2;
}
else if(oper == '-') {
return v1 - v2;
}
else if(oper == '*') {
return v1 * v2;
}
else if(oper == '/') {
if( v2 == 0){
printf("Denominator can't be zero\n");
exit(0);
}
return v1 / v2;
}
else if(oper == '^') {
printf("Power\n");
}
else {
error("Invalid credential!!!");
}
}
else {
printf("The values are not numeric!\n");
}
return -1;
}
else {
printf("Two operands are required for calculation\n");
return -1;
}
};
int main(int argc, char** argv){
int sockfd, newsockfd;
char buffer[256];
socklen_t cli_len;
struct sockaddr_in serv_addr, cli_addr;
uint16_t port;
if(argc < 2){
error("Argument Error!!!");
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
perror("Unable to create socket!!!");
}
bzero((char*)&serv_addr, sizeof(serv_addr));
port = (uint16_t)atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port);
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
error("Error in Binding!!!\n");
}
listen(sockfd, 5);
cli_len = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr*)& cli_addr, &cli_len);
if(newsockfd < 0){
error("Error on accepting socket!!!");
}
bzero(buffer, 256);
int n = (int) read(newsockfd, buffer, 255);
if(n < 0){
perror("Error to reading socket!!!");
}
printf("Here is your entries: %s\n", buffer);
float result = (float) calculator((char *) buffer);
printf("Result = %f\n" , result);
char msg[60];
char chAns[30]="";
sprintf(chAns,"%f",result);
strcat(msg,chAns);
n = (int) write(newsockfd, msg, strlen(msg));
if(n < 0){
error("Error to writing socket!!!");
}
close(newsockfd);
close(sockfd);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment