Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple calculator v3, works with real numbers,
- // with how it works prints and comments
- #include<stdio.h>
- #include <stdlib.h> // for atof() function
- int main()
- {
- // c is operator, str is entered string, snum1 and snum2 are entered strings of numbers num1 and num2
- char c, str[20], snum1[20], snum2[20];
- // i is counter for str, i1 is counter for snum1, i2 is counter for snum2
- int i=0, i1=0, i2=0, sign_num1=1, sign_num2=1;
- // num1 and num2 are entered real numbers
- float num1,num2;
- printf("\n CALCULATOR (for real numbers) \n");
- printf("\n Enter first number and operator (+,-,*,/) and second number: \n");
- printf("\n Example: -123+-456 \n\n");
- scanf("%s",str); // no need for &str because str is address of str[0]
- printf("\n 1. Entered string: %s \n", str);
- // print str[i]
- while(str[i]!='\0'){ // from beginning to end of str
- printf("\n 2. str[%d] = %c \n",i,str[i]);
- i++; // next character of str
- }
- i=0; // reset counter
- /*
- read sign_num1 (default is +1) of first number num1
- if first character of str:
- -, sign_num1 is -1
- +, sign_num1 is +1 (default is +1)
- */
- if(str[0]=='-'){
- sign_num1 = -1;
- i++; // next character of str, this is first character of snum1
- }
- else if(str[0]=='+'){
- //(sign_num1 default is +1)
- i++; // next character of str, this is first character of snum1
- }
- printf("\n 3. i = %d \t sign_num1 = %d \n",i,sign_num1);
- // read snum1[i] from str[i] until str[i] is operator (not digit)
- // while str[i] is digit and it is not the end of str
- while(isdigit(str[i]) && str[i]!='\0'){ // from beginning to end of str
- snum1[i1]=str[i]; // copy str[i] to snum1[i1]
- printf("\n 4. str[%d] = %c \t snum1[%d] = %c \n",i,str[i],i1,snum1[i1]);
- i++; // next character of str
- i1++; // next character of snum1
- }
- snum1[i1]='\0'; // finish snum1
- printf("\n 5. snum1 = %s \n", snum1);
- num1=sign_num1*atof(snum1); // convert string snum1 to float num1
- printf("\n 6. num1 = %f \n", num1);
- c=str[i]; // read operator c from str[i]
- printf("\n 7. i = %d \t operator c = %c \n",i,c);
- i++; // next character of str, this is first character of snum2
- printf("\n 8. i = %d \t str[%d] = %c\n",i,i,str[i]);
- /*
- read sign_num2 (default is +1) of second number num2
- if str[i]:
- -, sign_num2 is -1
- +, sign_num2 is +1 (default is +1)
- */
- if(str[i]=='-'){
- sign_num2 = -1;
- i++; // next character of str, this is first character of snum2
- }
- else if(str[i]=='+'){
- //(sign_num2 default is +1)
- i++; // next character of str, this is first character of snum2
- }
- printf("\n 9. i = %d \t sign_num2 = %d \n",i,sign_num2);
- // read snum2[i] from str[i] until the end of str
- // while str[i] is digit and it is not the end of str
- while(isdigit(str[i]) && str[i]!='\0'){ // from first character of snum2 in str, to end of str
- snum2[i2]=str[i]; // copy str[i] to snum2[i2]
- printf("\n 10. str[%d] = %c \t snum2[%d] = %c \n",i,str[i],i2,snum2[i2]);
- i++; // next character of str
- i2++; // next character of snum2
- }
- printf("\n 11. i = %d \t i2 = %d \n",i,i2);
- snum2[i2]='\0'; // finish snum2
- i2=0; // reset counter
- while(snum2[i2]!='\0'){
- printf("\n 12. snum2[%d] = %c \n",i2,snum2[i2]);
- i2++; // next character of snum2
- }
- printf("\n 13. snum2 = %s \n", snum2);
- num2=sign_num2*atof(snum2); // convert string snum2 to float num2
- printf("\n 14. num2 = %f \n", num2);
- printf("\n (%f) %c (%f) = ",num1,c,num2);
- switch(c) // analyze operator c
- {
- case '+':
- printf("(%f) \n",num1+num2);
- break;
- case '-':
- printf("(%f) \n",num1-num2);
- break;
- case '*':
- printf("(%f) \n",num1*num2);
- break;
- case '/':
- printf("(%f) \n",num1/num2);
- break;
- default:
- printf("Error! Invalid Operator.");
- }
- printf("\n\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement