C語(yǔ)言簡(jiǎn)單編程題,求大神請(qǐng)教
- 資格考試
- 2023-01-12 12:58:46
C語(yǔ)言編程題,求助大神
#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("累計(jì)相差:%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日\(chéng)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("請(qǐng)輸入兩個(gè)時(shí)間(格式:年 月 日):\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);
}
幾道簡(jiǎn)單的C語(yǔ)言編程題,請(qǐng)高手幫忙
1、求1-3+5-7+……-99+101的值。 #includeC語(yǔ)言簡(jiǎn)單編程題,由提示編寫(xiě)程序,謝謝大神的幫助??!十分感激?。?/h3>
#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("數(shù)組為:\n");
show(a,n);
printf("最大元素為:%d\n",max(a,n));
return 0;
}
C語(yǔ)言編程題求大神教!
有什么C語(yǔ)言的編程題,請(qǐng)講。不說(shuō)出來(lái),是不能幫到你的。簡(jiǎn)單的C語(yǔ)言程序編寫(xiě)問(wèn)題!求大神解答?。。?!萬(wàn)分感謝
for循環(huán)判斷是否存在數(shù)組中元素與查找數(shù)字相等,存在輸出Yes退出循環(huán)。如果遍歷完數(shù)組i=10表示未找到輸出No
#include "stdafx.h"
#include
using namespace std;
int main()
{
int num[10],x,i;
cout << "請(qǐng)輸入10個(gè)整數(shù):" << endl;
for (i = 0; i < 10; i++)
cin >> num[i];
cout << "請(qǐng)輸入要查找的數(shù):" << 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;
}