Thread: string calculater and string function, i have a question, plz help me

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    22

    Question string calculater and string function, i have a question, plz help me

    hi, I wrote a program, that get numbers and operation with string, and thats work, but I will do this, on function program, this mean I can't get 2 digit numbers for coefficient of terms, because the number of terms is not ditinct that we definition variant like str1 and str2 in calculater, how can I do this? but I will do this with string. plz help me
    Last edited by abbaskhan; 07-11-2011 at 02:44 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    120
    2 things:

    if ur gonna post all your program here, dont bother, either ask the question u must ask, or, if u have a problem with code u wrote, try to replicate your problem in a smaller program.

    second, if u ARE going to post ALL your programs code here, then at least give us a break and post something organized and that can be read easily.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It looks like you're reading in individual characters of a string, and if one of those characters is a number, you convert it into a float. However, your problem (if I understand correctly) is that a two-digit value is composed of two characters.

    Therefore, it would appear that they cannot be converted to a number in this manner. This would best be done by going a different route.

    For example, you can cleverly use "strtok()" to break the string into tokens, and then turn each (now individualized) set of characters representing a number into a double with the "atof()" function.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shiroaisu View Post
    2 things:

    if ur gonna post all your program here, dont bother, either ask the question u must ask, or, if u have a problem with code u wrote, try to replicate your problem in a smaller program.

    second, if u ARE going to post ALL your programs code here, then at least give us a break and post something organized and that can be read easily.
    Your propensity for "sms speak" notwithstanding...

    "you" not "u" ...
    "your" not "ur"..

    Also check out 2 really wonderful inventions... Capitalization and Punctuation.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by CommonTater View Post
    Your propensity for "sms speak" notwithstanding...

    "you" not "u" ...
    "your" not "ur"..

    Also check out 2 really wonderful inventions... Capitalization and Punctuation.
    Ok, ok, i guess you're right. I'll start paying more attention to those two wondefull inventions and start using "you" instead of "u", but my point still stands, I think that by doing those things, op would get allot more help, but hell, I could be wrong of course.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shiroaisu View Post
    I think that by doing those things, op would get allot more help, but hell, I could be wrong of course.
    I didn't say you were wrong...

    Merely illegible.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    ok, thanks from all of you, but master Matticus, how can I separate numbers from this shape string 6x^2+2x^1+1 whith strtok? because we dont know how much is the terms, in 3 term we will have this 6x^2+2x^1+1 in 4 term we will have this shape example 12x^4+6x^2+2x^1+1
    plz help on this shape with strtok.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'll tell you how to use strtok for this, but it probably isn't the tool you need to use. It is a problem that once strtok parses the expression, the operators will be overwritten, and you won't know what to do with the terms.

    Code:
    char *strtok(char *str, const char *delim);
    If str is the expression, delim should contain all the characters which make terms. "+-/*" for example.

    So you call:
    Code:
    /* expression is a string which has "12x^4+6x^2+2x^1+1" in it. */
    
    term = strtok(expression, "+-/*");
    
    /* term is assigned "12x^4" which is part of expression. expression has been edited like so: "12x^4\06x^2+2x^1+1". */
    You need to read this page. I also recommend reading this page for an alternative.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @abbaskhan: Listen to WhiteFlags on this one. I gave a suggestion I thought might help, but is probably not the best solution.

    But for the record, since I'm at work and can't really write it out fully, I just copied the example from the link I gave you and replaced the "search string" variable with your equation and second arguments in the two "strtok()" functions with operators, and received this output:

    Code:
    Item #0 is 12x^4.
    Item #1 is 6x^2.
    Item #2 is 2x^1.
    Item #3 is 1.
    You could then work with these results - but, as WhiteFlags mentioned, there is a flaw in this idea that makes it non-ideal for your requirements. But try scanning the page of the link I provided to see if anything there might better suit your needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-04-2010, 01:48 PM
  2. Replies: 2
    Last Post: 05-19-2008, 10:42 PM
  3. Replies: 9
    Last Post: 04-16-2007, 03:02 PM
  4. String Function Question
    By devilsknight in forum C Programming
    Replies: 5
    Last Post: 07-28-2006, 09:06 PM
  5. Replies: 4
    Last Post: 01-22-2002, 11:13 PM