关于一个时间计算器demo程序的算法优化(附四种语言源码)

写在前面:具体的文字解析今晚有空再写,白天先复习,马上就要考试了,补天ing……
本程序有三个功能:
1、输入起始日期和终止日期,计算相隔天数
2、输入起始日期和相隔天数,计算终止日期
3、输入终止日期和相隔天数,计算起始日期

先上源码:
去年写的C++版

#include 
#include
#include
#include
using namespace std;
int isleapyear(int year);
void functionA(int a,int b,int c,int d ,int e,int f);
void functionB(int a,int b,int c,int temp);
void functionC(int a,int b,int c,int temp);
int main()
{
  cout << "欢迎使用时间计算器\n" <<
  "制作人:非牛顿流体\n" <<
  "此程序仅供娱乐\n"<<
  "版权所有,侵权必究\n"<> n;
  switch(n)
  {
      case (0) :
      {
          count = 0;
          break;
      }
      case (1):
      {
          cout << "请输入起始日期,输入格式例如2000 01 01,三个数字中间使用空格分隔"<< endl;
          cin  >> a[1] >> b[1] >> c[1];
          cout << "请输入终止日期,输入格式例如2000 01 01,三个数字中间使用空格分隔"<< endl;
          cin  >> a[2] >> b[2] >> c[2];
          functionA(a[1],b[1],c[1],a[2],b[2],c[2]);
          break;
      }
    case (2):
    {
          cout << "请输入起始日期,输入格式例如2000 01 01,三个数字中间使用空格分隔"<< endl;
          cin  >> a[1] >> b[1] >> c[1];
          cout << "请输入时间间隔,输入格式例如100"<< endl;
          cin  >> temp;
          functionB(a[1],b[1],c[1],temp);
          break;
    }
    case (3):
    {
          cout << "请输入终止日期,输入格式例如2000 01 01,三个数字中间使用空格分隔"<> a[2] >> b[2] >> c[2];
          cout << "请输入时间间隔,输入格式例如100"<< endl;
          cin  >> temp;
          functionC(a[2],b[2],c[2],temp);
          break;
    }
    default :
    {
         cout << "出错!!!" <=0)
    {
        sum+= c;
        f-=c;
        functionA(a,b,c,d,e,f);
    }
    else
    {
        if(e==1)
        {
            f+=31;
            e=12;
            d--;
            functionA(a,b,c,d,e,f);
        }
        else if(e==3&&isleapyear(d)==1)
        {
            f+=29;
            e--;
            functionA(a,b,c,d,e,f);
        }
        else if(e==3&&isleapyear(d)==0)
        {
            f+=28;
            e--;
            functionA(a,b,c,d,e,f);
        }
        else if(e==5||e==7||e==10||e==12)
        {
            f+=30;
            e--;
            functionA(a,b,c,d,e,f);
        }
        else if(e==2||e==4||e==6||e==8||e==9||e==11)
        {
            f+=31;
            e--;
            functionA(a,b,c,d,e,f);
        }
    }
}
void functionB(int a,int b,int c,int temp)
{
    static int mark=1;
    if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
      {
          if((c+temp)>31)
            {
                b++;
                if(b>12)
                {
                    a++;
                    b-=12;
                }
                temp-=31;
                functionB(a,b,c,temp);
            }
            else if((c+temp)<=31&&mark==1)
            {
                c=c+temp;
                if(b<10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << b << "--" << "0" << c <30)
            {
                b++;
                temp-=30;
                functionB(a,b,c,temp);
            }
            else if((c+temp)<=30&&mark==1)
            {
                c=c+temp;
                if(b<10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << b << "--" << "0" << c <30)
            {
                b++;
                temp-=29;
                functionB(a,b,c,temp);
            }
            else if((c+temp)<=30&&mark==1)
            {
                c=c+temp;
                if(b<10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << b << "--" << "0" << c <30)
            {
                b++;
                temp-=28;
                functionB(a,b,c,temp);
            }
            else if((c+temp)<=30&&mark==1)
            {
                c=c+temp;
                if(b<10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "终止日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "终止日期为:"<< a << "--" << b << "--" << "0" << c <0&&mark==1)
        {
            c=c-temp;
            if(b<10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << b << "--" << "0" << c <0&&mark==1)
        {
            c=c-temp;
            if(b<10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << b << "--" << "0" << c <0&&mark==1)
        {
            c=c-temp;
            if(b<10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << b << "--" << "0" << c <0&&mark==1)
        {
            c=c-temp;
            if(b<10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << "0" << c <=10)
                {
                cout << "起始日期为:"<< a << "--" << "0" << b << "--" << c <=10&&c<10)
                {
                cout << "起始日期为:"<< a << "--" << b << "--" << "0" << c <

今年写的C语言版

//时间计算器 作者:厄斐琉斯     All Rights Reserved
//任何人不得转载、引用、发布本代码,否则必将追究法律责任
//本代码发布于吾爱破解论坛(www.52pojie.cn),其他任何途径获得的本代码均为抄袭

#include 
#include 
#include 

int isleapyear(int year)
{
    if(year % 400 == 0)
        return 1;
    if(year % 100 == 0 && year % 400 != 0)
        return 0;
    if(year % 4 == 0 && year % 100 != 0)
        return 1;
    if(year % 4 != 0)
        return 0;
}

int passedday(int year, int month, int day)
{
    int i, j, k, leapyear, sum;
    leapyear = isleapyear(year);
    sum = day;
    for(i=1; i 1)
    {
        for(i=a+1; i 0)
    {
        if(e==1 || e==3 || e==5 || e==7 || e==8 || e==10 || e==12)
            temp = 31;
        else if(e==4 || e==6 || e==9 || e==11)
            temp = 30;
        else if(e == 2)
        {
            if(isleapyear(d) == 1)
                temp = 29;
            else
                temp = 28;
        }
        if(sum + f > temp)
        {
            f = 1;
            e += 1;
            if(e > 12)
            {
                e = 1;
                d += 1;
            }
            sum -= (temp + 1 - f);
        }
        else
        {
            f = sum + f;
            sum = 0;
        }
    }
    printf("终止日期为%d-%02d-%02d\n\n\n", d, e, f);
    return;
}

void functionC()
{
    int a, b, c, d, e, f, sum, temp;
    printf("请输入终止日期,输入格式例如2000 01 01,三个数字中间使用空格分隔\n");
    scanf("%d %d %d", &d, &e, &f);
    printf("请输入时间间隔,输入格式例如100\n");
    scanf("%d", &sum);
    a = d;
    b = e;
    c = f;
    while(sum > 0)
    {
        if(b==1 || b==2 || b==4 || b==6 || b==8 || b==9 || b==11)
            temp = 31;
        else if(b==5 || b==7 || b==10 || b==12)
            temp = 30;
        else if(b == 3)
        {
            if(isleapyear(a) == 1)
                temp = 29;
            else
                temp = 28;
        }
        if(c - sum < 1)
        {
            sum -= c;
            c = temp;
            b -= 1;
            if(b < 1)
            {
                b = 12;
                a -= 1;
            }
        }
        else
        {
            c = c - sum;
            sum = 0;
        }
    }
    printf("起始日期为%d-%02d-%02d\n\n\n", a, b, c);
    return;
}

main()
{
    int n;
    SetConsoleTitle("时间计算器 by 厄斐琉斯");
    printf("欢迎使用时间计算器 作者:厄斐琉斯\n");
    printf("本程序仅供娱乐,All Rights Reserved!\n");
    printf("时间计算器使用说明书:\n");
    printf("输入1:输入起始日期和终止日期,计算相隔天数\n");
    printf("输入2:输入起始日期和相隔天数,计算终止日期\n");
    printf("输入3:输入终止日期和相隔天数,计算起始日期\n");
    printf("输入0:退出此程序\n\n\n");
    while(1)
    {
        printf("请选择你想要的功能:");
        scanf("%d", &n);
        if(n==0)
            break;
        else if(n==1)
            functionA();
        else if(n==2)
            functionB();
        else if(n==3)
            functionC();
        else
            printf("选择错误,请重新选择!\n\n");
    }
}

今年写的java版

import java.util.Scanner;
public class Timecalculate_1 {
	public static int isleapyear(int year)
	{
		if(year % 400 == 0)
	        return 1;
		else if(year % 100 == 0 && year % 400 != 0)
	        return 0;
		else if(year % 4 == 0 && year % 100 != 0)
	        return 1;
		else
			return 0;
	}
	public static int passedday(int year, int month, int day)
	{
		int i, leapyear, sum;
	    leapyear = isleapyear(year);
	    sum = day;
	    for(i=1; i 1)
	    {
	        for(i=a+1; i 0)
	    {
	        if(e==1 || e==3 || e==5 || e==7 || e==8 || e==10 || e==12)
	            temp = 31;
	        else if(e==4 || e==6 || e==9 || e==11)
	            temp = 30;
	        else if(e == 2)
	        {
	            if(isleapyear(d) == 1)
	                temp = 29;
	            else
	                temp = 28;
	        }
	        if(sum + f > temp)
	        {
	            f = 1;
	            e += 1;
	            if(e > 12)
	            {
	                e = 1;
	                d += 1;
	            }
	            sum -= (temp + 1 - f);
	        }
	        else
	        {
	            f = sum + f;
	            sum = 0;
	        }
	    }
	    System.out.format("终止日期为%d-%02d-%02d\n\n\n", d, e, f);
	    System.out.println("\n");
	    return;
	}
	public static void functionC()
	{
	    int a, b, c, d, e, f, sum, temp = 0;
	    Scanner in = new Scanner(System.in);
	    System.out.println("请输入终止日期,输入格式例如2000 01 01,三个数字中间使用空格分隔");
	    d = in.nextInt();		e = in.nextInt();		f = in.nextInt();
	    System.out.println("请输入时间间隔,输入格式例如100");
	    sum = in.nextInt();
	    a = d;
	    b = e;
	    c = f;
	    while(sum > 0)
	    {
	        if(b==1 || b==2 || b==4 || b==6 || b==8 || b==9 || b==11)
	            temp = 31;
	        else if(b==5 || b==7 || b==10 || b==12)
	            temp = 30;
	        else if(b == 3)
	        {
	            if(isleapyear(a) == 1)
	                temp = 29;
	            else
	                temp = 28;
	        }
	        if(c - sum < 1)
	        {
	            sum -= c;
	            c = temp;
	            b -= 1;
	            if(b < 1)
	            {
	                b = 12;
	                a -= 1;
	            }
	        }
	        else
	        {
	            c = c - sum;
	            sum = 0;
	        }
	    }
	    System.out.format("起始日期为%d-%02d-%02d\n\n\n", a, b, c);
	    System.out.println("\n");
	    return;
	}
	public static void main(String[] args) {
		int n;
		Scanner in = new Scanner(System.in);
		System.out.println("欢迎使用时间计算器 作者:厄斐琉斯");
		System.out.println("本程序仅供娱乐,All Rights Reserved!");
		System.out.println("时间计算器使用说明书:");
		System.out.println("输入1:输入起始日期和终止日期,计算相隔天数");
		System.out.println("输入2:输入起始日期和相隔天数,计算终止日期");
		System.out.println("输入3:输入终止日期和相隔天数,计算起始日期");
		System.out.println("输入0:退出此程序\n\n");
	    while(true)
	    {
	    	System.out.print("请选择你想要的功能:");
	        n = in.nextInt();
	        if(n==0)
	            break;
	        else if(n==1)
	            functionA();
	        else if(n==2)
	            functionB();
	        else if(n==3)
	            functionC();
	        else
	        	System.out.println("选择错误,请重新选择!\n");
	    }
	}
}

今年写的matlab版

fprintf("欢迎使用时间计算器 作者:厄斐琉斯\n");
fprintf("本程序仅供娱乐,All Rights Reserved!\n");
fprintf("时间计算器使用说明书:\n");
fprintf("输入1:输入起始日期和终止日期,计算相隔天数\n");
fprintf("输入2:输入起始日期和相隔天数,计算终止日期\n");
fprintf("输入3:输入终止日期和相隔天数,计算起始日期\n");
fprintf("输入0:退出此程序\n\n\n");
while(1)
    fprintf("请选择你想要的功能:");
    n = input('');
    if(n==0)
        break;
    elseif(n==1)
        A();
    elseif(n==2)
        B();
    elseif(n==3)
        C();
    else
        fprintf("选择错误,请重新选择!\n\n");
    end
end

function A()
    fprintf("请输入起始日期,输入格式例如[2000 01 01],三个数字中间使用空格分隔\n");
    buf = input('');
    a = buf(1);     b = buf(2);     c = buf(3);
    fprintf("请输入终止日期,输入格式例如[2000 01 01],三个数字中间使用空格分隔\n");
    buf = input('');
    d = buf(1);     e = buf(2);     f = buf(3);
    sum = 0;
    if(a == d)
        sum = passedday(d, e, f) - passedday(a, b, c);
    end
    if(d - a > 1)
        for i = a+1 : d-1
            if(isleapyear(i) == 1)
                sum = sum + 366;
            else
                sum = sum + 365;
            end
        end
    end
    x = passedday(a, b, c);
    y = passedday(d, e, f);
    leapyear = isleapyear(a);
    sum = sum + (365 + leapyear - x + y);
    fprintf("相隔天数为%d\n\n\n", sum);
end

function B()
    fprintf("请输入起始日期,输入格式例如[2000 01 01],三个数字中间使用空格分隔\n");
    buf = input('');
    a = buf(1);     b = buf(2);     c = buf(3);
    fprintf("请输入时间间隔,输入格式例如100\n");
    sum = input('');
    d = a;
    e = b;
    f = c;
    while(sum > 0)
        if(e==1 || e==3 || e==5 || e==7 || e==8 || e==10 || e==12)
            temp = 31;
        elseif(e==4 || e==6 || e==9 || e==11)
            temp = 30;
        elseif(e == 2)
            if(isleapyear(d) == 1)
                temp = 29;
            else
                temp = 28;
            end
        end
        if(sum + f > temp)
            f = 1;
            e  = e + 1;
            if(e > 12)
                e = 1;
                d  = d + 1;
            end
            sum  = sum - (temp + 1 - f);
        else
            f = sum + f;
            sum = 0;
        end
    end
    fprintf("终止日期为%d-%02d-%02d\n\n\n", d, e, f);
end

function sum = passedday(year, month, day)
    leapyear = isleapyear(year);
    sum = day;
    for i = 1 : month-1
        if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10)
            sum = sum + 31;
        elseif(i==4 || i==6 || i==9 || i==11)
            sum = sum + 30;
        else
            sum = sum + (28 + leapyear);
        end
    end
end 

function C()
    fprintf("请输入终止日期,输入格式例如[2000 01 01],三个数字中间使用空格分隔\n");
    buf = input('');
    d = buf(1);     e = buf(2);     f = buf(3);
    fprintf("请输入时间间隔,输入格式例如100\n");
    sum = input('');
    a = d;
    b = e;
    c = f;
    while(sum > 0)
        if(b==1 || b==2 || b==4 || b==6 || b==8 || b==9 || b==11)
            temp = 31;
        elseif(b==5 || b==7 || b==10 || b==12)
            temp = 30;
        elseif(b == 3)

            if(isleapyear(a) == 1)
                temp = 29;
            else
                temp = 28;
            end
        end
        if(c - sum < 1)
            sum  = sum - c;
            c = temp;
            b  = b - 1;
            if(b < 1)
                b = 12;
                a  = a - 1;
            end
        else
            c = c - sum;
            sum = 0;
        end
    end
    fprintf("起始日期为%d-%02d-%02d\n\n\n", a, b, c);
end

function res = isleapyear(year)
    if(mod(year, 400) == 0)
        res = 1;
    elseif(mod(year, 100) == 0 && mod(year, 400) ~= 0)
        res = 0;
    elseif(mod(year, 4) == 0 && mod(year, 100) ~= 0)
        res = 1;
    elseif(mod(year, 4) ~= 0)
        res = 0;
    end
end

python版还没写,有空写

你可能感兴趣的:(c语言,python,matlab,java)