饮水思源:金老师的自学网站

1、编写一个简单的控制台程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.Title = "my title" + DateTime.Now;
Console.ForegroundColor = System.ConsoleColor.DarkGreen;
Console.BackgroundColor = System.ConsoleColor.White; Console.WriteLine("Hello worldd 2019-04-28"); String userinput = Console.ReadLine();
Console.WriteLine("{0}这是两个占位符号{1}", userinput, userinput.Length); Console.Beep();
Console.ReadKey(); // ReadKey是Console类的另一个方法,用于接收按键
Console.ReadKey(true); // 添加true参数不回显所接收按键 // 生成的.exe文件可运行在任何具有相应版本.NET的计算机上
}
}
}

2、日期计算的结构化编程实现

结构化编程一般设计步骤:

  1. 先设计数据结构。
  2. 基于数据结构确定算法。简单的情形是,将人的计算方法转化为计算机算法,每个算法步骤用一个函数实现。
  3. 进一步细化与调整方案
  4. 将整体装配成一个函数,得到最终设计方案

PPT截图:

开发时,依据依赖关系由下至上。通常情况,避免跨层调用。

namespace CalculateDaysForSP
{
//封装日期信息
public struct MyDate
{
public int Year; //年
public int Month; //月
public int Day; //日
} }

MyDate.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CalculateDaysForSP
{
class Program
{
//存放每月天数,第一个元素为0是因为数组下标从0起,而我们希望按月份直接获取天数
static int[] months = { , , , , , , , , , , , , }; static void Main(string[] args)
{
MyDate d1, d2; //起始日期和结束日期
//1999年5月10日
d1.Year = ;
d1.Month = ;
d1.Day = ;
//2006年3月8日
d2.Year = ;
d2.Month = ;
d2.Day = ;
//计算结果
int days = CalculateDaysOfTwoDate(d1, d2); string str = "{0}年{1}月{2}日到{3}年{4}月{5}日共有天数:";
str = String.Format(str, d1.Year, d1.Month, d1.Day, d2.Year, d2.Month, d2.Day);
Console.WriteLine(str + days); //暂停,敲任意键结束
Console.ReadKey();
} //计算两个日期中的整天数
static int CalculateDaysOfTwoDate(MyDate beginDate, MyDate endDate)
{
int days = ;
days = CalculateDaysOfTwoYear(beginDate.Year, endDate.Year);
if (beginDate.Year == endDate.Year) days += CalculateDaysOfTwoMonth(beginDate, endDate, true);
else
days += CalculateDaysOfTwoMonth(beginDate, endDate, false); return days;
} //计算两年之间的整年天数,不足一年的去掉
static int CalculateDaysOfTwoYear(int beginYear, int endYear)
{
int days = ;
for (int i = beginYear + ; i <= endYear - ; i++)
{
if (IsLeapYear(i))
days += ;
else
days += ;
}
return days;
} //根据两个日期,计算出这两个日期之间的天数
static int CalculateDaysOfTwoMonth(MyDate beginDate, MyDate endDate, bool IsInOneYear)
{
int days = ;
//对于同一月,天数直接相减
if (beginDate.Month == endDate.Month)
if (IsInOneYear)
return endDate.Day - beginDate.Day;
else
if (IsLeapYear(beginDate.Year))
return + (endDate.Day - beginDate.Day);
else
return + (endDate.Day - beginDate.Day); //不同月
int i = ;
if (IsInOneYear)
{
//同一年
for (i = beginDate.Month; i <= endDate.Month; i++)
{
days += months[i];
//处理闰二月
if ((IsLeapYear(beginDate.Year) && (i == )))
days += ;
} //减去月初到起始日的天数
days -= beginDate.Day;
//减去结束日到月底的天数
days -= months[endDate.Month] - endDate.Day;
}
else
{
//不同年
//计算到年底的天数
for (i = beginDate.Month; i <= ; i++)
days += months[i]; //减去月初到起始日的天数
days -= beginDate.Day;
//从年初到结束月的天数
for (i = ; i <= endDate.Month; i++)
days += months[i]; //减去结束日到月底的天数
days -= months[endDate.Month] - endDate.Day;
}
return days;
} //根据年数判断其是否为闰年
static bool IsLeapYear(int year)
{
//如果年数能被400整除,是闰年
if (year % == )
{
return true;
}
//能被4整除,但不能被100整除,是闰年
if (year % == && year % != )
{
return true;
}
//其他情况,是平年
return false;
}
}
}

Program.cs

3、日期计算机面向对象编程实现

MyDate.cs同上,但命名空间改为CalculateDaysForOO

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CalculateDaysForOO
{
/// <summary>
/// 用于完成日期计算
/// </summary>
public class DateCalculator
{ //存放每月天数,第一个元素为0是因为数组下标从0起,而我们希望按月份直接获取天数
private int[] months = { , , , , , , , , , , , , }; //计算两个日期中的整天数
public int CalculateDaysOfTwoDate(MyDate beginDate, MyDate endDate)
{
int days = ;
days = CalculateDaysOfTwoYear(beginDate.Year, endDate.Year);
if (beginDate.Year == endDate.Year) days += CalculateDaysOfTwoMonth(beginDate, endDate, true);
else
days += CalculateDaysOfTwoMonth(beginDate, endDate, false); return days;
} //计算两年之间的整年天数,不足一年的去掉
private int CalculateDaysOfTwoYear(int beginYear, int endYear)
{
int days = ;
for (int i = beginYear + ; i <= endYear - ; i++)
{
if (IsLeapYear(i))
days += ;
else
days += ;
}
return days;
} //根据两个日期,计算出这两个日期之间的天数
private int CalculateDaysOfTwoMonth(MyDate beginDate, MyDate endDate, bool IsInOneYear)
{
int days = ;
//对于同一月,天数直接相减
if (beginDate.Month == endDate.Month)
if (IsInOneYear)
return endDate.Day - beginDate.Day;
else
if (IsLeapYear(beginDate.Year))
return + (endDate.Day - beginDate.Day);
else
return + (endDate.Day - beginDate.Day); //不同月
int i;
if (IsInOneYear)
{
//同一年
for (i = beginDate.Month; i <= endDate.Month; i++)
{
days += months[i];
//处理闰二月
if ((IsLeapYear(beginDate.Year) && (i == )))
days += ;
} //减去月初到起始日的天数
days -= beginDate.Day;
//减去结束日到月底的天数
days -= months[endDate.Month] - endDate.Day;
}
else
{
//不同年
//计算到年底的天数
for (i = beginDate.Month; i <= ; i++)
days += months[i]; //减去月初到起始日的天数
days -= beginDate.Day;
//从年初到结束月的天数
for (i = ; i <= endDate.Month; i++)
days += months[i]; //减去结束日到月底的天数
days -= months[endDate.Month] - endDate.Day;
}
return days;
} //根据年数判断其是否为闰年
private bool IsLeapYear(int year)
{
//如果年数能被400整除,是闰年
if (year % == )
{
return true;
}
//能被4整数,但不能被100整除,是闰年
if (year % == && year % != )
{
return true;
}
//其他情况,是平年
return false;
}
}
}

DateCalculator.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CalculateDaysForOO
{
class Program
{
static void Main(string[] args)
{
MyDate d1, d2; //起始日期和结束日期 //1999年5月10日
d1.Year = ;
d1.Month = ;
d1.Day = ;
//2006年3月8日
d2.Year = ;
d2.Month = ;
d2.Day = ; string str = "{0}年{1}月{2}日到{3}年{4}月{5}日共有天数:";
str = String.Format(str, d1.Year, d1.Month, d1.Day, d2.Year, d2.Month, d2.Day); //创建类CalculateDate的对象,让变量obj引用它
DateCalculator obj = new DateCalculator();
//调用对象obj的CalculateDaysOfTwoDate方法计算
int days = obj.CalculateDaysOfTwoDate(d1, d2); Console.WriteLine(str + days); //暂停,敲任意键结束
Console.ReadKey();
}
}
}

Program.cs

4、直接应用已有组件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CalculatorDaysUseDotNet
{
class Program
{
static void Main(string[] args)
{
DateTime d1 = new DateTime(, , );
DateTime d2 = new DateTime(, , );
//计算结果
double days = (d2 - d1).TotalDays; string str = "{0}年{1}月{2}日到{3}年{4}月{5}日共有天数:";
str = String.Format(str, d1.Year, d1.Month, d1.Day, d2.Year, d2.Month, d2.Day);
Console.WriteLine(str + days); //暂停,敲任意键结束
Console.ReadKey();
}
}
}

C sharp #001# hello world的更多相关文章

  1. swift 001

    swift 001  = 赋值是没有返回值的 所以 int a=10; int b=20; if(a=b){ printf("这个是错误的"); } swift  中的模运算 是支 ...

  2. 16 On Large-Batch Training for Deep Learning: Generalization Gap and Sharp Minima 1609.04836v1

    Nitish Shirish Keskar, Dheevatsa Mudigere, Jorge Nocedal, Mikhail Smelyanskiy, Ping Tak Peter Tang N ...

  3. [SDK2.2]Windows Azure Virtual Network (4) 创建Web Server 001并添加至Virtual Network

    <Windows Azure Platform 系列文章目录> 在上一章内容中,笔者已经介绍了以下两个内容: 1.创建Virtual Network,并且设置了IP range 2.创建A ...

  4. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数001·3D函数

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数001·3D函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“proce ...

  5. Android 开发错误信息001

    Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessExceptio ...

  6. python解无忧公主的数学时间编程题001.py

    python解无忧公主的数学时间编程题001.py """ python解无忧公主的数学时间编程题001.py http://mp.weixin.qq.com/s?__b ...

  7. php大力力 [005节] php大力力简单计算器001

    2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...

  8. php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28

    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28 话说,嗯嗯,就是我自己说,做事认真要用表格,学习技 ...

  9. Web前端学习笔记(001)

    ....编号    ........类别    ............条目  ................明细....................时间 一.Web前端学习笔记         ...

随机推荐

  1. USB包格式解析(转)

    本文对应usb2.0协议的第八章Protocol Layer. 数据是由二进制数字串构成的,首先数字串构成域(有七种),域再构成包,包再构成事务(IN.OUT.SETUP),事务最后构成传输(中断传输 ...

  2. 小甲鱼零基础python课后题 P21 020函数:内嵌函数和闭包函数

    测试题 0.如果希望在函数中修改全局变量的值,应该使用什么关键字? 答:globe 1.在嵌套函数中,如果希望在内部函数修改外部函数的局部变量,应该使用什么关键字? 答:nonlocal 2.pyth ...

  3. SQL开发——SQL语法

    文档资料参考: 参考:http://www.w3school.com.cn/sql/sql_syntax.asp 参考:http://wiki.jikexueyuan.com/project/sql/ ...

  4. [dev]typeof, offsetof 和container_of

    转一篇文章.写的比较好,浅显易懂,还画了图. https://www.cnblogs.com/idorax/p/6796897.html 概况一下: container_of用到了typeof和off ...

  5. 一、大体认识jspxcms

    声明:jspxcms的license写明,允许将jspxcms用于商业和非商业用途.此处只是作为研究.分享使用心德,并不涉及商用. 使用版本:jspxcms  9.5.0 一.下载源码,并部署到ecl ...

  6. 更改oracle数据库密码(因为密码过期)

    更改system的密码,然后用此用户登录数据库,在数据库里修改指定用户密码 alter user username identified by newpassword; --修改忘记密码用户的密码 让 ...

  7. 60道Python面试题&答案精选!找工作前必看

    需要Word/ PDF版本的同学可以在实验楼微信公众号回复关键词"面试题"获取. 1. Python 的特点和优点是什么? 答案:略. 2. 什么是lambda函数?它有什么好处? ...

  8. 在深谈TCP/IP三步握手&四步挥手原理及衍生问题—长文解剖IP

    如果对网络工程基础不牢,建议通读<细说OSI七层协议模型及OSI参考模型中的数据封装过程?> 下面就是TCP/IP(Transmission Control Protoco/Interne ...

  9. 利用Openssh后门 劫持root密码

    Linux操作系统的密码较难获取.而很多Linux服务器都配置了Openssh服务,在获取root权限的情况下, 可以通过修改或者更新OpenSSH代码等方法,截取并保存其SSH登录账号密码,甚至可以 ...

  10. 解决CentOS(6和7版本),/etc/sysconfig/下没有iptables的问题

    一.Centos 6版本解决办法: 1.任意运行一条iptables防火墙规则配置命令: iptables -P OUTPUT ACCEPT 2.对iptables服务进行保存: service ip ...