Professional Documents
Culture Documents
File: /home/cntt/btshell/chuongtrinhchinh - CPP Page 1 of 3: Double Short Int Char Char Double Int
File: /home/cntt/btshell/chuongtrinhchinh - CPP Page 1 of 3: Double Short Int Char Char Double Int
cpp Page 1 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;
}