如何将以下C ++代码转换为C在线,因为我不了解C ++? [英] How do I convert the following C++ code to C online as I have no knowledge of C++?

查看:83
本文介绍了如何将以下C ++代码转换为C在线,因为我不了解C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将此代码转换为C,因为我不懂C语言语法。



我尝试了什么: < br $> b $ b

Convert this code to C as I don't have a knowledge of C language syntax.

What I have tried:

#include <iostream>
#include <string.h>
#include <vector>
#include <ctime>
#include<stdlib.h>

const int MAX_RESP = 3;

typedef std::vector<std::string> vstring;

vstring find_match(std::string input);
void copy(char *array[], vstring &v);


typedef struct {
    char *input;
    char *responses[MAX_RESP];
}record;

record KnowledgeBase[] = {
    {"WHAT IS YOUR NAME",
    {"MY NAME IS CHATTERBOT2.",
     "YOU CAN CALL ME CHATTERBOT2.",
     "WHY DO YOU WANT TO KNOW MY NAME?"}
    },

    {"HI",
    {"HI THERE!",
     "HOW ARE YOU?",
     "HI!"}
    },

    {"HOW ARE YOU",
    {"I'M DOING FINE!",
    "I'M DOING WELL AND YOU?",
    "WHY DO YOU WANT TO KNOW HOW AM I DOING?"}
    },

    {"WHO ARE YOU",
    {"I'M AN A.I PROGRAM.",
     "I THINK THAT YOU KNOW WHO I'M.",
     "WHY ARE YOU ASKING?"}
    },

    {"ARE YOU INTELLIGENT",
    {"YES,OFCORSE.",
     "WHAT DO YOU THINK?",
     "ACTUALY,I'M VERY INTELLIGENT!"}
    },

    {"ARE YOU REAL",
    {"DOES THAT QUESTION REALLY MATERS TO YOU?",
     "WHAT DO YOU MEAN BY THAT?",
     "I'M AS REAL AS I CAN BE."}
    }
};

size_t nKnowledgeBaseSize = sizeof(KnowledgeBase)/sizeof(KnowledgeBase[0]);


int main() {
    srand((unsigned) time(NULL));

    std::string sInput = "";
    std::string sResponse = "";

    while(1) {
        std::cout << ">";
        std::getline(std::cin, sInput);
        vstring responses = find_match(sInput);
        if(sInput == "BYE") {
            std::cout << "IT WAS NICE TALKING TO YOU USER, SEE YOU NEXTTIME!" << std::endl;
            break;
        }
        else if(responses.size() == 0)  {
            std::cout << "I'M NOT SURE IF I  UNDERSTAND WHAT YOU  ARE TALKING ABOUT." << std::endl;
        }
        else {
            int nSelection = rand()  % MAX_RESP;
            sResponse =   responses[nSelection]; std::cout << sResponse << std::endl;
        }
    }

    return 0;
}

// make a  search for the  user's input
// inside the database of the program
vstring find_match(std::string  input) {
    vstring result;
    for(int i = 0; i < nKnowledgeBaseSize;  ++i) {
        if(std::string(KnowledgeBase[i].input) == input) {
            copy(KnowledgeBase[i].responses, result);
            return result;
        }
    }
    return result;
}

void copy(char  *array[], vstring &v) {
    for(int i = 0;  i < MAX_RESP; ++i) {
        v.push_back(array[i]);
    }
}

推荐答案

Quote:

将此代码转换为C,因为我不了解C语言语法。

Convert this code to C as I don't have a knowledge of C language syntax.

没有。我们不是代码转换服务,我们不是来做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但我们不打算为你做这一切!



但是我会免费给你一些建议:一个好的C ++解决方案似乎符合你的要求作业问题不会产生好的C解决方案。 C ++是C的超集,并且在非常不同的基础上工作。这就像为你的菲亚特500购买Mustang引擎并期望它插入并制造一辆好车......

No. We are not a code conversion service, and we aren't here to do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

But I'll give you a little advice for free: a good C++ solution that seems to meet your homework question will not produce a good C solution. C++ is a superset of C, and works on a very different basis. It's like buying a Mustang engine for your Fiat 500 and expecting it to slot right in and make a good car ...


这不是语法问题。你不能使用标准c ++库中的任何东西。你必须使用标准C库中的东西,比如printf和strcmp。您还必须使用原始文本缓冲区和指针。还有一件事 - 文本字符串应声明为const char *。
This is not a question of syntax really. You just can't use anything from the standard c++ library. You have to use things from the standard C library like printf and strcmp. You also have to use raw text buffers and pointers. One more thing - the text strings should be declared as "const char *".


这篇关于如何将以下C ++代码转换为C在线,因为我不了解C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆