Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99843

Re: [Structure of Array] pointer arithmetic in structures

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: [Structure of Array] pointer arithmetic in structures
Date 2016-12-18 21:26 +0000
Organization A noiseless patient Spider
Message-ID <87lgvd53pc.fsf@bsb.me.uk> (permalink)
References (5 earlier) <fd2917ab-e3a3-4f14-8651-a59e7a358639@googlegroups.com> <o36oh3$qtk$1@dont-email.me> <5f9d145d-4034-4595-b541-a7bc83133f9f@googlegroups.com> <87wpex57cu.fsf@bsb.me.uk> <5e9b2ec0-f2b8-422a-a32f-a5f11ac6e3c1@googlegroups.com>

Show all headers | View raw


Dexterr <dejangru@gmail.com> writes:

> On Sunday, December 18, 2016 at 9:07:41 PM UTC+1, Ben Bacarisse wrote:
>> Dexterr <dejangru@gmail.com> writes:
>> 
>> >> If the input will always be in the form A/B or (A/B), where A and B are 
>> >> integers, then that makes things easier. Read as two integers 
>> >> (discarding "(", "/" and ")"), and store as two integers. Optionally 
>> >> divide them (as (double)A/B otherwise you'll get 0) to calculate with.
>> >> 
>> > YES. Thats what I intend to do!
>> >
>> > I am looking for elegant solution right now.
>> >
>> >   const char str1[2] = "(";
>> >   const char str2[2] = ")";
>> >
>> >   char *token_01 = "";
>> >   char *token_02 = "";
>> >
>> >   token_01 = strtok(ptr_parts[2].value, str1);
>> >   token_02 = strtok(ptr_parts[2].value, str2);
>> 
>> Just some more food for thought:
>> 
>> #include <stdio.h>
>> 
>> int main(void)
>> {
>>      char line[100];
>>      while (fgets(line, sizeof line, stdin)) {
>>           int num, den;
>>           if (sscanf(line, " (%d/%d)", &num, &den) == 2)
>>                printf("%g\n", (double)num/den);
>>           else puts("Input line should have the form \"(A/B)\"");
>>      }
>> }
>
> Thanks Mr Ben. ;)
> Yes, this is very nice example but when user put from the terminal
> values such as (47/100).
> But, in my case it is about input text file which consist values such
> (A/B) and I must use this and convert from string into float form, to
> make some calculations and then result from float form turn again into
> a form (A/B).

You are seeing distinctions that don't exit.  I read a line from a
stream just as you do and I extract some numbers from the resulting
string.  This example is directly applicable to your situation.

But there is a problem...  If you have been following the advice to do
it one way, it can be unhelpful to be shown other ways.  If you choose
not to use sscanf, when you've finished your exercise, I'll post the
full version of how I would have done it so you can compare (though you
may need to remind me).

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 07:51 -0800
  Re: [Structure of Array] pointer arithmetic in structures fir <profesor.fir@gmail.com> - 2016-12-18 08:15 -0800
    Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 09:31 -0800
  Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 16:27 +0000
    Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 09:43 -0800
      Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 18:13 +0000
        Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 10:31 -0800
      Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-18 10:39 -0800
  Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 10:02 -0800
    Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-18 10:20 -0800
      Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 10:40 -0800
        Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-18 10:54 -0800
          Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:10 -0800
        Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 18:54 +0000
          Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:05 -0800
          Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:22 -0800
            Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-18 19:38 +0000
              Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 11:45 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-18 20:07 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 12:35 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-18 21:26 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-18 13:47 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-19 00:23 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 10:53 -0800
                Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-19 19:11 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 11:20 -0800
                Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-19 19:31 +0000
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-19 20:06 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 11:27 -0800
                Re: [Structure of Array] pointer arithmetic in structures BartC <bc@freeuk.com> - 2016-12-19 19:40 +0000
                Re: [Structure of Array] pointer arithmetic in structures Dexterr <dejangru@gmail.com> - 2016-12-19 12:09 -0800
                Re: [Structure of Array] pointer arithmetic in structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-19 20:15 +0000
                Re: [Structure of Array] pointer arithmetic in structures "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> - 2016-12-24 17:12 +1100
                Re: [Structure of Array] pointer arithmetic in structures Barry Schwarz <schwarzb@dqel.com> - 2016-12-19 16:15 -0800

csiph-web