Ⅰ 用c語言編程萬年歷
/*以下程序在TC2 中通過*/
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void rili(int,int);
int runniansub(int);
int week(int,int,int);
int monthday[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
struct date d;
getdate(&d);
printf("今天是:%d年 %d月 %d日\n",d.da_year,d.da_mon,d.da_day);
rili(d.da_year,d.da_mon-1);
system("pause");
return 0;
}
void rili(int year,int month)
{
int d,w,i,s,c,p;
char x;
while(month>11) {
month-=12;
year++;
}
while(month<0) {
year--;
month+=12;
}
if(runniansub(year)) monthday[1]=29;
printf("%d年%d月\n",year,month+1);
printf("S M T W T F S\n");
s=week(year,month+1,1);
for(i=0; i<3*s; i++ ) printf(" ");
c=s;
for(d=1;d<=monthday[month];d++) {
if(c>6) {
printf("\n");
c=0;
}
c++;
printf("%d",d);
printf("%s",(d<10)?" ":" ");
}
printf("\n");
printf("下月N,上月P,退出Q");
x=getch();
printf("%c\n",x);
if(x=='N' || x=='n') { rili(year,month+1); }
else if(x=='P' || x=='p') { rili(year,month-1); }
else { printf("感謝使用!88\n"); return;}
}
int runniansub(int year)
{
return ((year%4==0 && year%100!=0) || (year%4==0 && year%400==0));
}
int week(int y,int m,int d)
{
if((m==1)||(m==2)) {
m+=12;
y--;
}
return ((d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)+1)%7;
}
Ⅱ 用C#編寫一個輸入某一年的年份和月份,計算該月的天數的編程.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Maker
{
class Program
{
static void Main(string[] args)
{
int a = 31;
int b = 30;
int c, mouth=0, day=0,year;
int sum=0;
do
{
Console.WriteLine("請輸入年:");
year=int.Parse(Console.ReadLine());
Console.WriteLine("請輸入月份:");
mouth = int.Parse(Console.ReadLine());
Console.WriteLine("請輸入日數:");
day = int.Parse(Console.ReadLine());
if ((mouth < 1 || mouth > 12) || (day < 1 || day > 31)||year<=0)
{
Console.WriteLine("輸入有誤!");
}else
{
if(year%4!=0)
{
c = 28;
if (mouth <= 2)
{
sum = (mouth - 1) * 31 + day;
Console.WriteLine(year + "年" + mouth + "月" + day + "日是當年的" + sum + "天");
}
else
{
switch(mouth)
{
case 3:
sum =59+day;
break;
case 4:
sum = 90 + day;
break;
case 5:
sum = 120 + day;
break;
case 6:
sum = 151 + day;
break;
case 7:
sum = 181 + day;
break;
case 8:
sum = 212 + day;
break;
case 9:
sum = 243 + day;
break;
case 10:
sum = 273 + day;
break;
case 11:
sum = 303 + day;
break;
case 12:
sum = 333 + day;
break;
}
Console.WriteLine(year + "年" + mouth + "月" + day + "日是當年的" + sum + "天");
}
}else
{
if (mouth <= 2)
{
sum = (mouth - 1) * 31 + day;
}
else
{
switch (mouth)
{
case 3:
sum =59+day+1;
break;
case 4:
sum = 90 + day + 1;
break;
case 5:
sum = 120 + day + 1;
break;
case 6:
sum = 151 + day + 1;
break;
case 7:
sum = 181 + day + 1;
break;
case 8:
sum = 212 + day + 1;
break;
case 9:
sum = 243 + day + 1;
break;
case 10:
sum = 273 + day + 1;
break;
case 11:
sum = 303 + day + 1;
break;
case 12:
sum = 333 + day + 1;
break;
}
Console.WriteLine(year + "年" + mouth + "月" + day + "日是當年的" + sum + "天");
}
}
}
} while ((mouth < 1 || mouth > 12) || (day < 1 || day > 31));
}
}
}
是這樣么 ?剛好我的作業上有這一題
Ⅲ 用c語言中的數組編程,當填寫年月日時,輸出是這年的第幾天.
方法一:
#include<stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int day,month,year,sum,leap;
do{
printf("\nplease input year month day\n");
printf("\n請輸入年 月 日\n");
scanf("%d %d %d",&year,&month,&day);
if((month==1&&day<=31&&day>=1)||
((year%4==0&&year%100!=0||year%400==0)&&(month==2&&day<=29&&day>=1))||
(!(year%4==0&&year%100!=0||year%400==0)&&(month==2&&day<=28&&day>=1))||
(month==3&&day<=31&&day>=1)||(month==4&&day<=30&&day>=1)||
(month==5&&day<=31&&day>=1)||(month==6&&day<=30&&day>=1)||
(month==7&&day<=31&&day>=1)||(month==8&&day<=31&&day>=1)||
(month==9&&day<=30&&day>=1)||(month==10&&day<=31&&day>=1)||
(month==11&&day<=30&&day>=1)||(month==12&&day<=31&&day>=1))
{ switch(month)//先計算某月以前月份的總天數
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");
break;
}
sum=sum+day; //再加上某天的天數
if(year%400==0||(year%4==0&&year%100!=0)) //判斷是不是閏年
leap=1;
else
leap=0;
if(leap==1&&month>2) //如果是閏年且月份大於2,總天數應該加一天
sum++;
//clrscr(); //清屏
system("cls"); //清屏
printf("%d-%d-%dis the year\'s first %d days.\n\n",year,month,day,sum);
printf("%d-%d-%d是這年的第%d天.\n\n",year,month,day,sum);
}
else printf("\tData errors!!!\n\n");
}while(1);
}
方法二:
#include<stdio.h>
#include <stdlib.h>
struct
{
int year;
int month;
int day;
}date;
void main()
{int i,days;
int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
do{
printf("Input year,month,day:\n");
scanf("%d %d %d",&date.year,&date.month,&date.day);
system("cls"); //清屏
days=0;
if((date.month==1&&date.day<=31&&date.day>=1)||
((date.year%4==0&&date.year%100!=0||date.year%400==0)&&(date.month==2&&date.day<=29&&date.day>=1))||
(!(date.year%4==0&&date.year%100!=0||date.year%400==0)&&(date.month==2&&date.day<=28&&date.day>=1))||
(date.month==3&&date.day<=31&&date.day>=1)||(date.month==4&&date.day<=30&&date.day>=1)||
(date.month==5&&date.day<=31&&date.day>=1)||(date.month==6&&date.day<=30&&date.day>=1)||
(date.month==7&&date.day<=31&&date.day>=1)||(date.month==8&&date.day<=31&&date.day>=1)||
(date.month==9&&date.day<=30&&date.day>=1)||(date.month==10&&date.day<=31&&date.day>=1)||
(date.month==11&&date.day<=30&&date.day>=1)||(date.month==12&&date.day<=31&&date.day>=1))
{
for(i=1;i<date.month;i++)
days+=day_tab[i];
days+=date.day;
if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)
days++;
printf("\n\n");
printf("\t%d/%d is the %dth day in %d.\n",date.month,date.day,days,date.year);
}
else printf("\tData errors!!!\n\n");
}while(1);
}
Ⅳ C語言編程 從鍵盤輸入一個年份和月份,輸出該月有多少天(考慮閏年)
用switch選擇的:
#include<stdio.h>
int main()
{
int y,m;
printf("請依次輸入年,月:
");
scanf("%d,%d",&y,&m); //上機時注意打逗號,也可修改為其他格式
switch(m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:printf("這個月有31天
");break;
case 2 : if(y%4==0&&y%100!=0||y%400==0) //最基本的判斷閏年的條件
printf("這個月有29天
");
else printf("這個月有28天
");break;
case 4:
case 6:
case 9 :
case 11:printf("這個月有30天
");break;
}
return 0;
}
用指針型列舉的:
#include<stdio.h>
int a[]={31,28,31,30,31,30,31,31,30,31,30,31},*p1,*p2,*p3,i=1,k=0;
int main()
{
int p1,p2;
printf("請依次輸入年·月:
");
scanf("%d%d",&p1,&p2);
if(p1%4==0&&p1%100!=0||p1%400==0)
a[1]=29;
if(p2>12)
printf("您的輸入有誤!
");
printf("緩圓這個月的天數為清春:%d
",a[p2-1]);
return 0;
}#include <stdio.h>
main()
{int a,b;
printf("請輸入年份和月份:
");
scanf("%d%d",&a,&b);
if (a%4==0&&a%100!=0)
if(b==2)
printf("這個月有29天
");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天
");
else
printf("這個月有30天
");
else if(b==2)
printf("這個月有28天
");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天
");
else
printf("這個月有30天
");}
普通
#include <stdio.h>
main(){
int a,b;
printf("請輸入年份和月份:
");
scanf("%d%d",&a,&b);
if (a%4==0&&a%100!=0)
if(b==2)
printf("這個月有29天
");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天
");
else
printf("這個月有30天
");
else if(b==2)
printf("這個月有28天
");
else if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
printf("這個月有31天
");
else
printf("這個月有30天
");}
(4)用c編程寫一年的月份擴展閱讀
switch 語句可以處理多分支選擇問題,根據答哪耐其中break 語句的使用方法,一般分三種情況。
在switch 語句的每個語句段中都使用break 語句,這是switch 語句的主要使用方法,一般形式為:
switch (表達式)
case 常量表達式1: 語句段1; break;
case 常量表達式2: 語句段2; break;
case常量表達式n: 語句段n; break;
case常量表達式n+1:語句段n+ 1; break;
default :
參考資料c語言網路經驗