2020.12.30数组间数据转换

任意从键盘输入10个整数存入数组a中,从数组a的第二个元素起,
分别将后项乘以前项之积存入数组b中,并输出数组b的内容。
** 要求输入提示信息为:无输入提示信息
** 要求输入格式要求为:"%d"
*要求输出格式要求为:"%3d"

#include <stdio.h>
int main(void)
{
	int i, j;//i用来表示a数组下标,j用来表示b数组下表,
	//两个数组是不可共用一个变量来存放下标的;
	int a[10];
	int b[9];
	for (i = 0; i <= 9; i++)
	{//输入a数组数据;
		scanf_s("%d", &a[i]);//在vc中直接输入scanf会编译错误,
		//scanf在DevC++适用,scanf_s对输入结果不会产生影响。
	}
	j = 0;//规定b数组下标从0开始;
	for (i = 0; i <= 8; i++)
	{
		b[j] = a[i] * a[i + 1];
		printf("%3d", b[j]);
		j++;
	}
	printf("\n");
}

结果:在这里插入图片描述
代码块内备注详尽矣。
peace.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Date.h文件内容如下: ```c++ #ifndef DATE_H #define DATE_H class Date { public: Date(int y, int m, int d); // 构造函数 void addDays(int days); // 加上days天 void subDays(int days); // 减去days天 int diffDays(const Date& other) const; // 计算与另一个日期之相差的天数 void forwardDays(int days); // 距离当前日期向前days天的日期 void backwardDays(int days); // 距离当前日期向后days天的日期 void printDate() const; // 输出日期 private: int year, month, day; // 年、月、日 }; #endif // DATE_H ``` Date.cpp文件内容如下: ```c++ #include "Date.h" #include <iostream> // 数组存放每月天数,0为非闰年,1为闰年 int day_month[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31}, {31,29,31,30,31,30,31,31,30,31,30,31}}; // 是否为闰年 bool isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 计算给定日期距离2000年1月1日的天数 int calcDays(int year, int month, int day) { int days = 0; for (int i = 2000; i < year; i++) { days += isLeapYear(i) ? 366 : 365; } int leap = isLeapYear(year) ? 1 : 0; for (int i = 0; i < month - 1; i++) { days += day_month[leap][i]; } days += day; return days; } // 构造函数 Date::Date(int y, int m, int d) { year = y; month = m; day = d; } // 加上days天 void Date::addDays(int days) { int total_days = calcDays(year, month, day) + days; int y = 2000, m = 1, d = 1; while (total_days > (isLeapYear(y) ? 366 : 365)) { total_days -= isLeapYear(y) ? 366 : 365; y++; } int leap = isLeapYear(y) ? 1 : 0; while (total_days > day_month[leap][m - 1]) { total_days -= day_month[leap][m - 1]; m++; } d = total_days; year = y; month = m; day = d; } // 减去days天 void Date::subDays(int days) { int total_days = calcDays(year, month, day) - days; int y = 2000, m = 1, d = 1; while (total_days > (isLeapYear(y) ? 366 : 365)) { total_days -= isLeapYear(y) ? 366 : 365; y++; } int leap = isLeapYear(y) ? 1 : 0; while (total_days > day_month[leap][m - 1]) { total_days -= day_month[leap][m - 1]; m++; } d = total_days; year = y; month = m; day = d; } // 计算与另一个日期之相差的天数 int Date::diffDays(const Date& other) const { int days1 = calcDays(year, month, day); int days2 = calcDays(other.year, other.month, other.day); return days1 - days2; } // 距离当前日期向前days天的日期 void Date::forwardDays(int days) { subDays(days); } // 距离当前日期向后days天的日期 void Date::backwardDays(int days) { addDays(days); } // 输出日期 void Date::printDate() const { std::cout << year << "." << month << "." << day << std::endl; } ``` main.cpp文件内容如下: ```c++ #include "Date.h" #include <iostream> int main() { // 测试数据 Date now(2023, 6, 1); Date other(2020, 10, 1); // 相差天数 std::cout << "现在是:"; now.printDate(); std::cout << "另一天是:"; other.printDate(); std::cout << "相差天数:" << now.diffDays(other) << std::endl; // 距离当前日期向前100天的日期 std::cout << "距现在向前100天是:"; now.forwardDays(100); now.printDate(); // 距离当前日期向后100天的日期 std::cout << "距现在向后100天是:"; now.backwardDays(200); now.printDate(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值