根据用户输入生成年、月、周和日
Generating Year, Months, Weeks and Days from user input
我正在创建一个应用程序,该应用程序从用户输入生成年,月,周和天我已经尝试过了,但只有几年和几个月的工作例如,当我输入 30 天时,它说 1 个月、2 周和 2 天,而不仅仅是 1 个月谢谢
// Description: This program prompts the user for an integer, which will represents the total
////number of days; The program then will break it apart into years, months, weeks, and days ////(each one of these will have their own local variables), and once this is done, the outcome will be //displayed on the screen.
//Enter no.of days : 1234Years : 3Months : 4Weeks : 2Days : 5
#include <iostream>
#include <cmath>
using namespace std;
const int daysInWeek = 7;
const int days_in_month = 30;
const int days_in_year = 365;
const int days_in_days = 1;
int main()
{
//Local variables
int totalDays;
int years;
int months;
int weeks;
int days;
//program info/intro
cout << "My name is Dianan";
cout << "Program 1: Convert Number of Days to Years, Months, Weeks, and Days" << endl;
cout << "---------------------------------------------------------------- ----- n";
//get numbers and develop math progress
cout << "Enter the total number of days : ";
cin >> totalDays;
years = totalDays / days_in_year;
months = (totalDays % days_in_year) / days_in_month;
weeks = (days_in_month % daysInWeek);
/*weeks = (totalDays%days_in_year) / daysInWeek;*/
days = (totalDays% days_in_year) % daysInWeek;
// Display it in the screen
cout << " " <<
cout << "Years = " << years <<
cout << "Months = " << months << endl;
cout << "Weeks = " << weeks << endl;
cout << "Days = " << days << endl;
system("pause");
return 0;
}
正如已经建议的那样,更好的方法是跟踪剩余的日子:
years = totalDays / days_in_year;
totalDays %= days_in_year;
months = totalDays / days_in_month;
totalDays %= days_in_month;
weeks = totalDays / days_in_week;
totalDays %= days_in_week;
days = totalDays;
相关文章:
- C++如何通过用户输入删除列表元素
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 打印时有二叉树问题.用户输入不打印任何内容
- C ++奇怪的数组由用户输入
- 如何允许大写和小写用户输入?
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 如何使用用户输入变量制作二维数组?
- 用户输入字符串的文件附加问题..C++
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 如何使用递归循环我的代码(当用户输入无效输入时,它会再次提示他们)?