Thread: Multi-level logic gates for begginers

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Pinkysuavo
    Inputs (and all the circuit description) must come from a textfile
    You might want to read algorism's post #14 again.

    Basically, while the input may ultimately come from a text file, there a few ways by which that can happen. For example:
    • You can hard code a specific file name in your program, then open the file with that name.
    • You can allow the user to provide a file name at run time. This is typically done either by having the user provide the file name as a command line argument, or by interactively requesting the file name as input from the user. You then open the file with the name provided.
    • You can read from standard input, then redirect input from the file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    I was thinking about something like fopen, then fscanf, etc.. For example, when I made a program printing a content of textfile I used operations like fopen, fgetc, so I thought I can use similar commands in this task (text file path would be included in program's code). I don't know which way is this (if it's even any way), cause I don't understand what you've written. Well I feel so dumb here

  3. #18
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    Sorry for double post, but I can't edit for some reason.
    Here's a quick mock-up to illustrate how to read values from a string (each string corresponding to one line in your file):


    Matticus, your example was helpful and thank you for this, tho I still don't understand how my reading could go line by line (like in ur example).
    In case of your example you sscanf string 0, then string 1, then string 2. But how my program can know that `001 TRUE` is one string, and `002 NOT 1` the second one?

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Pinkysuavo View Post
    Matticus, your example was helpful and thank you for this, tho I still don't understand how my reading could go line by line (like in ur example).
    Code:
    while(fgets(str, sizeof(str), fptr) != NULL)
    {
        fputs(str, stdout);
        /* use "sscanf()" to scan values from "str" */
    }
    Quote Originally Posted by Pinkysuavo View Post
    In case of your example you sscanf string 0, then string 1, then string 2. But how my program can know that `001 TRUE` is one string, and `002 NOT 1` the second one?
    Assuming your string is long enough to hold one line from the text file (an easy assumption since those lines look rather short), then each call to "fgets()" will get you one single line. The first call to "fgets()" will give you the first line as a string, the next call will give you the second line as a string, and so on.

  5. #20
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    Okay well looks like I won't finish this project. It's even harder than I thought

  6. #21
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Pinkysuavo View Post
    Okay well looks like I won't finish this project. It's even harder than I thought
    You can't even write a program to read lines from a text file and print them to stdout?

    Quote Originally Posted by Pinkysuavo
    We had stuff like loops, ifs, pointers, structures, linked lists, some file operations (like reading or writing text), mallocs, arrays, I don't know what else. Just basics I guess, although I need to practice on few of these.
    There should be enough information in the thread (and your course notes) to get you that far, at least.

  7. #22
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    There should be enough information in the thread (and your course notes) to get you that far, at least.
    Lectures give nothing, just a theory which I can find anywhere. On classes we did either simple stuff like printing a textfile on screen with getc loop, or some hard stuff like linked lists that has nobody done or understood. I didn't learn much there.
    You can't even write a program to read lines from a text file and print them to stdout?
    Looks like I don't. I haven't used fgets or stdout before, I don't know what fptr is (file pointer I guess but still doesn't tell me much). I just don't understand ur code. I'm sorry, maybe I'm whining now but I've been really sitting now googling everything etc but I feel like I just can't learn anything. If I finally understand one thing, there appears next, then next, like a nonending chain. I really want to learn it, today at uni I couldn't wait to come back and do anything with the project, but within the time I'm getting lost. I don't know how to get to it.

  8. #23
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The example I gave above in post #19 was intentionally bare, as it was implied you understood enough of the basics to make sense of it. If this is not the case, you need to study the material again.

    I'm assuming you have a text book? I would suggest starting there.

    The reality is, you cannot really learn programming from a class (or from searching the net). You need to spend your own time reading and practicing. Start from the beginning of your book, read through, type out all of the examples, and do all of the exercises. Write a few small programs yourself to test the concepts as you go along, to make sure you're absorbing the ideas.

    We can help you with problems on your journey of learning, but the main drive has to come from yourself.

  9. #24
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    I don't have a textbook. I'm reading and practicing (via internet articles, tutorials and videos) now but I still stay in the same place with the project. I mentioned the things we had discussed on classes, but I mentioned also that I don't know too much. On the classes we had some stuff but it has totally nothing to do with the project. And I won't buy a book anymore cause we move to C++ in next semester. Even if I write somehow this project I don't know how I'll deal in second semester with programming. I'm planning to practice more (in this semester I didn't do much). Also it would suck that I've dealt pretty good with other subjects and didn't pass cause of programming. I feel like this thing is not for me - I have tried like a year ago learning C++ on my own, and I stopped quite fast. Same with Pascal. Now I both want and must learn programming but it's just so complex for me.
    Nevermind, gotta learn. I'll be trying to do this til the end. If I don't finish this then well it's not for me
    Last edited by Pinkysuavo; 01-15-2016 at 04:04 PM.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This problem seems to be a fairly interesting exercise though: just keep going. I would start experimenting with either the reading, or the logic gate resolving. Nominal Animal's post #3 outlines an approach that I would have taken, though it is far more complex that I would suggest for you given your skills at this time.

    Simplify:
    • Assume that each logic gate is already resolved to TRUE or FALSE, or consists of a name and one (e.g., for the NOT gate) or two inputs (e.g., for the AND gate). This simplifies your reading of the input and allows you to get rid of the suggested inputs and input_gate members.
    • Assume that there is a fixed, small number of gates. This allows you to have a plain old fixed-size array instead of doing dynamic memory allocation, and perhaps rather importantly it simplifies your reading of the input.
    • Use an iterative approach to resolving the gates: "Iterative is slower (as it does several passes over all gates), but it is deterministic and has no weak spots (except for slowness). You do passes over all gates in UNKNOWN state, resolving them if none of their inputs are in unknown state. You are done when the pass no longer resolves any new gates. If there are gates left in UNKNOWN state, the graph is unresolvable (cyclic)."


    If it turns out that your requirements are more complex, that's fine: having acquired the experience writing this simplified program, you will be far better equipped to writing the more complex program than if you were to try writing the more complex program now.

    Quote Originally Posted by Nominal Animal
    To get you started, consider these enumerated constants, structure, and global variables:
    I recommend against the use of global variables here, even though for this case the gates may indeed be global state. Rather, practice the use of local variables, parameter declarations, and argument passing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    I woke up having big ambitions to write this ......... Let's see if I can do anything today or I'll end up depressed again.
    @UP I have no idea what's dynamic memory allocation, I don't know what are you talking about. I'm a dumb beginner with no experience...

  12. #27
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    Sry again for double post but seems like I've been writing and analysing this for over an hour, and it's not possible to edit after this period of time.

    PS I still don't know how I can read the textfile storing everything. I thought I can do it somehow with fscanf or anything but the one method in higher posts - I didn't understand it at all. Maybe some article about reading files will help me out there. I'll try to find it.

    PS2 Today I'll probably ask a lot of question. Even the most dumbest ones. Of course I'll try to find answers in the Internet also, not to spam here, but won't things be more clear if I learn from different sources and ,,point of views"? I'm determined to do this and I'll do anything I can to finish this project. I will do at least something today and slowly get to the end - I still have one week or even more!
    I'll give now my ideas and my thoughts about what exactly these things would do. I'll try to explain what is happening in my brain to try to let you know what I understand it like. I'll also ask some questions:

    1) Take a look at this structure:
    Code:
    typedef struct gate
    {
    int gate_number;
    char gate_type;
    int input1;
    int input2;
    int output;
    } gate;
    (does it even make sense?)
    I don't understand tho why you guys used typedef for it. I mean... Consider looking at these 2 struct and typedef articles:
    Structures in C
    typedef in C
    Either I am really dumb or I just can't see any bigger difference.
    Is the difference about ,,struct" word while declaring? That's all? If yes, then I understand it I guess, if not, please explain (at first I wanted to post it without typedef but I'll just leave it)
    Now my thoughts about what's the point of this structure. It can be used to store information about gates (lines in file), like when I'd fscanf it then it would ascribe 001 to gate_number[0] (we got to point when I start understanding idea of arrays here I think!), then for example A (AND) to gate_type[0] etc. Is my way of thinking fine?


    2) Enumerated constants
    I admit I didn't know what enumerated constans were when you first mentioned it. (PS. I'm sorry but I didnt understand algorism's idea in #4 post with the ,,left" and ,,right" variables. Yesterday I thought I get it but now I got kinda mindblown with that).

    So here's it:
    Code:
    typedef enum gate_type
    {
        gFalse = 0,
        gTrue = 1,
        gNot = 2,
        gAnd = 3,
        gOr = 4,
        gXor = 5,
    } gate_type;
    And also: Does it make sense? But wait a sec. Before I declared gate_type as a character. And we have numbers here. I start getting lost. How to get these 2 things together?
    Let's say we have a line
    001 FALSE
    After reading it and getting words to different variables (I don't know yet how to do it, but let's say I do) we have a 001 integer then string FALSE. With strcmp I can check if the string is FALSE/TRUE/AND/... etc. (Wait a second, will I have write somewhere like 6 ifs about checking what's that string?)
    Well, let say I detect FALSE - and if it's FALSE then it gets set up as gate_type[0]=gFalse=0.
    Okay I thought I got this kinda all together but at this point I got a little lost. What does it give me? We have a line with 001 gFalse=0 but what about inputs and outputs of this line? Should I add enumerated logic value constans? Maybe something like
    Code:
    typedef enum logic_state
    {
        lUnknown = 2, // I wanted to set it 0 as first, but prolly true and low would be confusing for me
        lLow = 0, // low as 0 is more clearer to me
        lTrue = 1, //same about 1 as true
    } logic_state;
    Okay, if my way of thinking is at least a little correct, this enumerated constants can help me a little. From 001 FALSE I have something like
    001 gFalse lUnknown lUnknown lUnknown (as nr/type/input1/input2/output)
    But I can do something like, when gate_type = g_False then output = lLow.
    So I have
    001 gFalse lUnknown lUnknown lLow
    And the unknowns will stay as they are. Although I am not sure if this would be a good solution...
    Okay, I've been writing this post for a while and I got confused a little. Not sure if this all made any sense. Please tell me what you think.
    Last edited by Pinkysuavo; 01-16-2016 at 09:35 AM.

  13. #28
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're right that the typedef simply allows you to refer to the struct as Gate instead of struct Gate.

    You don't need any enums at all. To keep things simple you probably shouldn't use them. Using the first character of the gate type string essentially replaces the enum.

    You don't need the gate_number member since the gate number will be the index in the gate array. I.e.,
    Code:
    #define GATES_SIZE 1000
    
    typedef struct Gate {  // struct Gate is the struct name
      char type;
      int in1, in2;
      int out;
    } Gate;                // Gate is the typedef'd name
                           // Now we can just say Gate instead of struct Gate
    
    int main() {
        // declare an array of Gate structs
        Gate gates[GATES_SIZE];   // the gate number is simply the
                                  // index position in this array
        int i;
        char gate_type_string[100];
        FILE *file = fopen ....; // open your input file
    
               // read the first two values from the current line
        while (fscanf(file, "%d %s", &i, gate_type_string) == 2) {
    
            // The gate number, i, is used as the index into the gates array
    
            gates[i].type = gate_type_string[0];  // first character is type code
    
           // Now that you have the type, you can determine
           // if you need to read any more integers from the line
    Code this part up. Read the input into the gates array and then simply print out the gates array to see if you read it properly.

  14. #29
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Pinkysuavo View Post
    Sry again for double post but seems like I've been writing and analysing this for over an hour, and it's not possible to edit after this period of time.
    This is correct. You have about an hour window to make sure your post is correct. This decision was made because people would make threads, get their answers, and then edit their original post to say something unhelpful, like "problem solved." This robbed the thread of its original context. So, it was fixed this way.

    PS2 Today I'll probably ask a lot of question. Even the most dumbest ones. Of course I'll try to find answers in the Internet also, not to spam here, but won't things be more clear if I learn from different sources and ,,point of views"? I'm determined to do this and I'll do anything I can to finish this project. I will do at least something today and slowly get to the end - I still have one week or even more!
    I admit I've skimmed the thread a little more than perhaps I should have. The thing we don't want to have happen is that you would cross-post to other forums, just to see who answers first. It's rude considering the time and effort people have to put into answering (consider one person at least created a small program to help you with your project). Another important point is that you might end up confused by all the different forums' advice, ending up with a terrible body of source, consisting of patchwork, when working mostly with one forum would have resulted in something more robust.

    Here we appreciate a good faith effort, so if you continue to post links to other sources, and ask clear questions you will get lots of help. I think you've earned a code review if you have any of your own source code to post for the project.
    I'll give now my ideas and my thoughts about what exactly these things would do. I'll try to explain what is happening in my brain to try to let you know what I understand it like. I'll also ask some questions:

    1) Take a look at this structure:
    Code:
    typedef struct gate
    {
    int gate_number;
    char gate_type;
    int input1;
    int input2;
    int output;
    } gate;
    (does it even make sense?)
    I don't understand tho why you guys used typedef for it. I mean... Consider looking at these 2 struct and typedef articles:
    Structures in C
    typedef in C
    Either I am really dumb or I just can't see any bigger difference.
    Is the difference about ,,struct" word while declaring? That's all? If yes, then I understand it I guess, if not, please explain (at first I wanted to post it without typedef but I'll just leave it)
    Well, the effect is pretty small in itself. The problem that the typedef here solves is that struct becomes a nuisance word in C, but the keyword is necessary for the compiler to distinguish user-defined types from nonsense. Typedef solves this problem because it declares an alias for the original type name.

    Without the typedef the way to make a gate is to say "struct gate foobar;" With the typdef, you just write "gate foobar;" and be done with it. After making say 100 gates, perhaps not all in an array, the savings to your fingers adds up.

    It is important to note that some people do find separating the two statements to be cleaner, but less compact:
    Code:
    struct gate
    {
    int gate_number;
    char gate_type;
    int input1;
    int input2;
    int output;
    };
    
    typedef struct gate gate;
    Now my thoughts about what's the point of this structure. It can be used to store information about gates (lines in file), like when I'd fscanf it then it would ascribe 001 to gate_number[0] (we got to point when I start understanding idea of arrays here I think!), then for example A (AND) to gate_type[0] etc. Is my way of thinking fine?
    Your thinking is not really correct, but it is something the compiler would catch right away if you tried to write it.

    It is important to remember that each element will have its own collection of variables. Take a look at this:
    Code:
     gate full_adder[MAX_GATES]; // MAX_GATES should be at least enough gates to make this.
    Now if we wanted to access the first gate we would write "gate[0]" and then accessing that gate's parts works by writing "gates[0].gate_number".

    2) Enumerated constants
    I admit I didn't know what enumerated constans were when you first mentioned it. (PS. I'm sorry but I didnt understand algorism's idea in #4 post with the ,,left" and ,,right" variables. Yesterday I thought I get it but now I got kinda mindblown with that).

    So here's it:
    Code:
    typedef enum gate_type
    {
        gFalse = 0,
        gTrue = 1,
        gNot = 2,
        gAnd = 3,
        gOr = 4,
        gXor = 5,
    } gate_type;
    And also: Does it make sense? But wait a sec. Before I declared gate_type as a character. And we have numbers here. I start getting lost.
    It makes sense. char can be used here since characters are actually small, single-byte integers. Characters only behave the way they do because they are specially treated by C's standard libraries.

    How to get these 2 things together?
    Let's say we have a line
    001 FALSE
    After reading it and getting words to different variables (I don't know yet how to do it, but let's say I do) we have a 001 integer then string FALSE. With strcmp I can check if the string is FALSE/TRUE/AND/... etc. (Wait a second, will I have write somewhere like 6 ifs about checking what's that string?)
    Well, let say I detect FALSE - and if it's FALSE then it gets set up as gate_type[0]=gFalse=0.
    Okay I thought I got this kinda all together but at this point I got a little lost. What does it give me? We have a line with 001 gFalse=0 but what about inputs and outputs of this line? Should I add enumerated logic value constans? Maybe something like
    Code:
    typedef enum logic_state
    {
        lUnknown = 2, // I wanted to set it 0 as first, but prolly true and low would be confusing for me
        lLow = 0, // low as 0 is more clearer to me
        lTrue = 1, //same about 1 as true
    } logic_state;
    Okay, if my way of thinking is at least a little correct, this enumerated constants can help me a little. From 001 FALSE I have something like
    001 gFalse lUnknown lUnknown lUnknown (as nr/type/input1/input2/output)
    But I can do something like, when gate_type = g_False then output = lLow.
    So I have
    001 gFalse lUnknown lUnknown lLow
    And the unknowns will stay as they are. Although I am not sure if this would be a good solution...
    Okay, I've been writing this post for a while and I got confused a little. Not sure if this all made any sense. Please tell me what you think.
    So you wrote a lot here, but I don't think you asked very much.

    1. When you read a file, you will need somewhere to store the information. This information can be rather complex at first and pared down to simpler results later on, but the point stands. Part of the task is determining what information you have. So yes, somewhere you will be comparing a bit of string from the file to all of the possibilities, discovering how to adjust the logic state variable.

    2. It's important to remember what logic state is for. You are going to have logic state inputs that you turn, through a series of gates, into logic state outputs.

    This means at a minimum you will have some small bits of logic state that start out somewhere:
    Code:
     logic_state output1 = lUnknown, output2 = lUnknown, output3 = lUnknown;
    You will want to follow the file as close as possible. If all of the inputs are false then you want them to start false. The only reason you start as unknown is because you want to have the opportunity to look ahead in the graph of gates and determine if there is a cycle.

    If you don't understand that, then imagine the inputs flowing from one gate to the next. What you want to avoid is the situation like this:
    0 HIGH
    1 XOR 0 2
    2 XOR 0 1

    So you Start out HIGH. The first XOR would make the bit LOW and it instructs you to the next gate. That gate takes the bit that is LOW and makes it HIGH, and takes you to the first gate. So the ultimate result is unknown -- the same two gates will keep flipping the bit, and you can't break this circle. So you would error out and exit in the best of circumstances.

    If you look ahead and determine there is no cycle, then you switch output1, output2, and output3 in the example above this one to FALSE, and go.

    Hope this helps.
    Last edited by whiteflags; 01-16-2016 at 10:37 AM.

  15. #30
    Registered User
    Join Date
    Jan 2016
    Posts
    22
    I admit I've skimmed the thread a little more than perhaps I should have. The thing we don't want to have happen is that you would cross-post to other forums, just to see who answers first. It's rude(...)
    I'm sorry, I don't want to be rude. This forum is the only one I use. I use internet to search for articles when some detail is not clear for me, like for example usage of typedef was in this case.


    If you don't understand that, then imagine the inputs flowing from one gate to the next. What you want to avoid is the situation like this:
    0 HIGH
    1 XOR 0 2
    2 XOR 0 1

    So you Start out HIGH. The first XOR would make the bit LOW and it instructs you to the next gate. That gate takes the bit that is LOW and makes it HIGH, and takes you to the first gate. So the ultimate result is unknown -- the same two gates will keep flipping the bit, and you can't break this circle. So you would error out and exit in the best of circumstances.

    If you look ahead and determine there is no cycle, then you switch output1, output2, and output3 in the example above this one to FALSE, and go.
    I don't understand you here, after the point 2.. I start out HIGH but why first XOR would make the bit LOW? I understand that there a infinit loop appears, as you said, but I don't know why would the outputs1,2,3 be false? I think I don't understand what you're trying to explain me. I'm sorry.


    Code:
    // read the first two values from the current line    
              while (fscanf(file, "%d %s", &i, gate_type_string) == 2) {
     
            // The gate number, i, is used as the index into the gates array
     
            gates[i].type = gate_type_string[0];  // first character is type code
    
     
           // Now that you have the type, you can determine
    
           // if you need to read any more integers from the line
    I got stuck here. I made a textfile
    Code:
    001 TRUE
    002 AND 1 3
    003 FALSE
    And I was trying to check different stuff. I could check that the first line has a true value but it always stopped there. I tried to create something like: when fscanf=3 then detect also the input 1, or when its equal to 4 to detect input 1 and input2 and get these things to arrays. But I failed. Usually it just ended on reading first line. I'm still mixing it but I'm starting to get confused.
    Last edited by Pinkysuavo; 01-16-2016 at 03:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-30-2015, 10:50 PM
  2. codes for displaying logic gates ???
    By shikha_muz in forum Windows Programming
    Replies: 1
    Last Post: 12-16-2012, 11:29 AM
  3. Logic Gates - Multiplexer Help
    By alexmallaburn in forum C Programming
    Replies: 21
    Last Post: 02-22-2012, 05:30 PM
  4. C program related to logic gates!
    By yellowmania in forum C Programming
    Replies: 4
    Last Post: 02-14-2012, 07:38 AM
  5. Logic Gates
    By JFK in forum C Programming
    Replies: 0
    Last Post: 10-21-2001, 05:56 AM