C-Program to implement a Simple Calculator
-
Upload
sendash-pangambam -
Category
Education
-
view
74 -
download
2
description
Transcript of C-Program to implement a Simple Calculator
![Page 1: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/1.jpg)
C-Program to implement a Simple Calculator
Developed by: Sendash Pangambam
![Page 2: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/2.jpg)
For suggestions and queries be free to contact me at [email protected]
This program can be used as a simple calculator.
Just type the arithmetic expression you want to compute in the following format:
<Operand_1><Operator><Operand_2>
e.g. 1900+20145
Operators:
ADDITION + e.g. 1234+4321
SUBTRACTION - e.g. 9712-3421
MULTIPLICATION * e.g. 5621*245
DIVISION / e.g. 915/56
EXPONENTIAL ^ e.g. 1231^64
***Terms and conditions:
This program is written for calculations involving integers only.
Inconvenience is regretted.
![Page 3: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/3.jpg)
For suggestions and queries be free to contact me at [email protected]
HOW MY PROGRAM WORKS?
1. A string of the format given above is supplied as input, e.g. 21900*914990
2. The two integers on the both sides of the operator, „*‟ in this case, are
extracted from the input string.
3. According to the operator in the input string, calculation is done.
VARIABLES USED IN THIS PROGRAM:
1. ch – To read any character from the keyboard.
2. exp – Input expression.
3. op – Operator.
4. fop, sop – 1st and 2nd operand.
5. tfop, tsop – Temporary 1st and 2nd operand, used during division.
6. ddif – Difference in digits of extracted integers and length of input string.
7. And some other variables that you have already known are also used.
![Page 5: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/5.jpg)
For suggestions and queries be free to contact me at [email protected]
/*Simple Calculator*/ #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<string.h> long int appzero(long int n,int d) /*Function to append zeroes to the right of a number*/ { int i; for(i=0;i<d;i++) n=n*10; return(n); } int ndgts(long int n) /*Function to count the number of digits in a given integer*/
![Page 6: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/6.jpg)
For suggestions and queries be free to contact me at [email protected]
{ int i; for(i=1;n!=0;i++) n=n/10; return(i-1); } long int reverse(long int n) /*Function to find reverse of an integer*/ { long int rev=0; while(n>0) { rev=rev*10+(n%10); n=n/10; } return(rev); }
![Page 7: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/7.jpg)
For suggestions and queries be free to contact me at [email protected]
void main() { char ch; clrscr(); /*SOME INSTRUCTION ABOUT THIS PROGRAM*/ printf("READ ME:\n\n\tThis program can be used as a simple calculator.\n"); printf("\n\tJust type the arithmetic expression you want to compute. \n \t
Syntax for input expression:\n \t\t\t\t < Operand_1 >< Operator >< Operand_2 >\n\t\t\t\t eg. 1900+20145");
printf("\nOperators:\n\tADDITION \t+\teg. 1234+4321 \n\tSUBTRACTION \t-\teg. 9712-3421 \n\tMULTIPLICATION \t*\teg. 5621*245 \n\tDIVISION \t/\teg. 915/56 \n\tEXPONENTIAL \t^\teg. 1231^64\n\n");
printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+= +=+=+=+=+=+=+=+="); printf("\n\n***Terms and conditions:\n\t\tThis program is written for
calculations involving integers.\n\t\tDecimal calculations will be updated in the next versions.\n\t\tInconvinience is regretted.\n\n");
printf("=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
![Page 8: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/8.jpg)
For suggestions and queries be free to contact me at [email protected]
+=+=+=+=+=+=+=+=\n"); getch(); clrscr(); printf("DO YOU WISH TO CONTINUE ? (Y/N) "); ch=getch(); if(ch=='N'||ch=='n') { clrscr(); printf("PRESS ANY KEY TO EXIT "); getch(); exit(1); } clrscr(); printf("Let's try my simple calculator program"); getch(); while(1) { char *exp,op;
![Page 9: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/9.jpg)
For suggestions and queries be free to contact me at [email protected]
long int fop,sop; double tfop,tsop; int ddif=0; clrscr(); printf("Enter the expression: "); gets(exp); fop=atol(exp); strrev(exp); sop=reverse(atol(exp)); ddif=strlen(exp) - ndgts(fop) - ndgts(sop); sop=appzero(sop,ddif-1); strrev(exp); if(exp[ndgts(fop)]=='+') { clrscr(); printf("\n%s = %ld",exp,fop+sop); } if(exp[ndgts(fop)]=='-')
![Page 10: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/10.jpg)
For suggestions and queries be free to contact me at [email protected]
{ clrscr(); printf("\n%s = %ld",exp,fop-sop); } if(exp[ndgts(fop)]=='*') { clrscr(); printf("\n%s = %ld",exp,fop*sop); } if(exp[ndgts(fop)]=='/') { clrscr(); tfop=fop; tsop=sop; printf("\n%s = %lf",exp,tfop/tsop); } if(exp[ndgts(fop)]=='^') {
![Page 11: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/11.jpg)
For suggestions and queries be free to contact me at [email protected]
clrscr(); tfop=fop; tsop=sop; printf("\n%s = %lf",exp,pow(tfop,tsop)); } getch(); clrscr();
printf("Do you have another calculation to do? (Y/N)\n\n*** For more help type 'H' \n\n\t\t\t");
ch=getch(); if(ch=='n'||ch=='N') { clrscr(); printf("\tThanks for your co-operation\n\tPress any key to exit"); getch(); exit(1); }
![Page 12: C-Program to implement a Simple Calculator](https://reader034.fdocuments.in/reader034/viewer/2022051313/548d91a7b479590d2b8b4a9d/html5/thumbnails/12.jpg)
For suggestions and queries be free to contact me at [email protected]
if(ch=='h'||ch=='H') main(); } }
TRY THIS PROGRAM AND CHECK WHETHER IT WORKS OR NOT.