Thread: Need help with basic C program

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Need help with basic C program

    I am a freshman in college trying to learn C and i need help with this program if anyone would mind helping me.

    For the first part i need to detect stuffed bits + SYN sequences and have the output match the input but have an "S" in place of a "1" in the SYN pattern. My SYN is 100101, to 10010S.

    I did that using FSM with 6 states. I need to now remove stuffed bits and SYN sequences from the input. (I don't know where to have print statements so that i print everyting but my syn/stuff bit)
    Ex. 1001010010010011
    I need to have: 001001011 (I think)

    I can copy and paste what i have, but its long and everyone here seems so advance and this seems basic.

    If anyone can help me, thanks.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you should post some code. it's unclear from what you're saying if you're manipulating strings or binary data...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Code:
    int main( void)
    {
      int state, next, c, out;
     
      setbuf( stdout, 0); /* make stdout unbuffered */
      next = START; /* set initial state */
     
      while( 1)
      {
        /* get next input */
        c = getchar(); if( c < 0) break;
        if( c != '0' && c != '1') continue; /* skip bad inputs */
        state = next; /* get next state */
        out = c; /* set default output */
        switch( state)
        {
          case START:
    	if( c == '1') next = SAW_1;
    	 else next = START;
            break; 
          
          case SAW_1:
    	if( c == '0') next = SAW_10;
    	 else next = SAW_1;
    	break; 
          
     
          case SAW_10:
    	if( c == '0') next = SAW_100;
    	 else next = SAW_1;
    	break; 
          
     
          case SAW_100:
    	if( c == '1') next = SAW_1001;
    	 else next = START;
    	break; 
          
             
          case SAW_1001:   
           if( c == '0') next = SAW_10010;
            else next = SAW_1;
           break; 
          
       
          case SAW_10010:
           if ( c == '1'){out = 'S'; next = START;}
            else next = START;
           break; 
    }
     printf( "%c", out);
      
      }
       putchar( '\n');
     
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    That works for the 1st part, now i need to have it print out everything except syn pattern and stuffed bits

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    you'll need to read the incoming data entirely (or at least until you know you haven't encountered the SYN pattern) before outputting anything. an easy way to do that without even having to use an fsm would be to use strstr to mark the position of the next SYN and then skip accordingly:

    Code:
    void
    process(const char * buffer, void (*process_char)(char))
    {
    	const char 
    		SYN[] = "10010",
    		* mark = buffer,
    		* end = buffer + strlen(buffer);
    		
    	while(mark != end)
    	{
    		mark = strstr(buffer, SYN);
    		if(mark == NULL)
    		{
    			mark = end;
    		}
    		while(buffer != mark)
    		{
    			process_char(*buffer++); 
    		}
    		buffer += sizeof(SYN) - 1;	
    	}
    }
    
    void
    put(char ch)
    {
    	putchar(ch);
    }
    
    void
    print(const char * buffer)
    {
    	process(buffer, put);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  3. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. Help me with this basic c program
    By [ToXiC]-[LeaK] in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:44 PM