ユーザーは 2 つの数字と +、-、/、* のいずれかを入力すると、回答が返されます。問題は、入力したものが正しく表示されず、計算されていないことです。どうしてこれなの?私が得ているものを示すスクリーンショットを提供しました。これを修正するにはどうすればよいですか? http://imageshack.us/photo/my-images/404/mathfn.jpg/
#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <winsock.h>
// Function prototype
void StreamClient(char *szServer, short nPort);
// Helper macro for displaying errors
#define PRINTERROR(s) \
fprintf(stderr,"\n%s: %d\n", s, WSAGetLastError())
////////////////////////////////////////////////////////////
void main(int argc, char **argv)
{
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
int nRet;
short nPort;
//
// Check for the host and port arguments
//
if (argc != 3)
{
fprintf(stderr,"\nSyntax: TCPTimeClient ServerName PortNumber\n");
return;
}
nPort = atoi(argv[2]);
//
// Initialize WinSock and check the version
//
nRet = WSAStartup(wVersionRequested, &wsaData);
if (wsaData.wVersion != wVersionRequested)
{
fprintf(stderr,"\n Wrong version\n");
return;
}
//
// Go do all the stuff a datagram client does
//
StreamClient(argv[1], nPort);
//
// Release WinSock resources
//
WSACleanup();
printf("\nPlease enter a floating point number: ");
float number1;
scanf("%f", &number1);
printf("\nPlease enter a mathematical operator (+,-,*,/): ");
char mathOperator;
scanf("%f", &mathOperator);
printf("\nPlease enter a floating point number: ");
float number2;
scanf("%f", &number2);
printf("\nCalculating %f %f %f...", &number1, &mathOperator, &number2);
printf("\nPlease enter a floating point number: ");
float number3;
scanf("%f", &number2);
}
////////////////////////////////////////////////////////////
void StreamClient(char *szServer, short nPort)
{
int nRet; // return code
char szBuf[256]; // client buffer area
char szSvr[256]; // server name
LPHOSTENT lpHostEntry; // host data structure
SOCKET theSocket; // client socket
SOCKADDR_IN saClient; // socket address structure
//
// Get local machine name
//
nRet = gethostname(szSvr, sizeof(szSvr));
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("gethostname()");
return;
}
//
// Display an informational message
//
printf("Datagram Client [%s] sending to server [%s] on port %d...\n",
szSvr, szServer, nPort);
//
// Find the server
//
lpHostEntry = gethostbyname(szServer);
if (lpHostEntry == NULL)
{
PRINTERROR("gethostbyname()");
return;
}
//
// Create a TCP/IP datagram socket
//
theSocket = socket(AF_INET, // Address family
SOCK_STREAM, // Socket type
0); // Protocol
//
// Check for errors
//
if (theSocket == INVALID_SOCKET)
{
PRINTERROR("socket()");
return;
}
//
// Fill in the address structure of the server
//
saClient.sin_family = AF_INET;
saClient.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// ^ Client's address
saClient.sin_port = htons(nPort); // Port number from command line
//
// Connect to the server
//
nRet = connect(theSocket,
(struct sockaddr *)&saClient,
sizeof(saClient));
//
// Check for errors
//
if(nRet == SOCKET_ERROR)
{
PRINTERROR("Connect()");
return;
}
//
// Prepare some data to send to the server
//
sprintf(szBuf, "From the Client [%s]", szSvr);
//
// Send data to the server
//
nRet = send(theSocket, // Socket
szBuf, // Data buffer
(int)strlen(szBuf), // Length of data
0); // Flags
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("send()");
closesocket(theSocket);
return;
}
//
// Zero out the incoming data buffer
//
memset(szBuf, 0, sizeof(szBuf));
//
// Wait for the reply
//
nRet = recv(theSocket, // Socket
szBuf, // Receive buffer
sizeof(szBuf), // Length of receive buffer
0); // Flags
//
// Check for errors
//
if (nRet == SOCKET_ERROR)
{
PRINTERROR("recv()");
closesocket(theSocket);
return;
}
//
// Display the data that was received
//
printf("\n%s", szBuf);
//
// Close the socket
//
closesocket(theSocket);
return;
}