My402ListElem* My402ListFind(My402List *list, void *obj){
	if(list != NULL){
		My402ListElem *i = NULL;
		for(i = My402ListFirst(list); i != NULL; i = My402ListNext(list, i)){
		    if(i -> obj == obj)
		        return i;
		}
	}
    return 0;
}
示例#2
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");
}
示例#3
0
 My402ListElem *My402ListFind(My402List* list, void* obj)
 {
	 My402ListElem *elem=NULL;

	 for (elem=My402ListFirst(list); elem != NULL; elem=My402ListNext(list, elem))
	 {
		 if(elem->obj == obj)
			 return elem;
	 }	
	 return NULL;
 }
示例#4
0
My402ListElem * My402ListFind(My402List * list, void * data)
{
    My402ListElem * elem = My402ListFirst(list);
    while(elem)
    {
        if(elem -> obj == data)
            return elem;
        elem = My402ListNext(list, elem);
    }
    return NULL;
}
// Finds a specific list element
My402ListElem *My402ListFind(My402List* myList, void* elem) { 
	My402ListElem *listElem = My402ListFirst(myList);
	if(listElem->obj == elem) {
		return listElem;
	} else {
		while(listElem->obj != elem)
			listElem = My402ListNext(myList,listElem);
		if(listElem != NULL) 
			return listElem;
	}
	return NULL;
}
示例#6
0
void display(My402List * list)
{
     My402ListElem * elem = NULL;
     for (elem=My402ListFirst(list);elem != NULL;elem=My402ListNext(list, elem)) 
	 {
        Mytransaction * disp =(Mytransaction*) elem->obj; 
        printf("%d ", disp->time );
        printf("%d address %d\n", elem , &(list->anchor) );
        if( elem->next == &(list->anchor) )
            printf("%d points to anchor\n", elem);
     } 
}
示例#7
0
extern void My402ListUnlinkAll(My402List*list)
{
	 My402ListElem *curr=NULL;
	 My402ListElem *elem=NULL;
	  for (elem=My402ListFirst(list);elem != NULL;elem=My402ListNext(list, elem)) 
	  {
		curr=elem;
		free(curr);
	  }
	 list->anchor.next=NULL;
	 list->anchor.prev=NULL;
	 list->num_members=0;
}
示例#8
0
void RemoveAllMemoryAllocated()
{
	My402ListElem *elem;
	PacketData *data;
	for(elem = My402ListFirst(PacketDetailsList) ; elem != NULL ; elem = My402ListNext(PacketDetailsList, elem))
	{
		data = (PacketData *)elem->obj;
		free(data);
		data = NULL;
	}
	My402ListUnlinkAll(PacketDetailsList);
	elem = NULL;
}
static
void RandomShuffle(My402List *pList, int num_items)
{
    int i=0;
    My402List list2;
    My402ListElem *elem=NULL;

    memset(&list2, 0, sizeof(My402List));
    (void)My402ListInit(&list2);

    for (i=0; i < num_items; i++) {
        int j=0, idx=0, num_in_list=num_items-i;
        void *obj=NULL;

        idx = RandomIndex(num_in_list);
        for (elem=My402ListFirst(pList); elem != NULL; elem=My402ListNext(pList, elem)) {
            if (j == idx) {
                break;
            }
            j++;
        }
        if (elem == NULL) {
            fprintf(stderr, "Unrecoverable error in RandomShuffle().\n");
            exit(1);
        }
        obj = elem->obj;
        My402ListUnlink(pList, elem);
        (void)My402ListAppend(&list2, obj);
    }
    if (!My402ListEmpty(pList)) {
        fprintf(stderr, "List not empty in RandomShuffle().\n");
        exit(1);
    }
    for (elem=My402ListFirst(&list2); elem != NULL; elem=My402ListNext(&list2, elem)) {
        (void)My402ListAppend(pList, elem->obj);
    }
    My402ListUnlinkAll(&list2);
}
示例#10
0
void My402ListUnlinkAll(My402List * list)
{
    if(My402ListEmpty(list))
        return;
    My402ListElem * cur = My402ListFirst(list);
    while(cur)
    {
        My402ListElem * del = cur;
        cur = My402ListNext(list, cur);
        My402ListUnlink(list, del);
    }
    (list -> anchor).next = &(list -> anchor);
    (list -> anchor).prev = &(list -> anchor);
}
示例#11
0
void display(My402List* final_list)
{
    setlocale(LC_ALL, "en_CA.UTF-8");
    My402ListElem *element= NULL;
    Data *tempData = NULL;
    //char *descp = NULL;
    double balance = 0;
    
    fprintf(stdout,"+-----------------+--------------------------+----------------+----------------+\n");
    fprintf(stdout,"|       Date      | Description              |         Amount |        Balance |\n");
    fprintf(stdout,"+-----------------+--------------------------+----------------+----------------+\n");

    

    for(element=My402ListFirst(final_list); element!=NULL; element=My402ListNext(final_list,element))
    {

            tempData=(Data*)element->obj;
        
      
            printtime(tempData);

            
            printdescription(tempData);
            
            checking amt=(tempData->tran_amt);
            double amount=(double)(amt.at)/100;
            if(amt.fg==1)
            {
                char *pt=strchr(amount,'.');
                
            }
            
            
            if((tempData->type)=='-')      //=====DID CORRECTION======//
            {
                amount=-amount;
            }
        
            int flag=0;
            printamount(amount,flag);
            
            balance=balance+amount;
            flag=1;
            printamount(balance,flag);
            
    }
    fprintf(stdout,"+-----------------+--------------------------+----------------+----------------+\n");
    
}
示例#12
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;

        }

}
示例#13
0
extern My402ListElem *My402ListFind(My402List*list, void*item)
{
	  My402ListElem *elem=NULL;
	  elem=malloc(sizeof(struct tagMy402ListElem));
	  for (elem=My402ListFirst(list);elem != NULL;elem=My402ListNext(list, elem)) 
	  {
		//printf("--%d--",(int*)elem->obj);
		if(elem->obj==item)//IS THIS CORRECT??? (item is in the list having some addr, check if its the same addr
		{
			return(elem);
		}
	  }
	  return(NULL);
}
static
void CreateTestList(My402List *pList, TransactionElem *tranElement)
{
		My402ListElem *listElem = My402ListFirst(pList);
		int i = 0;
		for(;i<pList->num_members;i++) {
			if(tranElement->eleTime != (unsigned int)(((TransactionElem *)listElem->obj)->eleTime)) {
			listElem = My402ListNext(pList,listElem);
			} else {
					fprintf(stderr,"Error: Identical Timestamps cannot be added to the list");
					PrintErrorMessage();
			}
		}
        (void)My402ListAppend(pList, (void*)tranElement);
}
示例#15
0
 void My402ListUnlinkAll(My402List* list)
 {
	 My402ListElem *elem, *prev;
	 elem = prev = NULL;

	 for(elem=My402ListFirst(list);elem != NULL;) 
	 {
		prev = elem;
		elem = My402ListNext(list, elem);
		free(prev);
		prev = NULL;
	 }
	 list->anchor.next = list->anchor.prev = NULL;
	 list->num_members = 0;
 }
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");
}
示例#17
0
int checkTime(My402List *pList,long chktime)
{
	 My402ListElem *elem=NULL;
	 Transact *x;
	  elem=malloc(sizeof(struct tagMy402ListElem));
	  for (elem=My402ListFirst(pList);elem != NULL;elem=My402ListNext(pList, elem)) 
	  {
		
		x=(Transact*)elem->obj;
		//printf("--%d--",(int*)elem->obj);
		if(x->ltime==chktime)
		{
			return(1);//similar time exists
		}
	  }
	  return(0);
}
示例#18
0
int printOutput(My402List *pList){

	int line_number = 0;
	My402Output output;
	memset(&output, '\0', sizeof(My402Output));
	My402Output *pOutput = &output; 
	
	long long *pBalance = (long long *)malloc(sizeof(long long));
	if(pBalance == NULL){
		perror("\nUnable to allocate memory");	
		return FALSE;
	}
	memset(pBalance, 0, sizeof(long long)) ;
	if(pBalance == NULL){
		fprintf(stderr, "\nUnable to allocate memory");	
	}

	My402ListElem *current = NULL;
	/*for(; i<pList->num_members; i++){
		output[i] = (char *)malloc(80*sizeof(char));	
		memset(output[i], '\0', 80*sizeof(char));	
	}*/
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	printf("|%7cDate%6c|%1cDescription%14c|%9cAmount%1c|%8cBalance%1c|\n",32,32,32,32,32,32,32,32);
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	for(current=My402ListFirst(pList);
		current!=NULL;
		current=My402ListNext(pList, current)){
		
		if(formatEachField((My402SortElem *)current->obj, pOutput, pBalance) == FALSE){
			return FALSE;	
		}
		line_number++;	
		//printf("| Tue Jan 22 2008 | Paycheck                 |        698.06  |        698.06  |\n");
		//printf("| Sun Jun 22 2008 | Gasoline                 | (    4,033.39) |      1,827.21  |\n");
		printf("|%1c%s%1c|%1c%s%1c|%1c%s%1c|%1c%s%1c|\n",32,pOutput->printDate,32,32,pOutput->printDesc,32,32,pOutput->printAmount,32,32,pOutput->printBalance,32);	      
		//printf("12345678901234567890123456789012345678901234567890123456789012345678901234567890\n");
		//printf("         10        20        30        40        50        60        70        80\n");	
		//printf("\n");
		
		//free the current node of type My402ListElem now
	}	
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	return TRUE;	
}
示例#19
0
int removeTokens(int num)
{
    int i;

    if(!num) {
        My402ListUnlinkAll(&tokList);
        return 1;
    }

    My402ListElem *tempTok, *tempTok1;
    tempTok = My402ListFirst(&tokList);
    for(i = 0; i<num ; i++) {
        tempTok1 = tempTok;
        tempTok = My402ListNext(&tokList, tempTok);
        My402ListUnlink(&tokList, tempTok1);
    }
    return 1;
}
示例#20
0
int Mychecker(My402List*list,Mytransaction * object)
{
	/*code for traverse from homework slide - start */       
    My402ListElem *elem=NULL;      
    for (elem=My402ListFirst(list);
		elem != NULL;
		elem=My402ListNext(list, elem))
	
	/*code for traverse from homework slide - end */                 
    {  
        Mytransaction * t =(Mytransaction *) elem->obj;
        if( t->time == object->time )
        {
            printf("error-timestamp same");
            return FALSE;
        }
    }  
    return TRUE;                  
} 
示例#21
0
void BubbleSortList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
    int i=0;
    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=((trnx*)(elem->obj))->timeval, next_val=0;

            next_elem=My402ListNext(pList, elem);
            next_val = ((trnx*)(next_elem->obj))->timeval;

            if (cur_val > next_val) {
                BubbleSwap(pList, &elem, &next_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
示例#22
0
void printOutput(My402List *pList){

	int line_number = 0;
	int i=0;
	My402Output output;
	memset(&output, '\0', sizeof(My402Output));
	My402Output *pOutput = &output; 
	
	long long *pBalance = (long long *)malloc(sizeof(long long));
	memset(pBalance, 0, sizeof(long long)) ;
	if(pBalance == NULL){
		fprintf(stderr, "\nUnable to allocate memory");	
	}

	My402ListElem *current = NULL;
	/*for(; i<pList->num_members; i++){
		output[i] = (char *)malloc(80*sizeof(char));	
		memset(output[i], '\0', 80*sizeof(char));	
	}*/
	printf("\n");	
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	printf("|%7cDate%6c|%1cDescription%14c|%9cAmount%1c|%8cBalance%1c|\n",32,32,32,32,32,32,32,32);
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	for(current=My402ListFirst(pList);
		current!=NULL;
		current=My402ListNext(pList, current)){
		
		formatEachField((My402SortElem *)current->obj, pOutput, pBalance);
		line_number++;	
		printf("|%1c%s%1c|%2c%s|%2c%s|%2c%s|\n",32,pOutput->printDate,32,32,pOutput->printDesc,32,pOutput->printAmount,32,pOutput->printBalance);	      
		//printf("01234567890123456789012345678901234567890123456789012345678901234567890123456789\n");
		//printf("0         10        20        30        40        50        60        70       79");	
		//printf("\n");
	}	
	printf("+-----------------+--------------------------+----------------+----------------+\n");
	
}
示例#23
0
static
void BubbleSortForwardList(My402List *pList, int num_items)
{
    My402ListElem *elem=NULL;
    int i=0;
	Transact *x,*y;
	
	
    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++)
		{
			x=(Transact*)elem->obj;
			
            //int cur_val=(int)(elem->obj), next_val=0;
			long cur_val=(long)(x->ltime), next_val=0;
            next_elem=My402ListNext(pList, elem);
			y=(Transact*)next_elem->obj;
			
            next_val = (long)(y->ltime);
			
            if (cur_val > next_val) {
                BubbleForward(pList, &elem, &next_elem);
                something_swapped = TRUE;
            }
        }
        if (!something_swapped) break;
    }
}
示例#24
0
void print_list(My402List *list)
{
	print_title();

    My402ListElem * elem = NULL;
    long long balance_value = 0;
    int amount_value = 0;
    for (elem = My402ListFirst(list); elem != NULL; elem = My402ListNext(list, elem))
    {
        My402TransData * data = (My402TransData *)(elem -> obj);
        char buffer[26], date[16], description[25], amount[15], balance[15];
        time_t timestamp;
        timestamp = data -> timestamp;
        strncpy(buffer, ctime(&timestamp), sizeof(buffer));
        format_time(date, buffer);
        format_description(description, data -> description);
        amount_value = cal_amount(data -> amount, data -> type);
        format_money(amount, amount_value);
        balance_value = cal_balance(balance_value, data -> amount, data -> type);
        format_money(balance, balance_value);
        print_data(date, description, amount, balance);
    }
    print_line();
}
示例#25
0
void sort(My402List* final_list)        
{
    My402ListElem *first_ptr, *second_ptr, *innerelemminus;
    Data *inner, *innerminus, *temp;
    int i=0;
    
    for(first_ptr=My402ListFirst(final_list);first_ptr!=NULL;first_ptr=My402ListNext(final_list,first_ptr))
    {
        i=0;
        for(second_ptr=My402ListLast(final_list);second_ptr!=My402ListFirst(final_list);second_ptr=My402ListPrev(final_list,second_ptr))      
        {
            inner=(Data*)second_ptr->obj;
            innerelemminus= My402ListPrev(final_list,second_ptr);
            innerminus=(Data*)innerelemminus->obj;
            
                       
            if(inner->time==innerminus->time)
            {
                printf("ERROR:Two transaction with same timestamp\n");
                exit(0);
            }
            if(innerminus->time > inner->time)
            {
                temp = (Data*) innerelemminus->obj;
                innerelemminus->obj = second_ptr->obj;
                second_ptr->obj = (void*)temp;
                i=1;
                
            }
            
        }
        
    }
  
    
}
示例#26
0
void print_list(My402List *list)
{
	print_title();

    My402ListElem * elem = NULL;
    int balance_value = 0, amount_value = 0;
    for (elem = My402ListFirst(list); elem != NULL; elem = My402ListNext(list, elem))
    {
        My402TransData * data = (My402TransData *)(elem -> obj);
        char line[LINE_LENGTH + 1], buffer[26];
        time_t timestamp;
        timestamp = data -> timestamp;
        strncpy(buffer, ctime(&timestamp), sizeof(buffer));
        format_line(line);
        format_time(line, buffer, 2);
        format_description(line, data -> description, 19);
        amount_value = cal_amount(data -> amount, data -> type);
        format_money(line, amount_value, 46, 61);
        balance_value = cal_balance(balance_value, data -> amount, data -> type);
        format_money(line, balance_value, 63, 78);
        fprintf(stdout, "%s\n", line);
    }
    print_line();
}
static
void PrintList(My402List *pList)
{
    My402ListElem *elem=NULL;
    char date[16], buff[26];
    int balance = 0;
    int a = 0,b = 0,absoluteSum;
    char *e = "?,???,???.??";
    time_t elTime;
    
    
    
    fprintf(stdout,"%s\n","+-----------------+--------------------------+----------------+----------------+");
    fprintf(stdout,"%s\n","|       Date      | Description              |         Amount |        Balance |");
	fprintf(stdout,"%s","+-----------------+--------------------------+----------------+----------------+");
	
    for (elem=My402ListFirst(pList); elem != NULL; elem=My402ListNext(pList, elem)) {
		int transactionType = (int)(((TransactionElem *)elem->obj)->transactionType);
		unsigned int eleTime = (unsigned int)(((TransactionElem *)elem->obj)->eleTime);
		int amount = (int)(((TransactionElem *)elem->obj)->amount);
		char *description = (char*)(((TransactionElem *)elem->obj)->description);
		
		elTime = eleTime;
		strncpy(buff,ctime(&elTime),sizeof(buff));
		date[0]=buff[0];date[1]=buff[1];date[2]=buff[2];date[3]=buff[3];date[4]=buff[4];date[5]=buff[5];date[6]=buff[6];
		date[7]=buff[7];date[8]=buff[8];date[9]=buff[9];date[10]=buff[10];date[11]=buff[20];date[12]=buff[21];date[13]=buff[22];
		date[14]=buff[23];date[15]='\0';
		
		fprintf(stdout,"\n| %15s |",date);
        fprintf(stdout," %-24s ",description);
        if(transactionType == -1) {
			balance -= amount;
		} else {
			balance += amount;
		}
		a = amount / 100;
		b = amount % 100;
		FormatAmount(abs(a),abs(b),transactionType);
		
		absoluteSum= abs(balance);    
            if((absoluteSum/100)>10000000)
            {   
				 if(balance <0)
                    fprintf(stdout, "| (%s) ",e);
                else
                    fprintf(stdout, "| (%s) ",e);    
            }
            else
            {
                a = absoluteSum/100;
                b = absoluteSum%100;
                if(balance <0)
                    FormatAmount(a,b, -1);
                else
                    FormatAmount(a,b,1);
                
            }        
		fprintf(stdout,"|"); 
    }
    fprintf(stdout,"\n%s\n","+-----------------+--------------------------+----------------+----------------+");
}
示例#28
0
/*****************sort***************************************/
void SortMyList(My402List *List)
{
    /*test the list
    My402ListElem *test;
    test = My402ListFirst(List);
    if(test == List->anchor.next)
    printf("%s\n","good1");
    while(test != NULL)
    {
        if(test->next==My402ListNext(List,test))
        {
            printf("%s\n","good2");

        }
        printf("%d\n",((My402ListElemObj*)test->obj)->TTime);
        //if(test == NULL)
        //printf("%s","good3");
        test = My402ListNext(List,test);

    }
    if(List->anchor.prev == My402ListLast(List))
    printf("%s","good");
    */

    My402ListElemObj *temp1, *temp2;
    My402ListElem *elem1, *elem2;
    //if(My402ListEmpty(List))
        //exit~
    elem1=My402ListFirst(List);
    while(elem1 != NULL)
    {
        elem2 = My402ListNext(List, elem1);
        while(elem2 != NULL)
        {
            temp1 = (My402ListElemObj*)elem1->obj;
            temp2 = (My402ListElemObj*)elem2->obj;
            if(temp1->TTime > temp2->TTime)
            {
                elem1->obj = temp2;
                elem2->obj = temp1;
            }
            if(temp1->TTime == temp2->TTime)
                ExitAll(" there are 2 time with the same value ",0);
            //exit~
            elem2 = My402ListNext(List, elem2);
            //if(elem2 != NULL)
            //printf("%d\n", ((My402ListElemObj*)elem2->obj)->TTime);
        }
        elem1 = My402ListNext(List, elem1);
    }

    /*//test
    My402ListElem *test = My402ListFirst(List);
    while(test != NULL)
    {
        printf("%d\n", ((My402ListElemObj*)test->obj)->TTime);
        test=My402ListNext(List,test);
    }
    */

}
示例#29
0
/*********************************************************
   Display Output function
*********************************************************/
void DisplayOutput(My402List *pList)
{
  My402ListElem *elem = NULL;
  time_t timestamp;
  int balance = 0;
  char *description;
  int i,j,k;
  /*
    Print the header information
  */
  fputs("+",stdout);
  RepeatChar("-",17);
  fputs("+",stdout);
  RepeatChar("-",26);
  fputs("+",stdout);
  RepeatChar("-",16);
  fputs("+",stdout);
  RepeatChar("-",16);
  fputs("+",stdout);
  fputs("\n",stdout);
  fputs("|",stdout);
  RepeatChar(" ",7);
  fputs("Date",stdout);
  RepeatChar(" ",6);
  fputs("|",stdout);
  fputs(" Description",stdout);
  RepeatChar(" ",14);
  fputs("|",stdout);
  RepeatChar(" ",9);
  fputs("Amount ",stdout);
  fputs("|",stdout);
  RepeatChar(" ",8);
  fputs("Balance ",stdout);
  fputs("|",stdout);
  fputs("\n",stdout);
  fputs("+",stdout);
  RepeatChar("-",17);
  fputs("+",stdout);
  RepeatChar("-",26);
  fputs("+",stdout);
  RepeatChar("-",16);
  fputs("+",stdout);
  RepeatChar("-",16);
  fputs("+",stdout);
  fputs("\n",stdout);
  /*
  Header Information Printed
  */
  /*
   Print each element in the list
  */
  for (elem=My402ListFirst(pList); elem != NULL; elem=My402ListNext(pList, elem)) {
  /*
   Print time
  */
  timestamp = (time_t)(((trnx*)(elem->obj))->timeval);
  fputs("| ",stdout);
  char *ctimestamp = ctime(&timestamp);
  //char *temptime;
  for(i=0;i<11;i++)
  {
    fprintf(stdout,"%c",ctimestamp[i]);
  }
  for(i=20;i<24;i++)
  {
    fprintf(stdout,"%c",ctimestamp[i]);
  }

  //fprintf(stdout,"%s here",ctimestamp);
  fputs(" | ",stdout);

  /*
   Print Description
  */

  description = (((trnx*)(elem->obj))->description);
  if (strlen(description) > 24)
  {

    for (i=0;i<24;i++)
    {
      fprintf(stdout,"%c",description[i]);
    }
    fputs(" ",stdout);
  }
  else
  {
    for (i=0;i<(strlen(description)-1);i++)
    {
      fprintf(stdout,"%c",description[i]);
    }

    for(i=0;i<=(25-strlen(description));i++)
    {
      fputs(" ",stdout);
    }

  }
  fputs("| ",stdout);

  /*
    Print Amount
  */
  char *amount= malloc(15);
  char *temp = malloc(16);
  sprintf(amount,"%d",((trnx*)(elem->obj))->amt);
  if(strcmp(((trnx*)(elem->obj))->op,"-") == 0)
  {
    fputs("(",stdout);
  }
  else
  {
    fputs(" ",stdout);
  }
  if(strlen(amount) == 2 )
  {
    temp[0] = '0';
    temp[1] ='.';
    temp[2] = amount[0];
    temp[3] = amount[1];
    k = 4;
  }
  else if (strlen(amount) == 1 )
  {
    temp[0] = '0';
    temp[1] ='.';
    temp[2] = '0';
    temp[3] = amount[0];
    k = 4;

  }
  else
  {
  for(i=strlen(amount)-1,j = 0,k=0;i>=0;i--,j++)
  {
    if(i == 8 || i == 5)
    {
      temp[k]=amount[j];
      k++;
      temp[k]= ',';
      k++;
    }
    else if( i == 2)
    {
      temp[k]=amount[j];
      k++;
      temp[k]= '.';
      k++;
    }
    else
    {
      temp[k]=amount[j];
      k++;
    }
  }
  }
  temp[k]= '\0';
  for(i=0;i<12-strlen(temp);i++)
  {
    fputs(" ",stdout);
  }

  fprintf(stdout,"%s",temp);
  if(strcmp(((trnx*)(elem->obj))->op,"-") == 0)
  {
    fputs(")",stdout);
  }
  else
  {
    fputs(" ",stdout);
  }
  fputs(" | ",stdout);
  free(temp);
  /*
    Print Balance
  */
  if(strcmp(((trnx*)(elem->obj))->op,"-") == 0)
  {
    balance = balance - ((trnx*)(elem->obj))->amt;
  }
  else
  {
    balance = balance + ((trnx*)(elem->obj))->amt;
  }
  char *bal_amount = malloc(15);
  temp = malloc(16);
  sprintf(bal_amount,"%d",abs(balance));

  if(balance < 0)
  {
    fputs("(",stdout);
  }
  else
  {
    fputs(" ",stdout);
  }
if(strlen(bal_amount)> 9)
{
  free(bal_amount);
  bal_amount = malloc(15);
  strncpy(bal_amount,"?,???,???.??",15);
  fprintf(stdout,"%s",bal_amount);
}
else
{
  for(i=strlen(bal_amount)-1,j = 0,k=0;i>=0;i--,j++)
  {
    if(i == 8 || i == 5)
    {
      temp[k]=bal_amount[j];
      k++;
      temp[k]= ',';
      k++;
    }
    else if( i == 2)
    {
      temp[k]=bal_amount[j];
      k++;
      temp[k]= '.';
      k++;
    }
    else
    {
      temp[k]=bal_amount[j];
      k++;
    }
  }
  temp[k]= '\0';
  for(i=0;i<12-strlen(temp);i++)
  {
    fputs(" ",stdout);
  }
}
  fprintf(stdout,"%s",temp);
  if(balance < 0)
  {
    fputs(")",stdout);
  }
  else
  {
    fputs(" ",stdout);
  }
  fputs(" |",stdout);
  //fputs("\n",stdout);
  free(temp);
  free(bal_amount);
  free(amount);

  fputs("\n",stdout);
}

fputs("+",stdout);
RepeatChar("-",17);
fputs("+",stdout);
RepeatChar("-",26);
fputs("+",stdout);
RepeatChar("-",16);
fputs("+",stdout);
RepeatChar("-",16);
fputs("+",stdout);
fputs("\n",stdout);


}
示例#30
0
void CalculateAndDisplayStatistics(double emuEndTime)
{
	int packetsServiced;
	double avgPktInterArrivalTime, avgPktServiceTime, avgTimeInSystem, avgPktsAtQ1, avgPktsAtQ2, avgPktsAtServer, tokenDropProbability, packetDropProbability;
	double ePowerX2, eXPower2, standardDeviation;
	My402ListElem *elem;
	PacketData *data;
	avgPktInterArrivalTime = 0;
	avgPktServiceTime = 0;
	avgTimeInSystem = 0;
	avgPktsAtQ1 = 0;
	avgPktsAtQ2 = 0;
	avgPktsAtServer = 0;
	standardDeviation = 0;
	ePowerX2 = 0;
	eXPower2 = 0;
	tokenDropProbability = 0;
	packetDropProbability = 0;
	packetsServiced = 0;
	for(elem = My402ListFirst(PacketDetailsList) ; elem != NULL ; elem = My402ListNext(PacketDetailsList, elem))
	{
		data = (PacketData *)elem->obj;
		data->serviceTime /= 1000;
		data->interArrivalTime /= 1000;
		data->timeInSystem /= 1000;
		data->timeInQ1 /= 1000;
		data->timeInQ2 /= 1000;
	}
	for(elem = My402ListFirst(PacketDetailsList) ; elem != NULL ; elem = My402ListNext(PacketDetailsList, elem))
	{
		data = (PacketData *)elem->obj;
		avgPktInterArrivalTime += data->interArrivalTime;
		if(data->packetServiced == true)
		{
			packetsServiced++;
			avgPktServiceTime += data->serviceTime;
			avgTimeInSystem += data->timeInSystem;
			avgPktsAtQ1 += data->timeInQ1;
			avgPktsAtQ2 += data->timeInQ2;
			ePowerX2 += ((data->timeInSystem)*(data->timeInSystem));
		}
	}
	avgPktsAtServer = avgPktServiceTime;
	if(packetsInSystem != 0 && packetsServiced !=0)
	{
		emuEndTime = emuEndTime/1000;
		ePowerX2 /= packetsServiced;
		avgPktInterArrivalTime /= packetsInSystem;
		avgPktServiceTime /= packetsServiced;
		avgTimeInSystem /= packetsServiced;
		avgPktsAtQ1 /= emuEndTime;
		avgPktsAtQ2 /= emuEndTime;
		avgPktsAtServer /= emuEndTime;
	}
	if(packetsInSystem != 0)
	{
		packetDropProbability = (double)droppedPackets / (double)packetsInSystem;
	}
	if(totalTokens != 0)
	{
		tokenDropProbability = (double)droppedTokens / (double)totalTokens;
	}
	eXPower2 = (avgTimeInSystem)*(avgTimeInSystem);
	standardDeviation = ePowerX2 - eXPower2;
	standardDeviation = sqrt(standardDeviation);
	DisplayStatistics(avgPktInterArrivalTime, avgPktServiceTime, avgTimeInSystem, avgPktsAtQ1, avgPktsAtQ2, avgPktsAtServer, standardDeviation, tokenDropProbability, packetDropProbability);
}