C++代码转C

三楼の郎 2021-03-03 01:12:42
#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;
}


这个是C++的吧,那位大哥有功夫的帮忙把上面那段代码转成C的,都二十来年没搞过C基本都忘光光了啊,C++是更不行,大体上知道是从输入流转输出的,有闲工夫的帮忙搭把手啊,先谢过了
...全文
146 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
三楼の郎 2021-03-03
  • 打赏
  • 举报
回复
引用 1 楼 赵4老师 的回复:
#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;
}
灰常感谢,昨晚上自己整了一晚上都不行
赵4老师 2021-03-03
  • 打赏
  • 举报
回复
#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;
}

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧