HKI - LTCB Tuần 4
Câu 1:
Câu 2:
Câu 3:
Câu 4:
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void nhap(char s[]);
void lietke(char a[]);
void count(char a[]);
void timkiem(char a[]);
void chuanhoa(char a[]);
int main(){
char s[32], a[32];
nhap(s);
strcpy(a,s);
chuanhoa(a);
count(a);
timkiem(a);
lietke(a);
return 0;
}
void nhap(char s[]){
printf("Nhap vao mot chuoi: "); fflush(stdin); gets(s);
}
void lietke(char a[]){
printf("\nMoi Tu Mot Dong:\n");
for(int i=0;i<strlen(a);i++){
if(a[i] !=' ') printf("%c", a[i]);
else printf("\n");
}
}
void timkiem(char a[]){
char x[32]; int dem= 0;
printf("\nNhap Tu Khoa Can Tim: "); fflush(stdin); gets(x);
for(int i=0; i<strlen(a); i++){
if(a[i]&&a[i+1] == x[0]&&x[1])
dem++;
}
printf("So Luong \"%s\" trong chuoi: %i", x, dem);
}
void count(char a[]){
printf("\nSo Luong Tu Trong Chuoi: %i",strlen(a));
}
void chuanhoa(char a[]){
int i, j;
while(a[0]==' '){
for(int i=0;i<strlen(a);i++)
a[i]= a[i+1];
}
i=0;
while(i<strlen(a)){
if(a[i]==' ' && a[i+1]== ' '){
for(int j=i;j<strlen(a);j++)
a[j]= a[j+1];
i--;
}
i++;
}
while(a[strlen(a)-1]== ' ') a[strlen(a)-1]= '\0';
strlwr(a);
a[0]= toupper(a[0]);
for(int i=0;i<strlen(a);i++)
if(a[i]==' ') a[i+1]= toupper(a[i+1]);
printf("\nChuoi Sau Khi Chuan Hoa: "); puts(a);
}
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define max 100
void input(char s[]);
void output(char s[]);
int main(){
char s[max];
input(s);
output(s);
printf("\nChuoi sau khi chuan hoa: \"%s\"", s);
return 0;
}
void input(char s[]){
printf("Nhap vao 1 chuoi: "); gets(s);
printf("Chuoi vua nhap: \"%s\"", s);
}
void output(char s[]){
int i, j;
while(s[0]==' '){
for(i=0;i<strlen(s);i++) s[i]= s[i+1];
}
i=0;
while(i<strlen(s)){
if(s[i]==' ' && s[i+1]==' '){
for(j=i;j<strlen(s);j++)
s[j]= s[j+1];
i--;
}
i++;
}
while(s[strlen(s)-1]==' ') s[strlen(s)-1]= '\0';
strlwr(s);
s[0]= toupper(s[0]);
for(i=0;i<strlen(s);i++)
if(s[i]==' ') s[i+1]= toupper(s[i+1]);
}
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<string.h>
void xoa(char s[], int vt);
void xoaSo(char s[]);
int main(){
char s[100];
printf("Nhap vao 1 chuoi: "); gets(s);
xoaSo(s);
return 0;
}
void xoa(char s[], int vt){
if(vt>= 0 && vt<= strlen(s)){
for(int i=vt;i<strlen(s);i++)
s[i]= s[i+1];
}
}
void xoaSo(char s[]){
int i= 0, n= strlen(s);
while(i < n){
if(s[i]>= '0' && s[i]<= '9'){
xoa(s,i);
n--;
}
else i++;
}
printf("\nSau Khi Xoa So: \"%s\"", s);
}
/*
Ten: Hoang Minh Tuan
Lop: GS19TH1
*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define MAX 100
// Nguyen Mau Ham
void Nhap(int a[], int &n);
void xuat(int a[], int n);
void Tim_Tong_Le(int a[], int n);
float Trung_Binh_Cong(int a[], int n);
void Gia_Tri_Nho_Nhat(int a[], int n);
void Vi_Tri_Phan_Tu_Lon_Nhat(int a[], int n);
void Kiem_Tran_Mang_Dieu_La_So_Chan(int a[], int n);
void Liet_Ke_So_Duong(int a[], int n);
void Gia_Tri_Lon_Hon_Trung_Binh_Cong(int a[], int n);
void Liet_Ke_Chan_Le(int a[], int n);
int Kiem_Tra_So_Nguyen_To(int n);
void Liet_Ke_So_Nguyen_To(int a[], int n);
int Kiem_Tra_So_Chinh_Phuong(int n);
void Liet_Ke_So_Chinh_Phuong(int a[], int n);
// Ham main
int main(){
int a[MAX], n;
printf("================ NHAP MANG ================\n\n"); Nhap(a,n);
printf("\n\n================ XUAT MANG ================\n\n"); xuat(a,n);
printf("\n\n================ TONG SO LE ================\n\n"); Tim_Tong_Le(a,n);
printf("\n\n================ TRUNG BINH CONG ================\n\n"); printf("Trung Binh Cong= %.2f", Trung_Binh_Cong(a,n));
printf("\n\n================ GIA TRI MIN ================\n\n"); Gia_Tri_Nho_Nhat(a,n);
printf("\n\n================ VI TRI MAX ================\n\n"); Vi_Tri_Phan_Tu_Lon_Nhat(a,n);
printf("\n\n================ KIEM TRA SO CHAN ================\n\n"); Kiem_Tran_Mang_Dieu_La_So_Chan(a,n);
printf("\n\n================ LIET KE SO DUONG ================\n\n"); Liet_Ke_So_Duong(a,n);
printf("\n\n================ GIA TRI LON HON TRUNG BINH ================\n\n"); Gia_Tri_Lon_Hon_Trung_Binh_Cong(a,n);
printf("\n\n================ LIET KE CHAN LE ================\n\n"); Liet_Ke_Chan_Le(a,n);
printf("\n\n================ LIET KE SO NGUYEN TO ================\n\n"); Liet_Ke_So_Nguyen_To(a,n);
printf("\n\n================ LIET KE SO CHINH PHUONG ================\n\n"); Liet_Ke_So_Chinh_Phuong(a,n);
_getch();
return 0;
}
// Dinh Nghia Ham
void Nhap(int a[], int &n){
printf("Nhap So Luong Phan Tu: "); scanf("%i", &n); printf("\n");
for(int i=0; i<n; i++){
printf("Nhap Phan Tu Thu [%i]: ", i+1); scanf("%i", &a[i]);
}
}
void xuat(int a[], int n){
for(int i=0; i<n; i++){
printf("Phan Tu Thu [%i]: %i\n", i+1, a[i]);
}
}
void Tim_Tong_Le(int a[], int n){
int tongle;
for(int i=0; i<n; i++){
if(a[i] % 2 != 0) tongle+= a[i];
}
printf("Tong Cac So Le: %i", tongle);
}
float Trung_Binh_Cong(int a[], int n){
float tb= 0;
for(int i=0; i<n; i++){
tb+= a[i];
}
return tb/n;
}
void Gia_Tri_Nho_Nhat(int a[], int n){
int min= a[0];
for(int i=0; i<n; i++){
if(a[i] < min) min= a[i];
}
printf("Gia Tri Nho Nhat: %i", min);
}
void Vi_Tri_Phan_Tu_Lon_Nhat(int a[], int n){
int max= a[0], m;
for(int i=0; i<n; i++){
if(max < a[i]){
max= a[i];
m= i;
}
}
printf("Vi Tri Phan Tu Lon Nhat: %i", m+1);
}
void Kiem_Tran_Mang_Dieu_La_So_Chan(int a[], int n){
int count= 0;
for(int i=0; i<n; i++){
if(a[i] % 2 == 0) count++;
}
if(count == n) printf("Toan Bo Phan Tu Cua Mang Deu La So Chan!");
else printf("Trong Mang Co So Le!");
}
void Liet_Ke_So_Duong(int a[], int n){
int m= 1;
for(int i=0; i<n; i++){
if(a[i] > 0){
printf("%i ", a[i]);
m= 1;
}
}
if(m == 0) printf("Trong Mang Khong Co SO Duong!");
}
void Gia_Tri_Lon_Hon_Trung_Binh_Cong(int a[], int n){
int k= Trung_Binh_Cong(a,n);
for(int i=0; i<n; i++){
if(a[i] > k) printf("%i ", a[i]);
}
}
void Liet_Ke_Chan_Le(int a[], int n){
printf("So Chan Trong Mang: ");
for(int i=0; i<n; i++){
if(a[i] % 2== 0) printf("%i ", a[i]);
}
printf("So Le Trong Mang: ");
for(int i=0; i<n; i++){
if(a[i] % 2!= 0) printf("%i ", a[i]);
}
}
int Kiem_Tra_So_Nguyen_To(int n){
int mark= 1;
if(n < 2) mark= 0;
else{
for(int i=2; i<=sqrt(n); i++){
if(n % i == 0) mark= 0;
}
}
return mark;
}
void Liet_Ke_So_Nguyen_To(int a[], int n){
for(int i=0; i<n; i++){
if(Kiem_Tra_So_Nguyen_To(a[i]) == 1) printf("%i ", a[i]);
}
}
int Kiem_Tra_So_Chinh_Phuong(int n){
int mark= 0;
if(sqrt(n) * sqrt(n) == n) mark= 1;
return mark;
}
void Liet_Ke_So_Chinh_Phuong(int a[], int n){
for(int i=0; i<n; i++){
if(Kiem_Tra_So_Chinh_Phuong(a[i]) == 1) printf("%i ", a[i]);
}
}
No Comment to " HKI - LTCB Tuần 4 "