| | |
calculator program in C -help needed
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 2
Reputation:
Solved Threads: 0
Main problem i am having is that with the program it can have a 2 string equation - eg 25 R or 25 S (Reciprocal and Squareroot)
or a 3 string equation where the second string will be a number and the third string will be the operator. eg 10 5 + or -10 20 -
I am not sure how to get this to work - i can get the coding to work if its just the 2 string equation but then if it is a 3 string equation i come unstuck.
Can anyone provide some assistance please.
or a 3 string equation where the second string will be a number and the third string will be the operator. eg 10 5 + or -10 20 -
I am not sure how to get this to work - i can get the coding to work if its just the 2 string equation but then if it is a 3 string equation i come unstuck.
Can anyone provide some assistance please.
I think you will want to investigate Reverse Polish Notation as it is applied to calculators. google has lots of information about it.
•
•
Join Date: Oct 2006
Posts: 2
Reputation:
Solved Threads: 0
Sorry about lack of coding in previous post.
here we go :
Comments are included in the code.
Main points are the calculations for squareroot and reciprocal.
also the error check for alphabetic characters input in the first 22 strings.
// Calculator
//main things that need to be checked/corrected
scan 1st two strings for anything other than '-' and numbers
if so then give ERROR1
//check calculations for squareroot and reciprocal
//otherwise i think most stuff seems to work ok.
#include <stdlib.h>
#include <stdio.h>
//#include <math.h>
#include <conio.h>
#include <string.h>
//#include <ctype.h>
//#define RECIPROCAL (1/Num1)
//#define SQUAREROOT Num1^0.5
#define ISDIGIT(x) ((x) >= '0' && (x) <='9')
#define ERROR5 "Input strings too long. Applies to numbers and operators."
#define ERROR4 "Operation not possible."
#define ERROR3 "Division by zero."
#define ERROR2 "Unknown operation."
#define ERROR1 "Number contains alphabetic characters."
#define MAX 11
#define RECIPROCAL 1/x
#define SQUAREROOT
void main()
{
int Add, Subtract, Multiply;
float Divide = 0.0;
double Squareroot;
double Reciprocal;
int Num1;
int Num2;
char Num2atoi;
char Str2[MAX];
char Str1[MAX];
char Operator;
printf("\nPlease enter the required calculation\n");
scanf("%s %s", &Str1, &Str2);
//checks on num1 for numbers or - at the start only - 10 digits max
// if ((Num1)
// error check #5 - test string length is max10
{
if (strlen(Str1) > MAX-1)
printf(ERROR5);
else if
(strlen(Str2) > MAX-1)
printf(ERROR5);
else if
((*Str2) == 'S')
//what is the equation for this??????
printf("this is Squareroot \n" );
//squareroot=Num1^0.5;
// printf("=%lf ", Num1^y);
else if
((*Str2) == 'R')
//n1=(float) Num1;
printf("=Recip\n" );
//error1 - test for alphabetic characters
//check cos this doesnt work.
else if
ISDIGIT(*Str1)
//(*Str1<'0' || *Str1>'0' || *Str1=='-')
// Num1= atoi(Str1);
// Num2= atoi(Str2);
printf("its a digit");
else
printf("its not a valid number");
{
scanf(" %s", &Operator);
Num1= atoi(Str1);
Num2= atoi(Str2);
switch(Operator)
{
case '+':
printf("= %d", Num1+Num2);
break;
case '-':
printf("= %d \n", Num1-Num2);
break;
case '*':
printf("=%d ", Num1*Num2);
break;
case '/':
if(*Str2 == '0')
{
printf(ERROR3);
break;
}
else
//Num1= atof(Str1);
{ //Num2= atof(Str1);
//does this need to change for decimal. Divide=Num1/Num2;
printf("=%f", Divide);
break;
}
default :
printf(ERROR2);
}
}
getch();
}}
here we go :
Comments are included in the code.
Main points are the calculations for squareroot and reciprocal.
also the error check for alphabetic characters input in the first 22 strings.
// Calculator
//main things that need to be checked/corrected
scan 1st two strings for anything other than '-' and numbers
if so then give ERROR1
//check calculations for squareroot and reciprocal
//otherwise i think most stuff seems to work ok.
#include <stdlib.h>
#include <stdio.h>
//#include <math.h>
#include <conio.h>
#include <string.h>
//#include <ctype.h>
//#define RECIPROCAL (1/Num1)
//#define SQUAREROOT Num1^0.5
#define ISDIGIT(x) ((x) >= '0' && (x) <='9')
#define ERROR5 "Input strings too long. Applies to numbers and operators."
#define ERROR4 "Operation not possible."
#define ERROR3 "Division by zero."
#define ERROR2 "Unknown operation."
#define ERROR1 "Number contains alphabetic characters."
#define MAX 11
#define RECIPROCAL 1/x
#define SQUAREROOT
void main()
{
int Add, Subtract, Multiply;
float Divide = 0.0;
double Squareroot;
double Reciprocal;
int Num1;
int Num2;
char Num2atoi;
char Str2[MAX];
char Str1[MAX];
char Operator;
printf("\nPlease enter the required calculation\n");
scanf("%s %s", &Str1, &Str2);
//checks on num1 for numbers or - at the start only - 10 digits max
// if ((Num1)
// error check #5 - test string length is max10
{
if (strlen(Str1) > MAX-1)
printf(ERROR5);
else if
(strlen(Str2) > MAX-1)
printf(ERROR5);
else if
((*Str2) == 'S')
//what is the equation for this??????
printf("this is Squareroot \n" );
//squareroot=Num1^0.5;
// printf("=%lf ", Num1^y);
else if
((*Str2) == 'R')
//n1=(float) Num1;
printf("=Recip\n" );
//error1 - test for alphabetic characters
//check cos this doesnt work.
else if
ISDIGIT(*Str1)
//(*Str1<'0' || *Str1>'0' || *Str1=='-')
// Num1= atoi(Str1);
// Num2= atoi(Str2);
printf("its a digit");
else
printf("its not a valid number");
{
scanf(" %s", &Operator);
Num1= atoi(Str1);
Num2= atoi(Str2);
switch(Operator)
{
case '+':
printf("= %d", Num1+Num2);
break;
case '-':
printf("= %d \n", Num1-Num2);
break;
case '*':
printf("=%d ", Num1*Num2);
break;
case '/':
if(*Str2 == '0')
{
printf(ERROR3);
break;
}
else
//Num1= atof(Str1);
{ //Num2= atof(Str1);
//does this need to change for decimal. Divide=Num1/Num2;
printf("=%f", Divide);
break;
}
default :
printf(ERROR2);
}
}
getch();
}}
![]() |
Similar Threads
- C++ Calculator Program (C++)
- calculator needed... (Assembly)
- starting a calculator program in C (C)
- Java Swing Calculator program not running. It has 0 errors (Java)
- Wierd error messages with calculator program (C++)
- need help with calculator program in C (C)
Other Threads in the C Forum
- Previous Thread: help with conversion calculator
- Next Thread: Drawing shapes with a while statement
Views: 1785 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework inches include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue number oddnumber odf opendocumentformat opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






