473,508 Members | 2,460 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 1552
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
2983
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
2277
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...
11
5350
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,...
4
2171
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...
4
1356
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...
5
1326
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...
3
1416
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
4577
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...
1
2646
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:...
8
20394
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...
0
7231
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
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
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,...
1
7066
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...
0
5643
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,...
1
5059
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...
0
4724
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...
0
3214
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...
0
435
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...

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.