Tìm thấy 7 mục

DarkEternal

thuật toán fordbellman - 5/12/2009, 08:38

Một lời khuyên cho bạn khi đã viết những bài thế này là nên dùng mã giả của thầy đưa rồi chuyển nó qua code của mình thì dễ kiểm tra lỗi hơn.Ở đây là bài của thầy làm nên mình nghĩ việc lỗi sai đở đây có lẽ ko có,bạn thử kiểm tra Debug lại từng bước xem.

Tiếp tục nhìn lại bài Tập hợp Phân số nào - 4/12/2009, 20:32

Code:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define Max 100
typedef struct
{
        int tu;
        int mau;
}ps;
typedef struct
{
    ps b[Max];
    int n;
}songuyen;
int Add(songuyen &a,ps x);
int Del(songuyen &a,ps x);
int Search(songuyen a,ps x);
int IsFull(songuyen a);
int IsEmpty(songuyen a);
void InPut(songuyen &a);
void OutPut(songuyen a);
void DelLike(songuyen &a);
void Quicksort(songuyen &a,songuyen &b,int l,int r);
void Hoi(songuyen a,songuyen b,songuyen &c);
void Giao(songuyen a,songuyen b,songuyen &c);
void PhanBu(songuyen c,songuyen &b);
int BCNN(int a,int b);
void quidong(songuyen &a);
int main()
{
    char t;
    songuyen a,c;
    int i;
    a.n=0;
    c.n=0;
    while(1)
    {     
            printf("\n1.Khoi tao Tap Hop");
            printf("\n2.Xuat Tap Hop");
            printf("\n3.Them phan tu vao Tap Hop");
            printf("\n4.Xoa phan tu");
            printf("\n5.Tim kiem phan tu thuoc Tap Hop");
            printf("\n6.Khoi tao mot Tap Hop thu 2");
            printf("\n7.Hoi cua 2 Tap Hop");
            printf("\n8.Giao cua 2 Tap Hop");
            printf("\n9.Hieu cua 2 Tap Hop");
            printf("\nBam Enter de ket thuc chuong trinh!!!\n");
            t=getch();
            if(t=='1') { InPut(a); 
                        songuyen b;
                        b=a;
                        quidong(b);
                        Quicksort(a,b,0,a.n-1);
                      }
            else if(t=='2')
            {    printf("\nTap Hop dang co la:");
                OutPut(a);
                }
          else if(t=='3')
            {
                while(1)
                {
                        ps x;
                        printf("\nNhap vao phan tu can them :");
                        scanf("%d/%d",&x.tu,&x.mau);
                        i=Add(a,x);
                        if(i==0) printf("\nChua them duoc phan tu!");
                        else    printf("\nDa them duoc phan tu!");
                        printf("\nBan co muon them tiep phan tu?-C/K");
                        if(toupper(getch())=='K') break;
                }
                DelLike(a);
                songuyen b;
                b=a;
                quidong(b);
                Quicksort(a,b,0,a.n-1);
            }
            else if(t=='4')
            {
                while(1)
                {
                        ps x;
                        printf("\nNhap vao phan tu can xoa :");
                        scanf("%d/%d",&x.tu,&x.mau);
                        i=Del(a,x);
                        if(i==0) printf("\nChua xoa duoc phan tu!");
                        else printf("\nDa xoa duoc phan tu!");
                        printf("\nBan co muon xoa tiep phan tu?-C/K");
                        if(toupper(getch())=='K') break;
                }
            }
            else if(t=='5')
            {
                while(1)
                {
                        ps x;
                        printf("\nNhap phan so can tim:");
                        scanf("%d/%d",&x.tu,&x.mau);
                        i=Search(a,x);
                        if(i==0) printf("\nKhong co phan tu do trong Tap Hop!");
                        else printf("\nCo phan tu do trong Tap Hop!");
                        printf("\nBan co muon tim kiem phan tu?-C/K");
                        if(toupper(getch())=='K') break;
                }
            }
            else if(t=='6')
            {
                InPut(c);
                songuyen b;
                b=c;
                quidong(b);
                Quicksort(c,b,0,c.n-1);
                printf("\nTap Hop 2 vua khoi tao la:");
                OutPut(c);
            }
            else if(t=='7')
            {    songuyen b;
                b.n=0;
                printf("Hoi cua 2 Tap Hop la:");
                Hoi(a,c,b);
                OutPut(b);
            }
        else if(t=='8')
            {
                songuyen b;
                b.n=0;
                printf("Giao cua 2 Tap Hop la:");
                Giao(a,c,b);
                OutPut(b);
            }
            else if(t=='9')
            {
                songuyen b,d;
                d=c;
                b.n=0;
                Giao(a,c,b);
                printf("Hieu cua 2 Tap Hop la:");
                PhanBu(b,d);
                OutPut(d);
                }
            else  break;
            }
}
void InPut(songuyen &a)
{
    int i=0;
    //printf("Ban co muon nhap Tap Hop?-C/K");
    //if(toupper(getch())=='C')
    {
    while(1)
    {   
        if(IsFull(a))
        {   
            printf("\nTap Hop da day!!");
            break;
        }
        printf("\nNhap vao phan tu thu %d cua Tap Hop:",i+1);
        scanf("%d/%d",&a.b[i].tu,&a.b[i].mau);
        a.n++;
        printf("Co nhap tiep hay ko?-C/K");
        if(toupper(getch())=='K') break;
        i++;
    }
    }
    DelLike(a); 
}
void DelLike(songuyen &a)
{    songuyen t;
    int b[Max],l=0;
    for(int i=0;i<a.n;i++)
    b[i]=1;
    for(int i=0;i<a.n;i++)
    {
            if(b[i]!=0)
            {
                        t.b[l]=a.b[i];
                        for(int j=i+1;j<a.n;j++)
                                if((a.b[i].tu==a.b[j].tu)&&(a.b[i].mau==a.b[j].mau)) b[j]=0;
                        l++;
            }
    }
    t.n=l;
    a=t;
}
   
void OutPut(songuyen a)
{
    int i;
    if(IsEmpty(a)) printf("\nTap hop rong!!");
    else
    {
        //printf("\nDay dang co la:");
        for(i=0;i<a.n;i++)
            printf("%d/%d    ",a.b[i].tu,a.b[i].mau);
    }
}
int IsEmpty(songuyen a)
{
    if(a.n==0) return 1;
    else return 0;
}
int IsFull(songuyen a)
{
    if(a.n==Max) return 1;
    else return 0;
}
int Add(songuyen &a,ps x)
{
    if(IsFull(a))
    {
        printf("Tap Hop da day!");
        return 0;
    }
    a.n++;
    a.b[a.n-1]=x;
}
int Del(songuyen &a,ps x)
{
    if(IsEmpty(a))
    {
        printf("Tap Hop rong!");
        return 0;
    }
    int i;
    for(i=0;i<a.n;i++)
            if((a.b[i].tu==x.tu)&&(a.b[i].mau==x.mau)) break;
    for(int j=i;j<a.n-1;j++)
    {
            a.b[j]=a.b[j+1];
    }
    a.b[a.n-1].tu=a.b[a.n-1].mau=0;
    a.n--;
    return 1;
}
void Quicksort(songuyen &a,songuyen &b,int l,int r)
{
    if(IsEmpty(a)) printf("Tap Hop khong co phan tu de sap xep!");
    else
    {    int i,j;
          songuyen t;
          int x;
          x=b.b[(l+r)/2].tu;
          i=l;
          j=r;
          do
          {
              while(b.b[i].tu<x) i++;
              while(b.b[j].tu>x) j--;
              if(i<=j)
          {
                t.b[i]=a.b[i];
                a.b[i]=a.b[j];
                a.b[j]=t.b[i];
                t.b[i]=b.b[i];
                b.b[i]=b.b[j];
                b.b[j]=t.b[i];
                i++;
                j--;
          }
          }while(i<j);
          if(l<j)
          Quicksort(a,b,l,j);
          if(i<r)
          Quicksort(a,b,i,r);
          }

int BCNN(int a,int b)
{  int bcnn,i,max;
    if(a>b) max=a;
    if(b>=a) max=b;
            for(i=max;i<=a*b;i++)
              if((i%a==0)&&(i%b==0))  break;
              bcnn=i;
    return(bcnn);
}
void quidong(songuyen &a)
{
    int bcnn=a.b[0].mau,s;
    for(int i=1;i<a.n;i++)
    {
            s=BCNN(bcnn,a.b[i].mau);
            bcnn=s;
    }
    for(int i=0;i<a.n;i++)
    {     
            if((a.b[i].tu<0)&&(a.b[i].mau<0))
            {
              s=(-1)*(bcnn/a.b[i].mau);
              a.b[i].tu*=-1;
              a.b[i].tu*=s;
              a.b[i].mau=s;
              }
              else
              {
                  s=(bcnn/a.b[i].mau);
              a.b[i].tu=(a.b[i].tu*s);
              a.b[i].mau=s;
              }
    }


int Search(songuyen a,ps x)
{
    if(IsEmpty(a))
    {
        printf("Tap Hop Rong!");
        return 0;
    }
    for(int i=0;i<a.n;i++) 
    if((a.b[i].tu==x.tu)&&(a.b[i].mau==x.mau)) return 1;
    return 0;
}       
void Hoi(songuyen a,songuyen b,songuyen &c)
{
    c=a;
    c.n=a.n+b.n;
    int j=0;
    for(int i=a.n;i<c.n;i++)
    {
            c.b[i]=b.b[j];
            j++;
    }
    DelLike(c);
}
void Giao(songuyen a,songuyen b,songuyen &c)
{
    int k=0;
    for(int i=0;i<a.n;i++)
    {
          for(int j=0;j<b.n;j++)
          {
              if((a.b[i].mau==b.b[j].mau)&&(a.b[i].tu==b.b[j].tu))
              {
                c.b[k]=a.b[i];
                k++;
                c.n++;
                }
          }
    }
}
void PhanBu(songuyen c,songuyen &b)
{
    int k=0;
    for(int i=0;i<c.n;i++)
    {
      for(int j=0;j<b.n;j++)
      {
            if((c.b[i].tu==b.b[j].tu)&&(c.b[i].mau==b.b[j].mau)) Del(b,c.b[i]);
      }
      }
}             
   
   

Không có một kiểu dữ liệu nào hay hơn Node trên C nhé .

+,-,*,/ hai đa thức.Vào cùng tưởng nhớ lại bài này đầu năm nào - 4/12/2009, 20:25

Code:
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
typedef struct
{
    float sm;
    float hs;
}dathuc;
typedef struct NODE
{
    dathuc info;
    NODE* pnext;
};
typedef struct
{
    NODE* phead;
    NODE* ptail;
    int n;
}List;
NODE* GetNode(dathuc x);
void AddFirst(List &l,NODE* new_ele);
NODE* InsertHead(List &l,dathuc x);
void Init(List &l);
void InPut(List &l);
void OutPut(List l);
void Multiplication(List &l,int k);
void Addition(List &l1,List l2);
int Like(List &l1,NODE* ele);
void Substraction(List &l1,List l2);
int Like1(List &l1,NODE* ele);
void AddTail(List &l,NODE* new_ele);
NODE* InsertTail(List &l,dathuc x);
void AddAfter(List &l,NODE* q,NODE* new_ele);
NODE* InsertAfter(List &l,NODE* q,dathuc x);
void Multip(List l1,List l2,List &l);
dathuc RemoveHead(List &l);
void RemoveAfter(List &l,NODE* q);
void Devide(List &l1,List l2,List &l);
int main()
{
    List l1,l2,l;
    printf("Da thuc thu nhat:");
    Init(l1);
    InPut(l1);
    printf("\nDa thuc vua nhap la :\n");
    OutPut(l1);
    printf("\nDa thuc thu hai:");
    Init(l2);
    InPut(l2);
    printf("Da thuc vua nhap la:\n");
    OutPut(l2);
    Init(l);
    //Tuy phep muon thuc hien goi ham tuong ung ,o day dang la phep chia
    Devide(l1,l2,l);
    printf("\n");
    OutPut(l);
    printf("+");
    OutPut(l1);
    getch();
   
}
NODE* GetNode(dathuc x)
{
    NODE* p;
    p=new NODE;
    if(p==NULL)
    {
        printf("Khong du bo nho!");
        return NULL;
    }
    p->info=x;
    p->pnext=NULL;
    return p;
}
void AddFirst(List &l,NODE* new_ele)
{
    if(l.phead==NULL)
        l.phead=l.ptail=new_ele;
    else
    {
        new_ele->pnext=l.phead;
        l.phead=new_ele;
    }
}
NODE* InsertHead(List &l,dathuc x)
{
    NODE* new_ele=GetNode(x);
    if(new_ele==NULL) return NULL;
    AddFirst(l,new_ele);
    return new_ele;
}
void AddTail(List &l,NODE* new_ele)
{
    if(l.phead==NULL)
    {
    l.phead=l.ptail=new_ele;
    }
    else
    {
        l.ptail->pnext=new_ele;
        l.ptail=new_ele;
    }
}
NODE* InsertTail(List &l,dathuc x)
{
      NODE* new_ele=GetNode(x);
      if(new_ele==NULL) return NULL;
      AddTail(l,new_ele);
      return new_ele;
}
void AddAfter(List &l,NODE* q,NODE* new_ele)
{
    if(q!=NULL)
    {
                new_ele->pnext=q->pnext;
                q->pnext=new_ele;
                if(q==l.ptail)
                l.ptail=new_ele;
    }
    else
    AddFirst(l,new_ele);
}
NODE* InsertAfter(List &l,NODE* q,dathuc x)
{
    NODE* new_ele=GetNode(x);
    if(new_ele==NULL) return NULL;
    AddAfter(l,q,new_ele);
    return new_ele;
}
dathuc RemoveHead(List &l)
{
      NODE* p;
      dathuc x;
      if(l.phead!=NULL)
      {
                        p=l.phead;
                        x=p->info;
                        l.phead=l.phead->pnext;
                        delete p;
                        if(l.phead==NULL) l.ptail=NULL;
      }
      return x;
}
void RemoveAfter(List &l,NODE* q)
{
    NODE* p;
    if(q!=NULL)
    {
                p=q->pnext;
                if(p!=NULL)
                {
                          if(p==l.ptail) l.ptail=q;
                          q->pnext=p->pnext;
                          delete p;
                }
    }
    else RemoveHead(l);
}                   
void Init(List &l)
{
    l.phead=l.ptail=NULL;
}
void InPut(List &l)
{
    int i=0;
    while(1)
    {
        dathuc x;
        printf("\nNhap so mu cua x thu %d:",i+1);
        scanf("%f",&x.sm);
        printf("Nhap he so cua x thu %d:",i+1);
        scanf("%f",&x.hs);
        InsertHead(l,x);
        i++;
        printf("\nBan co muon nhap tiep?-c/k");
        if(toupper(getch())=='K') break;   
    }
    l.n=i;
}
void OutPut(List l)
{
    NODE* p;
    p=l.phead;
    printf("(");
    while(p!=NULL)
    {
        if(p->pnext==NULL) printf("%.2fx^%.0f)",p->info.hs,p->info.sm);
        else printf("%.2fx^%.0f + ",p->info.hs,p->info.sm);
        p=p->pnext;
    }
}
void Multiplication(List &l,int k)
{
      NODE* p;
      p=l.phead;
      while(p!=NULL)
      {
          p->info.hs=p->info.hs*k;
          p=p->pnext;
      }
}
void Addition(List &l1,List l2)
{
    NODE* p,q;
    p=l2.phead;
    int a[l2.n];
    int i=0;
    for(i=0;i<l2.n;i++)
        a[i]=1;
    i=0;
      while(p!=NULL)
    {           
          if(Like(l1,p)) a[i]=0;
          if(a[i]!=0) InsertHead(l1,p->info);
          i++;
          p=p->pnext;
    }             
}
int Like(List &l1,NODE* ele)
{
    NODE* p;
    p=l1.phead;
    while(p!=NULL)
    {
          if(p->info.sm==ele->info.sm) 
          {                           
                p->info.hs+=ele->info.hs;
                return 1;
          }
          p=p->pnext;
    }
    return 0;
}
void Substraction(List &l1,List l2)

    NODE* p,q;
    p=l2.phead;
    int a[l2.n];
    int i=0;
    for(i=0;i<l2.n;i++)
        a[i]=1;
    i=0;
      while(p!=NULL)
    {           
          if(Like1(l1,p)) a[i]=0;
          if(a[i]!=0)
          {
                      p->info.hs*=-1;
                      InsertHead(l1,p->info);
          }
          i++;
          p=p->pnext;
    } 
}
int Like1(List &l1,NODE* ele)
{
    NODE* p;
    p=l1.phead;
    while(p!=NULL)
    {
          if(p->info.sm==ele->info.sm) 
          {                           
                p->info.hs-=ele->info.hs;
                return 1;
          }
          p=p->pnext;
    }
    return 0;
}

void Multip(List l1,List l2,List &l)
{
    NODE* p;
    int k=2;
    p=l1.phead;
    while(p!=NULL)
    {
          NODE* q;
          q=l2.phead;
          while(q!=NULL)
          {
                dathuc x;
                x.sm=p->info.sm+q->info.sm;
                x.hs=p->info.hs*q->info.hs;
                if(k>0)
                {
                      InsertTail(l,x);
                      k--;
                }
                else
                {
                    NODE* o;
                    NODE* t;
                    int temp=0,flag=1;
                    o=l.phead;
                    while(o!=NULL)
                    {         
                        if(x.sm==o->info.sm)
                        {
                                              o->info.hs=o->info.hs+x.hs;
                                              flag=0;
                                              break;
                        }
                        o=o->pnext;
                    }
                    if(flag) InsertTail(l,x);               
                }
                q=q->pnext;
          }
          p=p->pnext;
    }
}
void Devide(List &l1,List l2,List &l)
{
    NODE* p;
    while(1)
    {
            p=l1.phead;
            NODE* q;
            dathuc x;
            q=l2.phead;
            x.sm=p->info.sm-q->info.sm;
            x.hs=p->info.hs/q->info.hs;
            InsertTail(l,x);
            while(q!=NULL)
            {
                  p->info.hs-=(q->info.hs*x.hs);
                  p=p->pnext;
                  q=q->pnext;
            }   
          RemoveHead(l1);
          p=l1.phead;
          if(p->info.sm<l2.phead->info.sm) break;
    }

 

Nào cùng vào nhớ lại những ngày đâu năm hai nhé

DarkEternal

Giúp em với!!!! - 3/12/2009, 23:05

Bài này thật sự rất dễ nếu như bạn đã học Queue,thao tác có thể diễn tả như sau:đầu tiên đưa 1 chữ số đầu tiên của mỗi số,cộng nó lại rồi bỏ vào Queue,lần lượt tiếp tục với cặp số thứ 2.Nếu cặp số thứ 2<=8 thì dequeue,>=10 thì +1 vào đầu Queue sau đó cũng dequeue.Nếu =9,thì lập tức kiểm tra tổng 2 số phía sau,nếu tổng >=10,+1 vào đầu Queue rồi cũng dequeue.Tương tự cho thao tác với cặp 4,5,6.....,n.

DarkEternal

bài Danh sách liên kết đơn - 3/12/2009, 21:44

Bài danh sách liên kết đơn của bạn khá ổn nhưng quá rối mắt về phần code.
-Thứ nhất,theo mình bạn nên để thân hàm main của bạn chỉ là việc gọi hàm Input để vào dữ liệu sau đó hàm Output.Tiếp theo bạn cho menu vào sau đó dùng vòng lặp để bắt đầu chọn menu và cứ mỗi số từ việc chọn bạn viết nó thành 1 hàm riêng.
-Thứ hai,Hàm IsEmptyList là không cần thiết,có lẽ ở đây ý bạn nó là hàm Init khởi dựng 1 list nên cho head và tail nó bằng NULL ?Hoặc bạn muốn nó kiểm tra rằng List đã rỗng?.
Chúc bạn có thể cải thiện tốt bài tập này

DarkEternal

bài Danh sách liên kết đơn - 3/12/2009, 19:09

kocogi đã viết:lỗi ở đây là ko ai chịu xài nhập xuất = file cả. :O

Đúng là với các bài struct về quản lí thế này thì việc tối ưu nhất đó là nhập xuất bằng file.Như thế tránh cho máy phải vất vả việc xử lý nhập xuất từng biến 1.Ngoài ra ,không biết có phải do các phần mềm như Cfree hay DevC ,việc fflush(stdin) vẫn ko ổn định cho việc nhập chuỗi kí tự từ bàn phím,trong khi BorlandC thì ok hết.Nhưng tất nhiên nhập xuất từ bàn phím cũng ko là vấn đề gì đối với bài tập này.

DarkEternal

Tam giác Pascal - 3/12/2009, 18:45

-Muốn in tam giác Pascal ra theo kiểu tam giác cân thì bạn dùng lệnh gotoxy() với việc lần lượt cho nó in trả lùi từ khoảng cách n,n-1,..1,0.
-Bài in từ 1->m dưới dạng n cột thì y như vythanhdinh đã nói.


Về Đầu Trang

Hôm nay: 20/5/2012, 19:16