By GokiSoft.com|
20:38 16/12/2021|
Lập Trình C
[Video] Bài tập tổng quát - quản lý mảng số nguyên - Lập trình C - C2110L
Bài tập tổng quát - quản lý mảng số nguyên - Lập trình C
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
//B1. Khai bao mang dataList
int dataList[100], count = 0, i, choose, j;
do {
printf("\n1. Them");
printf("\n2. Sap xep");
printf("\n3. Tim kiem");
printf("\n4. Xoa");
printf("\n5. Hien thi");
printf("\n6. Thoat");
printf("\nChon: ");
scanf("%d", &choose);
switch(choose) {
case 1: {
printf("\nNhap dataList[%d] = ", count);
scanf("%d", &dataList[count]);
count++;
//scanf("%d", &dataList[count++]);
break;
}
case 2: {
for(i=0;i<count-1;i++) {
for(j=i+1;j<count;j++) {
if(dataList[i] > dataList[j]) {
int tmp = dataList[j];
dataList[j] = dataList[i];
dataList[i] = tmp;
}
}
}
printf("\nSap xep thanh cong!!!");
break;
}
case 3: {
int search, num = 0;
printf("\nNhap phan tu can tim: ");
scanf("%d", &search);
for(i=0;i<count;i++) {
if(dataList[i] == search) {
num++;
}
}
if(num > 0) {
printf("\nSo phan tu tim duoc: %d", num);
} else {
printf("\nKhong tim thay phan tu nao");
}
break;
}
case 4: {
int del;
printf("\nNhap phan tu can xoa: ");
scanf("%d", &del);
for(i=0;i<count;i++) {
if(dataList[i] == del) {
//Tim thay phan tu xoa
for(j=i+1;j<count;j++) {
dataList[j-1] = dataList[j];
}
i--;
count--;
}
}
printf("\nXoa thanh cong!!!");
break;
}
case 5: {
printf("\nDanh sach cac phan tu trong mang: ");
for(i=0;i<count;i++) {
printf("%d ", dataList[i]);
}
break;
}
case 6: {
printf("\nThoat chuong trinh!!!");
break;
}
default: {
printf("\nNhap sai!!!");
break;
}
}
} while(choose != 6);
return 0;
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)