Hello Experts,
I was wondering if there is a built-in function that I could use to convert a 'char' to
a 'double' without loosing the initial values. Yes! It's HomeWork. I just need some pointers. This program takes in a postscript expression and changes it back to infix. I am getting stuck because the data was inputed as 'char' and I need it to be 'double'. I tried 'strtod' but it didn't work.
Newbie Code below .....hehe
--------------------------
----------
----
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MAX = 8;
double array[MAX];
bool isempty(int);
int pop(void);
int push(double x);
bool isfull(int num);
bool isnumber(char num);
int validate(char p1, int, int);
void evaluate(char postfix[]);
int packStack(char getData[], int length);
/*************************
**********
**********
**********
**********
**********
**********
********
** FUNCTION MAIN : ........
**************************
**********
**********
**********
**********
**********
**********
********/
int main()
{
int num = -1;
int tos = -1;
double answer = 0;
char array2[20];
char * p1 = NULL;
p1 = array2;
cout<<"Enter your expression"<<endl;
cin.getline(array2,20,'\n'
);
int length = strlen(array2);
for(int j = 0; j < length; j++)
{
array[j] = array2[j];
if(array2[j] == ' ')
continue;
cout<<"HERE: "<<array2[j]<<endl;
}
answer = packStack(array2,length);
cout<<"THE ANSWER: "<<answer<<endl;
return 0;
}
/*************************
**********
**********
**********
**********
**********
**********
********
FUNCTION DECLARATIONS : PUSH, POP, ISEMPTY, ISFULL, TOS, VALIDATE, SP........
**************************
**********
**********
**********
**********
**********
**********
********/
bool isempty(int num)
{
if(num == -1)
return true;
else
return false;
}
bool isfull(int num)
{
int max = 10;
if(num == max - 1)
return true;
else
return false;
}
int pop(void)
{
int num = 5;
if(isempty(num) == 1)
{
cout<<"ERROR: Stack UnderFlow\n";
}
else
{
array[num--];
}
return array[num];
}
int push(double x)
{
int num = 5;
char *p1;
char mim = array[0];
double test;
//test = (strtod(mim,&p1));
if(isfull(num) == 1)
{
cout<<"ERROR: Stack OverFlow\n";
}
else
{
array[num++] = x;
}
return array[num];
}
bool isnumber(char num)
{
return isdigit(num);
}
int validate(char p1, int ops, int nums)
{
char mul = '*';
char div = '/';
char min = '-';
char pows = '$';
char plus = '+';
if(p1 == mul)
return 6;
if(p1 == 'min')
return 5;
if(p1 == div)
return 4;
if(p1 == plus)
return 3;
if(p1 == pows)
return 2;
else
return 1;
return 0;
}
int packStack(char getData[], int length)
{
char *numbers = new char [length];
char *operators = new char[length];
char *infix = new char[length];
char *postfix = new char[length];
int opcount = 0;
int numcount = 0;
for(int j = 0; j < length; j++)
{
postfix[j] = getData[j];
if(isnumber(getData[j])==1
)
{
numbers[j] = getData[j];
numcount++;
}
if(isnumber(getData[j])==0
)
{
operators[j] = getData[j];
opcount++;
cout<<endl;
}
}
char temp;
for(int k = 0; k < length; k++)
{
temp = getData[k];
int ret = 0;
ret = validate(temp,opcount,numc
ount);
if (ret == 6)
{
cout<<"Multiply";
double temp2 = 0;
temp2 = pop();
push(pop() * temp2);
}
if(ret == 3)
{
cout<<"ADD"<<endl;
int temp2 = pop();
push(pop() + temp2);
}
}
return 0;
}