Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 9:10 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: C++ calculator with modification.
PostPosted: Mon Mar 02, 2009 7:54 am 
Offline
Newbie
User avatar

Joined: Mon Mar 02, 2009 7:47 am
Posts: 1
Has thanked: 0 time
Have thanks: 0 time

Hi,
i have this calculator,but i need diffrent code style for the same calculator.i need this for my project.please modify this calculator ASAP. :cry: 8O


#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;
}


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: C++ calculator with modification.
PostPosted: Mon Mar 02, 2009 10:24 pm 
Offline
Moderator
User avatar

Joined: Fri Nov 21, 2008 6:18 pm
Posts: 51
Location: thessaloniki
Has thanked: 0 time
Have thanks: 2 time
read the policy about homework projects......

_________________
if you want make an effort yourself no one will make it for you...
best regards


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Simple JavaScript calculator  JavaScript examples  msi_333  0
 Scientific Calculator  Finished Projects  Casper  5
 Build Calculator in OpenGL  C-C++  HolyAngel927  1
 Making calculator in JAVA  Java  lovieducky  0
 C++ Calculator  C++ examples  mileloader  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team