BAI TOAN QUAN LY SINH VIEN
2. QUẢN LÝ SINH VIÊN 02
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#define nmax 100
//nmax la co toi da cua mang danh sach sinh vien
//==================
struct nhansu
{char ten[20];
int tuoi;
};
nhansu ds[nmax];int n=0;
//Mang dslop[] la danh sach sinh vien trong lop, n la so sinh vien
//==================
void nhap(int &n)
{clrscr();
printf("
Hay nhap so sinh vien trong lop: ");scanf("%d",&n);
for(i=0;i<n;i++)
{printf("
Nhap thong tin cho nguoi thu %d:",i+1);
printf("
Ten : ");fflush(stdin);gets(ds[i].ten);
printf("Tuoi: ");scanf("%d",&ds[i].tuoi);
}
}
//==================
void xem()
{clrscr();
printf("
Danh sach sinh vien trong lop:");
printf("
tt Ho va ten Tuoi");
for(i=0;i<n;i++)
printf("
%d %-20s %d ",i+1,ds[i].ten,ds[i].tuoi);
}
//==================
void sapxepten()
{int i,j,k;nhansu t;
char s[20];
for(i=0;i<n;i++)
{strcpy(s,ds[i].ten);k=i;
for(j=i+1;j<n;j++)
if(strcmpi(ds[j].ten,s)<0) {k=j;strcpy(s,ds[j].ten);}
if(k!=i) {t=ds[i];ds[i]=ds[k];ds[k]=t;}
};
}
//==================
void main()
{char chon;
while(1)
{clrscr();
printf("
1. Nhap danh sach sinh vien");
printf("
2. Xem danh sach sinh vien");
printf("
3. Sap xep theo ten (khong phan biet chu hoa chu thuong)");
printf("
z. Ket thuc chuong trinh");
printf("
Hay nhan phim 1,2 hoac z de chon muc can thuc hien: ");
chon=getch();//chon se la ky tu vua nhan
chon=toupper(chon);//Chuyen sang chu hoa
if(chon=='Z') break;//Neu nhan z hoac Z thi ket thuc
switch(chon)
{case '1': nhap(n);break;
case '2': xem();break;
case '3': sapxepten();break;
}
gotoxy(1,25);
printf("Nhan phim bat ky de tiep tuc");
getch();
}
}
BAI 2
CODE chương trình minh họa thuật toán tìm kiếm tuyến tính
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <mem.h>
#include <dos.h>
#include <graphics.h>
int XX = 10, YY = 10;
void initArray(int* A, int N)
{
for (int i=0; i<N; i++)
A[i] = rand() % 100;
}
void printArray(int* A, int N)
{
textcolor(3);
gotoxy(XX-5, YY);
cprintf("A = [");
for (int i=0; i<N; i++)
{
gotoxy(XX + i* 4, YY);
cprintf("%3d ", A[i]);
}
cprintf("]");
gotoxy(1, YY);
}
void printLineSearch(int* A, int N, int X)
{
for (int i=0; i<N; i++)
{
for (int j=0; j<4; j++)
{
delay(100);
textcolor(2);
gotoxy(XX+(i-1)*4+j, YY+2);
cprintf(" %3d ?", X);
}
if (A[i] == X)
{
textcolor(4 + BLINK);
gotoxy(XX+(i-1)*4+j, YY);
cprintf("%3d ", A[i]);
gotoxy(XX, YY + 4);
cprintf("ANS: Found");
gotoxy(XX-3, YY);
return;
}
else
{
textcolor(4 + BLINK);
gotoxy(XX+(i-1)*4+j, YY);
cprintf("%3d ", A[i]);
}
gotoxy(XX-3, YY);
delay(1000);
textcolor(6);
gotoxy(XX+(i-1)*4+j, YY);
cprintf("%3d ", A[i]);
}
textcolor(4 + BLINK);
gotoxy(XX-4, YY + 4);
cprintf("ANS: Not Found");
gotoxy(XX-3, YY);
}
void sort(int* A, int N)
{
for (int i=0; i<N-1; i++)
{
for (int j=i+1; j<N; j++)
{
if (A[i]>A[j])
{
int t = A[i];
A[i] = A[j];
A[j] = t;
}
}
}
}
void printBinarySearch(int* A, int N, int X)
{
int l = 0;
int r = N-1;
while (l<=r)
{
int m = (l+r) / 2;
if (A[m] < X)
l = m;
if (A[m] > X)
r = m;
if (A[m] == X)
{
textcolor(4 + BLINK);
gotoxy(XX+(m-1)*4+4, YY);
cprintf("%3d ", A[m]);
gotoxy(XX, YY + 4);
cprintf("ANS: Found");
gotoxy(XX-3, YY);
return;
}
}
}
void main()
{
int A[100], N =15;
clrscr();
initArray(A, N);
printArray(A, N);
printLineSearch(A, N, A[11]);
sort(A, N);
printBinarySearch(A, N, A[10]);
getch();
}
Bạn đang đọc truyện trên: AzTruyen.Top