473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

help needed

24
hi guys below are the code for user to key in the epression like x^3+9x+8, i am only able to do + n - sign so i am wondering how can i add on to do expression like x^7*9x+9 or even (5x+3)(9/78+9)???? I really need help on this. Thank you

#include "expression.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


express chkterm(char*,int,int);

myexp createExpression()
{
myexp exp;

/*allocate memory*/
exp=(myexp)malloc(sizeof(struct expressions));
if(exp!=NULL)
{
exp->numberOfTerms=0; /*set inital terms to zero*/
return exp; /*return pointer*/
}
else
printf("Out of range!!!\n");
return NULL;
}
void string2Expression(char* inString,myexp* inExp)
{
char* tempString;
int length;
int i=0;
int j=0;
int sign=0;

tempString=(char*)calloc(1,sizeof(char));

length=strlen(inString);
/*determine the sign*/
if(inString[0]=='+')
sign = 1;
else
sign = 0;

for(i=0;i<length;i++)
{
/*checl for the position of the string*/
if(inString[i]!='+'&&inString[i]!='-'&&i!=length-1)
{
tempString = (char*)realloc(tempString,sizeof(char)*j+1);
tempString[j]=inString[i];
j++;
continue;
}
else
{
/*to add the last value before the end of the string*/
if(i==length-1)
{
tempString = (char*)realloc(tempString,sizeof(char)*j+1);
tempString[j]=inString[i];
j++;
}

if(j != 0)
{
/*save the term into expression format*/
if((*inExp)->numberOfTerms == 0)
(*inExp)->exp = (express*)calloc(3,sizeof(express));
else
(*inExp)->exp = (express*)realloc((*inExp)->exp,sizeof(express)*((*inExp)->numberOfTerms+1));


(*inExp)->exp[(*inExp)->numberOfTerms] = chkterm(tempString,sign,j);
(*inExp)->numberOfTerms++;

if(inString[i] == '+')
sign = 1;

else
sign = 0;

}
j = 0;
tempString = (char*)realloc(tempString,sizeof(char)*0);
}
}
}

/*function to handle term*/
express chkterm(char* inTerm,int inSign, int length)
{
char* tempString;
express part;
int xFlag=0,powerFlag=0;
int i,j=0;

part = (express)malloc(sizeof(struct terms));
part->KnowX = 0;
part->positive = inSign;
part->isX = 0;
part->x_power = 1;
part->x_times = 1;
part->x_value = 0;

tempString = (char*)calloc(1,sizeof(char));

/*function to check if x and power were given*/
for(i=0; i < length; i++)
{
if(inTerm[i] != 'x' && inTerm[i] != '^')
{
tempString = (char*)realloc(tempString,sizeof(char)*j+1);
tempString[j] = inTerm[i];
j++;

if(i == length-1 && !xFlag && !powerFlag)
{
part->x_value = atof(tempString);
part->KnowX = 1;
}
continue;
}
else
{
if(inTerm[i] == 'x' && strlen(tempString) > 0)
{
part->x_times = atof(tempString);
xFlag = 1;
}
else if(inTerm[i] == 'x')
xFlag = 1;

if(inTerm[i] == '^')
{
powerFlag = 1;
if(!xFlag)
part->x_value = atof(tempString);
}

j = 0;
tempString = (char*)realloc(tempString,sizeof(char)*0);

}
}

if(xFlag)
{
part->isX = 1;
part->KnowX = 0;
}

if(powerFlag)
part->x_power = atof(tempString);

if(inSign)
part->positive = 1;
else
part->positive = 0;

return part;


}
}
}
Oct 6 '06 #1
4 1728
vninja
40
Ok really simple and not hard at all using namespace std and iostream.


double ex(double x, double pwr);

void main()
{
double power,
power = ex(2,2);
cout << power;
}

double ex(double x, double pwr)
{
for(int i=1; i<=pwr; i++)
x*=x;
return x;
}


there you go this can do many numbers to many powers just stay within range of the double.
Oct 12 '06 #2
Banfa
9,065 Expert Mod 8TB
double ex(double x, double pwr);

void main()
{
double power,
power = ex(2,2);
cout << power;
}

double ex(double x, double pwr)
{
for(int i=1; i<=pwr; i++)
x*=x;
return x;
}
Have you tried this code?

a. it doesn't compile because you have a , after double power but if fixed then

b. It outputs 16 which is the wrong answer

c. It will never work for negative powers but it goes ahead and tries anyway

d. Although power is a double it only works for integer values

e. basing any sort of comparison on a double is a dodgy move (although at least in this case the variable is not changed)

f. since you use a int for the loop control variable having pwr as double is pointless because values of pwr outside the range an int will just cause it to go into an infiniate loop (probably, actually it invokes undefined behaviour because adding 1 to a signed int with a value of MAX_INT produces this).


Solutions would be accept that you can't do negative or non integer powers and rewite the function with a unsigned long type for pwr

Expand|Select|Wrap|Line Numbers
  1. double ex(double x, unsigned long pwr)
  2. {
  3.     double result = 1.0;
  4.     for(unsigned long i=0; i<pwr; i++)
  5.     {
  6.         result*=x;
  7.     }
  8.     return result;
  9. }
  10.  
however you will find that even this is slow for large values of pwr, it can be speeded up as

Expand|Select|Wrap|Line Numbers
  1. double ex(double x, unsigned long pwr)
  2. {
  3.     double result = 1.0;
  4.     double multi = x;
  5.     unsigned long powers = 1;
  6.     unsigned long increment;
  7.  
  8.     for(unsigned long i=0; i<pwr; i+=increment)
  9.     {
  10.         if (powers > pwr-i)
  11.         {
  12.             if (powers/2 >= pwr-i)
  13.             {
  14.                 multi = x;
  15.                 powers = 1;
  16.             }
  17.             else
  18.             {
  19.                 while (powers > pwr-i)
  20.                 {
  21.                     multi /= x;
  22.                     powers--;
  23.                 }
  24.             }
  25.         }
  26.  
  27.         increment = powers;
  28.  
  29.         result*=multi;
  30.  
  31.         if ((i+increment) <= pwr/2)
  32.         {
  33.             multi=result;
  34.             powers=i+increment;
  35.         }
  36.     }
  37.     return result;
  38. }
  39.  
Which gets the result more quickly by using squaring where possible

but probably what you want to do is use the math.h function

double pow(double, double);

that does it all for you.
Oct 13 '06 #3
cool17
24
hi guys

But i need to write my code in C not in C++. That is why i am facing the problem...hopefully u guys will be able to help me again...thank
Oct 13 '06 #4
Banfa
9,065 Expert Mod 8TB
The ex function is compileable C, it is just the cout in the example that isn't so just replace it with a printf.
Oct 13 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: stu_gots | last post by:
I have been losing sleep over this puzzle, and I'm convinced my train of thought is heading in the wrong direction. It is difficult to explain my circumstances, so I will present an identical...
7
by: ChadDiesel | last post by:
Hello everyone, I'm having a problem with Access that I need some help with. The short version is, I want to print a list of parts and part quantities that belong to a certain part group---One...
7
by: Tina | last post by:
I have an asp project that has 144 aspx/ascx pages, most with large code-behind files. Recently my dev box has been straining and taking long times to reneder the pages in the dev environment. ...
10
by: Mae Lim | last post by:
Dear all, I'm new to C# WebServices. I compile the WebService project it return no errors "Build: 1 succeeded, 0 failed, 0 skipped". Basically I have 2 WebMethod, when I try to invoke the...
2
by: trihanhcie | last post by:
I m currently working on a Unix server with a fedora 3 as an os My current version of mysql is 3.23.58. I'd like to upgrade the version to 5.0.18. After downloading from MYSQL.COM the package on...
2
by: Steve K | last post by:
I got a bit of a problem I like some help on. I'm designing an online training module for people that work in food processing plants. This is my target audience. These workers have little or no...
3
by: Kitana907 | last post by:
Hi- I'm attempting to write a module that uses and updates info from two tables and does the following: Opens the recordset of a table called "tblstoreinv" If the Needed Field in the...
9
by: smartbei | last post by:
Hello, I am a newbie with python, though I am having a lot of fun using it. Here is one of the excersizes I am trying to complete: the program is supposed to find the coin combination so that with...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.