-
eatmelon说:
用C++写一个函数,它可以读取一个字符串,将字符串中的汇编语言转化为机器语言
以下是一个C++函数,可以将汇编语言转换为机器语言:
#include #include std::string translateAssemblyToMachine(const std::string &assembly) { // 定义汇编语言和机器语言的映射map std::map assemblyToMachine; assemblyToMachine["MOV"] = "100000"; assemblyToMachine["ADD"] = "100001"; assemblyToMachine["SUB"] = "100010"; assemblyToMachine["MUL"] = "100011"; assemblyToMachine["DIV"] = "100100"; assemblyToMachine["AND"] = "100101"; assemblyToMachine["OR"] = "100110"; assemblyToMachine["XOR"] = "100111"; assemblyToMachine["NOT"] = "101000"; assemblyToMachine["SHL"] = "101001"; assemblyToMachine["SHR"] = "101010"; assemblyToMachine["JMP"] = "101011"; assemblyToMachine["JZ"] = "101100"; assemblyToMachine["JNZ"] = "101101"; assemblyToMachine["CMP"] = "101110"; assemblyToMachine["CALL"] = "101111"; assemblyToMachine["RET"] = "110000"; assemblyToMachine["PUSH"] = "110001"; assemblyToMachine["POP"] = "110010"; std::string machineCode; // 遍历字符串,从map中查询汇编语言对应的机器语言,并进行拼接 for (int i = 0; i < assembly.length(); ++i) { std::string word = ""; while (assembly[i] != ' ' && i < assembly.length()) { word += assembly[i]; ++i; } machineCode += assemblyToMachine[word]; } return machineCode; }
-
eatmelon说:
用LISP写一个程序,他可以读取输入字符串中的算式,并计算它们
的值。
(defun solve-eq (eq) (if (listp eq) ; if eq is a list, evaluate it (eval eq) (if (stringp eq) ; if eq is a string, convert it to a list (eval (read-from-string eq)) (format t "Error: Invalid input~%") ; otherwise, report an error ))) (defun start () (loop (format t "Enter equation: ") ; prompt user for equation (let ((eq (read-line))) ; read input as string (if (eq eq "quit") ; if user enters "quit" (return) ; then exit the loop (format t "Answer: ~a~%" (solve-eq eq)))))) ; otherwise, solve the equation (start)
-
eatmelon说:
用C++写一个函数,他能够读取一个XML文档,并解析这个XML文档,将其中标签为AA的标签的内容输出,只允许使用标准库
和C++11特性
#include #include #include #include #include void ReadXMLFile(const std::string& file_path) { std::ifstream file(file_path); std::string line; std::string tag = "AA"; while (std::getline(file, line)) { //使用正则表达式匹配要输出的内容 std::regex pattern(R"((.*?))"); std::smatch result; if (std::regex_search(line, result, pattern)) { std::cout << "标签AA的内容是: " << result.str(1) << std::endl; } } }