f



[Structure of Array] pointer arithmetic in structures

For instance, I have an structure defined. Then I need to reach some values.

#define COUNT 3

typedef struct 
{
  char component[5];
  unsigned a_node;
  unsigned b_node;
  double value;
}Components;


int main(void)
{
  Components parts[COUNT] = { {"R1", 0, 1, 100.0}, 
                                                     {"C0", 1, 2, 0.000001},
                                                     {"L1", 2, 3, 0.047}};

  Component *ptr_parts = parts;

 printf("The first component is named %s ", ptr_parts->????? );

 return 0;
}

I would like to get the name of the second component "C0"
0
Dexterr
12/18/2016 3:51:59 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

33 Replies
786 Views

Similar Articles

[PageSpeed] 46

W dniu niedziela, 18 grudnia 2016 16:52:06 UTC+1 u=C5=BCytkownik Dexterr na=
pisa=C5=82:
> For instance, I have an structure defined. Then I need to reach some valu=
es.
>=20
> #define COUNT 3
>=20
> typedef struct=20
> {
>   char component[5];
>   unsigned a_node;
>   unsigned b_node;
>   double value;
> }Components;
>=20
>=20
> int main(void)
> {
>   Components parts[COUNT] =3D { {"R1", 0, 1, 100.0},=20
>                                                      {"C0", 1, 2, 0.00000=
1},
>                                                      {"L1", 2, 3, 0.047}}=
;
>=20
>   Component *ptr_parts =3D parts;
>=20
>  printf("The first component is named %s ", ptr_parts->????? );
>=20
>  return 0;
> }
>=20
> I would like to get the name of the second component "C0"

ptr_parts[1].component=20
 but you have bad names it should be more like=20

typedef struct=20
 {
   char name[5];
   unsigned a_node;
   unsigned b_node;
   double value; } Component;

0
fir
12/18/2016 4:15:00 PM
On 18/12/2016 15:51, Dexterr wrote:
> For instance, I have an structure defined. Then I need to reach some values.
>
> #define COUNT 3
>
> typedef struct
> {
>   char component[5];
>   unsigned a_node;
>   unsigned b_node;
>   double value;
> }Components;
>
>
> int main(void)
> {
>   Components parts[COUNT] = { {"R1", 0, 1, 100.0},
>                                                      {"C0", 1, 2, 0.000001},
>                                                      {"L1", 2, 3, 0.047}};
>
>   Component *ptr_parts = parts;
>
>  printf("The first component is named %s ", ptr_parts->????? );
>
>  return 0;
> }
>
> I would like to get the name of the second component "C0"

Example here for first and second components:

#include <stdio.h>

#define COUNT 3

typedef struct
{
   char component[5];
   unsigned a_node;
   unsigned b_node;
   double value;
}Components;


int main(void)
{
   Components parts[COUNT] = { {"R1", 0, 1, 100.0},
                               {"C0", 1, 2, 0.000001},
                               {"L1", 2, 3, 0.047}};

   Components *ptr_parts = parts;

  printf("The first component is named %s  \n", parts[0].component);
  printf("The second component is named %s \n", parts[1].component);

  printf("The first component is named %s  \n", ptr_parts[0].component);
  printf("The second component is named %s \n", ptr_parts[1].component);

  return 0;
}


This shows both directly using the array 'parts', and then using the 
pointer 'ptr_parts'.

(Yes, both accesses are done the same way; welcome to C.)

-- 
Bartc



0
BartC
12/18/2016 4:27:50 PM
On Sunday, December 18, 2016 at 5:15:11 PM UTC+1, fir wrote:
> W dniu niedziela, 18 grudnia 2016 16:52:06 UTC+1 u=C5=BCytkownik Dexterr =
napisa=C5=82:
> > For instance, I have an structure defined. Then I need to reach some va=
lues.
> >=20
> > #define COUNT 3
> >=20
> > typedef struct=20
> > {
> >   char component[5];
> >   unsigned a_node;
> >   unsigned b_node;
> >   double value;
> > }Components;
> >=20
> >=20
> > int main(void)
> > {
> >   Components parts[COUNT] =3D { {"R1", 0, 1, 100.0},=20
> >                                                      {"C0", 1, 2, 0.000=
001},
> >                                                      {"L1", 2, 3, 0.047=
}};
> >=20
> >   Component *ptr_parts =3D parts;
> >=20
> >  printf("The first component is named %s ", ptr_parts->????? );
> >=20
> >  return 0;
> > }
> >=20
> > I would like to get the name of the second component "C0"
>=20
> ptr_parts[1].component=20
>  but you have bad names it should be more like=20
>=20
> typedef struct=20
>  {
>    char name[5];
>    unsigned a_node;
>    unsigned b_node;
>    double value; } Component;

Thanks Mr fir on your response.
0
Dexterr
12/18/2016 5:31:04 PM
On Sunday, December 18, 2016 at 5:28:01 PM UTC+1, Bart wrote:
> On 18/12/2016 15:51, Dexterr wrote:
> > For instance, I have an structure defined. Then I need to reach some values.
> >
> > #define COUNT 3
> >
> > typedef struct
> > {
> >   char component[5];
> >   unsigned a_node;
> >   unsigned b_node;
> >   double value;
> > }Components;
> >
> >
> > int main(void)
> > {
> >   Components parts[COUNT] = { {"R1", 0, 1, 100.0},
> >                                                      {"C0", 1, 2, 0.000001},
> >                                                      {"L1", 2, 3, 0.047}};
> >
> >   Component *ptr_parts = parts;
> >
> >  printf("The first component is named %s ", ptr_parts->????? );
> >
> >  return 0;
> > }
> >
> > I would like to get the name of the second component "C0"
> 
> Example here for first and second components:
> 
> #include <stdio.h>
> 
> #define COUNT 3
> 
> typedef struct
> {
>    char component[5];
>    unsigned a_node;
>    unsigned b_node;
>    double value;
> }Components;
> 
> 
> int main(void)
> {
>    Components parts[COUNT] = { {"R1", 0, 1, 100.0},
>                                {"C0", 1, 2, 0.000001},
>                                {"L1", 2, 3, 0.047}};
> 
>    Components *ptr_parts = parts;
> 
>   printf("The first component is named %s  \n", parts[0].component);
>   printf("The second component is named %s \n", parts[1].component);
> 
>   printf("The first component is named %s  \n", ptr_parts[0].component);
>   printf("The second component is named %s \n", ptr_parts[1].component);
> 
>   return 0;
> }
> 
> 
> This shows both directly using the array 'parts', and then using the 
> pointer 'ptr_parts'.
> 
> (Yes, both accesses are done the same way; welcome to C.)
> 
> -- 
> Bartc

Thanks Mr Bart. I have learned here that I don't need to define a pointer *ptr_parts, so I can
acheive the same goal with existing variable "parts". 

Please one more question. This is very important for me.

I have a structure defined (please forget the  language for now)

typedef struct 
{
  char *komponent;
  char *a_knoten;
  char *b_knoten;
  char *wert;
} Komponenten;

Now, I have wrote a small program to read a text file with data :

R1 0 1 (100/1)
C0 1 2 (1/1000000)
L1 2 3 (47/1000)

I would like to copy a structure Komponenten into a new structure and make cast for same variables

typedef struct
{
  char components[5];
  unsigned a_node;
  unsigned b_node
 < here I am thinking what is smart to do ??? > value;
} Components;

By "value" variable actually I need a type of double, maybe double long, not sure for now.

But tha form such as "(100/1)" I need to convert to make some calculations later in function I will define, later.

---

Is this copy of structure to structure smart to do in "for loop" with "strcpy()" function and to make cast for the second, third and fourth variable ?
0
Dexterr
12/18/2016 5:43:04 PM
I have tried to do this but it doesn't work. To copy struct into a new struct and make cast!

typedef struct 
{
  char component[5];
  char aknoten;
  char bknoten;
  double value;
}Part;

typedef struct
{
  // int a_node;
}Components;

int main()
{
  for(int i = 0; i < 3; i++)
  {
  	
    strcpy(components[i].a_node,(int) ptr_parts[i].aknoten);
  }
  
 return 0;
}
0
Dexterr
12/18/2016 6:02:39 PM
On 18/12/2016 17:43, Dexterr wrote:

> Please one more question. This is very important for me.
>
> I have a structure defined (please forget the  language for now)
>
> typedef struct
> {
>   char *komponent;
>   char *a_knoten;
>   char *b_knoten;
>   char *wert;
> } Komponenten;

For reading from a text file? You can use char* here, but it means 
having to allocate four different pointers (and deallocate them at some 
future point).

If there are known limits to each field, then just use that:
  char komponent[20] for example. No allocation needed.

If a_knoten and b_knoten will be integers, and will be simple integer 
constants in the file (not expressions), then it will be easier to just 
read them as integers, or convert them immediately, then store as integers.

> Now, I have wrote a small program to read a text file with data :
>
> R1 0 1 (100/1)
> C0 1 2 (1/1000000)
> L1 2 3 (47/1000)

Will 100/1 etc be general expressions, or always in this form (dividing 
by a power of 10), in order to scale? In which case having 100, 0.000001 
and 0.047 in the file is much simpler!

Or is this to signify units (eg. ohms, micro-farads and, what, 
milli-henrys)?

Evaluating expressions within a string is difficult. If possible, just 
use 0.000001 in a file, or "1 mf", something that is straightforward to 
convert. Then it can be done immediately too.


> I would like to copy a structure Komponenten into a new structure and make cast for same variables
>
> typedef struct
> {
>   char components[5];
>   unsigned a_node;
>   unsigned b_node
>  < here I am thinking what is smart to do ??? > value;
> } Components;
>
> By "value" variable actually I need a type of double, maybe double long, not sure for now.

If these are electronic component values, you don't need long double! 
(As values will only be a few percent accurate anyway.)

-- 
Bartc
0
BartC
12/18/2016 6:13:20 PM
On Sun, 18 Dec 2016 10:02:39 -0800 (PST), Dexterr <dejangru@gmail.com>
wrote:

>I have tried to do this but it doesn't work. To copy struct into a new struct and make cast!

The phrase doesn't work tells us nothing.  

     Did the compiler issue a diagnostic?  Did the linker?  

     Did you execute the program?  What happened that was different
than what you expected?

What do you mean by the phrase "make cast"?  The cast operator does
not make anything.  I causes a value, not an object, to be converted
from one type to another.

>typedef struct 
>{
>  char component[5];
>  char aknoten;
>  char bknoten;
>  double value;
>}Part;
>
>typedef struct
>{
>  // int a_node;
>}Components;
>
>int main()
>{
>  for(int i = 0; i < 3; i++)
>  {
>  	
>    strcpy(components[i].a_node,(int) ptr_parts[i].aknoten);

If these member names are related to your other posts, then the cast
is unnecessary.  It is always legal to assign a char value to an int
without a cast.

>  }
>  
> return 0;
>}

If you want help debugging your code, you need to show us code that
can be compiled.  This code cannot be compiled.  
     There is no definition for an object named components.  
     There is no definition for an object named ptr_parts.  
     You are missing a #include directive.
     The two arguments to strcpy have the wrong type.  (You would not
want to use strcpy here anyway.  It will not copy integer values and
produce usable results.  Since neither argument is a string, why did
you think a string function would appropriate?)

-- 
Remove del for email
0
Barry
12/18/2016 6:20:40 PM
Could I explain what I need to do.

(1) First start a program with (I did this part to check command line arguments):

../name netlist.txt <some integer for omega>

-> for instance: 

   ./ass test.netlist 314

(2) Then read a text file as this one

* Simple netlist test for EP
R1 0 1 (100/1)
C0 1 2 (1/1000000)
L1 2 3 (47/1000)

<this file can have 20 lines with this 4 fields, from 0 - 19>

(3) After reading the text file to print out the content above in other form, to do some "sorting" but it is not sorting in sense of algorithms. Just to print out above content in this form.

0 - R1(100/1) - 1
1 - C0(1/1000000) - 2
2 - L1(47/1000) - 3

--

All above I did till now and it works perfect.

--

(4) The next step is:

ep> 

Okey this is easy - printf("ep> ");
then to take two unsigned integers from 0 to 19

That means :

ep> 0 1 

or 

ep> 1 3

<first number> is index from the left side
<second number> is index from the right side

than to do some calcultaions.

OK, this I will do with - do - while loop

do {

   printf("ep> ");

   if(scanf ....

 ... ....
}
while(1);


----

(5) Now I should do calculations.

But my variable "value" is string, i.e. (1/100) or (47/1000). I need this number to do some calculations and than to convert back into a string to put the result in the proper form as
it is asked from the exercise.

That means, i.e.

ep> 1 3
100/1) - j(99577/30)

--

So this part makes me crazy. I have no to much time (till tuesday 14:00 PM to finish it).
I have some ideas but, ther are complicated.

-- 

This is code for the part I have did till now.

--

#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);
int is_comment(const char *line);
//int tokenise();
void output(Components *ser_conn);

char line[130] = "";
int line_number = 0;
Components *series_connection;

int main(void)
{
  readComponentFile();
  return 0;
}

void readComponentFile()
{
  FILE *fp = fopen(FILENAME, "r");

  series_connection = malloc(sizeof *series_connection);

  if(series_connection == NULL)
  {
  	fputs("[ERR] Out of memory", stderr);
  	exit(2);
  }

  if(fp != NULL)
  {
  	while(fgets(line, sizeof line, fp) != NULL)
  	{
  	  ++line_number;

      if(!is_comment(line))
      {
        // if(tokenise(series_connection, line) == NUM_FIELDS)
        // {
        series_connection->component = strtok(line, " \t\n"); 
        series_connection->a_node = strtok(NULL, " \t\n"); 
        series_connection->b_node = strtok(NULL, " \t\n"); 
        series_connection->value = strtok(NULL, " \t\n");  
        output(series_connection);
        // }
      } // End of tokenise function - if condition
      else
      {
         fprintf(stderr, "Input error on line %d \n", line_number);
      }
    }
    // Close the file fp after reading and sorting
    fclose(fp);  
  } 
  else
  {
    fprintf(stderr, "Can't find %s.\n", FILENAME);
  }
  free(series_connection);
  series_connection = NULL;
} // End of readComponentFile function

int is_comment(const char *line)
{
  return line[0] == '*';
}

/*
int tokenise(Components *ser_con, const char *line)
{
  ser_con->component = strtok(line, " \t\n"); 
  ser_con->a_node = strtok(NULL, " \t\n"); 
  ser_con->b_node = strtok(NULL, " \t\n"); 
  ser_con->value = strtok(NULL, " \t\n"); 

  return 
}
*/

void output(Components *ser_conn)
{
  if(ser_conn->component != NULL && 
     ser_conn->a_node != NULL && 
     ser_conn->b_node != NULL && 
     ser_conn->value != NULL) 
  {
    printf("%s - %s%s - %s\n", 
               ser_conn->b_node, ser_conn->a_node, ser_conn->value,
               ser_conn->component);
  }
  
}
0
Dexterr
12/18/2016 6:31:41 PM
On Sun, 18 Dec 2016 09:43:04 -0800 (PST), Dexterr <dejangru@gmail.com>
wrote:

<snip>

>Please one more question. This is very important for me.
>
>I have a structure defined (please forget the  language for now)

It is important to realize that here you defined a structure type. You
have not defined an object of that type.

>typedef struct 
>{
>  char *komponent;
>  char *a_knoten;
>  char *b_knoten;
>  char *wert;
>} Komponenten;
>
>Now, I have wrote a small program to read a text file with data :
>
>R1 0 1 (100/1)
>C0 1 2 (1/1000000)
>L1 2 3 (47/1000)
>
>I would like to copy a structure Komponenten into a new structure and make cast for same variables
>typedef struct
>{
>  char components[5];
>  unsigned a_node;
>  unsigned b_node
> < here I am thinking what is smart to do ??? > value;
>} Components;

I assume you mean to copy the data from an object of type Komponenten
to an object of type Components.  But I don't know what you mean by
"make cast for same variables."  You don't need a cast is if there is
an implicit conversion from the type of your source value to the type
of your target variable.  (For example, you can assign a char to an
int or an int to a float without using a cast operator.)

>By "value" variable actually I need a type of double, maybe double long, not sure for now.

Neither are we since we don't know what you are trying to accomplish.

>But tha form such as "(100/1)" I need to convert to make some calculations later in function I will define, later.

Are you saying you want to read an arithmetic expression and perform
the corresponding arithmetic?  If the expression is always a simple
division of two values, that would be relatively easy.  If you intend
to accept operators other than /, it is a little more complex but
still doable.  If you intend to accept more complicated expressions
such as 100+50/28-6*30, your knowledge of C is not yet up to the task.
First master the language before attempting to build an arithmetic
expression evaluator.

>Is this copy of structure to structure smart to do in "for loop" with "strcpy()" function and to make cast for the second, third and fourth variable ?

NO!  

     A for loop does help you step through the members of a structure.
It can help you step through an array of objects (including
structures) but that is not what you appear to be asking about.

     strcpy only works for strings, not for numerical values.  Even
memcpy, which would allow you to copy every byte of an object
regardless of value, is not normally a suitable choice.

     Trying to use casts for this is almost always the exact wrong
decision.

-- 
Remove del for email
0
Barry
12/18/2016 6:39:15 PM
On Sunday, December 18, 2016 at 7:20:56 PM UTC+1, Barry Schwarz wrote:
> On Sun, 18 Dec 2016 10:02:39 -0800 (PST), Dexterr <dejangru@gmail.com>
> wrote:
> 
> >I have tried to do this but it doesn't work. To copy struct into a new struct and make cast!
> 
> The phrase doesn't work tells us nothing.  
> 
>      Did the compiler issue a diagnostic?  Did the linker?  
> 
>      Did you execute the program?  What happened that was different
> than what you expected?
> 
> What do you mean by the phrase "make cast"?  The cast operator does
> not make anything.  I causes a value, not an object, to be converted
> from one type to another.
> 

Thanks Mr Barry on you response.

Yes I did it, and does don't work. The compiler return a lot of "error" messages.

I just want to copy some variables from one struct to another and to change from char to integer.

For instance the last one is the most difficult. The "wert" is of type char, something like:

"(1/100)". I need this number for further calculations. I am not smart for now what to do with this. Is it good to take two new variables. First nominator and second denominator then to convert to some double or float ... etc to make calculations. 

But it is complicated. For sure, it must better solution but I don't see it for now.
0
Dexterr
12/18/2016 6:40:03 PM
On Sun, 18 Dec 2016 10:40:03 -0800 (PST), Dexterr <dejangru@gmail.com>
wrote:

<snip>

>Yes I did it, and does don't work. The compiler return a lot of "error" messages.
>
>I just want to copy some variables from one struct to another and to change from char to integer.

Since a char is an integer, what kind of change do you mean?
    Does the char contain an integer value such as 1 and you want to
store that value in an int?
    Does the char contain a digit character, such as '5', and you want
to convert that to an int with the equivalent value 5?
    Does the char contain an integer value, such as 6, and you want to
convert it to a string representing that value, such as "6"?
    Are you aware that the integer value 5 and the character value '5'
and the string value "5" are all very different from each other?  You
cannot convert from one to the other using a cast.

>For instance the last one is the most difficult. The "wert" is of type char, something like:
>
>"(1/100)". I need this number for further calculations. I am not smart for now what to do with this. Is it good to take two new variables. First nominator and second denominator then to convert to some double or float ... etc to make calculations. 
>
>But it is complicated. For sure, it must better solution but I don't see it for now.

Rather than showing us relatively useless code, you might be better
off showing us a concrete example of your input (this appears to be a
line from a text file and NOT a structure) and what you would like the
output to be (a structure maybe?).

You also claim to have some code that solves part of the problem and
"works perfectly."  Given your other posts, this seems unlikely.  Why
don't you start by showing us that code.


-- 
Remove del for email
0
Barry
12/18/2016 6:54:04 PM
On 18/12/2016 18:40, Dexterr wrote:

> I just want to copy some variables from one struct to another and to change from char to integer.

The sort of conversion you want to do (from string "1234" to integer 
1234) can't be done with a cast.

You have to use something atol() or perhaps strtol() - to convert to 
integer (and strtod to double). But they won't convert an expression 
such as "`48/100" to 0.48. Or "(1/100)" to 0.01.

You can do this, but because it's quite difficult, it's best to see 
first if you really need to do it.

Does it /have/ to be "(1/100)" in the input; and does it /have/ to be 
0.01 in the program? (Or can it just stay as "(1/100)" if you don't 
calculate with it.)


-- 
Bartc

0
BartC
12/18/2016 6:54:05 PM
> Does it /have/ to be "(1/100)" in the input; and does it /have/ to be 
> 0.01 in the program? (Or can it just stay as "(1/100)" if you don't 
> calculate with it.)
> 

Yes. It have to be (1/100) in the input.
It does not have to be in program, but the output must be something like this:

> (100/1) - j(99577/30)

j - is imaginer number

When you have to calculate - R + RL + RC with this numbers such as 1/100, 47/1000 ... 
maybe is a better idea but the result will be some float or double number and it will be
at the end transformated to form as :

100/1) - j(99577/30)
0
Dexterr
12/18/2016 7:05:41 PM
On Sunday, December 18, 2016 at 7:54:13 PM UTC+1, Barry Schwarz wrote:
> On Sun, 18 Dec 2016 10:40:03 -0800 (PST), Dexterr <dejangru@gmail.com>
> wrote:
> 
> <snip>
> 
> >Yes I did it, and does don't work. The compiler return a lot of "error" messages.
> >
> >I just want to copy some variables from one struct to another and to change from char to integer.
> 
> Since a char is an integer, what kind of change do you mean?
>     Does the char contain an integer value such as 1 and you want to
> store that value in an int? - > YES

>     Does the char contain a digit character, such as '5', and you want
> to convert that to an int with the equivalent value 5? -> YES

>     Does the char contain an integer value, such as 6, and you want to
> convert it to a string representing that value, such as "6"? . -> NO

>     Are you aware that the integer value 5 and the character value '5'
> and the string value "5" are all very different from each other?  YES

 You cannot convert from one to the other using a cast. NO, I did't know that, honestly
> 
> >For instance the last one is the most difficult. The "wert" is of type char, something like:
> >
> >"(1/100)". I need this number for further calculations. I am not smart for now what to do with this. Is it good to take two new variables. First nominator and second denominator then to convert to some double or float ... etc to make calculations. 
> >
> >But it is complicated. For sure, it must better solution but I don't see it for now.
> 
> Rather than showing us relatively useless code, you might be better
> off showing us a concrete example of your input (this appears to be a
> line from a text file and NOT a structure) and what you would like the
> output to be (a structure maybe?).

 Yes Mr Barry. Thats true. I have posted the whole exercise. What I have did till now and what I need to do.


> You also claim to have some code that solves part of the problem and
> "works perfectly."  Given your other posts, this seems unlikely.  Why
> don't you start by showing us that code.

I have posted. Please to 3 steps back and you will see what I did and what I need to solve.

Thanks Mr Barry.

0
Dexterr
12/18/2016 7:10:33 PM
On Sunday, December 18, 2016 at 7:54:16 PM UTC+1, Bart wrote:
> On 18/12/2016 18:40, Dexterr wrote:
> 
> > I just want to copy some variables from one struct to another and to change from char to integer.
> 
> The sort of conversion you want to do (from string "1234" to integer 
> 1234) can't be done with a cast.
> 
> You have to use something atol() or perhaps strtol() - to convert to 
> integer (and strtod to double). But they won't convert an expression 
> such as "`48/100" to 0.48. Or "(1/100)" to 0.01.

No, no, I don't expect that "48/100" be converted to 0.48. No at all. That is imposible.

I has planed to have 3 new variables:

- first var = 48
- second var = 100
- > the third var = 48/100 - to get some float number to use it to make calculations I need


 
> You can do this, but because it's quite difficult, it's best to see 
> first if you really need to do it

- Yes it is difficult I don't need it for now. 

> Does it /have/ to be "(1/100)" in the input; and does it /have/ to be 
> 0.01 in the program? (Or can it just stay as "(1/100)" if you don't 
> calculate with it.)

Yes it is in the input file (1/100) but with such value I can't make calculations for 
impendance. 

0
Dexterr
12/18/2016 7:22:03 PM
On 18/12/2016 19:22, Dexterr wrote:
> On Sunday, December 18, 2016 at 7:54:16 PM UTC+1, Bart wrote:
>> On 18/12/2016 18:40, Dexterr wrote:
>>
>>> I just want to copy some variables from one struct to another and to change from char to integer.
>>
>> The sort of conversion you want to do (from string "1234" to integer
>> 1234) can't be done with a cast.
>>
>> You have to use something atol() or perhaps strtol() - to convert to
>> integer (and strtod to double). But they won't convert an expression
>> such as "`48/100" to 0.48. Or "(1/100)" to 0.01.
>
> No, no, I don't expect that "48/100" be converted to 0.48. No at all. That is imposible.
>
> I has planed to have 3 new variables:
>
> - first var = 48
> - second var = 100
> - > the third var = 48/100 - to get some float number to use it to make calculations I need

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.

-- 
Bartc
0
BartC
12/18/2016 7:38:29 PM
> 
> 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);
0
Dexterr
12/18/2016 7:45:42 PM
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)\"");
     }
}

-- 
Ben.
0
Ben
12/18/2016 8:07:29 PM
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)\"");
>      }
> }
> 
> -- 
> Ben.

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).
0
Dexterr
12/18/2016 8:35:54 PM
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.
0
Ben
12/18/2016 9:26:23 PM
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.
0
Dexterr
12/18/2016 9:47:44 PM
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.
0
Ben
12/19/2016 12:23:14 AM
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)\"");
    }
  }
 }
} 

0
Dexterr
12/19/2016 6:53:41 PM
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
0
BartC
12/19/2016 7:11:03 PM
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)"
0
Dexterr
12/19/2016 7:20:19 PM
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)
0
Dexterr
12/19/2016 7:27:00 PM
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
0
BartC
12/19/2016 7:31:56 PM
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
0
BartC
12/19/2016 7:40:15 PM
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.
0
Ben
12/19/2016 8:06:57 PM
> 
> 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

0
Dexterr
12/19/2016 8:09:13 PM
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.
0
Ben
12/19/2016 8:15:24 PM
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
0
Barry
12/20/2016 12:15:56 AM
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!!
0
Peter
12/24/2016 6:12:12 AM
Reply: