double h, dataX[100], dataY[100];
} my_data_t;
my_data_t msg_out, msg;
void Inter( ){
int i=0, j=0;
double Xh;
msg_out.maxX = msg.maxX;
msg_out.maxY = msg.maxY;
msg_out.h = msg.h;
Xh = msg.dataX[0];
msg_out.hdr.type = 0x00;
msg_out.hdr.subtype = 0x00;
while( i < msg.len ){
while( msg.dataX[i] <= Xh && Xh < msg.dataX[i+1] ){
msg_out.dataY[j] = (double)( msg.dataY[i]*(msg.dataX[i+1]-Xh) + msg.dataY[i+1]*(Xh-msg.dataX[i]) ) / (double)( msg.dataX[i+1]-msg.dataX[i] );
msg_out.dataX[j] = Xh;
j++;
printf("%d %0.3f %0.3f \n", j, msg_out.dataY[j-1], Xh );
Xh += msg.h;
if( j == 99 ) break;
}
i++;
if( j == 99 ) break;
}
msg_out.dataX[j] = Xh;
msg_out.dataY[j] = msg.dataY[i-1];
j++;
printf("%d %0.3f %0.3f \n", j, msg_out.dataY[j-1], Xh );
msg_out.len = j;
}
int main( int argc, char** argv ){
name_attach_t *attach;
int rcvid, i;
double data;
if( argc > 1 && !strcmp( argv[1], "global") ){
if ((attach = name_attach(NULL, ATTACH_POINT, NAME_FLAG_ATTACH_GLOBAL)) == NULL) {
printf("GLOBAL Name Attach Faild!!! \n");
return EXIT_FAILURE;
}
printf("GLOBAL channel \n");
}else{
if ((attach = name_attach(NULL, ATTACH_POINT, 0)) == NULL) {
printf("GLOBAL Name Attach Faild!!! \n");
return EXIT_FAILURE;
}
printf("LOCAL channel \n");
}
while (1) {
rcvid = MsgReceive(attach->chid, &msg, sizeof(msg), NULL);
if (rcvid == 0) {
/* Pulse received */
switch (msg.hdr.code) {
case _PULSE_CODE_DISCONNECT:
printf("Get pulse Disconnect\n");
ConnectDetach(msg.hdr.scoid);
break;
case _PULSE_CODE_UNBLOCK:
printf("Get pulse UNLBOCK\n");
break;
}
continue;
}
/* name_open() sends a connect message, must EOK this */
if (msg.hdr.type == _IO_CONNECT ) {
printf("Get Message IO_CONNECT\n");
MsgReply( rcvid, EOK, NULL, 0 );
continue;
}
printf("Server receive %d \n", msg.len);
printf("h = %f \n", msg.h);
for( i=0; i<msg.len; i++ )
printf( "X[%d] = %0.3f Y[%d] = %0.3f \n", i, msg.dataX[i], i, msg.dataY[i] );
Inter();
MsgReply(rcvid, 0, &msg_out, sizeof( msg_out ) );
}
/* Remove the name from the space */
name_detach(attach, 0);
return EXIT_SUCCESS;
}
2. Тестирование
Формат входного файла:
N maxX maxY
x(0) y(0)
x(1) y(1)
…
x(N-1) y(N-1)
2.1. Тест1
test1.in
6 5 89
0 4
1 1
2 17
3 89
4 26
5 55
Клиент :
Рис.2.1. Результат выполнения теста 1
Сервер:
./inter LOCAL channel
Server receive 6
h = 0.500000
X[0] = 0.000 Y[0] = 4.000
X[1] = 1.000 Y[1] = 1.000
X[2] = 2.000 Y[2] = 17.000
X[3] = 3.000 Y[3] = 89.000
X[4] = 4.000 Y[4] = 26.000
X[5] = 5.000 Y[5] = 55.000
1 4.000 0.000
2 2.500 0.500
3 1.000 1.000
4 9.000 1.500
5 17.000 2.000
6 53.000 2.500
7 89.000 3.000
8 57.500 3.500
9 26.000 4.000
10 40.500 4.500
11 55.000 5.000
Get pulse Disconnect
2.2. Тест 2
Test2.in
10 9 50
0 50
1 40
2 30
3 20
4 10
5 0
6 10
7 20
8 30
9 40
Клиент:
Рис.2.2. Результат выполнения теста 2
Сервер:
./inter LOCAL channel
Server receive 10
h = 0.500000
X[0] = 0.000 Y[0] = 50.000
X[1] = 1.000 Y[1] = 40.000
X[2] = 2.000 Y[2] = 30.000
X[3] = 3.000 Y[3] = 20.000
X[4] = 4.000 Y[4] = 10.000
X[5] = 5.000 Y[5] = 0.000
X[6] = 6.000 Y[6] = 10.000
X[7] = 7.000 Y[7] = 20.000
X[8] = 8.000 Y[8] = 30.000
X[9] = 9.000 Y[9] = 40.000
1 50.000 0.000
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.