64,666
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
int main(int argc, char* argv[])
{
std::cout.setf( std::ios_base::unitbuf ); //instead of "<< eof" and "flushall"
unsigned int a, c, i, t=0;
std::string inp;
bool bCommunicationEnds = false;
do {
inp="";
t=0;
// Sum the first 4 chars from stdin (the length of the message passed).
for (i = 0; i <= 3; i++)
{
// t += getchar();
t += std::cin.get();
}
// Loop getchar to pull in the message until we reach the total
// length provided.
for (i=0; i < t; i++)
{
//c = getchar();
c = std::cin.get();
//if(c == EOF)
if(c == 65)
{
bCommunicationEnds = true;
i = t;
}
else
{
inp += c;
}
}
if(!bCommunicationEnds)
{
//Collect the length of the message
unsigned int len = inp.length();
//unsigned int len = strJson.length();
//// We need to send the 4 btyes of length information
std::cout << char(((len>>0) & 0xFF))
<< char(((len>>8) & 0xFF))
<< char(((len>>16) & 0xFF))
<< char(((len>>24) & 0xFF));
//// Now we can output our message
std::cout << inp;
}
}while(!bCommunicationEnds);
return 0;
}
#include <stdio.h>
int main(int argc, char* argv[])
{
unsigned int a, c, i, t=0;
char inp[1024];//assume max len is 1024
int k=0;
int bCommunicationEnds = 0;
k=0;
do {
inp[k++]=0;
t=0;
// Sum the first 4 chars from stdin (the length of the message passed).
for (i = 0; i <= 3; i++)
{
t += getchar();
}
// Loop getchar to pull in the message until we reach the total
// length provided.
for (i=0; i < t; i++)
{
c = getchar();
//if(c == EOF)
if(c == 65)
{
bCommunicationEnds = 1;
i = t;
}
else
{
inp[k++] = c;
}
}
if(!bCommunicationEnds)
{
//Collect the length of the message
unsigned int len = k;
//unsigned int len = strJson.length();
//// We need to send the 4 btyes of length information
printf("%c%c%c%c",
char(((len>>0) & 0xFF)),
char(((len>>8) & 0xFF)),
char(((len>>16) & 0xFF)),
char(((len>>24) & 0xFF)));
//// Now we can output our message
for (i=0;i<k;i++) printf("%c",inp[i]);
}
}while(!bCommunicationEnds);
return 0;
}