C语言简单编程题,求大神请教
- 资格考试
- 2023-01-12 12:58:46
C语言编程题,求助大神
#include
#include
unsigned int CalcDayOfMonth(unsigned int year, unsigned int month);
bool IsLeapYear(unsigned int year);
struct Time
{
unsigned int year;
unsigned int month;
unsigned int day;
void setTime(unsigned int y = 1900, unsigned int m = 1, unsigned int d= 1)
{
year = y; month = m;
if (d > CalcDayOfMonth(year, month))
day = CalcDayOfMonth(year, month);
else
day = d;
}
Time operator-(Time& t)
{
Time tmp;
if (t > * this)
{
tmp = t;
t = *this;
*this = tmp;
}
else
tmp = *this;
if (tmp.day >= t.day)
{
tmp.day -= t.day;
}
else
{
tmp.day = CalcDayOfMonth(tmp.year, tmp.month-1) + tmp.day-t.day;
tmp.month--;
}
if (tmp.month >= t.month)
tmp.month -= t.month;
else
{
tmp.month = tmp.month + 12 - t.month;
tmp.year--;
}
tmp.year -= t.year;
Time tmp1 = t;
int d=0;
while (!(*this==tmp1))
{
tmp1.day++;
d++;
if (CalcDayOfMonth(tmp1.year, tmp1.month) < tmp1.day)
{
tmp1.day = 1;
tmp1.month++;
}
if (tmp1.month > 12)
{
tmp1.month = 1;
tmp1.year++;
}
}
printf("累计相差:%d天\n", d);
return tmp;
}
bool operator==(Time& t)
{
bool b=this->day == t.day && this->month == t.month && this->year == t.year;
return b;
}
bool operator>(Time& t)
{
if (this->year > t.year)
return true;
else if (this->year == t.year)
{
if (this->month > t.month)
return true;
else if (this->month == t.month)
{
if (this->day > t.day)
return true;
else
return false;
}
else
return false;
}
else
return false;
}
};
void show(Time t)
{
printf_s("%d年-%u月-%u日\n", t.year, t.month, t.day);
}
bool IsLeapYear(unsigned int year)
{
if ((year % 400 == 0) || (year % 100 != 0) && (year % 4 == 0))
return true;
return false;
}
unsigned int CalcDayOfMonth(unsigned int year, unsigned int month)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
return IsLeapYear(year) ? 29 : 28;
}
return 0;//返回0则表示月份输入不正确
}
int main()
{
Time t1, t2;
printf_s("请输入两个时间(格式:年 月 日):\n");
scanf_s("%u %u %u", &t1.year, &t1.month, &t1.day);
scanf_s("%u %u %u", &t2.year, &t2.month, &t2.day);
show(t1 - t2);
}
几道简单的C语言编程题,请高手帮忙
1、求1-3+5-7+……-99+101的值。 #includeC语言简单编程题,由提示编写程序,谢谢大神的帮助!!十分感激!!
#include
int max(int a[], int n) {
int i = 0,m;
m = a[0];
for(i = 1;i < n;i++)
if(a[i] > m) m = a[i];
return m;
}
void show(int a[],int n) {
int i;
for(i = 0; i < n; ++i)
printf("%d ",a[i]);
printf("\n");
}
int main() {
int a[] = {21,23,10,65,8,7,90,11,49,60,33,54,22,91,39,87,66,58,39,80};
int n = sizeof(a)/sizeof(a[0]);
printf("数组为:\n");
show(a,n);
printf("最大元素为:%d\n",max(a,n));
return 0;
}
C语言编程题求大神教!
有什么C语言的编程题,请讲。不说出来,是不能帮到你的。简单的C语言程序编写问题!求大神解答!!!!万分感谢
for循环判断是否存在数组中元素与查找数字相等,存在输出Yes退出循环。如果遍历完数组i=10表示未找到输出No
#include "stdafx.h"
#include
using namespace std;
int main()
{
int num[10],x,i;
cout << "请输入10个整数:" << endl;
for (i = 0; i < 10; i++)
cin >> num[i];
cout << "请输入要查找的数:" << endl;
cin >> x;
for (i = 0; i < 10; i++)
{
if (x == num[i])
{
cout << "Yes" << endl;
break;
}
}
if (x == 10)
cout << "No" << endl;
system("pause");
return 0;
}