there is a string like

0123456789
string a = "25 + 5.6 - (54 - (-61))"
a[0] = 2
a[1] = 5
  2  =
  3  = +
  4  = 
  5  = 5
  6  = .
  7  = 6
  8  =
  9  = -
 10  =
 11  = ( 
 12  = 5
 13  = 4
..

how can i get these numbers(integers or doubles) ?

is there function that works like that or similar ?
or i will do it :

f( string, index)
f(a,0) which returns 25
f(a,5) which returns 5.6
f(a,12) which returns 54
f(a,18) which returns -61

Recommended Answers

All 6 Replies

Have you tried using combinations of strtok(), atoi(), and atof()?

strtok() tokenizes character arrays (c-style strings) and atoi() and atof() pull numbers out of strings.

Have you tried using combinations of strtok(), atoi(), and atof()?

strtok() tokenizes character arrays (c-style strings) and atoi() and atof() pull numbers out of strings.

Yes maybe with strtok it helps me.
I did a function fur integers now.
I try strtok for floats then.

#include <stdio.h>
#include <string.h>
#include <math.h>
int main ()
{
	char str[] ="25 + 5.6 - (- 54 - (-61))";
	char * pch;
	printf ("Splitting string \"%s\" into tokens:\n",str);
	pch = strtok (str," ()+");
	printf ("the string %s\n",pch);
	while (pch != NULL)
	{
		printf("the number = %f\n",atof(pch));		
		pch = strtok (NULL, " ()+");
		printf ("the string %s\n",pch);
	}
	return 0;
}

but the minus character with space is a problem.

I think you'll wind up with something weird, but what happens if you eliminate (space) as a delimiter? Also, this code looks more like C than C++, you might consider trying over there as well.

You can do a char by char analysis of the string looking char one at a time using (usually somewhat convoluted) logic to determine what is legal to follow any given char, particularly if you want to handle something like "6 - (- 54 - (-61))". That space between the - and 54 is something you and I can handle okay, but it is a bit a futsy business when you try to write code to handle it.

whatever I did my function(it's a member function) it's ok now

void skipSpaces(void)
	{
		while( (lastPoint + 1) != point && isspace(problemString[point]) )
		{
			point++;			
		}
	}
int takeNumber(void)
	{
		int number = 0;
		char digitC = 0;
		double digit;
		double grade = 0;
		bool  numberIsNegative = false;
		//point is the index (where we are at string)
		int tempPoint = point; 
		//lastPoint string lenght
		while( point != (lastPoint + 1) && ( isdigit( digitC = problemString[point] )  || problemString[point] == '-' ||  problemString[point] == '+' ))
		{
			//looking the number is negative or positive
			if(problemString[point] == '-')
			{
				numberIsNegative = true;
				point ++;
			}
			else if (problemString[point] == '+')
				point++;
			
			skipSpaces();
			tempPoint = point;

			//looking grade of number example if the number 121 it will be 3
                        //if it's 1041 it will be four
			while(  tempPoint != (lastPoint + 1) && isdigit( digitC = problemString[tempPoint] ) )
			{
				grade++;
				tempPoint++;
			}
			//taking the number from string
			while( point != (lastPoint + 1) &&  isdigit( digitC = problemString[point]  ) )
			{
				digit = atoi(&digitC);	
				number = number + digit* pow(BASE,grade-1);//BASE = 10
				point++;
				grade--;				
			}
			if (grade == 0)
				break;
		}

		if(numberIsNegative)
			number = -number;

		return number;
	}

but that had been easy part of my program. it will be a calculator and you can use parenthesises.

it can solve
(-2)+1-((-5)-6+4)*(5-(-6))*(-5-3)-1
result = -618

but it gives the wrong value with that simple question :)
48/12*4

result = 1 :) i didn't think the precedence

it must be 16
i will handle this tomorrow

thx for all replies.

I think you'll wind up with something weird, but what happens if you eliminate (space) as a delimiter? Also, this code looks more like C than C++, you might consider trying over there as well.

i wrote the code in C. . Because I was just trying atof and other functions.
in fact i edited some code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.