473,666 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with expression

24 New Member
Expand|Select|Wrap|Line Numbers
  1. #include "expression.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6.  
  7. xContainer splitTerm(string,int,int);
  8.  
  9.  
  10.  
  11. expContainer createExpression()
  12. {
  13.     expContainer exp;
  14.  
  15.     /*allocate memory*/
  16.     exp = (expContainer)malloc(sizeof(struct expressions));
  17.     if(exp != NULL)
  18.     {
  19.       /*set inital terms to zero*/
  20.         exp->numberOfTerms = 0;
  21.         return exp; /*return pointer*/
  22.     }
  23.     else
  24.         printf("Out Of Heap!!!\n");
  25.         return NULL;
  26.  
  27. }
  28.  
  29. void string2Expression(string inString,expContainer* inExp)
  30. {
  31.     string tempString;
  32.     int length;
  33.     int i=0, j=0,sign=0;
  34.  
  35.     tempString = (string)calloc(1,sizeof(char));
  36.     /*get string length*/
  37.     length = strlen(inString);
  38.  
  39.     /*determine first term sign*/
  40.  
  41.     if(inString[0] == '-')
  42.         sign = 0;
  43.  
  44.     else if(inString[0] == '+')
  45.         sign = 1;
  46.  
  47.     else if(inString[0] == '*')
  48.         sign = 2;
  49.     else
  50.         sign = 3;
  51.  
  52.     /*The operation down there is to splite the whole string into separate term*/
  53.     /*the term is split by detecting the sign*/
  54.  
  55.     for(i=0; i < length; i++)
  56.     {
  57.       /*pass if char not '+' and not '-' and current pos is not the end of the string*/
  58.         if(inString[i] != '+' && inString[i] != '-' && inString[i] != '*' && inString[i] != '/' && i != length-1)
  59.         {
  60.  
  61.             tempString = (char*)realloc(tempString,sizeof(char)*j+1);
  62.             tempString[j] = inString[i];
  63.             j++;
  64.             continue;
  65.         }
  66.         else
  67.         {    
  68.           /*to add the last value before the end of the string*/
  69.             if(i == length-1)
  70.             {
  71.                     tempString = (string)realloc(tempString,sizeof(char)*j+1);
  72.                     tempString[j] = inString[i];
  73.                     j++;
  74.             }
  75.  
  76.             if(j != 0)
  77.             {
  78.               /*save the term into expression format*/
  79.                 if((*inExp)->numberOfTerms == 0)
  80.                     (*inExp)->exp = (xContainer*)calloc(3,sizeof(xContainer));
  81.                 else
  82.                     (*inExp)->exp = (xContainer*)realloc((*inExp)->exp,sizeof(xContainer)*((*inExp)->numberOfTerms+1));
  83.  
  84.  
  85.                 (*inExp)->exp[(*inExp)->numberOfTerms] = splitTerm(tempString,sign,j);
  86.                 (*inExp)->numberOfTerms++;
  87.  
  88.                 if(inString[i] == '-')
  89.                     sign = 0;
  90.  
  91.                 else if(inString[i] == '+')
  92.                     sign = 1;
  93.  
  94.                 else if(inString[i] == '*')
  95.                     sign = 2;
  96.  
  97.                 else
  98.                     sign = 3;
  99.  
  100.             }
  101.             j = 0;
  102.             tempString = (string)realloc(tempString,sizeof(char)*0);
  103.         }
  104.     }
  105. }
  106.  
  107. /*function to handle term*/
  108. xContainer splitTerm(string inTerm,int inSign, int length)
  109. {
  110.     string tempString;
  111.     xContainer part;
  112.     int xFlag=0,powerFlag=0;
  113.     int i,j=0;
  114.  
  115.     part = (xContainer)malloc(sizeof(struct terms));
  116.     part->KnowX = 0;
  117.     part->positive = inSign;
  118.     part->isX = 0;
  119.     part->x_power = 1;
  120.     part->x_times = 1;
  121.     part->x_value = 0;
  122.  
  123.     tempString = (string)calloc(1,sizeof(char));
  124.  
  125.     /*function below is to determine x and square root*/
  126.     for(i=0; i < length; i++)
  127.     {
  128.         if(inTerm[i] != 'x' && inTerm[i] != '^')
  129.         {
  130.             tempString = (string)realloc(tempString,sizeof(char)*j+1);
  131.             tempString[j] = inTerm[i];
  132.             j++;
  133.  
  134.             if(i == length-1 && !xFlag && !powerFlag)
  135.             {
  136.                 part->x_value = atof(tempString);
  137.                 part->KnowX = 1;
  138.             }
  139.             continue;
  140.         }
  141.         else
  142.         {
  143.             if(inTerm[i] == 'x' && strlen(tempString) > 0)
  144.             {
  145.                 part->x_times = atof(tempString);
  146.                 xFlag = 1;            
  147.             }
  148.             else if(inTerm[i] == 'x')
  149.                 xFlag = 1;
  150.  
  151.             if(inTerm[i] == '^')
  152.             {
  153.                 powerFlag = 1;
  154.                 if(!xFlag)
  155.                     part->x_value = atof(tempString);
  156.             }
  157.  
  158.             j = 0;
  159.             tempString = (string)realloc(tempString,sizeof(char)*0);
  160.  
  161.         }    
  162.     }
  163.  
  164.     if(xFlag)
  165.     {
  166.         part->isX = 1;
  167.         part->KnowX = 0;
  168.     }
  169.  
  170.     if(powerFlag)
  171.         part->x_power = atof(tempString);
  172.  
  173.     switch(inSign)
  174.  
  175.         case 0:
  176.         case 1:
  177.         case 2:
  178.         case 3:
  179.  
  180.  
  181.     return part;
  182.  
  183.  
  184. }

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?
Oct 23 '06 #1
2 1560
cool17
24 New Member
can someone help with it? I am trying to get my code going... thank thank
Oct 25 '06 #2
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?
Oct 25 '06 #3

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

Similar topics

6
2987
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));
0
2281
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): ...
11
5371
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:
4
2173
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...
4
1360
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
5
1335
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...
3
1421
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; }
6
4590
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) %>'
1
2657
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
8
20419
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<--------------------------------
0
8876
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, 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...
0
8784
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 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...
1
8556
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,...
0
8642
tracyyun
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...
0
7387
agi2029
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...
1
6198
isladogs
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...
0
5666
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();...
0
4198
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...
2
1777
bsmnconsultancy
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...

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.