1. Trang chủ
  2. » Công Nghệ Thông Tin

Bài tập đề cương Turbo C - Struct, Union, DSLK thuận - nghịch ppt

15 330 4
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 15
Dung lượng 76 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Trang 1

Struct – union – DSLK thuận + nghịch.

Bài 1.

#include<stdio.h>

#include<conio.h>

#include<string.h>

typedef struct slbh

{

int stt;int dg;int sl;char ten[30];long int tien;

};

int i=-1,j,tt,n;char c;char *g[]={"STT","tenhang","Don gia","So luong","Thanh tien"}; struct slbh a[50],swap;

void nhapdl(int stt,char ten[30],int *dg,int *sl,long int *tien)

{

printf("\n nhap mat hang thu %d : ",stt);

printf("\n ten hang : ");fflush(stdin);gets(ten);

printf("\n don gia : ");scanf("%d",dg);

printf("\n so luong : ");scanf("%d",sl);

*tien=(long int)(*dg)*(long int)(*sl);

}

void indl(int stt,char ten[30],int dg,int sl,long int tien)

{

printf("%3d %30s %10d %10d %10ld \n",stt,ten,dg,sl,tien);

}

void sapxep()

{

for(i=0;i<=n-1;i++)

for(j=0;j<=n-i;j++)

if (strcmp(a[j].ten,a[j+1].ten)>0)

{

tt=a[j].stt;a[j].stt=a[j+1].stt;a[j+1].stt=tt;

swap=a[j];a[j]=a[j+1];a[j+1]=swap;

}

}

void main()

{

do

{

clrscr();

i++;

a[i].stt=i+1;

nhapdl(a[i].stt,a[i].ten,&a[i].dg,&a[i].sl,&a[i].tien);

printf("\n ban muon nhap tiep khong?");

fflush(stdin);

Trang 2

scanf("%c",&c);

}

while (c!='k');

n=i;

clrscr();

printf("%d",n);

printf("\n\t\t\t SO LIEU BAN HANG");

printf("\n %3s %30s %10s %10s %10s \n",g[0],g[1],g[2],g[3],g[4]);

sapxep();

for(i=0;i<=n;i++)

{

indl(a[i].stt,a[i].ten,a[i].dg,a[i].sl,a[i].tien);

}

getch();

}

Bài 2.

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <alloc.h>

typedef struct

{

float TOAN,LY,HOA;

}KA;

typedef struct

{

float VAN,SU,DIA;

}KC;

typedef struct

{

char HOTEN[30],QUE[20],NGAYSINH[10],KHOITHI;

float TONGDIEM;

union

KA K1;

KC K2;

}KHOI;

}THI_SINH;

THI_SINH THISINH[100],TG;

int n,i;

#define TS THISINH[i]

#define DA THISINH[i].KHOI.K1

#define DC THISINH[i].KHOI.K2

void NHAP()

{

Trang 3

float M1,M2,M3;

printf("NHAP VAO SO THI SINH : ");

scanf("%d",&n);

for (i=1;i<=n;i++)

printf("\nNHAP TEN THI SINH : ");

fflush(stdin);

gets(TS.HOTEN);

printf("\nNHAP QUE QUAN THI SINH : ");

gets(TS.QUE);

printf("\nNHAP NGAY SINH CUA THI SINH : ");

gets(TS.NGAYSINH);

printf("\nNHAP KHOI THI CUA THI SINH : ");

TS.KHOITHI=getch();

if (TS.KHOITHI=='A'||TS.KHOITHI=='a')

{

printf("\nNHAP DIEM TOAN : ");

scanf("%f",&M1);

printf("\nNHAP DIEM LY : ");

scanf("%f",&M2);

printf("\nNHAP DIEM HOA : ");

scanf("%f",&M3);

DA.TOAN=M1;

DA.LY=M2;

DA.HOA=M3;

}

else if (TS.KHOITHI=='C'||TS.KHOITHI=='c')

{

printf("\nNHAP DIEM VAN : ");

scanf("%f",&M1);

printf("\nNHAP DIEM SU : ");

scanf("%f",&M2);

printf("\nNHAP DIEM DIA : ");

scanf("%f",&M3);

DC.VAN=M1;

DC.SU=M2;

DC.DIA=M3;

}

TS.TONGDIEM=M1+M2+M3;

}

}

void IN()

{

printf("\nSTT\tHO TEN\t\tQUE\t\tNGAY SINH\tDIEM TOAN DIEM LY DIEM HOA TONG\n");

Trang 4

for (i=1;i<=n;i++)

if (TS.KHOITHI=='A'||TS.KHOITHI=='a')

printf("\n%d\t%s\t\t%s\t\t%s\t%f %f %f

%f\n",i,TS.HOTEN,TS.QUE,TS.NGAYSINH,DA.TOAN,DA.LY,DA.HOA,TS.TONGD IEM);

printf("\n\n DANH SACH THI SINH KHOI C");

printf("\nSTT\tHO TEN\t\tQUE\t\tNGAY SINH\tDIEM VAN DIEM SU DIEM DIA TONG\n");

for (i=1;i<=n;i++)

if (TS.KHOITHI=='C'||TS.KHOITHI=='c')

printf("\n%d\t%s\t\t%s\t\t%s\t%f %f %f

%f\n",i,TS.HOTEN,TS.QUE,TS.NGAYSINH,DC.VAN,DC.SU,DC.DIA,TS.TONGDIE M);

}

void SAPXEP()

{

for(i=1;i<=n;i++)

if (THISINH[i].TONGDIEM<THISINH[i+1].TONGDIEM)

{

TG=THISINH[i];

THISINH[i]=THISINH[i+1];

THISINH[i+1]=TG;

}

}

main()

{

NHAP();

SAPXEP();

IN();

getch();

}

Bài 3

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<string.h>

typedef struct hoc_vien

{

char ho_ten[20];

int tuoi;

float d_tb;

struct hoc_vien *tiep;

}p_hv;

p_hv *first,*p,*q,*r;

Trang 5

char ht[20],ch;

float x;int tuoihv;

void tao_ds(p_hv **first)

{int i=0;

printf("NHAP DANH SACH SINH VIEN\n");

*first=NULL; //khi danh sach chua co gi

do

{

fflush(stdin);

printf("Ho ten sv thu %d:",++i);

gets(ht);

if(*first==NULL)

{

*first=(p_hv*)malloc(sizeof(p_hv)); //cap phat vung nho do con tro first tro vao

p=*first; // phan tu tiep theo do p tro toi }

else

{

p->tiep=(p_hv*)malloc(sizeof(p_hv));

p=p->tiep;

}

strcpy(p->ho_ten,ht);

p->tiep=NULL;

printf("Tuoi :");

scanf("%d",&tuoihv);

p->tuoi = tuoihv;

printf("Diem trung binh:");

scanf("%f",&x);

p->d_tb = x;

printf("\n ban co nhap nua hay ko?y/n:\n");

ch=getch();

}while(ch=='y'||ch=='Y');

}

void in_ds(p_hv *first)

{

int i=0;

printf(" DANH SACH HOC VIEN\n");

printf(" ==================\n");

printf(" STT HO_TEN TUOI DIEM_TB\n");

p=first;

while(p!=NULL)

{

printf(" %d %s %d %2.2f\n",++i,p->ho_ten,p->tuoi,p->d_tb);

Trang 6

p=p->tiep;

}

getch();

}

void chen_sau(p_hv *first)

{

fflush(stdin);

p=(p_hv*)malloc(sizeof(p_hv));

p->tiep=NULL;

printf("\nHo ten sv can chen:");

gets(p->ho_ten);

printf("Tuoi :");

scanf("%d",&tuoihv);

p->tuoi = tuoihv;

printf("Diem trung binh:");

scanf("%f",&x);

p->d_tb=x;

do{printf("Muon chen vao sau hoc vien nao:");

fflush(stdin);

q=first;

gets(ht);

while((q!=NULL) && strcmpi(q->ho_ten,ht))

q=q->tiep;

if(q==NULL)

printf("\nKhong tim thay vi tri chen!");

printf("\nCo muon tim tiep hay ko?y/n:\n");

ch=getch();}

while(ch=='y'||ch=='Y');

{ if(q->tiep==NULL) //tim thay o cuoi ds

q->tiep=p;

else

{

p->tiep=q->tiep;

q->tiep=p;

}

}

printf("Da chen xong!");

getch();

}

void chen_truoc(p_hv *first)

{

fflush(stdin);

p=(p_hv*)malloc(sizeof(p_hv));

p->tiep=NULL;

Trang 7

printf("\nHo ten sv can chen:");

gets(p->ho_ten);

printf("Tuoi :");

scanf("%d",&tuoihv);

p->tuoi = tuoihv;

printf("Diem trung binh:");

scanf("%f",&x);

p->d_tb=x;

do{printf("Muon chen vao truoc hoc vien nao:");

fflush(stdin);

q=first;

gets(ht);

while((q!=NULL) && strcmpi(q->ho_ten,ht))

q=q->tiep;

if(q==NULL)

printf("\nKhong tim thay vi tri chen!");

printf("\nCo muon tim tiep hay ko?y/n:\n");

ch=getch();

}

while(ch=='y'||ch=='Y');

{r=first;

while(r->tiep!=q) r=r->tiep;

q=(p_hv*)malloc(sizeof(p_hv));

q->tiep=r->tiep;

r->tiep=q;

}

printf("\n da chen xong");

}

void xoa_ds(p_hv **first)

{

do{ printf("Nhap ho ten nguoi can xoa:");

gets(ht);

q=*first;

while((q!=NULL)&&strcmpi(q->ho_ten,ht))

{

r=q;

q=q->tiep;

}

if(q==NULL)

printf("\nKhong tim thay hoc vien can xoa!");

printf("\n ban co can xoa tiep ko?y/n:\n");

ch=getch(); }

while(ch=='y'||ch=='Y');

{

if(q==NULL)

Trang 8

r->tiep=NULL;

else

if(q==*first)

*first=q->tiep;

else

r->tiep=q->tiep;

free(q);

printf("Da xoa xong!");

}

getch();

}

void main()

{ do{clrscr();

printf("\n 1.NHAP SV");

printf("\n 2.IN SV");

printf("\n 3.CHEN TRUOC");

printf("\n 4.CHEN SAU");

printf("\n 5.XOA SV");

printf("\n 6.KET THUC");

fflush(stdin);

ch=getch(); //ko hien ky tu nhap ra man hinh

switch(ch)

{ case '1': tao_ds(&first); break;

case '2': in_ds(first); break;

case '3': chen_truoc(first); break;

case '4': chen_sau(first); break;

case '5': xoa_ds(&first); break;

}

}

while(ch!='6');

}

Bài 4.

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

#include<string.h>

typedef struct hoc_vien

{

char ho_ten[20];

int tuoi;

float d_tb;

struct hoc_vien *tiep;

}p_hv;

p_hv *first,*p,*q,*r;

Trang 9

char ht[20],ch;

float x;int tuoihv;

void tao_ds(p_hv **first)

{int i=0;

printf("NHAP DANH SACH SINH VIEN\n");

*first=NULL; //khi danh sach chua co gi

do

{

fflush(stdin);

printf("Ho ten sv thu %d:",++i);

gets(ht);

first=NULL;

p=(p_hv*)malloc(sizeof(p_hv));

p->tiep=*first;

*first=p;

strcpy(p->ho_ten,ht);

p->tiep=NULL;

printf("Tuoi :");

scanf("%d",&tuoihv);

p->tuoi = tuoihv;

printf("Diem trung binh:");

scanf("%f",&x);

p->d_tb = x;

printf("\n ban co nhap nua hay ko?y/n:\n");

ch=getch();

}while(ch=='y'||ch=='Y');

}

void in_ds(p_hv *first)

{

int i=0;

printf(" DANH SACH HOC VIEN\n");

printf(" ==================\n");

printf(" STT HO_TEN TUOI DIEM_TB\n");

p=first;

while(p!=NULL)

{

printf(" %d %s %d %2.2f\n",++i,p->ho_ten,p->tuoi,p->d_tb); p=p->tiep;

}

getch();

}

void chen_sau(p_hv *first)

{

Trang 10

fflush(stdin);

p=(p_hv*)malloc(sizeof(p_hv));

p->tiep=NULL;

printf("\nHo ten sv can chen:");

gets(p->ho_ten);

printf("Tuoi :");

scanf("%d",&tuoihv);

p->tuoi = tuoihv;

printf("Diem trung binh:");

scanf("%f",&x);

p->d_tb=x;

do{printf("Muon chen vao sau hoc vien nao:");

fflush(stdin);

q=first;

gets(ht);

while((q!=NULL) && strcmpi(q->ho_ten,ht))

q=q->tiep;

if(q==NULL)

printf("\nKhong tim thay vi tri chen!");

printf("\nCo muon tim tiep hay ko?y/n:\n");

ch=getch();}

while(ch=='y'||ch=='Y');

{ if(q->tiep==NULL) //tim thay o cuoi ds

q->tiep=p;

else

{

p->tiep=q->tiep;

q->tiep=p;

}

}

printf("Da chen xong!");

getch();

}

void chen_truoc(p_hv *first)

{

fflush(stdin);

p=(p_hv*)malloc(sizeof(p_hv));

p->tiep=NULL;

printf("\nHo ten sv can chen:");

gets(p->ho_ten);

printf("Tuoi :");

scanf("%d",&tuoihv);

p->tuoi = tuoihv;

printf("Diem trung binh:");

scanf("%f",&x);

Trang 11

p->d_tb=x;

do{printf("Muon chen vao truoc hoc vien nao:");

fflush(stdin);

q=first;

gets(ht);

while((q!=NULL) && strcmpi(q->ho_ten,ht))

q=q->tiep;

if(q==NULL)

printf("\nKhong tim thay vi tri chen!");

printf("\nCo muon tim tiep hay ko?y/n:\n");

ch=getch();

}

while(ch=='y'||ch=='Y');

{r=first;

while(r->tiep!=q) r=r->tiep;

q=(p_hv*)malloc(sizeof(p_hv));

q->tiep=r->tiep;

r->tiep=q;

}

printf("\n da chen xong");

}

void xoa_ds(p_hv **first)

{

do{ printf("Nhap ho ten nguoi can xoa:");

gets(ht);

q=*first;

while((q!=NULL)&&strcmpi(q->ho_ten,ht))

{

r=q;

q=q->tiep;

}

if(q==NULL)

printf("\nKhong tim thay hoc vien can xoa!");

printf("\n ban co can xoa tiep ko?y/n:\n");

ch=getch(); }

while(ch=='y'||ch=='Y');

{

if(q==NULL)

r->tiep=NULL;

else

if(q==*first)

*first=q->tiep;

else

r->tiep=q->tiep;

free(q);

Trang 12

printf("Da xoa xong!");

}

getch();

}

void main()

{ do{clrscr();

printf("\n 1.NHAP SV");

printf("\n 2.IN SV");

printf("\n 3.CHEN TRUOC");

printf("\n 4.CHEN SAU");

printf("\n 5.XOA SV");

printf("\n 6.KET THUC");

fflush(stdin);

ch=getch(); //ko hien ky tu nhap ra man hinh

switch(ch)

{ case '1': tao_ds(&first); break;

case '2': in_ds(first); break;

case '3': chen_truoc(first); break;

case '4': chen_sau(first); break;

case '5': xoa_ds(&first); break;

}

}

while(ch!='6');

}

Bài 5.

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct cs

{

char c;

struct cs *next;

} ;

struct cs *p,*top;

void push(int t)

{

p=(cs *)malloc(sizeof(cs));

if (t<10) p->c=t;

else p->c=t+87;

p->next=top;top=p;

}

char *pop()

{

char t;

if (top==NULL) return("");

Trang 13

t=top->c;

top=top->next;

return &t;

}

void doics(int x,int a)

{

do

{

push(x%a);

x=x/a;

}

while (x!=0);

}

void in()

{

p=(cs *)malloc(sizeof(cs));

p=top;

if (p==NULL) printf("\n danh sach rong ");

else

do

{

if (p->c<10) printf("%d",p->c);

else printf("%c",p->c);

p=p->next;

}

while (p!=NULL);

}

void main()

{

int x,a;

printf("\n nhap x va a = ");scanf("%d%d",&x,&a);

doics(x,a);in();

getch();

}

Bài 6.

#include<conio.h>

#include<stdio.h>

#include<alloc.h>

typedef struct node

{ int t,cn,cd;

struct node *tiep;

} p_node;

p_node *p,*first,*last;

void push(int n,int c1,int c2)

Trang 14

{

p=(p_node*)malloc(sizeof(p_node));

p->t=n;

p->cn=c1;

p->cd=c2;

if(first==NULL)

{

first=p;

last=p;

}

else

{

last->tiep=p;

last=p;

}

last->tiep=NULL;

}

p_node *pop()

{

p_node *q;

q=first;

first=first->tiep;

return q;

}

void chuyen_thap(int n,int c1,int c2,int c3)

{

if(n==1)

push(1,c1,c3);

else

{

chuyen_thap(n-1,c1,c3,c2);

push(n,c1,c3);

chuyen_thap(n-1,c2,c1,c3);

}

}

void hienkq()

{

do

{

p=pop();

printf("\nchuyen tang %d tu cot %d sang cot %d",p->t,p->cn,p->cd);

}

while (first!=NULL);

getch();

}

Trang 15

void main()

{int n,c1,c2,c3;

printf("nhap so tang can chuyen:");

scanf("%d",&n);

chuyen_thap(n,1,2,3);

hienkq();

getch();

}

To be continue…

Ngày đăng: 06/07/2014, 06:20

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w