【问题标题】:How to turn Python code into C++ [closed]如何将 Python 代码转换为 C++ [关闭]
【发布时间】:2014-02-08 19:48:45
【问题描述】:

我应该如何编写与 C++ 相同的代码?我觉得这很困难..

### X,Y and Z will all be integers (numbers). The user will input a number 3 three times   thus determining the type of triangle###

x = int(input("Input your first length:  "))
y = int(input("Input your second length: "))
z = int(input("Input your third length: "))

### "=" is an equality operater. If the side of x, y and x are all equal the triangle will be equilateral.###

if  (x == y == z):
    print ("The program recognises this as an Equilateral triangle.")

【问题讨论】:

  • 在 C++ 中不要“转换”而是“编写”一些新代码……当然你需要阅读一本好的 C++ 编程书籍。
  • Python 代码几乎不是算法;您从用户那里获取 3 个整数并测试它们是否都相等。你可以用一句话来描述。
  • 在 haskell 中创建一个 python 解析器,它会生成一些 C++ 代码。 (jk)

标签: python c++ python-3.2


【解决方案1】:

我不会为你写代码,但我会给你一些指导:

  1. 用 C++ 编写“Hello World”教程
  2. 使用 std::cin 读取每个长度(参见标题iostream
  3. 查找等效运算符(即相等和逻辑与)和条件语句

这是您将获得的最大帮助,除了为您编写代码(这对您的学习没有帮助)。

【讨论】:

    【解决方案2】:
    #include <iostream>
    
    int main()
    {
        int x, y, z;
        std::cin >> x;
        std::cin >> y;
        std::cin >> z;
    
        if (x == y && x == z)
            std::cout << "equilateral" << std::endl;
    }
    

    请注意,在 C++ 中没有具有三个操作数的 == 运算符。 (x == y) == z 会将 x 与 y 进行比较并返回真或假。然后它会将 true/false 与 z 进行比较,这不是您想要的。

    【讨论】:

      猜你喜欢
      • 2010-10-18
      • 1970-01-01
      • 2010-10-01
      • 2023-03-09
      • 1970-01-01
      • 2018-05-13
      • 2011-05-06
      • 1970-01-01
      • 2011-03-18
      相关资源
      最近更新 更多