Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #99809 > unrolled thread
| Started by | Dexterr <dejangru@gmail.com> |
|---|---|
| First post | 2016-12-18 07:51 -0800 |
| Last post | 2016-12-19 16:15 -0800 |
| Articles | 14 on this page of 34 — 6 participants |
Back to article view | Back to comp.lang.c
[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
Page 2 of 2 — ← Prev page 1 [2]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-18 21:26 +0000 |
| Message-ID | <87lgvd53pc.fsf@bsb.me.uk> |
| In reply to | #99837 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-18 13:47 -0800 |
| Message-ID | <93d38915-3e06-4916-8578-0cec387a6990@googlegroups.com> |
| In reply to | #99843 |
On Sunday, December 18, 2016 at 10:26:30 PM UTC+1, Ben Bacarisse wrote:
> 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.
Mr Ben, did you read the specifications what I need to do in my example ?
The algorithm for calculations is extrem complicated. I am very desperate because I am
doing this for 2 weeks completely alone. My collegue work nothing. We are in 2 students group.
I have to do it till Tuesday and I am in the position to cry. I fill so sad. If I fail I must wait one year but it is not to wait, I realy need to learn C and understand. So many hours invest and after all it will be nothing.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-19 00:23 +0000 |
| Message-ID | <8760mg6a31.fsf@bsb.me.uk> |
| In reply to | #99844 |
Dexterr <dejangru@gmail.com> writes:
> On Sunday, December 18, 2016 at 10:26:30 PM UTC+1, Ben Bacarisse wrote:
>> 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).
>
> Mr Ben, did you read the specifications what I need to do in my example ?
> The algorithm for calculations is extrem complicated.
No, I must have missed that. I saw only that you must do some
computations but I did not what they were. But I don't think the nature
of the computations changes much since the code you posted dealt only
with interpreting the input. My suggestion was specifically about that.
But, as I said, if using sscanf is not a helpful suggestion, then just
forget I said anything.
> I am very
> desperate because I am
> doing this for 2 weeks completely alone. My collegue work nothing. We
> are in 2 students group.
> I have to do it till Tuesday and I am in the position to cry. I fill
> so sad. If I fail I must wait one year but it is not to wait, I realy
> need to learn C and understand. So many hours invest and after all it
> will be nothing.
You should get partial credit for whatever parts you have managed to
complete, and there should be a process for you to register the fact
that your team mate as not contributed anything.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-19 10:53 -0800 |
| Message-ID | <6669c766-06eb-49b1-9b69-7a3802e1b673@googlegroups.com> |
| In reply to | #99850 |
On Monday, December 19, 2016 at 1:23:21 AM UTC+1, Ben Bacarisse wrote:
> Dexterr <dejangru@gmail.com> writes:
>
> > On Sunday, December 18, 2016 at 10:26:30 PM UTC+1, Ben Bacarisse wrote:
> >> 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).
> >
> > Mr Ben, did you read the specifications what I need to do in my example ?
> > The algorithm for calculations is extrem complicated.
>
> No, I must have missed that. I saw only that you must do some
> computations but I did not what they were. But I don't think the nature
> of the computations changes much since the code you posted dealt only
> with interpreting the input. My suggestion was specifically about that.
>
> But, as I said, if using sscanf is not a helpful suggestion, then just
> forget I said anything.
>
> > I am very
> > desperate because I am
> > doing this for 2 weeks completely alone. My collegue work nothing. We
> > are in 2 students group.
> > I have to do it till Tuesday and I am in the position to cry. I fill
> > so sad. If I fail I must wait one year but it is not to wait, I realy
> > need to learn C and understand. So many hours invest and after all it
> > will be nothing.
>
> You should get partial credit for whatever parts you have managed to
> complete, and there should be a process for you to register the fact
> that your team mate as not contributed anything.
>
> --
> Ben.
Thanks Mr Ben.
I like your solution, but it not work.
---
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FILENAME "netlist.txt"
#define NUM_FIELDS 4
typedef struct
{
char *component;
char *a_node;
char *b_node;
char *value;
}Components;
void readComponentFile(void);
char line[130] = "";
int main(void)
{
readComponentFile();
return 0;
}
void readComponentFile()
{
FILE *fp = fopen(FILENAME, "r");
char line[100];
if(fp != NULL)
{
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)\"");
}
}
}
}
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-12-19 19:11 +0000 |
| Message-ID | <o39b9l$36j$1@dont-email.me> |
| In reply to | #99864 |
On 19/12/2016 18:53, Dexterr wrote:
> On Monday, December 19, 2016 at 1:23:21 AM UTC+1, Ben Bacarisse wrote:
>> Dexterr <dejangru@gmail.com> writes:
>>
>>> On Sunday, December 18, 2016 at 10:26:30 PM UTC+1, Ben Bacarisse wrote:
>>>> 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).
>>>
>>> Mr Ben, did you read the specifications what I need to do in my example ?
>>> The algorithm for calculations is extrem complicated.
>>
>> No, I must have missed that. I saw only that you must do some
>> computations but I did not what they were. But I don't think the nature
>> of the computations changes much since the code you posted dealt only
>> with interpreting the input. My suggestion was specifically about that.
>>
>> But, as I said, if using sscanf is not a helpful suggestion, then just
>> forget I said anything.
>>
>>> I am very
>>> desperate because I am
>>> doing this for 2 weeks completely alone. My collegue work nothing. We
>>> are in 2 students group.
>>> I have to do it till Tuesday and I am in the position to cry. I fill
>>> so sad. If I fail I must wait one year but it is not to wait, I realy
>>> need to learn C and understand. So many hours invest and after all it
>>> will be nothing.
>>
>> You should get partial credit for whatever parts you have managed to
>> complete, and there should be a process for you to register the fact
>> that your team mate as not contributed anything.
>>
>> --
>> Ben.
>
> Thanks Mr Ben.
>
> I like your solution, but it not work.
>
> ---
>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
>
> #define FILENAME "netlist.txt"
> #define NUM_FIELDS 4
>
> typedef struct
> {
> char *component;
> char *a_node;
> char *b_node;
> char *value;
> }Components;
>
> void readComponentFile(void);
>
>
> char line[130] = "";
>
> int main(void)
> {
> readComponentFile();
> return 0;
> }
>
> void readComponentFile()
> {
> FILE *fp = fopen(FILENAME, "r");
>
> char line[100];
>
> if(fp != NULL)
> {
> while (fgets(line, sizeof line, stdin))
Change stdin to fp if you're going to read from the file.
Then I found it worked if the file looks like this:
(10/3)
(48/100)
(1/1000000)
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-19 11:20 -0800 |
| Message-ID | <4ae5cf7f-028c-47f0-b97b-030775324478@googlegroups.com> |
| In reply to | #99865 |
On Monday, December 19, 2016 at 8:11:15 PM UTC+1, Bart wrote:
> On 19/12/2016 18:53, Dexterr wrote:
> > On Monday, December 19, 2016 at 1:23:21 AM UTC+1, Ben Bacarisse wrote:
> >> Dexterr <dejangru@gmail.com> writes:
> >>
> >>> On Sunday, December 18, 2016 at 10:26:30 PM UTC+1, Ben Bacarisse wrote:
> >>>> 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).
> >>>
> >>> Mr Ben, did you read the specifications what I need to do in my example ?
> >>> The algorithm for calculations is extrem complicated.
> >>
> >> No, I must have missed that. I saw only that you must do some
> >> computations but I did not what they were. But I don't think the nature
> >> of the computations changes much since the code you posted dealt only
> >> with interpreting the input. My suggestion was specifically about that.
> >>
> >> But, as I said, if using sscanf is not a helpful suggestion, then just
> >> forget I said anything.
> >>
> >>> I am very
> >>> desperate because I am
> >>> doing this for 2 weeks completely alone. My collegue work nothing. We
> >>> are in 2 students group.
> >>> I have to do it till Tuesday and I am in the position to cry. I fill
> >>> so sad. If I fail I must wait one year but it is not to wait, I realy
> >>> need to learn C and understand. So many hours invest and after all it
> >>> will be nothing.
> >>
> >> You should get partial credit for whatever parts you have managed to
> >> complete, and there should be a process for you to register the fact
> >> that your team mate as not contributed anything.
> >>
> >> --
> >> Ben.
> >
> > Thanks Mr Ben.
> >
> > I like your solution, but it not work.
> >
> > ---
> >
> > #include <stdio.h>
> > #include <string.h>
> > #include <stdlib.h>
> >
> > #define FILENAME "netlist.txt"
> > #define NUM_FIELDS 4
> >
> > typedef struct
> > {
> > char *component;
> > char *a_node;
> > char *b_node;
> > char *value;
> > }Components;
> >
> > void readComponentFile(void);
> >
> >
> > char line[130] = "";
> >
> > int main(void)
> > {
> > readComponentFile();
> > return 0;
> > }
> >
> > void readComponentFile()
> > {
> > FILE *fp = fopen(FILENAME, "r");
> >
> > char line[100];
> >
> > if(fp != NULL)
> > {
> > while (fgets(line, sizeof line, stdin))
>
> Change stdin to fp if you're going to read from the file.
>
> Then I found it worked if the file looks like this:
>
> (10/3)
> (48/100)
> (1/1000000)
>
>
> --
> Bartc
Thanks Mr Bart.
Yes I did it, and have problem again.
---
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FILENAME "netlist.txt"
#define NUM_FIELDS 4
typedef struct
{
char *component;
char *a_node;
char *b_node;
char *value;
}Components;
void readComponentFile(void);
char line[130] = "";
int main(void)
{
readComponentFile();
return 0;
}
void readComponentFile()
{
FILE *fp = fopen(FILENAME, "r");
char line[100];
if(fp != NULL)
{
while (fgets(line, sizeof line, fp))
{
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)\"");
}
}
fclose(fp);
}
}
----
This is what the compiler said:
MacBook-Pro-od-Dejan:NED putolov$ gcc -Wall -o barry barry.c
MacBook-Pro-od-Dejan:NED putolov$ ./barry
Input line should have the form "(A/B)"
Input line should have the form "(A/B)"
Input line should have the form "(A/B)"
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-12-19 19:31 +0000 |
| Message-ID | <o39cgq$80o$1@dont-email.me> |
| In reply to | #99867 |
On 19/12/2016 19:20, Dexterr wrote:
> On Monday, December 19, 2016 at 8:11:15 PM UTC+1, Bart wrote:
>> Then I found it worked if the file looks like this:
>>
>> (10/3)
>> (48/100)
>> (1/1000000)
>>
>>
>> --
>> Bartc
>
> Thanks Mr Bart.
>
> Yes I did it, and have problem again.
What do you have inside netlist.txt? Post the first few lines using
copy&paste.
> while (fgets(line, sizeof line, fp))
> {
> int num, den;
Try this line here:
printf("Line<%s>\n",line);
It will print out what has just been read in, before it's processed by
scanf. I get output such as:
Line<(10/3)
>
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-19 20:06 +0000 |
| Message-ID | <87h95z7kf2.fsf@bsb.me.uk> |
| In reply to | #99867 |
Dexterr <dejangru@gmail.com> writes:
<snip>
> void readComponentFile()
> {
> FILE *fp = fopen(FILENAME, "r");
Another plan is not to open any file...
> char line[100];
>
> if(fp != NULL)
...so you don't need to check this...
> {
> while (fgets(line, sizeof line, fp))
...and leave stdin in place of fp.
> {
> 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)\"");
> }
> }
> fclose(fp);
> }
> }
>
>
> ----
>
> This is what the compiler said:
>
> MacBook-Pro-od-Dejan:NED putolov$ gcc -Wall -o barry barry.c
> MacBook-Pro-od-Dejan:NED putolov$ ./barry
Then, to run the program, you redirect the input from the file you want
the program to read like this:
$ ./barry <netlist.txt
I do this because I find it much easier to run lots of test cases. I
will end up with files called test1.txt, test2.txt etc. with examples of
where some version of the program has gone wrong during development.
With every edit, I run
$ ./barry <test1.txt
...
$ ./barry <test2.txt
and so on to check they all still work. I also like the advantage of
being able to type a few test lines by hand.
(Actually I do something just a little more complicated enabling me to
read either a named file or from stdin but that's not going to help you
much right now.)
Of course, I your teacher requires a program that reads just one
pre-decided named file, then you have to do that, but I still make the
the last version I wrote.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-19 11:27 -0800 |
| Message-ID | <4f19b676-b77c-4897-aec0-87daaa34c589@googlegroups.com> |
| In reply to | #99865 |
On Monday, December 19, 2016 at 8:11:15 PM UTC+1, Bart wrote:
> On 19/12/2016 18:53, Dexterr wrote:
> > On Monday, December 19, 2016 at 1:23:21 AM UTC+1, Ben Bacarisse wrote:
> >> Dexterr <dejangru@gmail.com> writes:
> >>
> >>> On Sunday, December 18, 2016 at 10:26:30 PM UTC+1, Ben Bacarisse wrote:
> >>>> 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).
> >>>
> >>> Mr Ben, did you read the specifications what I need to do in my example ?
> >>> The algorithm for calculations is extrem complicated.
> >>
> >> No, I must have missed that. I saw only that you must do some
> >> computations but I did not what they were. But I don't think the nature
> >> of the computations changes much since the code you posted dealt only
> >> with interpreting the input. My suggestion was specifically about that.
> >>
> >> But, as I said, if using sscanf is not a helpful suggestion, then just
> >> forget I said anything.
> >>
> >>> I am very
> >>> desperate because I am
> >>> doing this for 2 weeks completely alone. My collegue work nothing. We
> >>> are in 2 students group.
> >>> I have to do it till Tuesday and I am in the position to cry. I fill
> >>> so sad. If I fail I must wait one year but it is not to wait, I realy
> >>> need to learn C and understand. So many hours invest and after all it
> >>> will be nothing.
> >>
> >> You should get partial credit for whatever parts you have managed to
> >> complete, and there should be a process for you to register the fact
> >> that your team mate as not contributed anything.
> >>
> >> --
> >> Ben.
> >
> > Thanks Mr Ben.
> >
> > I like your solution, but it not work.
> >
> > ---
> >
> > #include <stdio.h>
> > #include <string.h>
> > #include <stdlib.h>
> >
> > #define FILENAME "netlist.txt"
> > #define NUM_FIELDS 4
> >
> > typedef struct
> > {
> > char *component;
> > char *a_node;
> > char *b_node;
> > char *value;
> > }Components;
> >
> > void readComponentFile(void);
> >
> >
> > char line[130] = "";
> >
> > int main(void)
> > {
> > readComponentFile();
> > return 0;
> > }
> >
> > void readComponentFile()
> > {
> > FILE *fp = fopen(FILENAME, "r");
> >
> > char line[100];
> >
> > if(fp != NULL)
> > {
> > while (fgets(line, sizeof line, stdin))
>
> Change stdin to fp if you're going to read from the file.
>
> Then I found it worked if the file looks like this:
>
> (10/3)
> (48/100)
> (1/1000000)
>
>
> --
> Bartc
Thanks Mr Bart.
It WORKS perfect!! ;)
This is a great idea.
But one more question - what to do when you have file with 4 fields in one line
and only the fourth field has such a form ???
R1 0 1 (100/1)
C0 1 2 (1/1000000)
L1 2 3 (47/1000)
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-12-19 19:40 +0000 |
| Message-ID | <o39d0d$9ta$1@dont-email.me> |
| In reply to | #99868 |
On 19/12/2016 19:27, Dexterr wrote:
> On Monday, December 19, 2016 at 8:11:15 PM UTC+1, Bart wrote:
> But one more question - what to do when you have file with 4 fields in one line
> and only the fourth field has such a form ???
>
> R1 0 1 (100/1)
> C0 1 2 (1/1000000)
> L1 2 3 (47/1000)
>
I'm not an expert on sscanf, but I tried this code on such a file:
.....
int num, den;
int a,b;
char name[20];
if (sscanf(line, "%2s %d %d (%d/%d)", name,&a,&b,&num, &den) == 5)
{
printf("Name=%s a=%d b=%d %g\n", name, a,b, (double)num/den);
}
.....
and it seemed to work. Others will have to tell the exact formats needed
(I'm not sure about the %2s, which will anyway not work for "R115" for
example.).
(For extra points - from me anyway - keep count of what line number has
been read, and report it when there is an error. Maybe print the line too.)
--
bartc
[toc] | [prev] | [next] | [standalone]
| From | Dexterr <dejangru@gmail.com> |
|---|---|
| Date | 2016-12-19 12:09 -0800 |
| Message-ID | <26b860ac-2de4-44ab-8d3b-d687c4d6b495@googlegroups.com> |
| In reply to | #99871 |
>
> I'm not an expert on sscanf, but I tried this code on such a file:
- > YES. Mr Bart you are an expert! IT WORKS perfect !!!!
I could not imagine that something can work on this way i C languge.
I love it, can not describe it on English, because it is not my mother tongue.
> .....
> int num, den;
> int a,b;
> char name[20];
>
> if (sscanf(line, "%2s %d %d (%d/%d)", name,&a,&b,&num, &den) == 5)
> {
> printf("Name=%s a=%d b=%d %g\n", name, a,b, (double)num/den);
> }
> .....
>
> and it seemed to work. Others will have to tell the exact formats needed
> (I'm not sure about the %2s, which will anyway not work for "R115" for
> example.).
>
> (For extra points - from me anyway - keep count of what line number has
> been read, and report it when there is an error. Maybe print the line too.)
>
> --
> bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-12-19 20:15 +0000 |
| Message-ID | <87bmw77k0z.fsf@bsb.me.uk> |
| In reply to | #99868 |
Dexterr <dejangru@gmail.com> writes: <snip> > But one more question - what to do when you have file with 4 fields in one line > and only the fourth field has such a form ??? > > R1 0 1 (100/1) > C0 1 2 (1/1000000) > L1 2 3 (47/1000) It's hard to say without knowing (a) what the general pattern is and (b) what you are going to do with the data. For example, with you always have R, C or L at the start (excepting comment lines of course)? Will there always be a digit following that letter? Do you need to store that initial letter? Is the third field always one more than the second? My guess is that you will need a format like this: "%c%d %d %d (%d/%d)" Can you, from reading the documentation about sscanf, work out if this is what you need and how you would write sscanf call? My second guess is that "[RCL]%*d %d %*d (%d/%d)" will also do fine but I can't tell without knowing a lot more about the problem. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> |
|---|---|
| Date | 2016-12-24 17:12 +1100 |
| Message-ID | <s6g1jd-5t4.ln1@walter.foo> |
| In reply to | #99874 |
Groovy hepcat Ben Bacarisse was jivin' in comp.lang.c on Tue, 20 Dec
2016 7:15 am. It's a cool scene! Dig it.
> Dexterr <dejangru@gmail.com> writes:
> <snip>
>
>> But one more question - what to do when you have file with 4 fields
>> in one line and only the fourth field has such a form ???
>>
>> R1 0 1 (100/1)
>> C0 1 2 (1/1000000)
>> L1 2 3 (47/1000)
>
> It's hard to say without knowing (a) what the general pattern is and
> (b)
> what you are going to do with the data. For example, with you always
> have R, C or L at the start (excepting comment lines of course)? Will
> there always be a digit following that letter? Do you need to store
> that initial letter? Is the third field always one more than the
> second?
>
> My guess is that you will need a format like this:
>
> "%c%d %d %d (%d/%d)"
My guess is that R1, C0 and L1 are identifiers of some sort. Anyhow,
each does seem to be a single token. So a more likely solution would be
something like this:
"%s %d %d (%d/%d)"
Also, from Dexterr's other thread, he seems to want to read in the
arithmetic expression as a single field first, and then parse and
calculate it later (though I could be wrong). So maybe what he needs is
this:
"%s %d %d %s"
or, even better, since he's specified in the other thread the sizes of
arrays in which he is storing the data (5 for the identifier and 25 for
the arithmetic expression), this:
"%4s %d %d %24s"
and later, parse the arithmetic expression with this format string:
"(%d/%d)"
> Can you, from reading the documentation about sscanf, work out if this
> is what you need and how you would write sscanf call?
>
> My second guess is that "[RCL]%*d %d %*d (%d/%d)" will also do fine
I *think* you meant this:
"%[RCL]%d %d %d (%d/%d)"
> but I can't tell without knowing a lot more about the problem.
--
----- Dig the NEW and IMPROVED news sig!! -----
-------------- Shaggy was here! ---------------
Ain't I'm a dawg!!
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2016-12-19 16:15 -0800 |
| Message-ID | <butg5c91ovpc191fn8kh4q79dqvgtcn58r@4ax.com> |
| In reply to | #99868 |
On Mon, 19 Dec 2016 11:27:00 -0800 (PST), Dexterr <dejangru@gmail.com> wrote: <snip> >Thanks Mr Bart. > >It WORKS perfect!! ;) > >This is a great idea. > >But one more question - what to do when you have file with 4 fields in one line >and only the fourth field has such a form ??? > >R1 0 1 (100/1) >C0 1 2 (1/1000000) >L1 2 3 (47/1000) One easy solution is to tokenize the line as you do in your other thread. Then each field will be in its own string. You process the "arithmetic" string with the same code you used here. -- Remove del for email
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.c
csiph-web