将算法转换为C ++ [英] Convert algorithm to C++

查看:86
本文介绍了将算法转换为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写程序以输入字符串中的消息然后加密消息。



输出输入消息 
输入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 ++

解决方案

引用:

最近启动了c ++,经历了基础。赋值是将它转换为我已经完成的python代码,但我想用c ++进行实验。我是新手,所以我只能做输入和输出但是卡在循环中不知道如何写它



即使你的Python代码也缺少 +3 part。



但是在C / C ++中它更简单。如果你有C字符串( char 数组)或 std :: string 你可以通过索引/ <访问数组code> [] 运算符:

 input [i] + =  3 ; 

请注意,上面的代码片段假设输出已使用输入字符串的副本进行初始化。



或者使用基于范围的循环(自C ++ 11开始) - cppreference.com [ ^ ]:

< pre lang =c ++> // 输入类型为std :: string
std ::字符串输出;
for auto c:input)
output + = c + 3 ;


参见 C-C ++语言和标准库 [ ^ ]


A program is to be written to input a a message in string and then encrypt the message.

output "Enter message"
Input MyMessage
EncryptString= ""

For i- 1 to CHARACTERCOUNT (mymessage)
NextNum - ASC(ONECHAR(MYMessage,i) + 3)
EncryptString - EncryptString & CHR(NextNum)
Endfor
Output encryptstring

ONECHAR("Barcelona", 3) returns 'r'
CHARACTERCOUNT ("South Africa") returns 12



What I have tried:

Please help me write this algorithm in C++

Mystring=Input ('key in string')

Stringtotal=0

for i in range (0,len(Mystring)):

NextNum = ord( Mystring[i])

StringTotal= StringTotal + NextNum

Print(MyString, stringTotal)



Have tried this in python help me convert it to C++

解决方案

Quote:

Have recently started c++ , gone through basic. Assignment was to convert it into python code which I have done but I want to experiment it in c++. I am quite new to it, so I could only do input and output but got stuck in the loop no idea how to write it


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::string
std::string output;
for (auto c : input)
    output += c + 3;


See C-C++ Language and Standard Libraries[^]


这篇关于将算法转换为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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