Total members 11895 |It is currently Tue Dec 03, 2024 6:45 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This is calculator,but i need different code style for the same calculator.i need this for my project.please modify this calculator.
cpp code
#include <ctype.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Defining the constant values for error
#define ERROR "Error"

//Check the error if number contains alphabetic characters
#define ERROR1 "ERROR Number 1"

//Check the ERROR for unknown operation
#define EROR2 "ERROR Number 2"

//ERROR for division by Zero
#define EROR3 "ERROR Number 3"

//ERROR if operation not possible
#define EROR4 "ERROR Number 4"

//ERROR if input strings are longer than limit
#define EROR5 "ERROR Number 5"

//Defining prototypes for functions
int DATA_RECALL(int *num_1, int *num_2, char *operation);
void CALCULATE(int num1, int num2, char operator_);
int VALID_CHECK_1 (char input[11], int *output);
int VALID_CHECK_2(char input[11], int *output, char*valid_operator);
int ERROR_CHECK (int retval);
int OPERATOR_VERIFICATION(char optor_in, char *optor_out);

void main ()
{
printf("Please enter the values for required calculation :\n");
char string[5];
int num_1 = 0, num_2 = 0;
while(strcmp(string,"QUIT"))
{
flushall();
char operation = ' ';
if (!ERROR_CHECK( DATA_RECALL(&num_1, &num_2, &operation)))
CALCULATE (num_1, num_2, operation);
printf("\n-----------------------------\n");
printf("Type 'QUIT' to exit.\n");
printf("Press any key to continue\n");
printf("And press Enter key: ");
scanf("%s",string);
printf("-----------------------------\n");
printf("\nPlease enter the values for required calculation :\n");
puts("");

}
}

int DATA_RECALL(int *num_1, int *num_2, char *operation)
{
int value= 0;
int counter = 0;
char buffer[48], overflow[24]="", tmpNum2[12]="\0", tmp_operator = ' ', tmpNum1[12]= "\0";

gets (buffer);
sscanf(buffer, "%11s", tmpNum1);
sscanf(buffer, "%*s %11s", tmpNum2);
sscanf(buffer, "%*s %*s %c", &tmp_operator );
sscanf(buffer, "%*s %*s %*c %20s", overflow);

if ( tmpNum1!= "\0")
{
counter = 1;
if ( tmpNum2!= "\0")
{
counter = 2;
if ( tmp_operator!= ' ')
counter = 3;
}
}
value = VALID_CHECK_1(tmpNum1, num_1);

if (value == 0)
value = VALID_CHECK_2(tmpNum2, num_2, operation);

if ((*operation == ' ') && (value == 0))
{
value = OPERATOR_VERIFICATION(tmp_operator, operation);
}

if ((value == 0) && (strlen(overflow) > 0))
value = 5;
return value;
}

void CALCULATE(int num1, int num2, char optr)
{
float a,b,c=0.00001,d,e;
e=num1;
float srt;

switch (optr)
{
case '+': /*step for the Addition */
printf("%d\n", num1 + num2);
break;
case '-': /*step for the Subtraction*/
printf("%d\n", num1 - num2);
break;
case '*': /*step for the Multiplication*/
printf("%d\n", num1 * num2);
break;
case '/': /*step for the Divison*/
if (num2 == 0)
{
puts(EROR3);
break;
}
else
{
printf("%f\n",(float)num1 / num2);
break;
}
case 'S': /* step for the Square Root*/
if (num1 < 0)
{
puts(EROR4);
break;
}
else
{
a=e;d=a*a;
while(d-e>=c)
{
b=(a+(e/a))/2;
a=b;
d=a*a;
srt=a;
}
printf("%f\n",srt);
break;
}
case 'R':/*Calculatting the reciprocal*/
if (num1 == 0)
{
puts(EROR3);
break;
}
else
{
printf("%f\n",1/(float) num1);
break;
}
}
}

int VALID_CHECK_1 (char input[20], int *output)
{
int length = 0;
int ret_value = 0;
length = strlen(input);

if (length == 0)
{
ret_value = 2; /*Nothing was Entered*/
}
else if (length <= 10)
{
for (int i = 0; i < length; i++)
{
if ((length ==1)&& (input[0]=='-'))
{
ret_value = 1;
break;
}
if (!(((i == 0) && (input[i] == '-')) || (isdigit(input[i]))))
{
ret_value = 1; /*detects -sign and check if there are no alphabetic
characters. */
break;
}
}
if (ret_value == 0)
*output = atoi(input);/*All valid,convert to 'int'*/
}
else
ret_value = 5;/*more then 10 digits*/
return ret_value;
}

int VALID_CHECK_2(char input[20], int *output, char *valid_operator)
{
char *str = "RS", tmp_operator = *valid_operator; /*modification: assign *valid operator to tmp_operator */
int length = 0;
int ret_value = 0;

/* Following check if:the numbers consist of a maximum of 10 characters*/
length = strlen(input);
if (length == 0)
{
ret_value = 4;
}
else if(length == 1)
{
if (strrchr(str, input[0]))
{
*valid_operator = input[0];/*one digit and an operator*/
}
else if (isdigit(input[0]))
{
*output = atoi(input);/* If the input value is a number*/
}
else if ((input[0]== '+' )||(input[0]== '-')||(input[0]=='/')||(input[0]=='*'))
ret_value = 4;
else if ((tmp_operator == ' ')&&((input[0]!='R')||(input[0]!= 'S')))
ret_value = 2;
else
ret_value = 1;
}
else if (length <=10)
{
for (int i=0; i < length; i++)
{
if ((tmp_operator =='R')||(tmp_operator =='S'))
{
ret_value = 4;
break;
}
if (!(((i == 0) && (input[i] == '-')) || (isdigit(input[i]))))
{
ret_value = 1;
break;
}
}
}
else
ret_value = 5;
if (ret_value ==0)
{
*output = atoi(input); //Converting the value to int if valid
}
return ret_value;
}

int OPERATOR_VERIFICATION(char operIn, char *valid_operator)
{
char *str = "/*-+";
int ret_value = 0;
if (operIn =='\0') /*considering that return value isnt true*/
ret_value = 4;
else if (strrchr(str, operIn))
*valid_operator = operIn;
else
ret_value = 2;
return ret_value;
}


int root(int num1)
{
return(sqrt((double)num1));
}


int ERROR_CHECK (int ERROR_value)
{
switch (ERROR_value)
{
case 0:
ERROR_value = 0;
break;
case 1:
puts(ERROR1);
break;
case 2:
puts(EROR2);
break;
case 3:
puts(EROR3);
break;
case 4:
puts(EROR4);
break;
case 5:
puts(EROR5);
break;
default:
puts(ERROR);
break;
}
return ERROR_value;
}





Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : C++ calculator with modification.
 Calculator     -  
 C++ Calculator     -  
 Calculator     -  
 Simple Calculator     -  
 Scientific Calculator     -  
 Making calculator in JAVA     -  
 Build Calculator in OpenGL     -  
 Complex numbers calculator (C++)     -  
 Simple JavaScript calculator     -  
 how to program a windows standard calculator     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com