You are on page 1of 3

File: /home/cntt/btshell/chuongtrinhchinh.

cpp Page 1 of 3

#include <stdio.h>//Thu vien xuat nhapchuan//


#include <unistd.h>//Khai bao su dung Pipe va mot so hamcua Linux//
#include <stdlib.h>//Cac ham chuan cua Linux//
#include <sys/types.h>//
#include <math.h>//Thu vien toan hoc//
#include <string.h>//Khai bao su dung chuoi//
double a[10],tam,m[10],kq;
short int i,j,n,k;
char str[10],str1[10];
char ch,gan;
FILE *f,*f1;
double xuly(int i)//Ham de xu ly so lieu trong file
{ tam=0;n =i;
for(j=0;j<=(i-1);j++)
{ tam=tam+(a[j]*(pow(10,(n-1))));
n--;
}
return (tam);
}
void read_file(int input_pipe[], int output_pipe[])//Ham dieu khien tien trinh cha doc du lieu//
{ int rc;
close(input_pipe[1]);//Dong chuc nang ghi cua tien trinh cha khi gap Pipe doc//
close(output_pipe[0]);//Dong chuc nang doc cua tien trinh doc khi gap Pipe ghi//
f=fopen("/home/tranhoang/hoang.txt","r");//Mo file va doc
i=0;k=0;
if(f==NULL)
{printf("\n");
printf("\n\n\n\n\n\n\n\n\n\n\n");
printf(" DO AN NGUYEN LY HE DIEU HANH \n\n");
printf(" UNG DUNG CUA PIPE TRINH GIAO TIEP \n\n");
printf(" GIAO VIEN HUONG DAN : THAY NGUYEN VAN NGUYEN \n\n");
printf(" SINH VIEN THUC HIEN : TRAN MINH HOANG\n\n\n");
printf(" Do muc dich cua chuong trinh ban phai tao file can sua truoc
\n ");
printf(" FILE DO TEN LA hoang.txt DAT CUNG VOI CHUONGTRINHCHINH.CPP
\n");
printf("\n");
exit(1);
}
else {printf("\n\n\n\n\n\n\n\n\n\n\n");
printf(" DO AN NGUYEN LY HE DIEU HANH \n\n");
printf(" UNG DUNG CUA PIPE TRINH GIAO TIEP \n\n");
printf(" GIAO VIEN HUONG DAN : THAY NGUYEN VAN NGUYEN \n\n");
printf(" SINH VIEN THUC HIEN : TRAN MINH HOANG\n\n\n");
printf("
>>>>>>>>>>>>>>>>>>>>>>>>0000000000000000<<<<<<<<<<<<<<<<<<<<<<< \n\n");
printf(" CHUONG TRINH DA CHAY XONG DA IN RA KET QUA BAN HAY MO
HOANG.TXT XEM KQ\n\n");
printf(" >>>>>>>>>>CHUC MOI NGUOI MOT NGAY VUI VE<<<<<<<<<\n
\n");
printf(" >>>>>>>>>>>>>>>HAPPY NEW YEAR>>>>>>>>>>>>>\n\n\n\n
\n\n\n");
}
while((ch=fgetc(f))!=EOF)//Ghi trong khi chua gap ky tu ket thuc file//
{ rc= write(output_pipe[1],&ch,1);
if(rc==-1)
{
perror("pipe loi ghi");//Thong bao loi ghi//
close(input_pipe[0]);
close(output_pipe[1]);
exit(1);
}
f1=fopen("/home/tranhoang/hoang.txt","a");//Mo file de ghi ket qua//
rc=read(input_pipe[0],str,sizeof(str));
if(rc<=0)
{
perror("doc va da ghi vao file");//Thong bao loi doc//
File: /home/cntt/btshell/chuongtrinhchinh.cpp Page 2 of 3

perror("\n");

close(input_pipe[0]);
close(output_pipe[1]);
exit(1);
}
fputs(str,f1);
fflush(f1);
}
fclose(f);
fclose(f1);
close(input_pipe[0]);
close(output_pipe[1]);
exit(0);
}
void operator_result(int input_pipe[], int output_pipe[])
{ int rc;
close(input_pipe[1]);//Dong chuc nang ghi cua tien trinh con khi gap Pipe doc//
close(output_pipe[0]);//Dong chuc nang doc cua tien trinh con khi gap Pipe ghi//
while ((read(input_pipe[0],&ch,1)!=EOF))//Doc khi chua gap ky tu ket thuc File//
{ if(ch!=10)//Neu chua xuong dong//
{if((ch!='+')&&(ch!='-')&&(ch!='*')&&(ch!='/'))
{ strcpy(str," ");
a[i] = double (ch);
a[i] = (a[i] - 48);
gcvt(a[i],4,str);
i++;
n=i;
}

else
{
strcpy(str," ");
gan = ch;
strcpy(str,&gan);
m[k]=xuly(i);
i=0;
k++;
}
}
else //Neu gap ky tu sang dong//
{ strcpy(str,"=");
strcpy(str1," ");
m[k]=xuly(i);
switch(gan)
{ case '+':
{ strcpy(str,"=");
kq=m[k-1]+m[k];
gcvt(kq,5,str1);
strcat(str,str1);
strcat(str,"\n");
break;
}
case '-':
{ strcpy(str,"=");
kq=m[k-1]-m[k];
gcvt(kq,5,str1);
strcat(str,str1);
strcat(str,"\n");
break;
}
case '*':
{ strcpy(str,"=");
kq=m[k-1]*m[k];
gcvt(kq,5,str1);
strcat(str,str1);
strcat(str,"\n");
File: /home/cntt/btshell/chuongtrinhchinh.cpp Page 3 of 3

break;
}
case '/' :
{ strcpy(str,"=");
kq=m[k-1]/m[k];
gcvt(kq,5,str1);
strcat(str,str1);
strcat(str,"\n");
break;
}
}
k=0;
i=0;
}

rc = write(output_pipe[1],str,sizeof(str));//Tien trinh con doc cac xau ket qua vao duong
ong//
if (rc==-1)
{
perror("operator_result: Write");
close(input_pipe[0]);
close(output_pipe[1]);
exit(1);
}
}

close(input_pipe[0]);
close(output_pipe[1]);
exit(0);
}
int main()
{ int chatocon[2];
int contocha[2];
int pic;
int rc;

rc=pipe(chatocon);
if(rc==-1)
{ perror("main: chatoicon error");
exit(1);
}

rc=pipe(contocha);
if(rc==-1)
{ perror("main: contoicha error");
exit(1);
}

pic=fork();
switch(pic)
{case -1:
perror("main: hien loi");
exit(1);
case 0:
{
operator_result(contocha,chatocon);
}
default:
read_file(chatocon,contocha);
}
return 0;
}

You might also like