从键盘任意输入一个日期(年,月,日),输出第二天的日期(年,月,日)做了好久了,不知道错哪里,求助

作者&投稿:孙学 (若有异议请与网页底部的电邮联系)
用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日)。~

源程序如下:
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int main()
{
int s[2][13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31,
0,31,29,31,30,31,30,31,31,30,31,30,31, };
int year = 0;
int month = 0;
int day = 0;
int n, i, daytemp;
int flag = 0;
int nyear = 0, nmonth = 0, nday = 0;
printf("输入年月日
");
scanf("%d%d%d", &year, &month, &day);
//printf("输入天数
");
//scanf("%d",&n);
n = 1;
if (year 12 || day < 1 || n < 0)
{
printf("输入数据错误
");
return 1;
}
daytemp = day + n;//累加天数
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)//闰年置1
flag = 1;
if (day > s[flag][month])
{
printf("输入日期与年月不符.
");
return 1;
}
if (daytemp <= s[flag][month])//当前日期加天数在本月
{
nyear = year;
nmonth = month;
nday = daytemp;
}
else
{
i = month;
nyear = year;
while (daytemp > s[flag][i])//循环递减,直到当前日期加天数在本月
{
daytemp = daytemp - s[flag][i];
i++;
if (i > 12)//超过一年,年累加
{
nyear++;
if ((nyear % 4 == 0 && nyear % 100 != 0) || nyear % 400 == 0)
flag = 1;
else
flag = 0;
i = i - 12;
}
}
nmonth = i;
nday = daytemp;
}
printf("%d年%d月%d日第%d天后是:
", year, month, day, n);
printf("%d年%d月%d日
", nyear, nmonth, nday);
return 0;
}
程序运行结果如下:




扩展资料:
其他实现方式:
int monthsize(int year, int month) {
int days;
if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 29;
return 28;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:days = 31; break;
case 4:
case 6:
case 9:
case 11: days = 30; break;
}
return days;
}
int main()
int year, month, day, days;
printf("年 月 日:");
while (scanf("%d%d%d", &year, &month, &day) == 3) {
days = monthsize(year, month);
if (days == day) {
if (month == 12) {
++year;
month = 1;
day = 1;
}
else ++month;
}
else ++day;
printf("第二天是:%d/%02d/%02d
", year, month, day);
printf("年 月 日(q to quit):");
}
return 0;
}

这个吧,不过只适合今年(2015年的哦~)
main(){int month,day,d;scanf("%d,%d",&month,&day);switch(month){case 1:d=day;break; case 2:d=31+day;break; case 3:d=59+day;break; case 4:d=90+day;break; case 5:d=120+day;break; case 6:d=151+day;break; case 7:d=181+day;break; case 8:d=212+day;break; case 9:d=243+day;break; case 10:d=273+day;break; case 11:d=304+day;break; case 12:d=334+day;}printf("%d
",d);getch();}

// 楼主你好,你的代码看起来比较混乱不好阅读,我已重写了你的代码。参看下面:
#include<stdio.h>
#include<conio.h>
// 定义全局变量数组用于保存一年12个月分各月的天数
int _MONTH[]={{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};
void ShowDate(int,int,int);

void main(void)
{
   char ch='Y';
   int _y,_m,_d;
   _y=_m=_d=0;
   
   do 
   {
      printf("Enter year,month and days:
");
  scanf("%d,%d,%d",&_y,&_m,&_d);
  if((_y%4==0)&&(_y%100!=0)||(_y%400==0))
  _MONTH[1]=29; // 当前输入的年份若为闰年,则将2月份置为29天
  else _MONTH[1]=28; // 反之为28天 
  ShowDate(_y,_m,_d);
  printf("Press any key to continue.(Press'E' to exit)
");
  ch=getch();
      fflush(stdin); // 清空缓冲区,防止仅迭代一次。
   } while (ch!='E');
}

void ShowDate(int _year,int _month,int _days)
{
// 如果月份为12,且天数为31,所以要调整年、月、日(其它判断与此原理相同)
    if (_month==12&&_days>=31) 
    {
  ++_year;
  _month=_days=1;
      printf("%d,%d,%d
",_year,_month,_days);
    }
else if(_days==_MONTH[_month-1])
{
  ++_month;
  _days=1;
      printf("%d,%d,%d
",_year,_month,_days);
}
    else{
++_days;
      printf("%d,%d,%d
",_year,_month,_days);
}
}


当然,最后还有一些细节的东西需要你自己去完成,比如用户输入的数据是否合法等一些细节的东西。



你好!!

   能不能不发图片,想调试还要一个个字符去输入



你很多switch判断不全。
比如r=30里面的switch(y),你处理了月份为4、6、9、11的,但是没有处理其他月份的。所以肯定是有错误的。

而且你的第一层switch的case都没有写break

case 28 里面,嵌套的第二个switch那里,printf里面应该是r = 1而不是 r + 1


电脑键盘如何换符号?
电脑键盘换符号的方法如下,以切换标点符号为例介绍:1、使用时,可以使用“ Shift”键切换键盘上的标点符号。 例如,在不按“ shift”的情况下按“ \/”将显示“、”,如下图所示。2、键盘上方的数字键行上的标点符号要求按下“ Shift”键才能进行输入操作,例如同时按下“Shift”和“3”,将显示“...

c语言 从键盘上任意输入一个字符(字母大小写,数字,控制字符和其他字符...
i=0;i<sum;i++) { if(s[i]==' ') space++; if((s[i]>=65&&s[i]<=90)||(s[i]>=97&&s[i]<=122)) letter++; if(s[i]>=48&&s[i]<=57) num++; } other=sum-space-letter-num; printf("数字%d个,字母%d个,空格%d个,其他字符%d个。",num,letter,...

C语言编程 从键盘输入一个年份和月份,输出该月有多少天(考虑闰年)_百度...
}用指针型列举的:#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("请依次输入年·月:\\n");scanf("%d%d",&p1,&p2);if(p1%4==0&&p1%100!=0||p1%400==0)a[1]=29;if(p2>12)printf("您...

汇编程序 输入月日年,输出年月日
Input_Date: Output Prompt_Date1 ;提示输入:月\/日\/年 call GetCursor ;取当前光标位置 sub Cursor_Col,8 mov dx,WORD ptr Cursor_Row call SetCursor ;置光标位置 lea di,@@Month ;月份 call Input_Dec ;键盘输入一个指定位数(N)的十进制数字,将其转换成二进制数并保存在指定的内存单元 add dh,3 mov...

从键盘上输入任意一个小写字母,然后将该字符转换为对应的大写字母并输...
include <stdio.h> main(){ char ch;printf("input a char:");scanf("%c",&ch);if(ch>='a'&&ch<='z')ch=ch-32;printf("%c ASCII is %d\\n",ch,ch);}

从键盘输入一个任意的字符串,再输入一个指定字符,要求输出字符串中指...
\/\/输入输出我就不写了 就以题目中的字串字符为例 char s1[20] = "Programming in C";char s2 = 'a';int len1 = strlen(s1);char* p = find(s1, s1 + len1, s2);if (p != s1 + len1) \/\/在s2中没找到与s1[i],s1[i]就是有效字符 { printf("%s", p+1);} ...

...从键盘输入10个日期数据(用数组存储),然后将输入输出
\/ 2012 12 28 2012 12 31 2013 1 1 2013 1 20 2013 1 17 2013 1 16 2008 8 8 2009 6 19 2010 9 16 2011 11 30 2012 - 12 - 28 2012 - 12 - 31 2013 - 01 - 01 2013 - 01 - 20 2013 - 01 - 17 2013 - 01 - 16 2008 - 08 - 08 2009 - 06 - 19 2010 - 09 ...

怎么在电脑上打上一个小方框?
电脑上面打小方框,可以使用电脑里面的输入法来打出来,(如使用搜狗输入法)。方法:1、首先,打开电脑,点击电脑下方的状态栏,在弹出来的搜狗输入法条上面选择上面的工具箱进入。2、进入到工具箱里面,选择符号大全。3、在符号大全里面,选择左边栏的标点符号,然后就会在右边栏看见小方框的符号了。

从键盘输入一个0~1000之间的任意整数,输出该整数的所有因子.
include<iostream.h> int main(){ int a,i;cout<<"input:";cin>>a;i=1;cout<<"output:";while(i<=a){ if(a%i==0)cout<<i<<" ";i++;} return 0;} 以上程序在win2000下通过c-free编译成功

从键盘输入一个数(1-7),输出该数对应的星期,星期用汉语拼音表示,用swi...
include <stdio.h> int main(){ int n;scanf("%d",&n);switch(n){ case 1:printf("Xingqi yi\\n");break;case 2:printf("Xingqi er\\n");break;case 3:printf("Xingqi san\\n");break;case 4:printf("Xingqi si\\n");break;case 5:printf("Xingqi wu\\n");break;case 6:printf("...

芦溪县18662403341: 用c语言从键盘任意输入一个日期(年月日),输出第二天的日期(年月日). -
赏肿洁脂: 源程序如下:#include "pch.h"#define _CRT_SECURE_NO_WARNINGS#include <iostream>using namespace std;int main(){ int s[2][13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31, 0,31,29,31,30,31,30,31,31,30,31,30,31, }; int year = ...

芦溪县18662403341: 编写C语言:从键盘输入某个日期(包括年、月、日),编写程序,计算并输出这一天是该年的第多少天. -
赏肿洁脂: #include<stdio.h> struct date{int y,m,d;}; int fun(struct date *p){int a=0;switch(p->m){case 12:a+=30;case 11:a+=31;case 10:a+=30;case 9:a+=31;case 8:a+=31;case 7:a+=30;case 6:a+=31;case 5:a+=30;case 4:a+=31;case 3:if(p->y%...

芦溪县18662403341: word怎样直接敲键盘就能打出当前的年月日期 -
赏肿洁脂: 方法一:插入日期和时间的快捷键 Alt+Shift+D:当前日期 Alt+Shift+T:当前时间 或者打入某年回车.方法二:1、单击“插入”菜单下的“日期时间”2、在弹出的对话框中选择一种时间格式即可,

芦溪县18662403341: 编写程序实现:从键盘输入一个年月日,要求出该天是该年的第几天(注意判断概念是否是闰年) -
赏肿洁脂: 诠释乐趣(诠释y,INT,INT Me) { ?我,天= 0; ?((M> 12 | | M <1)| |((M == 1 | | M == | | M == | | M == | | M == 8 | | M = = 10 | | M == 12)&&(D> 31 | | D <1))| |((M == | | M == | | M == | | M == 11)&&( D> 30 | | D <1))| |((D == 2)&&((Y%4 == 0 | |(Y%100 == 0 &&(...

芦溪县18662403341: C语言 从键盘输入日期(年,月,日),计算并输出它是该年的第几天. -
赏肿洁脂: 这个吧,不过只适合今年(2015年的哦~)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19main() {intmonth,day,d; scanf("%d,%d",&month,&day); switch(month) {case1:d=day;break;case2:d=31+day;break;case3:d=59+day;break;case4:d=...

芦溪县18662403341: 请教,C语言如何实现:从键盘输入某一天的年月日,计算该天是当年的第几天. -
赏肿洁脂: main() { int day,month,year,sum,leap; printf("\nplease input year,month,day\n"); scanf("%d,%d,%d",&year,&month,&day); switch(month)/*先计算某月以前月份的总天数*/ { case 1:sum=0;break; case 2:sum=31;break; case 3:sum=59;...

芦溪县18662403341: 编写程序实现从键盘输入年、月、日,计算并输出该日是该年的第几天 -
赏肿洁脂: #include<stdio.h> int main() { int y,m,d,i;scanf("%d%d%d",&y,&m,&d);for(i=1;i<m;i++)switch(i){case 1:case 3:case 5:case 7:case 8:case 10:d+=31;break;case 4:case 6:case 9:case 11:d+=30;break;case 2:d+=28+(y%4==0&&y%100||y%400==0);}printf("THE DATE IS THE %dth DAY\n",d);return 0; }

芦溪县18662403341: 从键盘输入日期型的数据 -
赏肿洁脂: 往Oracle数据库中插入日期型数据(to_date的用法) INSERT INTO FLOOR VALUES ( to_date ( '2007-12-20 18:31:34' , 'YYYY-MM-DD HH24:MI:SS' ) ) ; 查询显示:2007-12-20 18:31:34.0 ------------------- INSERT INTO FLOOR VALUES ( to_...

芦溪县18662403341: 求从键盘输入年月日,编写程序输出该日是该年的第几天 -
赏肿洁脂: #include<stdio.h> int N; void main() { int zong_tian_shu( int, int ); int y,r,d; do scanf("%d%d%d",&N,&y,&r); while(r<1||r>31||y<1||y>12); d=zong_tian_shu(y,r); printf("%d年%d月%d日是%d的第%d天\n",N,y,r,N,d); } int zong_tian_shu( int y, int r ) ...

芦溪县18662403341: 快捷键输入当前日期 - 快捷输入当前日期的快捷键
赏肿洁脂: 1. 快捷键输入当前日期原因:没有通过粘贴源格式的方式粘贴日期,导致复制单元格的格式变动.解决方法:1、首先打开excel表格,在A1单元格中输入一组日期数据....

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网