static
void BubbleSortForwardList(My402List *pList)
{
    My402ListElem *elem=NULL;
    int i=0;

    if (My402ListLength(pList) == 0) {
        fprintf(stderr, "List is empty!");
        PrintErrorMessage();
        exit(1);
    }
    for (i=0; i < My402ListLength(pList); i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *next_elem=NULL;

        for (elem=My402ListFirst(pList), j=0; j < My402ListLength(pList)-i-1; elem=next_elem, j++) {
           unsigned int cur_val=(unsigned int)((TransactionElem *)(elem->obj))->eleTime, next_val=0;

            next_elem=My402ListNext(pList, elem);
            next_val = (unsigned int)((TransactionElem *)(next_elem->obj))->eleTime;

            if (cur_val > next_val) {
                BubbleForward(pList, &elem, &next_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
static
void CompareTestList(My402List *pList1, My402List *pList2, int num_items)
{
    My402ListElem *elem1=NULL, *elem2=NULL;
    int idx=0;

    if (My402ListLength(pList1) != num_items) {
        fprintf(stderr, "List1 length is not %1d in CompareTestList().\n", num_items);
        exit(1);
    }
    if (My402ListLength(pList2) != num_items) {
        fprintf(stderr, "List2 length is not %1d in CompareTestList().\n", num_items);
        exit(1);
    }
    for (elem1=My402ListFirst(pList1), elem2=My402ListFirst(pList2);
            elem1 != NULL && elem2 != NULL;
            elem1=My402ListNext(pList1, elem1), elem2=My402ListNext(pList2, elem2), idx++) {
        int ival1=(int)(elem1->obj);
        int ival2=(int)(elem2->obj);

        if (ival1 != ival2) {
            fprintf(stderr, "(%1d,%1d): item %1d not identical in CompareTestList().\n", ival1, ival2, idx);
            exit(1);
        }
    }
}
static
void BubbleSortBackwardList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
    int i=0;

    if (My402ListLength(pList) != num_items) {
        fprintf(stderr, "List length is not %1d in BubbleSortBackwardList().\n", num_items);
        exit(1);
    }
    for (i=0; i < num_items; i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *prev_elem=NULL;

        for (elem=My402ListLast(pList), j=0; j < num_items-i-1; elem=prev_elem, j++) {
            int cur_val=(int)(elem->obj), prev_val=0;

            prev_elem=My402ListPrev(pList, elem);
            prev_val = (int)(prev_elem->obj);

            if (cur_val < prev_val) {
                BubbleBackward(pList, &elem, &prev_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
static
void BubbleSortForwardList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
    int i=0;

    if (My402ListLength(pList) != num_items) {
        fprintf(stderr, "List length is not %1d in BubbleSortForwardList().\n", num_items);
        exit(1);
    }
    for (i=0; i < num_items; i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *next_elem=NULL;

        for (elem=My402ListFirst(pList), j=0; j < num_items-i-1; elem=next_elem, j++) {
            int cur_val=(int)(elem->obj), next_val=0;

            next_elem=My402ListNext(pList, elem);
            next_val = (int)(next_elem->obj);

            if (cur_val > next_val) {
                BubbleForward(pList, &elem, &next_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
示例#5
0
static
void BubbleSort(My402List *pList)
{
    My402ListElem *elem=NULL;
    int i=0;
    int num_items = My402ListLength(pList);
    for (i=0; i < num_items; i++) {
        int j=0, something_swapped=FALSE;
        My402ListElem *next_elem=NULL;

        for (elem=My402ListFirst(pList), j=0; j < num_items-i-1; elem=next_elem, j++) {
          struct transaction_info *cur_val=(struct transaction_info *)&elem->obj;
          struct transaction_info *next_val;

            next_elem=My402ListNext(pList, elem);
            next_val = (struct transaction_info *)&next_elem->obj;
            int *a = (int *)cur_val->t_date;
            int *b = (int *)next_val->t_date;
            //printf("%d %d\n",*a,*b );


            if (*a > *b) {
              //printf("ok\n");
              BubbleSwap(pList, &elem, &next_elem);
              something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
示例#6
0
void* arriveThread(void* arg)
{
	useconds_t sleepTime; //time to sleep
	TimeVal startTime, endTime;
	
	int emptyFlag;
	int waitingTime;

	sleepTime = -1;
	emptyFlag = -1;
	waitingTime = -1;

	while(TRUE)
	{
		if(-1 == usleep(sleepTime))
		{
			printf("something wrong with usleep\n");
		}

		gettimeofday(&startTime, &tz);

		Packet* p = createPacket();

		if(p->requiredToken > g_tokenSize)
			continue;


		// ******start critical section*************
		pthread_mutex_lock(&m);
		emptyFlag = MyQueueEmpty(&arrivalQueue);
		MyQueueEnqueue(&arrivalQueue, (void*)p);

		if (emptyFlag && p->requiredToken <= g_tokens)
		{
			g_tokens -= p->requiredToken;
			MyQueueDequeue(&arrivalQueue);
			MyQueueEnqueue(&serverQueue, (void*)p);

			if( 1 == My402ListLength(&serverQueue.list))
			{
				pthread_cond_signal(&newPacketToServer);
			}

		}
		pthread_mutex_unlock(&m);
		// *********end critical section*************
		gettimeofday(&endTime, &tz);


		// read file here if needed
		sleepTime = waitingTime - diff2Timeval(startTime, endTime);


	}
}
示例#7
0
static
void PrintTestList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
	int i=0,bal=0;
	Transact *x;
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	printf("|       Date      | Description              |         Amount |        Balance |\n");
	printf("+-----------------+--------------------------+----------------+----------------+\n");
		
	
    if (My402ListLength(pList) != num_items)
	{
        fprintf(stderr, "List length is not %1d in PrintTestList().\n", num_items);
        exit(1);
    }
    for (elem=My402ListFirst(pList); elem != NULL; elem=My402ListNext(pList, elem),i++)
	{//Transact *x;
		x=(Transact*)elem->obj;
		char op=(char)(x->op);
		long time=(long)(x->ltime);
        int amt=(int)(x->amt);
		char *desc=x->desc;
		char t[24],amount[15],balance[15];
		strcpy(t,ctime(&time));
		
		char fintime[15];
		strcpy(fintime,processDate(t));
		//int i;
		if(op=='+')
		{
			bal=bal+amt;
		}
		else
		{
			bal=bal-amt;
		}
		
		strcpy(amount,processNum(amt));
		strcpy(balance,processNum(bal));
		
		//printf("%lu\t",(long)(((Transact*)(elem->obj))->ltime));
		// printf("%c\t%lu\t%d\t%s\n",op,time,amt,desc);
      
//	  printf("|%lu\t%s\t\t%0.2f\t%0.2f\n",time,desc,amount,balance);
	if(op=='-')
		printf("| %s | %-24s | (%12s) | %14s |\n",b,desc,amount,balance);
	else
		printf("| %s | %-24s | %14s | %14s |\n",b,desc,amount,balance);
		//printf("FINTIME %s",fintime);
    }
	printf("+-----------------+--------------------------+----------------+----------------+");
	
    fprintf(stdout, "\n");
}
示例#8
0
 void My402ListUnlink(My402List* list, My402ListElem* elem)
 {
	 // do I need to use list?
	if( My402ListLength(list) == 0)
		return;

	 elem->prev->next = elem->next;
	 elem->next->prev = elem->prev;
	 --list->num_members;
	 free(elem); elem = NULL;
 }
示例#9
0
void BubbleSortForwardList(My402List *pList, int num_items)

{

        My402ListElem *elem=NULL;   
        int i=0;  
        tfile_list* tmp1;

        if (My402ListLength(pList) != num_items) {
            fprintf(stderr, "List length is not %1d in BubbleSortForwardList().\n", num_items);
            exit(1);

        }

        for (i=0; i < num_items; i++) {
            int j=0, something_swapped=FALSE;
            My402ListElem *next_elem=NULL;

            for (elem=My402ListFirst(pList), j=0; j < num_items-i-1; elem=next_elem, j++) {

                tmp1 = (tfile_list*) elem -> obj;
                long cur_val=/*(long)*/atol(tmp1->tran_time);
                long next_val=0;

                next_elem=My402ListNext(pList, elem);
                tfile_list* tmp2 = (tfile_list*) next_elem -> obj;



                next_val = /*(long)*/atol(tmp2->tran_time);





                if (cur_val > next_val) {

                    BubbleForward(pList, &elem, &next_elem);

                    something_swapped = TRUE;

                }

            }

            if (!something_swapped) break;

        }

}
static
void PrintTestList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;

    if (My402ListLength(pList) != num_items) {
        fprintf(stderr, "List length is not %1d in PrintTestList().\n", num_items);
        exit(1);
    }
    for (elem=My402ListFirst(pList); elem != NULL; elem=My402ListNext(pList, elem)) {
        int ival=(int)(elem->obj);

        fprintf(stdout, "%1d ", ival);
    }
    fprintf(stdout, "\n");
}
示例#11
0
void* tokenThread(void* arg)
{
	int tokenIndex;
	useconds_t waitingTime, sleepTime;
	TimeVal startTime, endTime;

	waitingTime = turnRateToMicroSeconds(input.r);
	if(waitingTime > 10 * 1000000)
		waitingTime = 10 * 1000000;

	tokenIndex = 0;
	while(TRUE)
	{	

		usleep(sleepTime);
		tokenIndex++;
		pthread_mutex_lock(&m);

		
		gettimeofday(&startTime, &tz);

		if(g_tokens == input.B)
			printf("token overflow");
		else
		{
			++g_tokens;
			Packet* p = (Packet*)MyQueueTop(&arrivalQueue);
			if(p->requiredToken <= g_tokens)
			{
				g_tokens -= p->requiredToken;
				MyQueueDequeue(&arrivalQueue);
				MyQueueEnqueue(&serverQueue, (void*)p);

				if( 1 == My402ListLength(&serverQueue.list))
				{
					pthread_cond_signal(&newPacketToServer);
				}
			}

		}
		gettimeofday(&endTime, &tz);
		sleepTime = waitingTime - diff2Timeval(startTime, endTime);
		pthread_mutex_unlock(&m);
	}
}
void ParseFile(FILE *file){
  char buffer[1024];
  memset(buffer, 0, sizeof(buffer));
  
  transaction *obj = NULL;
  My402List list;
  memset(&list, 0, sizeof(My402List));
  (void)My402ListInit(&list);

  while(fgets(buffer, sizeof(buffer), file) != NULL){
    obj = (transaction*)malloc(sizeof(transaction));
    if(obj == NULL){
      fprintf(stderr, "%s\n", strerror(errno));
    }
    else{
      char *start_ptr = buffer;
      char *tab_ptr = strchr(start_ptr, '\t');
      
      int noofptr = 0;
      while(tab_ptr != NULL){
	noofptr++;
	tab_ptr++;
	start_ptr = tab_ptr;
	tab_ptr = strchr(start_ptr, '\t');
      }
      
      if(noofptr > 3){
	fprintf(stderr, "Too many tabs, Clearing memory and Quiting Program\n");
	free(obj);
	My402ListUnlinkAll(&list);
	exit(0);
      }

      if(noofptr < 3){
	fprintf(stderr, "Format Malformed, Clearing memory and Quiting Program\n");
	free(obj);
	My402ListUnlinkAll(&list);
	exit(0);
      }

      start_ptr = buffer;
      tab_ptr = strchr(start_ptr, '\t');

      if(Transaction_Type(obj, start_ptr, tab_ptr) == 0){
	fprintf(stderr, "Transaction Type failed, Clearing Memory and Quiting Program\n");
	free(obj);
	My402ListUnlinkAll(&list);
	exit(0);
      }
      tab_ptr++;
      start_ptr = tab_ptr;
      tab_ptr = strchr(start_ptr, '\t');
      
      if(Transaction_TimeStamp(obj, start_ptr, tab_ptr) == 0){
	fprintf(stderr, "Transaction Time failed, Clearing Memory and Quiting Program\n");
	free(obj);
	My402ListUnlinkAll(&list);
	exit(0);
      }

      tab_ptr++;
      start_ptr = tab_ptr;
      tab_ptr = strchr(start_ptr, '\t');
      
      if(Transaction_Amount(obj, start_ptr, tab_ptr) == 0){
	fprintf(stderr, "Transaction Amount failed, Clearing Memory and Quiting Program\n");
	free(obj);
	My402ListUnlinkAll(&list);
	exit(0);
      }

      tab_ptr++;
      start_ptr = tab_ptr;
      tab_ptr = strchr(start_ptr, '\n');

      Transaction_Description(obj, start_ptr, tab_ptr);
      

      //Now the struct is filled with the string fields read from the first line of the file
      if(My402ListLength(&list) == 0)
	(void)My402ListPrepend(&list, obj);
      else{
	My402ListElem *elem = NULL;
	for(elem = My402ListFirst(&list); elem != NULL; elem=My402ListNext(&list, elem)){
	  if((((transaction*)(elem->obj))->trans_time == obj->trans_time)){
	    fprintf(stderr, "Error - Two entry at same time, Quiting program\n");
	    free(obj);
	    My402ListUnlinkAll(&list);
	    exit(0);
	  }
	  if((((transaction*)(elem->obj))->trans_time > obj->trans_time)){
	    (void)My402ListInsertBefore(&list, obj, elem);
	    break;
	  }
	  if(My402ListNext(&list, elem) == NULL){
	    (void)My402ListAppend(&list, obj);
	    break;
	  }
	}
      }
    }
  }
  PrintTransactionList(&list);
  My402ListUnlinkAll(&list);
}
示例#13
0
int main(int argc,char *argv[])

{

    FILE *fp;

    char line[2000];

    int num_mem;

    My402List list;

    memset(&list, 0, sizeof(My402List));

    (void)My402ListInit(&list); 
    

    if(argc==1)

    {

        printf("malformed command\n");

                exit(EXIT_FAILURE);

    }   



    if(argc==4)

    {

        printf("malformed command\n");

                exit(EXIT_FAILURE);

    }

    if(strlen(line)>1024)
    {
        printf("Length of line is more than 1024\n");
        exit(EXIT_FAILURE);

    }



    if(argc == 3 && (strncmp(argv[1],"sort",strlen(argv[1]))==0))

    {

    fp = fopen(argv[2], "r");

       

    if (fp==NULL)

    {

        fprintf(stderr, "Unable to open '%s'\n",argv[3]);

                exit(EXIT_FAILURE);

    }

    while (fgets(line,2000, fp)!=NULL)

    {
        //printf("%s\n",line);
        parse_line(line, &list);

    } 

     fclose(fp);

    }
    else if(argc == 2 && (strncmp(argv[1],"sort",strlen(argv[1]))==0))

    {

         fp =stdin;

    if (fp==NULL)

        {

        fprintf(stderr, "Unable to open '%s'\n",argv[3]);

                exit(EXIT_FAILURE);

        }

   

    while (fgets(line,2000, fp)!=NULL)

    {

      
         parse_line(line, &list);

               

    }
  fclose(fp);
}
    
else 
  {

    printf("Malformed Command");
    Usage();

  }  

    num_mem=My402ListLength(&list);

     BubbleSortForwardList(&list,num_mem);

   

    printlist(&list);

   

    return 0;

}       
示例#14
0
main( int argc, char *argv[] )
{
      int status;
      char tt;
      unsigned int ti;
      float ff;
      char amt[10];
      char des[100];
      int number = 1024;
      char line[1026];
      My402List *list =(My402List *)malloc(sizeof(My402List));
      printf("start");
      FILE * fp;
      if( argc == 1)
      { 
		fp = stdin;
        if(ferror (fp))
		{
            printf("error opening file!");
            clearerr(fp);
            exit(0);
		} 
      }
      else
	  {
		fp = fopen( "gg.txt" , "r" );
		if(ferror (fp))
		{
            printf("error opening file!");
            clearerr(fp);
            exit(0);
		} 
		else
		{                              
			My402ListElem *elem = (My402ListElem *)malloc(sizeof(My402ListElem));
			status = My402ListInit(list);
			if(status != FALSE)
			{
				while( fgets(line,number,fp) != NULL)
				{         
					sscanf(line,"%c\t%d\t%s\t%[^\n]s",&tt,&ti,amt,des);
					printf("%c\n%d\n%s\n%s\n",tt,ti,amt,des);
					Mytransaction * tr =(Mytransaction*)malloc(sizeof(Mytransaction));
					tr->transtype = tt;
					tr->time      = ti;
					strcpy( tr->amounts , amt );
					strcpy( tr->desc , des );      
					status = Mychecker(list,tr);
					if(status == TRUE)
					{
						status = My402ListPrepend(list,tr);
						if(status != TRUE )
						printf("not prepended");
					}
				} 
            }
            else
            {
                printf("list not initialised");
            }              
		}	
      
      BubbleSortForwardList(list,My402ListLength(list));
      //***output********* 
      printf("\n00000000011111111112222222222333333333344444444445555555555666666666677777777778");
      printf("\n12345678901234567890123456789012345678901234567890123456789012345678901234567890");
      printf("\n+-----------------+--------------------------+----------------+----------------+");
      printf("\n|       Date      | Description              |         Amount |        Balance |");
      printf("\n+-----------------+--------------------------+----------------+----------------+");
      MyListTraverse(list);  
      printf("\n+-----------------+--------------------------+----------------+----------------+");  
      }
}