The website of Lei Chen

Sunshine and rain of a developer

Archive for the ‘C’ Category

Travelling Salesman Problem

Posted by hide1713 on June 23, 2009

Posted in C | Leave a Comment »

Virtual Studio Smart Operator ‘=’. Add Spaces Around Every ‘=’.

Posted by hide1713 on June 22, 2009

I find it’s really hard to extend the environemnt under VS 2008. Anyway, I have to learn some VB.

I really like the SmartOperator.el in Emacs. When I press ‘=’ key, it automatically insert two spaces around ‘=’.  The following code can do the same thing. You need to create a new macro in VS and put the code in the new macro and bind ‘=’ key to this function.  I hope the code could inspire someone.


Sub space_around_eq()
 Dim s As TextSelection = DTE.ActiveDocument.Selection
 s.CharLeft(True, 2)
 Dim selection = s.Text
 If selection.StartsWith("=") Then
 DTE.ActiveDocument.Selection.Text = "== "
 ElseIf selection.EndsWith("-") Or selection.EndsWith("+") Or selection.EndsWith("*") Or selection.EndsWith("=") Or selection.EndsWith(" ") Then
 DTE.ActiveDocument.Selection.Text = selection.ToString() + "= "
 Else
 DTE.ActiveDocument.Selection.Text = selection.ToString() + " = "
 End If
 End Sub

Posted in C, CPP | Leave a Comment »

Concatenate String in #define with # and ##

Posted by hide1713 on May 16, 2009

According to C prefprcessor wiki page, There are two kinds of cacatenation.

# string concatenation combines input string value with other strings.

For example.

#define PRINT_NAME(name) printf(“Hi all, my name is “#name” !\n”)

PRINT_NAME(leo);   –>Hi all, my name is leo !

##  tokens concatenation.

For instance:

#define MYCASE(item,id) \
case id: \
  item##_##id = id;\
break

switch(x) {
    MYCASE(widget,23);
}

The line MYCASE(widget,23); gets expanded here into

case 23:
  widget_23 = 23;
break;

Posted in C | Leave a Comment »

GDB with Multiple Source Code Program

Posted by hide1713 on December 7, 2008

If you try to debug a multiple files program with gdb, someting you see error output from gdb like this

….

/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(.rodata+0×0):

multiple definition of `_fp_hw’

/usr/local/condor/lib/condor_rt0.o(.rodata+0×0): first defined here

hello.remote: In function `__moddi3′:

/usr/src/build/87732-i386/BUILD/gcc-2.96-20000731/obj-i386-redhat-linux/gcc/../../gcc/libgcc2.c(.data+0×4):

multiple definition of `__dso_handle’

/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o(.data+0×0): first

defined here

hello.remote: In function `_init’:

hello.remote(.init+0×0): multiple definition of `_init’

……

add --allow-multiple-definition to your compile flags. This tells the linker allow
multiple definition of _init or other fucntions

Now you can use gdb to debug.

Posted in C | Tagged: | Leave a Comment »

Get Assembly Code Output From GCC

Posted by hide1713 on November 26, 2008

Sometime we want to know the assembly code output for piece of c code. We can use this following command to do this.

gcc -S c_file.c

then it generates a c_file.s which is the assembly code of c_file.c

Read it and have fun.

Posted in C | Tagged: | Leave a Comment »

C Link List Source Code

Posted by hide1713 on November 4, 2008

/* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */

#include<stdio.h>
#include<stdlib.h>

/* STRUCTURE CONTANING A DATA PART AND A LINK PART */

   struct node
 {
   int data;
   struct node *next;
 }*p;

  /* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE FIRST NODE IN
LIST
*/

 /*THIS FUNCTION DELETES A NODE */

     delnode(int num)
 {
     struct node *temp, *m;
temp=p;
      while(temp!=NULL)
    {
       if(temp->data==num)
       {
           if(temp==p)
           {
              p=temp->next;
              free(temp);
              return;
           }
           else
         {
           m->next=temp->next;
           free(temp);
           return;
         }
      }else
        {
           m=temp;
          temp= temp->next;
        }

}
    printf("ELEMENT %d NOT FOUND ", num);
}/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */

    append( int num )
 {
     struct node *temp,*r;
     /* CREATING A NODE AND ASSIGNING A VALUE TO IT */

       temp= (struct node *)malloc(sizeof(struct node));
       temp->data=num;
       r=(struct node *)p;

      if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */
     {
    p=temp;
         p->next =NULL;
     }
  else
     {        /* GO TO LAST AND ADD*/

             while( r->next != NULL)
       r=r->next;
       r->next =temp;
       r=temp;
       r->next=NULL;
     }
  }/* ADD A NEW NODE AT BEGINNING  */

       addbeg( int num )
   {
        /*  CREATING A NODE AND INSERTING VALUE TO IT  */

   struct node *temp;
        temp=(struct node *)malloc(sizeof(struct node));
        temp->data=num;

       /* IF LIST IS NULL ADD AT BEGINNING  */
        if ( p== NULL)
       {
          p=temp;
          p->next=NULL;
       }

   else
      {
          temp->next=p;
          p=temp;
      }
   }

 /*  ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */

 addafter(int num, int loc)
  {
     int i;
     struct node *temp,*t,*r;
     r=p;       /* here r stores the first location */
      if(loc > count()+1 || loc <= 0)
   {
         printf("insertion is not possible :");
            return;
   }
        if (loc == 1)/* if list is null then add at beginning */
         {
           addbeg(num);
           return;
         }
      else
 {
       for(i=1;i<loc;i++)
          {
             t=r;   /* t will be holding previous value */
             r=r->next;
          }
         temp=(struct node *)malloc(sizeof(struct node));
         temp->data=num;
         t->next=temp;
         t=temp;
         t->next=r;
        return;
       }
}/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */

  display(struct node *r)
  {
      r=p;
      if(r==NULL)
     {
       printf("NO ELEMENT IN THE LIST :");
       return;
     }
       /* traverse the entire linked list */
       while(r!=NULL)
    {
      printf(" -> %d ",r->data);
      r=r->next;
    }
     printf("\n");
  }
//THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST
count()
 {
   struct node *n;
   int c=0;
   n=p;
    while(n!=NULL)
   {
     n=n->next;
     c++;
   }
  return(c);
 }
//THIS FUNCTION REVERSES A LINKED LIST
reverse(struct node *q)
{
   struct node *m, *n,*l,*s;
   m=q;
   n=NULL;
  while(m!=NULL)
 {
   s=n;
   n=m;
  m=m->next;
  n->next=s;
 }
   p=n;
}

/* THIS IS THE MAIN PROGRAM  */

  main()
 {
        int i;
   p=NULL;
   while(1) /* this is an indefinite loop */
 {
    printf("1.INSERT A NUMBER AT BEGINNING;");
    printf("2.INSERT A NUMBER AT LAST:");
    printf(" 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST:");
    printf(" 4.PRINT THE ELEMENTS IN THE LIST :");
    printf(" 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST  ");
    printf(" 6.DELETE A NODE IN THE LINKED LIST:");
    printf(" 7.REVERSE A LINKED LIST :");
    printf(" 8.GET OUT OF LINKED LIST (BYEE BYEE):");
    printf(" PLEASE, ENTER THE NUMBER:");

    scanf("%d",&i); /* ENTER A VALUE FOR SWITCH  */

      switch(i)
    {
         case 1:
      {
        int num;
        printf("PLEASE ENTER THE NUMBER :");
        scanf("%d",&num);
        addbeg(num);
        break;
      }
          case 2:
       {
         int num;
         printf(" PLEASE ENTER THE NUMBER :");
         scanf("%d",&num);
         append(num);
         break;
       }

   case 3:
     {
      int num, loc,k;
      printf(" PLEASE ENTER THE NUMBER :");
      scanf("%d",&num);
      printf("PLEASE ENTER THE LOCATION NUMBER :");
      scanf("%d",&loc);
      addafter(num,loc);
      break;
    }  case 4:
      {
         struct node *n;
         printf("   THE  ELEMENTS IN THE LIST ARE : ");
         display(n);
         break;
      }

      case 5:
   {
      struct node *n;
      display(n);
      printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());
      break;
   } case 6:
    {
            int    num;
      printf(" PLEASE ENTER A NUMBER FROM THE LIST :");
      scanf("%d",&num);
      delnode(num);
     break;
    }
   case 7:
    {
      reverse(p);
        display(p);
        break;
    }
  case 8:
 {
  exit(0);
 }
    }/* end if switch */
 }/* end of while */
}/* end of main */

gcc -o link link.c

1.INSERT A NUMBER AT BEGINNING;2.INSERT A NUMBER AT LAST: 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST: 4.PRINT THE ELEMENTS IN THE LIST : 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST 6.DELETE A NODE IN THE LINKED LIST: 7.REVERSE A LINKED LIST : 8.GET OUT OF LINKED LIST (BYEE BYEE): PLEASE, ENTER THE NUMBER:1
PLEASE ENTER THE NUMBER :12
1.INSERT A NUMBER AT BEGINNING;2.INSERT A NUMBER AT LAST: 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST: 4.PRINT THE ELEMENTS IN THE LIST : 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST 6.DELETE A NODE IN THE LINKED LIST: 7.REVERSE A LINKED LIST : 8.GET OUT OF LINKED LIST (BYEE BYEE): PLEASE, ENTER THE NUMBER:2
PLEASE ENTER THE NUMBER :2323
1.INSERT A NUMBER AT BEGINNING;2.INSERT A NUMBER AT LAST: 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST: 4.PRINT THE ELEMENTS IN THE LIST : 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST 6.DELETE A NODE IN THE LINKED LIST: 7.REVERSE A LINKED LIST : 8.GET OUT OF LINKED LIST (BYEE BYEE): PLEASE, ENTER THE NUMBER:4
THE ELEMENTS IN THE LIST ARE : -> 12 -> 2323

Posted in C | Tagged: , | Leave a Comment »