繁体   English   中英

创建 C++ 程序

[英]Creating C++ Program

我正在上 C++ 课程,因为我刚刚开始我的大学第一年,它一直在摧毁我。 我已经尝试了几个小时来做​​功课,但无法找到解决方案。

我的任务是制作一个 C++ 程序,当给定的分钟数会告诉你年和日。

我们一直在课堂上使用 float 、 cout 和 cin 以及一些对我来说很陌生的 % 和 / 结构。 如果有人可以提供帮助,那就太好了,因为此时我已经失去了所有希望。

#include <iostream> 
using namespace std; 
float = minutes 
float = hours 
float = days 
float = years 
float = seconds 
int main() 
{ 
    using namespace std; 
    int days, years, minutes, hours, seconds; 
    cout << "Please Enter Minutes" << endl; 
    cin >> minutes; 
    days = input_minutes / 60 / 60 / 24; 
    hours = (input_minutes / 60 / 60) % 24; 
    minutes = (input_minutes / 60) % 60; 
    seconds = input_minutes % 60; 
    cout << days << " seconds = " << years << " years "; 
    cin.get(); 
    cin.get(); 
    return 0; 
}

我冒昧地查看了您在评论框中的代码;

第一件事:

声明一个变量来存储输入值或保存计算结果

           int days;   //<--- declaration of a int variable called days  

所以这行我不知道你想做什么但float = minutes float = hours float = days float = years float = seconds请不要这样做

第二件事:

Don't repeated `using namespace std` twice. Therefore remove it from the `int main` function.

第三:你的计算有点关闭,尝试用数学方法解决然后编码。

你的代码应该是这样的:(这不是答案)

#include <iostream> 
using namespace std; 

 int main() 
  { 

       int days, years, input_minutes, hours, seconds,minutes;
       cout << "Please Enter Minutes" << endl; 
        cin >> input_minutes; 

        days = input_minutes / 60 / 60 / 24; 
        hours = (input_minutes / 60 / 60) % 24; 
        minutes = (input_minutes / 60) % 60; 
        seconds = input_minutes % 60; 
        cout << days << " seconds = " << years << " years "; 

        system("Pause");
        return 0; 

  } 

我可以给你一些帮助,了解每一个的含义。 以下是非超级技术定义。

浮点数是可以有小数位的整数。

cout将输出<<旁边的值

cin将存储来自输入的值( cin >> x )将用户输入存储在 x 中。

%是模数字符。 它将返回两个数字相除后的余数。 3%2将返回 1。

/只是简单的,普通的,除法。

乔,我认为我们不应该为你做那份工作,这不完全是一个“技术”问题。 考虑到这一点,我会尝试给你一些想法。以获得一些额外的分数:

1 - 从命令行参数中获取用户输入“分钟数”,例如: int main(int argc, char *argv[]) { int num_mim = atoi(argv[1]); 2 - 取年数做int num_years = num_mins / (60 * 24 * 365); (不考虑闰年) 3 - 取天数做int num_days = num_mins % (60 * 24 * 365) / 60 / 24;

当然,如果您愿意,可以通过执行可以手动进行的乘法和除法来简化运算。

% 是模运算符,它为您提供 dvision 的余数,这里我们使用它来从年份 cound 中获取剩余的分钟数并以天数表示。

现在由您决定,寻找其他信息来源并整理您的作业。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM