导航:首页 > 编程语言 > 用c编程写一年的月份

用c编程写一年的月份

发布时间:2024-11-04 16:58:00

Ⅰ 用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语言编程 从键盘输入一个年份和月份,输出该月有多少天(考虑闰年)

  1. 用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;
    }

  2. 用指针型列举的:
    #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天 ");}

  3. 普通

    #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语言网络经验

阅读全文

与用c编程写一年的月份相关的资料

热点内容
网址加密后能防止被封网吗 浏览:757
pic单片机项目 浏览:177
oppo手机忘记加密密码忘了怎么办 浏览:149
算法竞赛入门经典代码 浏览:152
多功能防盗加密锁闭阀 浏览:862
遥控灯单片机 浏览:813
网络时间同步算法 浏览:473
单片机p1口怎么检测 浏览:911
pdf高亮颜色 浏览:320
银行程序员的工资多少 浏览:19
服务器如何使用移动硬盘 浏览:533
pc饥荒为什么没有服务器 浏览:440
阿里云服务器地址是什么 浏览:148
如何戒除网瘾app 浏览:955
时间戳服务器是什么 浏览:936
文件怎么转成pdf格式的文件格式 浏览:632
网易编程硕士就业 浏览:625
中国文化概论pdf 浏览:987
单片机转换表格 浏览:242
3d内部算法大全视频 浏览:367