本文介绍了将算法转换为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 编写程序以输入字符串中的消息然后加密消息。 输出输入消息 输入MyMessage EncryptString = i-1到CHARACTERCOUNT(mymessage) NextNum - ASC(ONECHAR(MYMessage,i)+ 3) EncryptString - EncryptString& CHR(NextNum) Endfor 输出encryptstring ONECHAR(Barcelona,3)返回'r' CHARACTERCOUNT(南非)返回12 我的尝试: 请帮我用C ++编写这个算法 Mystring =输入('key in string') Stringtotal = 0 in i in range(0,len(Mystring)): NextNum = ord(Mystring [i]) StringTotal = StringTotal + NextNum 打印(MyString,stringTotal) 在python中试过这个帮我转换为C ++ 解决方案 即使你的Python代码也缺少 +3 part。 但是在C / C ++中它更简单。如果你有C字符串( char 数组)或 std :: string 你可以通过索引/ [] 运算符: input [i] + = 3 ; 请注意,上面的代码片段假设输出已使用输入字符串的副本进行初始化。 或者使用基于范围的循环(自C ++ 11开始) - cppreference.com [ ^ ]: // 输入类型为std :: string std ::字符串输出; for ( auto c:input) output + = c + 3 ; A program is to be written to input a a message in string and then encrypt the message.output "Enter message"Input MyMessageEncryptString= ""For i- 1 to CHARACTERCOUNT (mymessage)NextNum - ASC(ONECHAR(MYMessage,i) + 3)EncryptString - EncryptString & CHR(NextNum)EndforOutput encryptstringONECHAR("Barcelona", 3) returns 'r'CHARACTERCOUNT ("South Africa") returns 12What I have tried:Please help me write this algorithm in C++Mystring=Input ('key in string')Stringtotal=0for i in range (0,len(Mystring)):NextNum = ord( Mystring[i])StringTotal= StringTotal + NextNumPrint(MyString, stringTotal)Have tried this in python help me convert it to C++ 解决方案Even your Python code is missing the +3 part.But in C/C++ it is much simpler. If you have C string (char array) or a std::string you can access the array by index / [] operator:input[i] += 3;Note that the above code snippet assumes that output has been initialised with a copy of the input string.Alternatively use a Range-based for loop (since C++11) - cppreference.com[^] :// input is of type std::stringstd::string output;for (auto c : input) output += c + 3; 这篇关于将算法转换为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 08:46