- #include "expression.h"
-
#include <stdlib.h>
-
#include <stdio.h>
-
#include <string.h>
-
-
-
xContainer splitTerm(string,int,int);
-
-
-
-
expContainer createExpression()
-
{
-
expContainer exp;
-
-
/*allocate memory*/
-
exp = (expContainer)malloc(sizeof(struct expressions));
-
if(exp != NULL)
-
{
-
/*set inital terms to zero*/
-
exp->numberOfTerms = 0;
-
return exp; /*return pointer*/
-
}
-
else
-
printf("Out Of Heap!!!\n");
-
return NULL;
-
-
}
-
-
void string2Expression(string inString,expContainer* inExp)
-
{
-
string tempString;
-
int length;
-
int i=0, j=0,sign=0;
-
-
tempString = (string)calloc(1,sizeof(char));
-
/*get string length*/
-
length = strlen(inString);
-
-
/*determine first term sign*/
-
-
if(inString[0] == '-')
-
sign = 0;
-
-
else if(inString[0] == '+')
-
sign = 1;
-
-
else if(inString[0] == '*')
-
sign = 2;
-
else
-
sign = 3;
-
-
/*The operation down there is to splite the whole string into separate term*/
-
/*the term is split by detecting the sign*/
-
-
for(i=0; i < length; i++)
-
{
-
/*pass if char not '+' and not '-' and current pos is not the end of the string*/
-
if(inString[i] != '+' && inString[i] != '-' && 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 = (string)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 = (xContainer*)calloc(3,sizeof(xContainer));
-
else
-
(*inExp)->exp = (xContainer*)realloc((*inExp)->exp,sizeof(xContainer)*((*inExp)->numberOfTerms+1));
-
-
-
(*inExp)->exp[(*inExp)->numberOfTerms] = splitTerm(tempString,sign,j);
-
(*inExp)->numberOfTerms++;
-
-
if(inString[i] == '-')
-
sign = 0;
-
-
else if(inString[i] == '+')
-
sign = 1;
-
-
else if(inString[i] == '*')
-
sign = 2;
-
-
else
-
sign = 3;
-
-
}
-
j = 0;
-
tempString = (string)realloc(tempString,sizeof(char)*0);
-
}
-
}
-
}
-
-
/*function to handle term*/
-
xContainer splitTerm(string inTerm,int inSign, int length)
-
{
-
string tempString;
-
xContainer part;
-
int xFlag=0,powerFlag=0;
-
int i,j=0;
-
-
part = (xContainer)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 = (string)calloc(1,sizeof(char));
-
-
/*function below is to determine x and square root*/
-
for(i=0; i < length; i++)
-
{
-
if(inTerm[i] != 'x' && inTerm[i] != '^')
-
{
-
tempString = (string)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 = (string)realloc(tempString,sizeof(char)*0);
-
-
}
-
}
-
-
if(xFlag)
-
{
-
part->isX = 1;
-
part->KnowX = 0;
-
}
-
-
if(powerFlag)
-
part->x_power = atof(tempString);
-
-
switch(inSign)
-
-
case 0:
-
case 1:
-
case 2:
-
case 3:
-
-
-
return part;
-
-
-
}
With this code i am able to capture expression like 3x+9x^2-2/7..something like that...So i am wondering if i want to have some like (((x-3)+(x+9))(x+6)) or (x+3)(3x-9)(4-x)....The figure can be anything up to the user... So how am i goin to write the code?
2 1560
can someone help with it? I am trying to get my code going... thank thank
Banfa 9,065
Recognized Expert Moderator Expert
Give a more precise description of your problem. Why will your code capture the first expression successfully but not the second?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: James Aguilar |
last post by:
Hello all,
I am trying to use an istringstream to do some input off of cin by lines.
The following snippet does not work:
char buf;
cin.getline(buf, 90);
istringstream line1(string(buf));
|
by: Aravind |
last post by:
Hi folks.
I have a form, frmHistory, which has 3 toggle buttons (1 of which is
tglName, which I will be using to demonstrate my problem). The buttons
are used to sort the form (explanations below). I have set the
button's "Triple State" value to Yes, and the default value to False.
Here is the macro that I used for tglName (I assigned the macro to the
button's On Click event):
...
|
by: Dimitris Georgakopuolos |
last post by:
Hello,
I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However, I want to use the
text included within the brackets to do a lookup so that I can replace
the expression with the new text.
For example:
|
by: Ben Dewey |
last post by:
Hey,
I have only been playing with regular expressions for some time. I am
working on some code that parses and object 560 event log. I have created
two expressions the first one which works okay is for the actual csv of each
log. The second one parses out the description of the log. My problem is
with the accesses section of the description.
How do I parse multiple groups that have the same name. When I do a for
each through the...
|
by: |
last post by:
Here is an interesting one. Running asp.net 2.0 beta 2. I have a regular
expression used in a regex validator that works on the client side in
Firefox but not in IE. Any ideas? IE always reports the field is invalid.
The expression is:
^(?!\d)(?=.*\d)(?=.*)(?=.*)(?=.*).{8,25}$
If I enter "Test_Field1" Firefox considers it valid on client side, IE
doesnt. Server side considers it valid too because when I submit the form in
| |
by: =?Utf-8?B?SkF1bA==?= |
last post by:
I am currently working on a project and need to get a return… even if that
return is a failure. I must also add that I have no control over either the
Regular Expression that will be used or the text file that will be parsed,
and while my text example is XML, there is no guarantee that the text file
will be xml.
This is a portion of the file I am testing with:
<Stock ticker="ACEGX" type="EQUITY" title="VAN KAMPEN STRATEGIC GROWTH
FUND...
|
by: Tomasz J |
last post by:
Hello Developers,
I have a control derived from System.Web.UI.WebControls.WebControl. Control
has this property:
public string Value
{
set { _value = value; }
get { return _value; }
|
by: Tomasz J |
last post by:
Hello developers,
I bind my TextBox control specyfying a format stored in my application
global ApplicationContext object - it has a static string CurrencyFormat
property.
The problem - this works fine:
Text='<%# Eval("Price", ApplicationContext.CurrencyFormat) %>'
|
by: Alex Vinokur |
last post by:
Hi,
I have compilation problem on SUN CC compiler with template while
using option -m64 (64-bit addressing model).
No problem while using option -m32 (32-bit addressing model)
$ CC -V
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
|
by: Stefano Sabatini |
last post by:
Hi all, I'm encountering this while trying to implement a factory
singleton method to generate objects.
The singleton has a static map which binds a static creation function
defined in each class to the type of the object to be created.
Here it is the code, which is a modification of the wikipedia C++
factory example code:
----------------------------------8<--------------------------------
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |