SlideShare a Scribd company logo
1 of 9
Download to read offline
//operating system ubuntu,linux,Mac
Program will work only if you give command like
sum 2 3 or
sum 4 (sub 4 3)
#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)
{
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, " ");
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{
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':
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 ubuntu,linux,Mac
Program will work only if you give command like
sum 2 3 or
sum 4 (sub 4 3)
#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)
{
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, " ");
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{
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':
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

More Related Content

Similar to operating system ubuntu,linux,MacProgram will work only if you g.pdf

Using CUDA Within Mathematica
Using CUDA Within MathematicaUsing CUDA Within Mathematica
Using CUDA Within Mathematicakrasul
 
Using Cuda Within Mathematica
Using Cuda Within MathematicaUsing Cuda Within Mathematica
Using Cuda Within MathematicaShoaib Burq
 
fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdfanjanadistribution
 
Unit 4
Unit 4Unit 4
Unit 4siddr
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfmarketing413921
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfmdameer02
 
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfsutharbharat59
 
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdfimplement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdfforladies
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docxannetnash8266
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?jonbodner
 

Similar to operating system ubuntu,linux,MacProgram will work only if you g.pdf (20)

Functions
FunctionsFunctions
Functions
 
Using CUDA Within Mathematica
Using CUDA Within MathematicaUsing CUDA Within Mathematica
Using CUDA Within Mathematica
 
Using Cuda Within Mathematica
Using Cuda Within MathematicaUsing Cuda Within Mathematica
Using Cuda Within Mathematica
 
fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdf
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Unit 4
Unit 4Unit 4
Unit 4
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdf
 
week-15x
week-15xweek-15x
week-15x
 
c programming
c programmingc programming
c programming
 
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdfMerge Sort implementation in C++ The implementation for Mergesort gi.pdf
Merge Sort implementation in C++ The implementation for Mergesort gi.pdf
 
week-16x
week-16xweek-16x
week-16x
 
Sysprog 13
Sysprog 13Sysprog 13
Sysprog 13
 
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdfInterfacepackage PJ1; public interface SimpleFractionInterface.pdf
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
 
About Go
About GoAbout Go
About Go
 
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdfimplement the following funtions. myg1 and myg2 are seperate. x and .pdf
implement the following funtions. myg1 and myg2 are seperate. x and .pdf
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docx
 
Go Says WAT?
Go Says WAT?Go Says WAT?
Go Says WAT?
 
Templates
TemplatesTemplates
Templates
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 

More from aptcomputerzone

b) advanced threat (advanced persistant threat ) is serious and cont.pdf
b) advanced threat (advanced persistant threat ) is serious and cont.pdfb) advanced threat (advanced persistant threat ) is serious and cont.pdf
b) advanced threat (advanced persistant threat ) is serious and cont.pdfaptcomputerzone
 
Book.java To change this license header, choose License Hea.pdf
Book.java  To change this license header, choose License Hea.pdfBook.java  To change this license header, choose License Hea.pdf
Book.java To change this license header, choose License Hea.pdfaptcomputerzone
 
Answere.none of the above (all are true statements)Applications.pdf
Answere.none of the above (all are true statements)Applications.pdfAnswere.none of the above (all are true statements)Applications.pdf
Answere.none of the above (all are true statements)Applications.pdfaptcomputerzone
 
Answer1.To print the size of my home directory $HOME we usee.pdf
Answer1.To print the size of my home directory $HOME we usee.pdfAnswer1.To print the size of my home directory $HOME we usee.pdf
Answer1.To print the size of my home directory $HOME we usee.pdfaptcomputerzone
 
AnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdf
AnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdfAnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdf
AnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdfaptcomputerzone
 
AnsTriploidy is a desirable trait in all angiosperms except orchi.pdf
AnsTriploidy is a desirable trait in all angiosperms except orchi.pdfAnsTriploidy is a desirable trait in all angiosperms except orchi.pdf
AnsTriploidy is a desirable trait in all angiosperms except orchi.pdfaptcomputerzone
 
a) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdf
a) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdfa) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdf
a) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdfaptcomputerzone
 
A pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdfA pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdfaptcomputerzone
 
26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf
26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf
26. DNA molecule contains deoxyribose and is double stranded.27. B.pdfaptcomputerzone
 
(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf
(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf
(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdfaptcomputerzone
 
1. what is the central dogma of geneticsans Central dogma of gen.pdf
1. what is the central dogma of geneticsans Central dogma of gen.pdf1. what is the central dogma of geneticsans Central dogma of gen.pdf
1. what is the central dogma of geneticsans Central dogma of gen.pdfaptcomputerzone
 
Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf
  Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf  Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf
Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdfaptcomputerzone
 
A group of isotopes representing various stages of radioactive decay.pdf
  A group of isotopes representing various stages of radioactive decay.pdf  A group of isotopes representing various stages of radioactive decay.pdf
A group of isotopes representing various stages of radioactive decay.pdfaptcomputerzone
 
n should be 5 here Calcium sulfate (or calcium su.pdf
                     n should be 5 here Calcium sulfate (or calcium su.pdf                     n should be 5 here Calcium sulfate (or calcium su.pdf
n should be 5 here Calcium sulfate (or calcium su.pdfaptcomputerzone
 
Molarity = moles Litres of solution. Work out .pdf
                     Molarity = moles  Litres of solution.  Work out .pdf                     Molarity = moles  Litres of solution.  Work out .pdf
Molarity = moles Litres of solution. Work out .pdfaptcomputerzone
 
How many ml of .20 M NaOh are required to neutral.pdf
                     How many ml of .20 M NaOh are required to neutral.pdf                     How many ml of .20 M NaOh are required to neutral.pdf
How many ml of .20 M NaOh are required to neutral.pdfaptcomputerzone
 
It looks like you have two molecules there. CH3C.pdf
                     It looks like you have two molecules there.  CH3C.pdf                     It looks like you have two molecules there.  CH3C.pdf
It looks like you have two molecules there. CH3C.pdfaptcomputerzone
 
In H3O+ ion there are four electronpairs on the v.pdf
                     In H3O+ ion there are four electronpairs on the v.pdf                     In H3O+ ion there are four electronpairs on the v.pdf
In H3O+ ion there are four electronpairs on the v.pdfaptcomputerzone
 
     According to periodic properties Theatomic radius is increases .pdf
     According to periodic properties Theatomic radius is increases .pdf     According to periodic properties Theatomic radius is increases .pdf
     According to periodic properties Theatomic radius is increases .pdfaptcomputerzone
 
Why does one would like all of this social control stuff the most g.pdf
Why does one would like all of this social control stuff the most g.pdfWhy does one would like all of this social control stuff the most g.pdf
Why does one would like all of this social control stuff the most g.pdfaptcomputerzone
 

More from aptcomputerzone (20)

b) advanced threat (advanced persistant threat ) is serious and cont.pdf
b) advanced threat (advanced persistant threat ) is serious and cont.pdfb) advanced threat (advanced persistant threat ) is serious and cont.pdf
b) advanced threat (advanced persistant threat ) is serious and cont.pdf
 
Book.java To change this license header, choose License Hea.pdf
Book.java  To change this license header, choose License Hea.pdfBook.java  To change this license header, choose License Hea.pdf
Book.java To change this license header, choose License Hea.pdf
 
Answere.none of the above (all are true statements)Applications.pdf
Answere.none of the above (all are true statements)Applications.pdfAnswere.none of the above (all are true statements)Applications.pdf
Answere.none of the above (all are true statements)Applications.pdf
 
Answer1.To print the size of my home directory $HOME we usee.pdf
Answer1.To print the size of my home directory $HOME we usee.pdfAnswer1.To print the size of my home directory $HOME we usee.pdf
Answer1.To print the size of my home directory $HOME we usee.pdf
 
AnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdf
AnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdfAnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdf
AnswerAcidithiobacillusExplanationMany greenhouse gases occu.pdf
 
AnsTriploidy is a desirable trait in all angiosperms except orchi.pdf
AnsTriploidy is a desirable trait in all angiosperms except orchi.pdfAnsTriploidy is a desirable trait in all angiosperms except orchi.pdf
AnsTriploidy is a desirable trait in all angiosperms except orchi.pdf
 
a) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdf
a) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdfa) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdf
a) Rotenone is an isoflavone used as insecticide and pesticide. It w.pdf
 
A pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdfA pivot table is a table which is used to store the summary of a cer.pdf
A pivot table is a table which is used to store the summary of a cer.pdf
 
26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf
26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf
26. DNA molecule contains deoxyribose and is double stranded.27. B.pdf
 
(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf
(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf
(A) Current Ratio = Current AssetsCurrent LiabilitiesCurrent Asse.pdf
 
1. what is the central dogma of geneticsans Central dogma of gen.pdf
1. what is the central dogma of geneticsans Central dogma of gen.pdf1. what is the central dogma of geneticsans Central dogma of gen.pdf
1. what is the central dogma of geneticsans Central dogma of gen.pdf
 
Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf
  Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf  Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf
Phosphorous reacts readily with hydrogen to form hydrides; with halo.pdf
 
A group of isotopes representing various stages of radioactive decay.pdf
  A group of isotopes representing various stages of radioactive decay.pdf  A group of isotopes representing various stages of radioactive decay.pdf
A group of isotopes representing various stages of radioactive decay.pdf
 
n should be 5 here Calcium sulfate (or calcium su.pdf
                     n should be 5 here Calcium sulfate (or calcium su.pdf                     n should be 5 here Calcium sulfate (or calcium su.pdf
n should be 5 here Calcium sulfate (or calcium su.pdf
 
Molarity = moles Litres of solution. Work out .pdf
                     Molarity = moles  Litres of solution.  Work out .pdf                     Molarity = moles  Litres of solution.  Work out .pdf
Molarity = moles Litres of solution. Work out .pdf
 
How many ml of .20 M NaOh are required to neutral.pdf
                     How many ml of .20 M NaOh are required to neutral.pdf                     How many ml of .20 M NaOh are required to neutral.pdf
How many ml of .20 M NaOh are required to neutral.pdf
 
It looks like you have two molecules there. CH3C.pdf
                     It looks like you have two molecules there.  CH3C.pdf                     It looks like you have two molecules there.  CH3C.pdf
It looks like you have two molecules there. CH3C.pdf
 
In H3O+ ion there are four electronpairs on the v.pdf
                     In H3O+ ion there are four electronpairs on the v.pdf                     In H3O+ ion there are four electronpairs on the v.pdf
In H3O+ ion there are four electronpairs on the v.pdf
 
     According to periodic properties Theatomic radius is increases .pdf
     According to periodic properties Theatomic radius is increases .pdf     According to periodic properties Theatomic radius is increases .pdf
     According to periodic properties Theatomic radius is increases .pdf
 
Why does one would like all of this social control stuff the most g.pdf
Why does one would like all of this social control stuff the most g.pdfWhy does one would like all of this social control stuff the most g.pdf
Why does one would like all of this social control stuff the most g.pdf
 

Recently uploaded

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 

Recently uploaded (20)

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 

operating system ubuntu,linux,MacProgram will work only if you g.pdf

  • 1. //operating system ubuntu,linux,Mac Program will work only if you give command like sum 2 3 or sum 4 (sub 4 3) #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);
  • 2. 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]; 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; }
  • 3. /*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){ 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); }
  • 4. } }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)); } } } 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]
  • 5. 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 Solution //operating system ubuntu,linux,Mac Program will work only if you give command like sum 2 3 or sum 4 (sub 4 3) #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*/
  • 6. 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) { printf("Unable to allocate memory. "); exit(1); } for (c = 0 ; c < length ; c++) { *(pointer+c) = *(string+position-1); string++; } *(pointer+c) = '0'; return pointer; }
  • 7. /*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, " "); 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);
  • 8. 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{ 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){
  • 9. 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': 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