#include<stdio.h>
#include <conio.h>
// minh hoa toan tu logic tren bit
int main1()
{
int a = 5, b = 17;
// 5 = 0000 0000 0000 0000 0000 0000 0000 0101
// 17 = 0000 0000 0000 0000 0000 0000 0001 0001
//5^17 = 0000 0000 0000 0000 0000 0000 0001 0100
//5|17 = 0000 0000 0000 0000 0000 0000 0001 0101
//5&17 = 0000 0000 0000 0000 0000 0000 0000 0001
/*int c = a^b;
printf("%d ^ %d = %d", a, b, c);*/
/*
// 5 = 0000 0000 0000 0000 0000 0000 0000 0101
//~5 = 1111 1111 1111 1111 1111 1111 1111 1010
int d = ~a;
printf("~%d = %d", a, d);
*/
// 5 = 0000 0000 0000 0000 0000 0000 0000 0101
//5<<2 = 0000 0000 0000 0000 0000 0000 0001 0100
int d = a<<2;
printf("%d<<2 = %d", a, d);
_getch();
return 0;
}
========================================================
#include <stdio.h>
#include <stdlib.h>
// int ra man hinh co quy cach
int main2()
{
printf("1234567890123456789012345678901234567890\n");
printf("%5c\n", 'A');
printf("%-5c\n", 'A');
/*
printf("%f\n", 34.2);
printf("%7.2f\n", 34.2);
printf("%-7.2f\n", 34.2);
printf("%7.2f\n", 34.256);
printf("%.2f\n", 34.256);
*/
/*
printf("%5d\n", 4);
printf("%-5d\n", 4);
printf("%-5d\n", 123456);
*/
system("pause");
return 0;
}
========================================================
#include <stdio.h>
#include <stdlib.h>
// minh hoa if..else
int main3()
{
//1 nhap so kwh tieu thu
//2 tinh va in ra gia theo cong thuc
int soKwh; // so kwh
printf("Nhap vao so Kwh:");
scanf_s("%d", &soKwh);
int tien;
if (soKwh <= 50)
tien = soKwh * 500;
else if (soKwh <= 100)
tien = 50 * 500 + (soKwh - 50) * 1000;
else
tien = 50 * 500 +50*1000+ (soKwh - 100) * 2000;
printf("Tien phai tra cho so %d kwh la %d\n", soKwh, tien);
system("pause");
return 0;
}
========================================================
#include <stdio.h>
#include <stdlib.h>
// minh hoa switch..case
int main()
{
//1 nhap vao thang va in ra so ngay
int thang;
printf("Nhap vao thang can tra cuu:");
scanf_s("%i", &thang);
switch (thang)
{
case 2: printf("Thang co 28/29 ngay\n");
//break;
case 4: case 6: case 9: case 11: printf("Thang co 30 ngay\n");
break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("Thang co 31 ngay\n");
break;
default:
printf("%i la thang khong hop le\n", thang);
}
system("pause");
return 0;
}
========================================================
/*
Nhap vao mot so nguyen,
tinh tong cac chu so cua so nguyen do
VD: 1234 -> tong la 10
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, tong;
printf("Nhap vao mot so nguyen:");
scanf_s("%d", &n);
//tach tung chu so cua so nguyen? vd 1234
// chia lay phan du cho 10, (ta duoc 4)
//phan con lai la so chia lay phan nguyen cho 10 (duoc 123)
// lap toi khi phan nguyen bang 0 thi dung
/*
1234 ==> 123 va 4
123 ==> 12 va 3
12 ==> 1 va 2
1 ==> 0 va 1
*/
tong = 0;
int count=0;
while (n > 0)
{
/*count++;
printf("lan lap %d\n", count);
printf("Chu so don vi: %d\n", n % 10);
printf("So nguyen con lai: %d\n", n / 10);*/
tong = tong + n % 10;
n = n / 10;
}
printf("Tong cua cac chu so tao nen so la %d\n", tong);
system("pause");
return 0;
}
======================================================
/*
minh hoa switch .. case
*/
#include <stdio.h>
#include <stdlib.h>
int main4()
{
/*
nhap vao mot tuoi di hoc, va in ra trinh do va
cap hoc tuong ung
VD. 1-3 : mam non
4-5: mau giao
6-10: pho thong co so (tieu hoc)
11-15: trung hoc co so (trung hoc)
16-18: trung hoc pho thong (pho thong)
19-23: dai hoc
24->100: truong doi
*/
int n; // n>=1 va n<=100
printf("Nhap vao do tuoi:");
scanf_s("%d", &n);
while (n < 1 || n>100)
{
printf("Do tuoi khong hop le, nhap lai do tuoi:");
scanf_s("%d", &n);
}
if(n<=3)
printf("Hoc truong mam non");
else if(n<=5) //n>3 AND n<=5
printf("Hoc truong mau giao");
else if(n<=10)
printf("Hoc truong tieu hoc");
else if(n<=15)
printf("Hoc truong trung hoc");
else if(n<=18)
printf("Hoc truong pho thong");
else if (n <= 23)
printf("Hoc truong dai hoc");
else
printf("Hoc truong doi");
printf("\n");
system("pause");
return 0;
}
==================================================
/*
minh hoa switch .. case
*/
#include <stdio.h>
#include <stdlib.h>
int main3()
{
/*
nhap vao mot tuoi di hoc, va in ra trinh do va
cap hoc tuong ung
VD. 1-3 : mam non
4-5: mau giao
6-10: pho thong co so (tieu hoc)
11-15: trung hoc co so (trung hoc)
16-18: trung hoc pho thong (pho thong)
19-23: dai hoc
24->100: truong doi
*/
int n; // n>=1 va n<=100
printf("Nhap vao do tuoi:");
scanf_s("%d", &n);
while (n < 1 || n>100)
{
printf("Do tuoi khong hop le, nhap lai do tuoi:");
scanf_s("%d", &n);
}
switch (n)
{
case 1: case 2: case 3:
printf("Hoc truong mam non");
break;
case 4: case 5:
printf("Hoc truong mau giao");
break;
case 6: case 7: case 8: case 9: case 10:
printf("Hoc truong tieu hoc");
break;
case 11: case 12: case 13: case 14: case 15:
printf("Hoc truong trung hoc");
break;
default:
printf("Hoc truong doi");
break;
}
printf("\n");
system("pause");
return 0;
}
========================================================
#include <stdio.h>
#include <stdlib.h>
// minh hoa break, continue
int main()
{
/*
for (int i = 0; i < 10; i++)
for (int j = 1; j <= 10000; j++)
{
printf("Vong lap i = %d, j = %d\n", i, j);
if (j >= 5) break; //chi co tac dung voi vong lap cua j
}
*/
for (int i = 0; i < 5; i++)
for (int j = 1; j <= 20; j++)
{
if (j % 5) continue; //chi co tac dung voi vong lap cua j
//bo qua lenh phia sau khi
//j khong chia het cho 5
printf("Vong lap i = %d, j = %d\n", i, j);
}
system("pause");
return 0;
}
==================================================
#include <stdio.h>
#include <stdlib.h>
// minh hoa mang phan tu kieu so thuc (n<=100)
// 1. nhap mang
// 2. tim min, max
// 3. sap xep mang va in ra theo thu tu tang va giam dan
// (cung voi mang ban dau) ==> tao ra mang copy
int main()
{
double A[100], B[100];
int n;
//0<n va n<=100
printf("Nhap vao so phan tu cua mang:");
scanf_s("%d", &n);
while (n<=0 || n>100)
{
printf("Hay nhap nhap so luong hop le:");
scanf_s("%d", &n);
}
//nhap vao mang
printf("Nhap vao gia tri tung phan tu:\n");
for (int i = 0; i < n; i++)
{
printf("A[%d]=", i);
scanf_s("%lf", &A[i]);
}
// tim min va max
double max=A[0];
for (int i = 0; i < n; i++)
{
if (max < A[i]) max = A[i];
}
double min = A[0];
for (int i = 0; i < n; i++)
{
if (min > A[i]) min = A[i];
}
printf("Gia tri lon nhat la %.2lf\n", max);
printf("Gia tri nho nhat la %.2lf\n", min);
// tao ban copy cua mang ban dau
for (int i = 0; i < n; i++)
B[i] = A[i];
for (int i = n; i >= 2; i--)
for (int j = 0; j < i - 1; j++)
{
if (A[j] > A[j + 1])
{
double phu = A[j];
A[j] = A[j + 1];
A[j + 1] = phu;
}
}
printf("Mang ban dau: \n");
for (int i = 0; i < n; i++)
{
printf("%.2lf, ", B[i]);
}
printf("\n");
printf("Mang sau khi sap xep: \n");
for (int i = 0; i < n; i++)
{
printf("%.2lf, ", A[i]);
}
printf("\n");
system("pause");
return 0;
}
=======================================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
nhap vao mot ten
tach ra ho va ten
VD. Nguyen Tan Dung
Ho: Nguyen Tan
Ten : Dung
*/
int main()
{
char fullname[50];
//nhap vao ten
printf("Nhap vao ten:");
gets_s(fullname); //nhap duoc xau co dau cach trong
//scanf_s("%s", fullname); // khong co dau &
//==> khong nhap duoc xau co dau cach trong
printf("Xau vua nhap: %s\n", fullname);
//Tach ra phan ten va phan Ho
//1. Tim vi tri dau cach trong cuoi cung
int vitri = 0;
int i = 0;
while (i < strlen(fullname))
{
if (fullname[i] == ' ')vitri = i;
i++;
}
printf("Vi tri dau cach trong cuoi cung: %d\n", vitri);
// Phan Ho tu vi tri 0 toi Dau cach trong cuoi cung
char surname[50];
printf("Ho: ");
for (i = 0; i < vitri; i++)
{
surname[i] = fullname[i];
}
surname[i] = '\0';
printf("%s", surname);
printf("\n");
// Phan ten tu dau cach trong cuoi cung toi het
printf("Ten: ");
// in tung ky tu!, dung strncpy
for (i = vitri+1; i < strlen(fullname); i++)
printf("%c", fullname[i]);
printf("\n");
system("pause");
return 0;
}
============================================================
#include <stdlib.h>
#include <stdio.h>
/*
minh hoa mang struct dung luu tru thong tin sinh vien
*/
typedef struct TTSV
{
char hoten[50];
char shsv[15];
float gk, th, ck;
float hp; //tinh theo cong thuc 0.2*(gk+th) + 0.6*ck
} SV;
//dung typedef de dinh nghia lai ten cau truc cho ngan
//typedef struct TTSV SV;
int main9()
{
SV lop[100];
int n; // so thanh vien thuc su (n>0 va n<=100)
//nhap thong tin cac thanh vien lop
printf("Nhap so thanh vien cua lop:");
scanf_s("%d", &n);
while (n <= 0 || n > 100)
{
printf("Hay nhap so luong hop le:");
scanf_s("%d", &n);
}
char buffer[100];
// dung vong lap for der nhap thong tin tung thanh vien
for (int i = 0; i < n; i++)
{
gets_s(buffer);
printf("Nhap vao thong tin thanh vien thu %d:\n", i + 1);
printf("Ho ten:"); fflush(stdin);// do bo nho dem truoc khi nhap xau
gets_s(lop[i].hoten);
printf("SHSV:"); fflush(stdin);// do bo nho dem truoc khi nhap xau
gets_s(lop[i].shsv);
printf("Diem giua ky:");
scanf_s("%f", &lop[i].gk);
printf("Diem thuc hanh:");
scanf_s("%f", &lop[i].th);
printf("Diem cuoi ky:");
scanf_s("%f", &lop[i].ck);
}
// tinh diem hoc phan
for (int i = 0; i < n; i++)
{
if (lop[i].gk < 3 || lop[i].th < 3)
lop[i].hp = 0;
else
lop[i].hp = 0.2*(lop[i].gk + lop[i].th) + 0.6*lop[i].ck;
}
//dem so luong sinh vien khong qua va in ra thong tin sinh vien qua
printf("Ket qua hoc tap:\n");
int sosinhvienkodat = 0;
printf("%30s %10s %4s %4s %4s %4s\n","Ho ten ","SHSV ","GK ","TH ","CK ","HP ");
for (int i = 0; i < n; i++)
{
if (lop[i].hp < 4) sosinhvienkodat++;
else
{
printf("%30s %10s %.2f %.2f %.2f %.2f\n", lop[i].hoten,
lop[i].shsv, lop[i].gk, lop[i].th, lop[i].ck, lop[i].hp);
}
}
printf("Tong so sinh vien khong dat: %d\n", sosinhvienkodat);
// sap xep va in ra thong tin sinh vien theo diem hoc phan giam dan
for (int i = n; i >= 2; i--)
for (int j = 0; j < i-1; j++)
{
if (lop[j].hp < lop[j + 1].hp)
{
SV phu = lop[j];
lop[j] = lop[j + 1];
lop[j + 1] = phu;
}
}
printf("\n========================================\n");
printf("Danh sach sinh vien voi diem hp giam dan:\n\n");
printf("%30s %10s %4s %4s %4s %4s\n", "Ho ten ", "SHSV ", "GK ", "TH ", "CK ", "HP ");
for (int i = 0; i < n; i++)
{
printf("%30s %10s %.2f %.2f %.2f %.2f\n", lop[i].hoten,
lop[i].shsv, lop[i].gk, lop[i].th, lop[i].ck, lop[i].hp);
}
system("pause");
return 0;
}
====================================================================
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/*
minh hoa ham xu ly tam giac
*/
double tinhChuvi(double a, double b, double c)
{
if (a <= 0 || b <= 0 || c <= 0) return -1; //Canh khong hop le
if (a + b <= c || b + c <= a || a + c <= b)
return -2;// khong tao thanh tam giac
return a + b + c;
}
double tinhDienTich(double a, double b, double c)
{
if (a <= 0 || b <= 0 || c <= 0) return -1; //Canh khong hop le
if (a + b <= c || b + c <= a || a + c <= b)
return -2;// khong tao thanh tam giac
double p = tinhChuvi(a, b, c) / 2;
return sqrt(p*(p - a)*(p - b)*(p - c));
}
int main10()
{
double x, y, z;
printf("Nhap vao do dai 3 canh tam giac:");
scanf_s("%lf%lf%lf", &x, &y, &z);
if (tinhChuvi(x, y, z) < 0) printf("Tam giac khong hop le!\n");
else printf("Chu vi tam giac : %lf\n", tinhChuvi(x, y, z));
if (tinhDienTich(x, y, z) < 0) printf("Tam giac khong hop le!\n");
else printf("Dien tich tam giac : %lf\n", tinhDienTich(x, y, z));
system("pause");
return 0;
}
=========================================================
#include <stdio.h>
#include <stdlib.h>
/*
mih hoa doc so co tu 1->3 chu so
*/
//hàm đọc chữ số
void docChuSo(int a)
{
if (a == 0) printf("khong");
else if (a == 1)printf("mot");
else if (a == 2)printf("hai");
else if (a == 3)printf("ba");
else if (a == 4)printf("bon");
else if (a == 5)printf("nam");
else if (a == 6)printf("sau");
else if (a == 7)printf("bay");
else if (a == 8)printf("tam");
else if (a == 9)printf("chin");
else printf("ERROR!!!");
}
// kiem tra so can doc co phai so tuf 1->3 chu so hay khong
// ham tra ve -1 neu khong hop le, nguoc lai tra ve 0
int kiemTra(int n)
{
if (n>999 || n<-999) return -1;
else return 0;
}
// doc so co tu 1->3 chu so
void docSo(int n)
{
int a, b, c;// phan tram, chuc va don vi
if (kiemTra(n) == -1)
printf("Loi! So khong hop le.\n");
else
{
if (n<0) {
printf("am ");
n = -n;
}
c = n % 10; n = n / 10;
b = n % 10; n = n / 10;
a = n % 10;
if (a>0) { docChuSo(a); printf(" tram "); }
if (a>0 && b == 0 && c!=0)printf("le ");
if (b == 1) { printf("muoi "); }
if (b>1) { docChuSo(b); printf(" muoi "); }
if(c!=0) docChuSo(c);
printf("\n");
}
}
int main11(void)
{
docSo(-115);
docSo(-125);
docSo(-105);
docSo(-5);
docSo(25);
docSo(15);
docSo(110);
docSo(100);
docSo(10);
system("pause");
return 0;
}
===================================================
#include <stdio.h>
#include <stdlib.h>
void increase(int x)
{
x = x * 2;
}
void increase(int A[], int size)
{
for (int i = 0; i < size; i++)
A[i] = A[i] * 2;
}
void printArray(int A[], int size)
{
for (int i = 0; i < size; i++)
printf("%d ", A[i]);
}
int main12()
{
int a = 5;
printf("Gia tri ban dau cua a:%d\n", a);
increase(a);
printf("Gia tri cua a sau khi goi ham:%d\n", a);
int A[] = { 1,3,5,7,4 };
printf("Mang ban dau:");
printArray(A, 5);
printf("\n");
increase(A, 5);
printf("Mang sau khi goi ham:");
printArray(A, 5);
printf("\n");
system("pause");
return 0;
}
=========================================================
#include <stdio.h>
#include <stdlib.h>
void sort(int a[], int n)
{
int i, j, temp;
for (i = 0; i < n - 1; ++i)
for (j = i + 1; j < n; ++j)
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
void display(int A[], int size)
{
int i;
for (i = 0; i<size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main(void)
{
int i;
int array[16] = { 34, -5, 6, 0, 12, 100, 56, 22,
44, -3, -9, 12, 17, 22, 6, 11 };
printf("Mang ban dau:\n");
display(array, 16);
sort(array, 16);
printf("Mang sau khi sap xep:\n");
display(array, 16);
system("pause");
return 0;
}
No comments:
Post a Comment