History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
1. //operating system Linux,ubuntu,Mac
#include
#include
#include
#include
/*Sum function implementation*/
int sum(int number1,int number2){
return number1+number2;
}
/*sub function implementation*/
int sub(int number1,int number2){
return number1-number2;
}
/*mul function implementation*/
int mul(int number1,int number2){
return number1*number2;
}
/*divide function implementation*/
float divide(int number1,int number2){
if(number2==0){
printf("We can not divide by 0 ");
return 0.0f;
}
return (float)(number1/number2);
}
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if (pointer == NULL)
{
2. printf("Unable to allocate memory. ");
exit(1);
}
for (c = 0 ; c < length ; c++)
{
*(pointer+c) = *(string+position-1);
string++;
}
*(pointer+c) = '0';
return pointer;
}
/*Main Function start*/
int main(int argc, char *argv[]){
/*Variable declarations*/
int number1,number2;
char *operator,*str1,*str2;
char line[50];
char s[2] = " ";
int errorFlag=0;// checking if user entered more then one operands
while(1){
/*User input*/
printf("Please Enter the command ");
gets(line);
int len=0;
for(int i=0; line[i]!='0'; ++i){len++;}
if(len==0){
return 1;
}
/*Spliting into Tokens*/
operator = strtok(line, " ");
3. if(strcmp(operator,"bye")==0){
break;
}
/*String to integer --- token1*/
number1=atoi(strtok(NULL, s));
/*Removing ( and ) from String*/
str2=strtok(NULL, s);
char *content;int length=0;
for(int i=0; str2[i]!='0'; ++i){length++;}
if(str2[0]=='('){
content=substring(str2,2,length-1);
char *opr=content;
int n1=atoi(strtok(NULL, s));
int n2=atoi(strtok(NULL, s));
/*If user entered more then two operands then it will return some token otherwise it will
return NULL pointer*/
if(strtok(NULL, s)!=NULL){
printf("You need to enter operator operand1 operand2 ");
errorFlag=1;
}else{
if(strcmp(opr,"sum")==0){
number2=sum(n1,n2);
}else if(strcmp(opr,"sub")==0){
number2=sub(n1,n2);
}else if(strcmp(opr,"mul")==0){
number2=mul(n1,n2);
} else if(strcmp(opr,"div")==0){
number2=divide(n1,n2);
}
}
}else{
4. number2=atoi(str2);
/*If user entered more then two operands then it will return some token otherwise it will return
NULL pointer*/
if(strtok(NULL, s)!=NULL){
printf("You need to enter operator operand1 operand2 ");
errorFlag=1;
}
}
if(errorFlag!=1){
/*Checking operator by strcmp function*/
if(strcmp(operator,"sum")==0){
printf("Result %d ",sum(number1,number2));
}else if(strcmp(operator,"sub")==0){
printf("Result %d ",sub(number1,number2));
}else if(strcmp(operator,"mul")==0){
printf("Result %d ",mul(number1,number2));
} else if(strcmp(operator,"div")==0){
printf("Result %.2f ",divide(number1,number2));
}
}
}
return 0;
}
/********************output************************/
gopal@gopal:~/Desktop/chegg$ gcc Calculator.c
Calculator.c: In function ‘main’:
Calculator.c:67:3: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-
declaration]
gets(line);
^
/tmp/ccgPlkE8.o: In function `main':
5. Calculator.c:(.text+0x154): warning: the `gets' function is dangerous and should not be used.
gopal@gopal:~/Desktop/chegg$ ./a.out
Please Enter the command sum 2 3
Result 5
Please Enter the command mul 5 (mul 7 3)
Result 105
Please Enter the command sum 2 3 4
You need to enter operator operand1 operand2
Please Enter the command mul 5 (mul 7 3 2)
You need to enter operator operand1 operand2
Please Enter the command mul 5 (mul 7 3) 2
You need to enter operator operand1 operand2
Please Enter the command bye
Solution
//operating system Linux,ubuntu,Mac
#include
#include
#include
#include
/*Sum function implementation*/
int sum(int number1,int number2){
return number1+number2;
}
/*sub function implementation*/
int sub(int number1,int number2){
return number1-number2;
}
/*mul function implementation*/
int mul(int number1,int number2){
return number1*number2;
}
/*divide function implementation*/
float divide(int number1,int number2){
if(number2==0){
6. printf("We can not divide by 0 ");
return 0.0f;
}
return (float)(number1/number2);
}
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if (pointer == NULL)
{
printf("Unable to allocate memory. ");
exit(1);
}
for (c = 0 ; c < length ; c++)
{
*(pointer+c) = *(string+position-1);
string++;
}
*(pointer+c) = '0';
return pointer;
}
/*Main Function start*/
int main(int argc, char *argv[]){
/*Variable declarations*/
int number1,number2;
char *operator,*str1,*str2;
char line[50];
7. char s[2] = " ";
int errorFlag=0;// checking if user entered more then one operands
while(1){
/*User input*/
printf("Please Enter the command ");
gets(line);
int len=0;
for(int i=0; line[i]!='0'; ++i){len++;}
if(len==0){
return 1;
}
/*Spliting into Tokens*/
operator = strtok(line, " ");
if(strcmp(operator,"bye")==0){
break;
}
/*String to integer --- token1*/
number1=atoi(strtok(NULL, s));
/*Removing ( and ) from String*/
str2=strtok(NULL, s);
char *content;int length=0;
for(int i=0; str2[i]!='0'; ++i){length++;}
if(str2[0]=='('){
content=substring(str2,2,length-1);
char *opr=content;
int n1=atoi(strtok(NULL, s));
int n2=atoi(strtok(NULL, s));
/*If user entered more then two operands then it will return some token otherwise it will
return NULL pointer*/
if(strtok(NULL, s)!=NULL){
8. printf("You need to enter operator operand1 operand2 ");
errorFlag=1;
}else{
if(strcmp(opr,"sum")==0){
number2=sum(n1,n2);
}else if(strcmp(opr,"sub")==0){
number2=sub(n1,n2);
}else if(strcmp(opr,"mul")==0){
number2=mul(n1,n2);
} else if(strcmp(opr,"div")==0){
number2=divide(n1,n2);
}
}
}else{
number2=atoi(str2);
/*If user entered more then two operands then it will return some token otherwise it will return
NULL pointer*/
if(strtok(NULL, s)!=NULL){
printf("You need to enter operator operand1 operand2 ");
errorFlag=1;
}
}
if(errorFlag!=1){
/*Checking operator by strcmp function*/
if(strcmp(operator,"sum")==0){
printf("Result %d ",sum(number1,number2));
}else if(strcmp(operator,"sub")==0){
printf("Result %d ",sub(number1,number2));
}else if(strcmp(operator,"mul")==0){
printf("Result %d ",mul(number1,number2));
} else if(strcmp(operator,"div")==0){
printf("Result %.2f ",divide(number1,number2));
}
9. }
}
return 0;
}
/********************output************************/
gopal@gopal:~/Desktop/chegg$ gcc Calculator.c
Calculator.c: In function ‘main’:
Calculator.c:67:3: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-
declaration]
gets(line);
^
/tmp/ccgPlkE8.o: In function `main':
Calculator.c:(.text+0x154): warning: the `gets' function is dangerous and should not be used.
gopal@gopal:~/Desktop/chegg$ ./a.out
Please Enter the command sum 2 3
Result 5
Please Enter the command mul 5 (mul 7 3)
Result 105
Please Enter the command sum 2 3 4
You need to enter operator operand1 operand2
Please Enter the command mul 5 (mul 7 3 2)
You need to enter operator operand1 operand2
Please Enter the command mul 5 (mul 7 3) 2
You need to enter operator operand1 operand2
Please Enter the command bye