Giải-bài-tập-ck-CBKHMT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng

GV: TS. Phạm Khắc Hùng MSSV 20196362

CÂU LỆNH ĐIỀU KHIỂN


Bài 1. Tính biểu thức s = asinb +exp(c) với a, b, c đọc từ bàn phím. In kết quả ra
màn hình.
//Tính biểu thức s = asinb +exp(c) với a, b, c đọc từ bàn phím. In kết quả ra
màn hình.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{ double a,b,c,s;
scanf("%lf%lf%lf",&a,&b,&c);
s= a*sin(b)+ exp(c);
printf("S= %fsin(%f)+ exp(%f)= %f",a,b,c,s);

}
Bài 2. Tìm nghiệm của phương trình ax2 + bx +c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{
float a,b,c,x1,x2,dd;
scanf("%f%f%f",&a,&b,&c);
dd=b*b-4*a*c;
if(a==0)
{
printf("phuong trinh bac mot\n");exit(1);
}
if(dd>0)
{
x1=(b-sqrt(dd))/2/a;
x2=(b+sqrt(dd))/2/a;
printf( "nghiem thuc la %f ”,x1 );
}
else
{
printf("phuong trinh co nghiem phuc\n");
printf("%.2f+i%.2f\n",b/2/a,sqrt(-dd)/2/a);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

printf("%.2f-i%.2f\n",b/2/a,sqrt(-dd)/2/a);
}
}
Bài 3. Tính tổng cosx + cos2x + … +cos100x
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{ int i; float s,x;
scanf("%f",&x);
s=0;
for(i=1;i<=100; i++)s+=cos(x*i);
printf("i= %d S= %f \n",i,s);
printf(" sinx = %f",sin(x));
}
Bài 4. Giải phương trình x5 + ax2 + b = 0 bằng phương pháp lặp.
#include <stdio.h>
#include <math.h>
#include<stdlib.h>
#define NITER 30
int main()
{ double a,b,x,x0,d,dx;
int count=0;
scanf("%lf", &a);
scanf("%lf",&b);
scanf("%lf",&x);
x0=x;
do
{d=b-a*x*x;
if(d<0)
{printf("error");
exit(1);
}
x=pow(d,0.2);
x0=x; dx=fabs((x-x0)/x);
printf("count= %d x0=%.4f dx= %.4f \n", count++,x0,dx);
}
while (count< NITER);
return 0;
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

HÀM SỐ
Bài 5. Tính giai thừa chương trình có hai hàm: hàm factorial() và hàm main()
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double factorial( int n)
{ int count;
double fact =1;
for(count=n; count > 0 ; --count) fact*=(double)count;
return fact;

}
int main ()
{ int i;
for(i=10 ; i <= 20; i++) printf ("i= %d factorial(%d)= %12.3e \n",i,i,factorial(i));
return 0;
}
Bài 6. Tính sin(x)n dùng hàm fun1 và fun2
//sin(x)^n
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double fun1(double, int);
double fun2(double, int);
int main ()
{
int i; double x=2.1;
for(i=1; i<= 5; i++) printf("i=%3d fun1=%12.3e fun2= %12.3e
\n",i,fun1(x,i),fun2(x,i));
return 0;
}
double fun1(double x, int n)
{ int i; double fact = 1;
for(i=1; i<= n; i++) fact*=sin(x);
return fact;
}
double fun2( double x, int n)
{ if ( n== 0) return 1;
else return sin(x)*fun2(x,n-1);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

}
Bài 7. Đọc n số nguyên từ file data.dat. Xác định tổng n số, số lớn nhất và số bé
nhất. In kết quả ra màn hình. Chương trình gồm hai hàm calculate() và main().
Hàm calculate() đọc dữ liệu từ data.dat và tính tổng, max và min.
//C:\\Users\\ADMIN\\Desktop\\bai7.txt
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void calculate( long k)
{
long i,t,tmax,tmin,s; FILE *p;
p=fopen("C:\\Users\\ADMIN\\Desktop\\bai7lol.txt","r +");
s=0;
for(i=0;i< k; i ++)
{
fscanf(p,"%ld",&t);
if ( t==0) tmax=tmin=t;
s+=t; if (t> tmax) tmax=t;
if(t < tmin) tmin=t;
}
printf(" tmax =%d tmin=%d s=%d \n",tmax,tmin,s);
fclose(p);
}
main()
{

calculate(10);
printf("main\n");
return 0;
}
Bài 8 Tìm nghiệm phương trình x5-ex+5=0 trong khoảng [2, 3] bằng phương
pháp vét cạn.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double fun(double x)
{ return x*x - exp(x)+5;
}
void solve(double xo, long n)
{ long i; double dx,x,r,rmin;
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

dx=0.001;
for(i=0; i < n; i++)
{
r = fun(xo+i*dx);
if (i%100==0) printf("x%6.2f r=%6.2f \n",xo+i*dx,r);
if( i== 0) { rmin=r;x=xo;}
if(fabs(r)<rmin) { rmin=fabs(r); x=xo+i*dx;}
}
printf("x=%6.4f fun= %8.6f \n",x,fun(x));

}
main ()
{solve(2,1000);
printf("main\n");
}
Bài 9 Tìm nghiệm phương trình x5-ex+5=0 trong khoảng [2, 3] bằng
phương pháp chia đôi.
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
double fun(double x)
{ return x*x -exp(x)+ 5;
}
void solve(double a, double b)
{ long i; double f1,f2,f12,x;
f1=fun(a);f2=fun(b);i=0;
while (i<100)
{
x=(a+b)/2;f12=fun(x);
if (f12*f1 > 0) a=x; else b=x;
i++;

}
printf("x=%6.4f fun=%f \n",x,fun(x));
}
main()
{ solve(2,3);
printf("main\n");
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

Bài 10 Tính tích phân của hàm f(x) với khoảng [a,b] bằng phương pháp Ole và
Simpson
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double fun(double x)
{ return x*x + x+1;
}
double integrate(double a, double b)
{
long i,n; double dx,x,f;
f=0; n=100;dx=(b-a)/n;
for(i=0; i < n+1; i++)
{ x= a+i*dx;
if(i==0 || i== n) f+=fun(x)/2;
else f+=fun(x); }
return f*dx;

}
double integrate1(double a, double b)
{ long i,n; double f,dx,x;
n=100;dx= (b-a)/n;
f=fun(a)+ 4*fun(a+dx/2);
for(i=1;i< n+1; i++)
{

x=a+i*dx;
if(i==n) f+=fun(x);
else
f+=fun(x) +fun(x+dx) + 4*fun(x+dx/2);
}

return f*dx/6;
}
main()
{
double f1,f2,f3,a,b;
a=0;b=1;f1=(b*b*b/3+b*b/2+b)-(a*a*a/3+a*a/2+a);
f2=integrate(a,b);f3=integrate1(a,b);
printf("f1=%8.6f f2=%8.6f f3=%8.6f\n",f1,f2,f3);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

CON TRỎ
Bài 10 In địa chỉ, giá trị của biến con trỏ.
#include<stdio.h>
void main()
{
int u=5,v;
int *pu,*pv;
pu=&u;
v=*pu;
pv=&v;
printf("\n u= %d &u= %x pu=%x *pu = %d",u,&u,pu,*pu);
printf("\n v= %d &v=%x *pv= %d \n",v,&v,pv,*pv);
}
Bài 10a Dùng hàm với đối con trỏ để trao đổi giá trị của hai biến đơn.
#include <stdio.h>
#include <stdlib.h>

void swap(double a, double b)


{
double t;
t=a;
a=b;
b=t;
}
void swap1(double *a, double *b)
{
double t;
t=*a;*a=*b;*b=t;
}

main()
{
double a,b;
scanf("%lf",&a);
scanf("%lf",&b);

swap(a,b);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

printf("a = %lf b = %lf", a,b);


swap1(&a,&b); printf("a = %.2f b = %.2f", a,b);
}
Bài 11 In giá trị của sinx + sin2x + .. sinnx. x nhập từ bàn phím; n từ 5 đến 10 trao
đổi dữ liệu bằng con trỏ.
#include <stdio.h>
#include <math.h>
void calculate(int n, double x, double *fact)
{ *fact=1;
for(;n>0;--n) *fact+=sin(n*x);
}
main ()
{ int j; double x=2.1, fact;
for(j=5; j <= 10; j++)
{ calculate(j,x,&fact);
printf(" j= %3d fact=%12.6f \n",j,fact);

}
}
Bài 11a Chương trình dùng tên hàm như đối của một hàm khác.
#include<stdio.h>
void cube(double(*)(double), double, double *);
double fun1(double);
double fun2(double);
main ()
{ double x,res1,res2;
printf("\n x="); scanf("%lf",&x);
cube (fun1,x,&res1);
cube(fun2,x,&res2);
printf("\n x=%8.4f res1= %8.4f res2 = %8.4f \n", x,res1,res2);
}
void cube(double(*fun)(double), double x,double *result)
{ double y;
y=(*fun)(x);
*result= y*y*y;
}
double fun1 (double z)
{ return 3.0*z*z - z;
}
double fun2(double z)
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

{ return 4.0*z - 5.0*z*z*z;


}

BIẾN TỔNG THỂ


Bài 12 Chương trình in giai thừa từ 1 đến 10 dùng biến tổng thể.
#include <stdio.h>
#include<stdlib.h>
void factorial();
int j; double fact;
main()
{ for(j=1; j <= 10; ++j)
{ factorial();
printf("\n j= %d factorial = %e",j,fact);
}
}
void factorial()
{ int count;
if ( j < 0)
{ printf("\n error: factorial of negature not defined\n");
exit(1);
}
for(count=j,fact=1; count > 0;-- count) fact*=(double)count;
}

Bài 12a Chương trình tính giá trị trung bình và sai phân của ba số.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

double a,b,c,x,y;

void calculate()
{
x=(a+b+c)/3;
y=(abs(a-x)+abs(b-x)+abs(c-x))/3;
}
main()
{
scanf("%lf%lf%lf",&a,&b,&c);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

calculate();
printf("x=%f y=%f \n",x,y);

}
MẢNG
Bài 13 Chương trình tính giá trị trung bình và sai phân của các số.
#include <stdio.h>
#include <stdlib.h>
#define nmax 100
main()
{ int n, count;
double sum = 0,d,avg;
double list[nmax];
scanf("%d",&n);
for( count=0; count < n; ++ count)
{ printf("i = %d x=",count+1);
scanf("%lf",&list[count]);
sum+=list[count];
}
avg= sum/(double)n;
printf("\n avg = %5.2f \n",avg);
for( count=0; count < n; ++ count)
{ d= list[count]- avg;
printf("i= %d x= %5.2f d= %5.2f \n ",count+1,list[count],d);
}
}

Bài số 14 In giá trị của giai thừa từ 10 đến 20 trao đổi mảng thông qua con trỏ.
#include <stdio.h>
void factorial(double []);
main()
{ int i;
double fact[21];
factorial(fact);
for(i=10; i<= 20; i++)
printf("i= %d factorial(%d) = %e\n", i,i,fact[i]);
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

void factorial( double fact[])


{ int count;
fact[0]=1;
for(count=0; count <= 20; count ++)
fact[count+1]=(double)(count+1)*fact[count];

}
Bài số 14a Tính tổng các phần tử của matrix sử dụng hàm sum với con trỏ.
/*chương trình tính tổng các phần tử đường chéo của ma trận*/
#include <stdio.h>
void sum(int (*y)[3], int *z, int n)
{
int i, *p;
p=&y[0][0];*z=0;
for(i=0; i<n; i++)*z+=*(p+i*n+i);
}
//********************************************************
main()
{
int x[3][3]=
{
{1,2,3},{11,6,9},{3,7,1},
};
int z;

sum(x, &z, 3); printf("%6d%6d\n", x[1][1], z);


}

CON TRỎ VÀ XÂU KÍ TỰ


Bài số 15 tính độ dài của xâu, strlen1.c
// strlen1.c tính độ dài xâu kí tự

#include <stdio.h>
#include <stdlib.h>

//*************************************************************
int strlen1(char *s) //Cach1
{
long n;
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

for(n=0; *s!='\0';s++)n++;
return n;
}
//*************************************************************
int strlen2(char *s)//Cach2
{
char *p=s;

while(*p!='\0')p++;
return p-s;
}
main()
{

printf("strlen %6d %6d\n",strlen1("hello"),strlen2("hello, world"));


printf("main\n");
}
Bài số 15a copy xâu, strcpy.c
// strcpy1.c bài tập copy xâu s vào xâu t

#include <stdio.h>
#include <stdlib.h>

void strcpy1(char s[], char *t)


{
int i=0;

while((s[i]=t[i])!='\0')i++;
}

void strcpy2(char *ps, char *t)


{
while((*ps=*t)!='\0'){ps++;t++;}
}

void strcpy3(char *ps, char *t)


{
while((*ps++=*t++)!='\0');
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

void strcpy4(char *ps, char *t)


{
while(*ps++=*t++);
}
main()
{
char s[10], *t; long i;

t="hello";
strcpy4(s, t);printf("%s\n",s);
printf("main\n");
}
Bài số 15b so sánh hai xâu, strcmp.c
// strcmp1 bài tập so sánh hai xâu s và t, strcmp.c

#include <stdio.h>
#include <stdlib.h>

int strcmp1(char *s, char *t)


{
int i;

for(i=0; s[i]==t[i]; i++)


if(s[i]=='\0') return 0;
return s[i]-t[i];
}

int strcmp2(char *s, char *t)


{
for(;*s==*t;s++,t++)
if(*s=='\0') return 0;
return *s-*t;
}

main()
{
char *s, *t; long k;

t="hellox"; s="hello";
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

k=strcmp1(s,t); printf("k=%d\n",k);// trừ theo stt ở bảng mã ASCII


printf("main\n");
}
MẢNG CON TRỎ
Bài 16 chương trình sắp xếp mảng số nguyên theo thuật toán Hoare (mảng).
// qsort.c phiên bản sử dụng mảng
#include <stdio.h>
#include <stdlib.h>

void swap(int v[], int, int);


void qsort(int v[], int, int);
main()
{
int i; int v[10]={3,1,4,9,8,7,2,5,10,6};

qsort(v,0,9);
for(i=0;i<10;i++)printf("i,v %6d %6d\n",i,v[i]);

printf("main\n");
}
void swap(int v[], int i, int j)
{
int temp;
temp=v[i];
v[i]=v[j];
v[j]=temp;
}
void qsort(int v[], int left, int right)
{
int i, last;
if(left >= right)return;
swap(v, left, (left+right)/2);
last=left;
for(i=left+1; i<=right; i++)
if(v[i] < v[left])swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

Bài 16a chương trình sắp xếp mảng số nguyên theo thuật toán Hoare (mảng con
trỏ).
// qsort.c phiên bản sử dụng mảng con trỏ
#include <stdio.h>
#include <stdlib.h>

void swap(int *v[], int, int);


void qsort(int *v[], int, int);
main()
{
int i; int *v[10], v1[10]={3,1,4,9,8,7,2,5,10,6};

for(i=0; i<10; i++)v[i]=&v1[i];


qsort(v, 0, 9);
for(i=0; i<10; i++)printf("%6d%6d\n",i,*v[i]);

printf("main\n");
}
void swap(int *v[], int i, int j)
{
int *temp;

temp=v[i]; v[i]=v[j]; v[j]=temp;


}
void qsort(int *v[], int left, int right)
{
int i, last;

if(left >= right)return;


swap(v, left, (left+right)/2);
last=left;
for(i=left+1; i<= right; i++)
if(*v[i] < *v[left])swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
Bài 17 chương trình in ngày tháng, arraypointer.c
#include <stdio.h>
#include <stdlib.h>
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

//* truyền theo (*daytab[13] */


int day_of_year(int (*daytab)[13], int year, int month, int day)
{
int i, leap;
leap = year%4==0 && year%100!=0 || year%400==0;
for(i=1; i<month; i++)day+=daytab[leap][i];
return day;
}
/* truyền theo daytab[][13] */
void month_day(int daytab[][13], int year, int yearday, int *pmonth, int *pday)
{
int i,leap;
leap = year%4==0 && year%100!=0 || year%400==0;
for(i=1; yearday>daytab[leap][i]; i++)yearday-=daytab[leap][i];
*pmonth=i;
*pday=yearday;
}
main()
{
int year, month, day, day_year; int *p, i, j;
int daytab[][13]=
{
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31},
};

p=&daytab[0][0];
i=1; j=4; printf("%6d%6d\n", daytab[1][4], *(p+i*13+j));

year=2001;month=3;day=15;day_year=day_of_year(daytab,year,month,day);
printf("day_of_year %6d%6d%6d%6d\n",year,month,day,day_year);

year=2001;day_year=76;month_day(daytab,year,day_year,&month,&day);
printf("month_day %6d%6d%6d%6d\n",year,day_year,day,month,day);
}
Bài 17a chương trình tính thời gian của x4, time.c
//chương trình tính thời gian thực hiện tổng a4 bằng cách nhân trực tiếp hoặc sử dụng
hàm pow(a,4)
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

#include <time.h>
#include <stdio.h>
#include <math.h>
#define n_loop 1000000
main()
{
int i; double a = 11234567890123456.0, b;
clock_t time_1, time_2;
time_1 = clock(); //tính thời gian bắt đầu
for (i = 0; i < n_loop; i++) b = a * a * a * a;
time_2 = clock(); //tính thời gian kết thúc
printf ("cpu time needed to evaluate a*a*a*a: %f microsecs\n",(double)(time_2 -
time_1) /CLOCKS_PER_SEC);
time_1 = clock();
for (i = 0; i < n_loop; i++) b = pow(a, 4.);
time_2 = clock();
printf ("cpu time needed to evaluate pow(a, 4.): %f microsecs\n", (double) (time_2
- time_1) / (double)CLOCKS_PER_SEC);
}
SỐ NGẪU NHIÊN
Bài 18 chương trình tính trung bình và phương sai của 106 số ngẫu nhiên,
random.c
/* tính trung bình và sai phân của 106 số ngẫu nhiên [0, 1] */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define n_max 10000000
main()
{
int i, seed;
double sum_0, sum_1, mean, var, x;
seed = time(NULL);
srand(seed);
for (i = 1, sum_0 = 0., sum_1 = 0.; i <= n_max; i++)
{
x = (double) rand() / (double) RAND_MAX;
sum_0 += x;
sum_1 += (x - 0.5) * (x - 0.5);
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

mean = sum_0 / (double) n_max;


var = sum_1 / (double) n_max;
printf("mean(x) = %12.10f var(x) = %12.10f\n", mean, var);
}
CHƯƠNG TRÌNH CHỨA NHIỀU FILE.C
Bài 18a chương trình chứa hai file: main() và factorial().
/*chương trình tính giai thừa lưu trong file main.c và factorial.c*/

//file main.c
#include <stdio.h>
#include <stdlib.h>
void factorial(int, double *);
main()
{
int i; double fact;
for (i = 1; i <6; i++)
{
factorial(i, &fact);
printf("factorial(%d) = %12.3f\n", i, fact);
}
}
#include "factorial.c"

//taofactorial.c
void factorial(int n, double *fact)
{
int count;
for (count = n, *fact = 1; count > 0; --count) *fact*= (double) count;
}
CẤU TRÚC
Bài số 19 chương trình khảo sát dữ liệu kiểu cấu trúc, test_struct.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

void test_struct1()
{
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

struct point
{ double x; double y;};
struct point pt1={1.2,2.3}, pt2={2.5,3.7};
double rx, ry;

rx = pt1.x - pt2.x; ry = pt1.y - pt2.y;


printf("distance = %.2f\n", sqrt(rx*rx + ry*ry));
}
void test_struct2()
{
struct point { double x, y;};
struct rect
{ struct point pt1; struct point pt2; };
struct point pt;
struct rect scr;
scr.pt1.x=1.2; scr.pt1.y=1.5;
scr.pt2.x=3.2; scr.pt2.y=2.5;
printf("pt.x="); scanf("%lf", &pt.x);
printf("pt.y="); scanf("%lf", &pt.y);
if(pt.x>scr.pt1.x && pt.x<scr.pt2.x && pt.y>scr.pt1.y && pt.y<scr.pt2.y)
printf("the point is in\n");
else printf("the point is out\n");
}
main()
{
test_struct1(); test_struct2();
printf("main \n");
}
Bài số 20 chương trình khảo sát con trỏ đến cấu trúc test_pointer.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define min(a,b) (a<b ? a:b)


#define max(a,b) (a>b ? a:b)
struct point
{ double x, y;};
struct rect
{
struct point pt1; struct point pt2;
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

};

struct point addpoint(struct point, struct point);


void cononrect(struct rect, struct rect *);
void test_pointer();
main()
{
struct point pt1={2.1, 3}, pt2={1.1, 4.1}, p;
struct rect r, *r1, *rp=&r;

p=addpoint(pt1, pt2); printf("p.x=%.1f p.y=%.1f\n", p.x, p.y);


r.pt1=pt1; r.pt2=pt2;
printf("r.pt1.x=%.1f rp->pt1.x=%.1f\n",r.pt1.x, rp->pt1.x);
cononrect(r, r1); printf("r1->pt1.x=%.1f\n", r1->pt1.x);
test_pointer();
}
struct point addpoint(struct point p1, struct point p2)
{
p1.x+= p2.x;
p1.y+= p2.y;
return p1;
}
void cononrect(struct rect r, struct rect *temp)
{
temp->pt1.x=min(r.pt1.x, r.pt2.x);
temp->pt1.y=min(r.pt1.y, r.pt2.y);
temp->pt2.x=max(r.pt1.x, r.pt2.x);
temp->pt2.y=max(r.pt1.y, r.pt2.y);
}
void test_pointer()
{
struct point p = {1.2, 2.3}, *p1;
p1=&p;
printf("(*p1).x=%.1f (*p1).y=%.1f\n", (*p1).x, (*p1).y);
printf("p1->x=%.1f p1->y=%.1f\n", p1->x, p1->y);
}
LỆNH TYPEDEF
Bài số 21 chương trình tính ngày của năm bằng con trỏ cấu trúc, day_year.c
/*Chương trình tính ngày của năm, day_year.c */
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

#include <stdio.h>

int day_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,32};
typedef struct {int day; int month;} date;
int day_year(date *);
main()
{
date x; int a;
printf("day="); scanf("%d", &a); x.day=a;
printf("month="); scanf("%d",&a); x.month=a;
printf("%d/%d is %d-th day of year\n", x.day, x.month, day_year(&x));
}
int day_year(date *y)
{
int i, day=0;

for(i=1;i<y->month;i++)day+=day_month[i];
return day+y->day;
}

Bài số 22 chương trình dựng cây nhị phân tree.c


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define n 100

struct treenode
{
int numb, count;
struct treenode *left;
struct treenode *right;
};
typedef struct treenode tnode;
int x[n];
void read_file(int n)
{
int i; FILE *p;

p=fopen("tree.dat","r");
for(i=0; i<n; i++)fscanf(p,"%d",&x[i]);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

printf("read_file %d\n", x[n-1]);

fclose(p);
}
//************************************************************
**
tnode *talloc()
{
return (tnode*) malloc(sizeof(tnode));
}
tnode *addtree(tnode *p, int q)
{
if(p==NULL)
{
p=talloc();
p->numb=q; p->count=1;
p->left=p->right= NULL;
}
else if(q==p->numb) p->count++;
else if(q<p->numb) p->left=addtree(p->left, q);
else p->right=addtree(p->right, q);
return p;
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362

You might also like