⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shiyan1.c

📁 有漂亮的用户界面,用链表实现多项式的各种运算,推荐刚学数据结构链表的人下载.
💻 C
字号:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>                                                                               
 typedef struct  pnode
{
    float coef;
    int expn;
    struct pnode *next;
}pnode;
pnode *head,*heada,*headb,*headc;
pnode *pcreat();
pnode *add(pnode *heada,pnode *headb);
pnode *sub(pnode *heada,pnode *headb);
void pprint(pnode *head);
pnode *psort(pnode *head);
void pcount(pnode *head);
void pgetvalue(pnode *head);
pnode *pdiff(pnode *head);
pnode *pmul(pnode *heada,pnode *headb);
void main()
  {
     char message[]={"INSTRUCTION:\n" 
                     "   Wellcom to use the Simple Polynomial Calculator!! Please input the coefficients and the exponents with the format as'coef_expn',then,end with'0_0'."};
     char menu[]={"-menu-\n"
                  "*******************\n"
                     "*'+'.add\n"
                     "*'-'.sub\n"
                     "*'='.val\n"
                     "*'/'.dif\n"
                     "*'*'.mul\n" 
                     "*'q'.exit\n"   
                  "*******************\n"};
     char e;
    textbackground(BLUE);
    textcolor(YELLOW);
    clrscr();
    gotoxy(4,9);
    printf("%s",message);
    gotoxy(10,22);
    printf("PRESS ANY KEY TO MENU!");
    getch();
    do
     {
      clrscr();
      gotoxy(1,9);
      printf("%s",menu);
      gotoxy(10,20);
      printf("make a choice according to the menu please:  ");
      scanf("%c",&e);
      switch(e)
         {
             case '+':{clrscr();
                       heada=pcreat();
                       pcount(heada);
                       pprint(heada);
                       printf("\n");
                       headb=pcreat();
                       pcount(headb);
                       pprint(headb);
                       printf("\n                      press any key to get the result\n");
                       getch();
                       headc=add(heada,headb);
                       printf("*****RESULT*****\n");
                       psort(headc);
                       pcount(headc);
                       pprint(headc);
                       printf("\n                       press any key to menu!");
                       getch();
                      }
                     break; 
             case '-':{clrscr();
                       heada=pcreat();
                       pcount(heada);
                       pprint(heada);
                       printf("\n");
                       headb=pcreat();
                       pcount(headb);
                       pprint(headb);
                       printf("\n                      press any key to get the result\n");
                       getch();
                       headc=sub(heada,headb);
                       printf("*****RESULT*****\n");
                       psort(headc);
                       pcount(headc);
                       pprint(headc);
                       printf("\n                      press any key to menu!");
                       getch(); 
                      } break;
            case '=':{clrscr();
                      head=pcreat(); 
                      pcount(head);
                      pprint(head);
                      pgetvalue(head);
                      break;} 
            case '*':{ clrscr();
                       heada=pcreat();
                       pcount(heada);
                       pprint(heada);
                       printf("\n");
                       headb=pcreat();
                       pcount(headb);
                       pprint(headb);
                       printf("\n                      press any key to get the result\n");
                       getch();
                       headc=pmul(heada,headb);
                       printf("*****RESULT*****\n");
                       pcount(headc);
                       pprint(headc);
                       printf("\n                      press any key to menu!");
                       getch(); }
                       break;
            case '/':{clrscr();
                      head=pcreat();
                      pcount(head);
                      pprint(head);
                      head=pdiff(head);
                      printf("                press any key to get the result!\n");
                      getch();  
                      pcount(head);     
                      pprint(head);
                      printf("                  press any key to menu!"); 
                      getch();
                      break;}
             case 'q':{gotoxy(10,22);
                        printf("***********"
                                "good bye!"
                               "***********\n");
                     break; 
                      }
              }
       }while(e!='q');
        getch();
        clrscr();
 }

   pnode *pcreat()
 {
   struct pnode *head,*tail,*p;
      int e,n,m;
    float c;
      int size=sizeof(struct pnode);
     head=(struct pnode*)malloc(size);
      
     head->coef=0;
     head->expn=0;
     head->next=NULL;
     tail=head;
    printf(" coef_expn:\n");
     scanf("%f %d",&c,&e);
     while(c!=0)
      {
         p=(struct pnode*)malloc(size);
         
         p->coef=c;
         p->expn=e;
		 if(p->expn==tail->expn&&tail!=head)
		 {tail->coef+=p->coef;
		  free(p);
		 }
		 else
		 {tail->next=p;
         tail=p;
		}
         printf(" coef_expn:\n");    
         scanf("%f %d",&c,&e);
       }
   tail->next=NULL;
   return head;
}
 pnode *psort(pnode *head)
{
  int key=1;
  int e;
  float c;
  pnode *newhead=head->next;
  pnode *p,*tail,*temp;
  if(head->next==NULL)
   {
       return head;
   }
  while(key==1)
    {
       key=1;
       tail=newhead;
       p=tail->next;
       if(tail==NULL||tail->next==NULL)
         {
           break;
         }
       while(tail!=NULL&&tail->next!=NULL)
        {
		   if(newhead->expn>newhead->next->expn)
           key=0;
           p=tail->next;
           if(tail->expn<p->expn)
             { c=tail->coef;
               e=tail->expn;
               tail->coef=p->coef;
               tail->expn=p->expn;
               p->coef=c;
               p->expn=e;
               key=1;
             }
           if(tail->expn==p->expn)
              {
                tail->coef+=p->coef;
                temp=tail->next;
                tail->next=temp->next;
                free(temp); 
                key=1;
              }
            tail=tail->next;
        }    
    } 
		
  return head;
}
 pnode *add(pnode *heada, pnode *headb)
{
  struct pnode *pa,*pb,*p,*tail,*headc; 
  float sum;
  int size=sizeof(pnode);
  headc=(struct pnode*)malloc(size);
 
  headc->coef=0;
  headc->expn=0;
  headc->next=NULL;
  tail=headc;
  pa=heada->next;
  pb=headb->next;
  while(pa!=NULL&&pb!=NULL)
    {
        if(pa->expn==pb->expn)
          { 
             sum=pa->coef+pb->coef;
             if(sum!=0)
              {
                 p=(struct pnode*)malloc(size);
                 p->coef=sum;
                 p->expn=pa->expn;
                 tail->next=p;
                 tail=p;
              }    
          pa=pa->next;
          pb=pb->next;   
          }
         else if(pa->expn>pb->expn)
          {
             p=(struct pnode*)malloc(size);
             
             p->coef=pb->coef;
             p->expn=pb->expn;
             tail->next=p;
             tail=p;
             pb=pb->next;
          }
          else if(pa->expn<pb->expn)
          {
             p=(struct pnode*)malloc(size);
             
             p->coef=pa->coef;
             p->expn=pa->expn;
             tail->next=p;
             tail=p;
             pa=pa->next;
           }
    }
  while(pa!=NULL)
     {
          p=(struct pnode*)malloc(size);
          
          p->coef=pa->coef;
          p->expn=pa->expn;
          tail->next=p;
          tail=p;
          pa=pa->next;
     }
   while(pb!=NULL)
     {
           p=(struct pnode*)malloc(size);
           
           p->coef=pb->coef;
           p->expn=pb->expn;
           tail->next=p;
           tail=p;
           pb=pb->next;
     }
    tail->next=NULL;
     return headc;

}
 pnode *sub(pnode *heada,pnode *headb)

{
    struct pnode *pa,*pb,*p,*tail,*headc;
    float sum;
    int size=sizeof(pnode);
    headc=(struct pnode*)malloc(size);
    
    headc->coef=0;
    headc->expn=0;
    headc->next=NULL;
    tail=headc;
    pa=heada->next;
    pb=headb->next;
    while(pa!=NULL&&pb!=NULL)
    {
        if(pa->expn==pb->expn)
        {
           sum=pa->coef-pb->coef;
            if(sum!=0)
            {
                p=(struct pnode*)malloc(size);
                
                p->coef=sum;
                p->expn=pa->expn;
                tail->next=p;
                 tail=p;
            }
        pa=pa->next;
        pb=pb->next;
        }
        else if(pa->expn>pb->expn)
          {
             p=(struct pnode*)malloc(size);
             
             p->coef=-pb->coef;
             p->expn=pb->expn;
             tail->next=p;
             tail=p;
             pb=pb->next;
          }
          else if(pa->expn<pb->expn)
          {
             p=(struct pnode*)malloc(size);
             
             p->coef=pa->coef;
             p->expn=pa->expn;
             tail->next=p;
             tail=p;
             pa=pa->next;
           }
    }
    while(pa!=NULL)
     {
          p=(struct pnode*)malloc(size);
         
          p->coef=pa->coef;
          p->expn=pa->expn;
          tail->next=p;
          tail=p;
          pa=pa->next;
     }
   while(pb!=NULL)
     {
           p=(struct pnode*)malloc(size);
          
           p->coef=-pb->coef;
           p->expn=pb->expn;
           tail->next=p;
           tail=p;
           pb=pb->next;
     }
    tail->next=NULL;
    return headc;
}
void pprint(pnode *head)
 {
   pnode *p;
   p=head->next;
   printf("the expression:\n");
   if(head->next==NULL)
   printf("%d",head->expn);
   while(p!=NULL)
   {     if(p==head->next)
         printf("%dterm(s):  ",head->expn);
         if(p->coef>0&&p!=head->next)
            printf("+");
         if(p->coef!=1)
			if(p->coef==-1)
             printf("-");  			  
           else printf("%g",p->coef);
		 else if(p->coef==1&&p->expn==0)
			printf("%g",p->coef);
         if(p->expn!=0)
           {
             printf("x");
             if(p->expn!=1)
             printf("^%d",p->expn);
           } 
         p=p->next;  
     } 
 }
 void pcount(pnode *head)
{
   int n=0;
   pnode *p;
   p=head;
   while(p->next!=NULL)
     { 
        p=p->next;
        n++;
     }
   head->expn=n;
}

 void pgetvalue(pnode *head)
 {  
    float sum=0;
    float xp=1;
    float x;
    int ex=0;
    int m;
    pnode *p;
    p=head->next;
    printf("\nplease input x:\n");
    scanf("%f",&x);
    while(p!=NULL)
     {  
         m=p->expn;
         xp=1;
       while(ex<m)
         {
          xp*=x;
           m--;
         }
        m=p->expn;
       while(ex>m)
         {xp*=(1/x);
          m++;
          }
       sum+=p->coef*xp;
       p=p->next;   
     }
   printf("the result is: %g \n",sum);
   printf(" \n            press any key to menu!");
    getch();
    }

pnode *pdiff(pnode *head)
 {  pnode *p,*q;
    p=head->next;
    
    while(p!=NULL)
     { if(p->expn==0)
        { 
          q=p;
          head->next=q->next;
          free(q);
        }
       if(p->expn!=0)
        { 
          p->coef=p->coef*p->expn;
          p->expn--;
        }
       p=p->next;
     }
    return head;
 }

pnode *pmul(pnode *heada,pnode *headb)
   {  pnode *pa,*pb,*headc,*tail,*r,*sum,*p;
      int size=sizeof(pnode);  
      headc=(struct pnode*)malloc(size);
         headc->expn=0;
         headc->coef=0;
         headc->next=NULL;
         tail=headc;
     sum=(struct pnode*)malloc(size);
         sum->expn=0;
         sum->coef=0;
         sum->next=NULL;
      if(heada->expn<=headb->expn) 
       {  pa=heada;
          pb=headb;
       }
      else {pa=headb;pb=heada;}

    while(pa->next!=NULL)
     {  p=headc->next;
        while(p!=NULL)
        {headc->next=p->next;
        free(p);
        p=headc->next;}
        tail=headc;
       if(heada->expn<=headb->expn) 
          pb=headb;
      else pb=heada; 
        while(pb->next!=NULL)
         { r=(struct pnode*)malloc(size);
           r->coef=pb->next->coef*pa->next->coef;
           r->expn=pb->next->expn+pa->next->expn;
           tail->next=r;
           tail=r;
           pb=pb->next;
         }
         tail->next=NULL;
         sum=add(sum,headc);
         pa=pa->next;
     }
   return sum;
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -