This web page requires JavaScript to be enabled.

JavaScript is an object-oriented computer programming language commonly used to create interactive effects within web browsers.

How to enable JavaScript?

Code quản lý kho hàng dùng mảng một chiều mà mỗi phần tử dùng Struct

10cth1-2 September 15, 2011 2

[codes=c]#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 100

typedef struct khohang
{
  char mahang[10];
   char tenhang[25];
   int soluong;
   int dongia;
}kho;

kho khohang[MAX];

void Nhap1ma(kho &x)
{
  printf("Nhap ma hang:");
  fflush(stdin);
  gets(x.mahang);
   printf("Nhap ten hang:");
   fflush(stdin);
   gets(x.tenhang);
   printf("Nhap so luong:");
   scanf("%d",&x.soluong);
   printf("Nhap don gia:");
   scanf("%d",&x.dongia);
}

void xuat1ma(kho x)
{
  printf("\n—————————");
  printf("\n&#124;Ma hang : %s            ",x.mahang);
  printf("\n&#124;Ten hang: %s              ",x.tenhang);
  printf("\n&#124;So luong: %4d             ",x.soluong);
  printf("\n&#124;Don gia : %4d             ",x.dongia);
    printf("\n—————————");
}

void nhapmang(kho a[],int &n)
{
  printf("So mat hang can nhap:");
  scanf("%d",&n);
  for(int i=0;i<n;i++)
    {
    printf("\nNhap thong tin thu %d\n",i);
    Nhap1ma(a[i]);
    printf("\n————————————\n");
  }
}

void xuatmang(kho a[],int n)
{
  for(int i=0;i<n;i++)
   {
       xuat1ma(a[i]);

   }
}

int timtuyentinh(kho a[],int n,char* x)
{
  for(int i=0; i<n; i++)
  {
    if(strcmp(a[i].mahang,x)==0)
    return i;
  }
  return -1;
}

int timnhiphan(kho a[], int n, char *x)
{
  int left=0, right=n-1, mid;
  while(left<=right)
  {
    mid=(left+right)/2;
    if(strcmp(a[mid].mahang,x)==0)
      return mid;
    if(strcmp(a[mid].mahang,x)<0)
       left=mid+1;
    else
       right=mid-1;
  }
  return -1;
}

void swap(kho &a, kho &b)      
{
  kho temp;
  temp =a;
  a=b;
  b=temp;
}

void selection(kho a[],int n)
{
  int min;
  for(int i=0; i<n-1; i++)
  {
    min=i;
    for(int j=i; j<n; j++)
      if(strcmp(a[j].mahang,a[min].mahang)<0)
        min=j;
    swap(a[i], a[min]);
  }
}

void InsertionSort(kho a[], int n)
{
  int pos;
  char *w;  
  for(int i=1; i < n; i++)
  {
     *w = *a[i].mahang;  
     pos = i-1;
     while ((pos >= 0) && (strcmp(a[pos].mahang,w)>0))
     {
           a[pos+1] = a[pos];
         pos–;  
    }
       *a[pos+1].mahang = *w;
  }
}

void bubble(kho a[], int n)
{
  for(int i=0; i<n-1; i++)
    for(int j=n-1; j<i; j++)
      if(strcmp(a[j].mahang,a[j-1].mahang)<0)
        swap(a[j], a[j-1]);
}

void interchange(kho a[], int n)
{
  for(int i=0; i<n-1; i++)
    for(int j=i+1; j<n; j++)
      if(strcmp(a[i].mahang,a[j].mahang)>0)
        swap(a[i], a[j]);
}

int main()
{

  kho a[MAX];
  int n;
  char *x;
   nhapmang(a,n);

   printf("THONG KE KHO HANG :\n");
    xuatmang(a,n);
    
     printf("\n—————————");
     printf("\nTim kiem tuan tu:");
  printf("\nNhap ma hang can tim :");
  fflush(stdin);
  gets(x);
   int kq=timtuyentinh(a,n,x);
    if(kq==-1)
       printf("\nKhong tim thay thong tin .");
    else
       xuat1ma(a[kq]);
      
  printf("\n—————————");
     printf("\nTim kiem nhi phan:");
  printf("\nNhap ma hang can tim :");
  selection(a,n);
  fflush(stdin);
  gets(x);
   int kq2=timnhiphan(a,n,x);
    if(kq2==-1)
       printf("\nKhong tim thay thong tin .");
    else
       xuat1ma(a[kq2]);
      
  printf("\n—————————");
  printf("\nSap xep chon:");getch();
  selection(a,n);
  xuatmang(a,n);
  printf("\n—————————");
  printf("\nSap xep chen:");getch();
  InsertionSort(a, n);
  xuatmang(a,n);
  printf("\n—————————");
  printf("\nSap xep noi bot:");getch();
  bubble(a,n);
  xuatmang(a,n);
  printf("\n—————————");
  printf("\nSap xep truc tiep:");getch();
  interchange(a,n);
  xuatmang(a,n);
   getch();
}
[/codes]

read.php?383


Last modified on December 9th, 2020 at 1:20 am

Nam Le
lequocnam



2 responds

  1. coutnho_congai says:

    thanks các tiền bối nhìu nha!

  2. coutnho_congai says:

    em mún có cái đề bài nữa!có thể post lên hok tiền bối!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.