Đáp án KTLT thầy Tùng
ĐHBKHN
MADE BY TẠ TUẤN MINH-K58
Email: [email protected]
Bài Tập
Chương 2 (T21)
Bài 3:
#include <stdio.h>
int main() {
struct SinhVien {
char ten[20];
int nam_sinh;
int khoa;
} sv;
printf("Nhap du lieu:\n");
printf("Ten: ");
fflush(stdin);
gets(sv.ten);
printf("Nam sinh: ");
scanf("%d", &sv.nam_sinh);
printf("Khoa: ");
scanf("%d", &sv.khoa);
printf("Thong tin sinh vien:\n"
" Ten: %s\n"
" Nam sinh: %d\n"
" Khoa: %d\n",
sv.ten, sv.nam_sinh, sv.khoa);
return 0;
fflush(stdin);
gets(oto.model);
printf("Khoi luong: ");
scanf("%f", &oto.khoi_luong);
printf("Mau son: ");
fflush(stdin);
gets(oto.mau_son);
printf("Nhap du lieu cho cac banh:\n");
for (i=0; i
if (delta > 0)
printf("Phuong trinh co 2 nghiem: %.3f va %.3f\n",
(-b-sqrt(delta))/(2*a),
(-b+sqrt(delta))/(2*a));
else if (delta == 0.)
printf("Phuong trinh co 1 nghiem: %.3f\n", -b/2/a);
else printf("Phuong trinh vo nghiem\n");
}
return 0;
}
Bài 2: (Tính tổng nghịch đảo…)
#include <stdio.h>
int main() {
int n;
float s;
for (n=2, s=0.; n
Bài 5 (dùng rẽ nhánh)
#include <stdio.h>
int main() {
int lua_chon;
printf("MENU:\n"
"1. Switch user\n"
"2. Open ...\n"
"3. Log off\n"
"4. Exit\n"
"Lua chon: ");
scanf("%d", &lua_chon);
switch(lua_chon) {
case 1:
printf("Ban da chon \"Switch user\"\n");
break;
case 2:
printf("Ban da chon \"Open ...\"\n");
break;
case 3:
printf("Ban da chon \"Log off\"\n");
break;
case 4:
printf("Ban da chon \"Exit\"\n");
break;
default:
printf("Lua chon khong co!\n");
}
return 0;
}
Chương 4: Con trỏ, mảng và quản lý bộ nhớ. (BT T17)
Bài 1:
#include <stdio.h>
int main()
{
int N, i;
int *p;
printf("Nhap N: ");
scanf("%d", &N);
p = (int*)malloc(N*sizeof(int));
for (i=0; i=0; i--)
printf("%d ", p[i]);
printf("\n");
free(p);
return 0;
}
Bài 2:
#include <stdio.h>
#include <stdlib.h>
int main()
}
Bài 3:
#include <stdio.h>
#include <stdlib.h>
int main() {
const char* s1 = "abcd123154";
int i;
char* s2;
for (i=0; s1[i]!=0; i++) ;
s2 = (char*)malloc(i+1);
for (i=0; s1[i]!=0; i++)
s2[i] = s1[i];
s2[i] = 0;
printf("s2 = %s\n", s2);
free(s2);
return 0;
}
Bài 4:
#include <stdio.h>
#include <stdlib.h>
int main() {
const char* s1 = "footballer";
const char* s2 = "football";
const char* p1, *p2;
int ketqua;
for (p1=s1, p2=s2; *p1!=0 && *p2!=0; p1++, p2++) {
if (*p1 < *p2) {
ketqua = -1;
/* n: số phần tử của mảng */
/* Kiểm tra xem đã nhập chuỗi ở tham số chưa */
if (argc != 2) {
printf("Chua nhap chuoi ky tu o tham so dong lenh\n");
return -1;
}
/* Gán s bằng chuỗi tham số */
s = argv[1];
for (n=0, s2=s1=s; ; n++) {
/* Bỏ qua các ký tự trắng */
for (s1=s2; *s1==' '; s1++) ;
if (*s1==0) break; /* Nếu đã tới cuối chuỗi s thì dừng */
/* Khi tới đây thì s1 trỏ tới ký tự đầu tiên của một từ */
/* Tìm tới vị trí kết thúc từ bằng cách tìm ký tự trắng tiếp theo sau s1 */
for (s2=s1; *s2!=0 && *s2!=' '; s2++) ;
/* Khi tới đây thì s2 trỏ tới ký tự trắng hoặc vị trí kết thúc chuỗi s */
/* Mở rộng mảng p thêm một phần tử mới */
p2 = (char**)malloc((n+1)*sizeof(char*)); /* Cấp phát vùng nhớ mới */
if (p!=NULL) {
/* Copy mảng cũ sang mảng mới */
for (i=0; i
else if (j==nb) c[k] = a[i++];
else if (a[i] < b[j]) c[k] = a[i++];
/* Copy từ */
/* Thêm ký tự 0 vào cuối từ */
else c[k] = b[j++];
}
printf("Mang da tron: \n");
for (k=0; k
hoan_doi(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
Bài 2:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int is_prime(int n)
{
int i;
int cn = sqrt(n);
if (n==1) return 0;
if (n==2) return 1;
for (i=2; i
{
printf("sin(x) = %g\n", sin(x));
return 0;
}
int tinh_cos()
{
printf("cos(x) = %g\n", cos(x));
return 0;
}
int tinh_tan()
{
printf("tan(x) = %g\n", tan(x));
return 0;
}
int thoat()
{
return 1;
}
int main()
{
MenuItem m[] = {
{"Nhap x", nhap_x},
{"Tinh sin x", tinh_sin},
{"Tinh cos x", tinh_cos},
{"Tinh tan x", tinh_tan},
{"Thoat", thoat}
};
int i, n = sizeof(m)/sizeof(m[0]);
int lua_chon;
Bài 5.1:
#include "bt_5_string.h"
int main()
{
String s1, s2;
khoi_tao_chuoi(&s1, "abcd");
khoi_tao_chuoi(&s2, "12345");
printf("s1 = %s, s2 = %s\n", s1, s2);
copy_chuoi(&s1, s2);
printf("s1 = %s, s2 = %s\n", s1, s2);
huy_chuoi(&s1);
huy_chuoi(&s2);
return 0;
}
Bài 5.2:
#include <string.h>
#include "bt_5_string.h"
void khoi_tao_chuoi(String* s, const char* s1)
{
int n = strlen(s1);
if (*s == NULL) free(*s);
*s = (char*)malloc(n+1);
strcpy(*s, s1);
}
void copy_chuoi(String* s1, const String s2)
{
int n = strlen(s2);
if (*s1 == NULL) free(*s1);
case Circle:
x = *(double*)s.data;
return 3.14*x*x;
case Square:
x = *(double*)s.data;
return x*x;
case Rect:
x = ((double*)s.data)[0];
y = ((double*)s.data)[1];
return x*y;
}
return 0;
}
int main()
{
Shape s[3];
int i;
s[0].type = Circle;
s[0].data = (double*)malloc(sizeof(double));
(*(double*)s[0].data) = 5.;
s[1].type = Square;
s[1].data = (double*)malloc(sizeof(double));
(*(double*)s[1].data) = 10.;
s[2].type = Rect;
s[2].data = (double*)malloc(2*sizeof(double));
((double*)s[2].data)[0] = 10.;
((double*)s[2].data)[1] = 20.;
for (i=0; i
return l1*l2;
}
int main()
{
Shape s[3];
int i;
s[0].data = (double*)malloc(sizeof(double));
(*(double*)s[0].data) = 5.;
s[0].area = area_circle;
s[1].data = (double*)malloc(sizeof(double));
(*(double*)s[1].data) = 10.;
s[1].area = area_square;
s[2].data = (double*)malloc(2*sizeof(double));
((double*)s[2].data)[0] = 10.;
((double*)s[2].data)[1] = 20.;
s[2].area = area_rect;
for (i=0; i