当前位置:编程学习 > C/C++ >>

求150行左右的完整C语言程序段?

追问:能对程序作些说明吗?例如这个程序段的题目,要求等!谢了、、、
答案:要什么内容的?

先给个自己写的,测试通过:

 

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

#define LEN sizeof(struct worker)

 


struct worker     //num为工号,score为工作量
 {long num;
  float score;
  struct worker *next;
 };
int n;            //全局变量--总人数(总结点数)

 


//***建立链表函数***
struct worker *creat(void)
 {struct worker *head;
  struct worker *p1,*p2;
  n=0;
  p1=p2=(struct worker *)malloc(LEN);  //开辟一个新结点
  scanf("%ld:%f",&p1->num,&p1->score);
  head=NULL;
  while(p1->num != 0)  //p1不断更新指向新开辟的节点,p2不断更新指向新结点之前一个结点,建立链表关系
  {n=n+1;
   if(n==1) head=p1;
   else p2->next=p1;
   p2=p1;
   p1=(struct worker *)malloc(LEN);
   scanf("%ld:%f",&p1->num,&p1->score);
  }
 p2->next=NULL;
 return(head);
 }


//***输出链表函数***
void print(struct worker *head)
 {struct worker *p,*temp;
  long nums[100]={0},T_num;
  int i=1,j=1;//i为名次;j为名次对应的人数
  int k,x,y;//;k,x,y为循环变量
 
  p=head;
  printf("\nNow,Totall %d records are:\n\n",n);
 
  if(head!=NULL)
     do
   { 
    temp = p->next;
    if(temp==NULL)
   {
    nums[j]=p->num;
    printf("\n\n名次:%d   工作量:%f   人数:%d\n",i,p->score,j);
    printf("工号是:");   
    
    for(x=1;x<j;x++)
     for(y=x+1;y<=j;y++)
       if(nums[x] > nums[y])
     {T_num = nums[x];
      nums[x] = nums[y];
      nums[y] = T_num;
     }
   
   
   for(k=1;k<=j;k++) printf("%ld;",nums[k]);
   }
    else
   {
   if(temp->score == p->score)
    {
     nums[j]=p->num;
     j++;
    }
   
   else
    {
     nums[j]=p->num;
     printf("\n\n名次:%d   工作量:%f   人数:%d\n",i,p->score,j);
     printf("工号是:");
    
     for(x=1;x<j;x++)
     for(y=x+1;y<=j;y++)
       if(nums[x] > nums[y])
     {T_num = nums[x];
      nums[x] = nums[y];
      nums[y] = T_num;
     }
    

     for(k=1;k<=j;k++) printf("%ld;",nums[k]);
     i++;
     j=1;
    }
   }
    p = p->next;  //更新p
   }while(p != NULL);
 }

//***累积数据函数***
void plus(struct worker *head,struct worker *P_ker)
 {struct worker *p;
  for(p = head;p != NULL;p = p->next)
  {if(p->num==P_ker->num)
  {p->score=p->score + P_ker->score;
  printf("成功累加更新%ld数据!\n\n",P_ker->num);
  system("pause");
  }
  }
  }

//***删除结点函数***
struct worker *del(struct worker *head,long num)
 {struct worker *p1,*p2;
 if(head==NULL) {printf("\nlist null! \n");goto end;}
 p1 = head;
 while(num != p1->num&&p1->next!=NULL)  //p1指向的不是所要的结点,并且后面还有结点
 {p2 = p1;p1 = p1->next;}  //p1指向下一个结点,p2更新为上一个p1
 if(num == p1->num)
  {if(p1 == head) head = p1->next;
  else p2->next = p1->next;
  printf("delete:%ld\n",num);
  n = n - 1;
  printf("成功删除%ld数据!\n\n",num);
  system("pause");
  }
  
 else printf("%ld 没有被找到! \n\n",num);   //程序的鲁棒性
 end:
 return(head);
 }


//***插入结点函数***
struct worker *insert(struct worker *head,struct worker *stud)
 {struct worker *p0,*p1,*p2;
  p1 = head;  //p1指向第一个结点
  p0 = stud;  //p0指向待插入结点
  if(head == NULL)
  {head = p0;p0->next = NULL;}
  else                           //实现按num大小顺序插入
  {while( (p0->num > p1->num) && (p1->next != NULL) )
   {p2 = p1;
    p1 = p1->next;}
   if(p0->num <= p1->num)
   {if(head == p1) head = p0;
    else p2->next = p0;
    p0->next = p1;}
   else
   {p1->next = p0;p0->next = NULL;}
  }
  n = n + 1;
  printf("已成功插入数据 %ld\n\n",stud->num);
  system("pause");
  return(head);
 }

 

//***链表排序函数***
struct worker *sort(struct worker *head)
 {struct worker *p1,*p2,*tmp_p,*pPre1,*pPre2,*tmp_pPre2,*pTmp,*headPre;
  float tmp_fScore;
  int i,j;
  headPre = (struct worker *)malloc(LEN);
  headPre->next = head;

  for (i =1; i < n; i++)
  {
  
  
   p1 = head; pPre1 = headPre;
  
   for (j =1; j < i; j++)
   {
    p1 = p1->next; pPre1 = pPre1->next;
   }
  
  
   tmp_p = p1;
   tmp_fScore = p1->score;

   for (pPre2 = p1, p2 = p1->next; p2 != NULL;p2 = p2->next,pPre2 = pPre2->next)
   {
   
    if (tmp_fScore < p2->score)
    {
     tmp_p = p2;
     tmp_pPre2 = pPre2;
     tmp_fScore = p2->score;
    }
   }
  

   if(p1 != tmp_p)   //jiao huan
   {   
    if(p1->next != tmp_p)
    {
     pTmp = p1->next;
     p1->next = tmp_p->next;
     tmp_p->next = pTmp;
     pTmp = pPre1->next;
     pPre1->next = tmp_pPre2->next;
     tmp_pPre2->next = pTmp;
    }
    else
    {
     pTmp = p1->next;
     p1->next = tmp_p->next;
     tmp_p->next = pPre1->next;
     pPre1->next= pTmp;
    }
   }
 
 
  }


 
  printf("

上一个:车厢调度 代码 C语言版 看清题在答
下一个:八数码难题。。。哪位大哥用C语言解下。。。

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,