一次逆向软件工程的尝试

本次逆向开发的项目来自于大一上c语言的小组期末作业,通过一年的学习后,再回到当初的作业进行代码的优化与升级。以下是源代码。

点击查看代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
/*1.公元1年1月1日,是星期一
  2.累加至输入年份之前的所有年的天数
  3.总天数模7,0(星期日)---6(星期六)
*/



void menu()//打印菜单
{
	printf("         万年历查询系统\n");
	printf("\n");
	printf("\n");
	printf("*********************************\n");
	printf("1 查询某年某月某日是星期几\n");
	printf("2 查询某年是否是闰年\n");
	printf("3 打印某年的日历\n");
	printf("4 查询某月的最大天数\n");
	printf("5 退出\n");
	printf("*********************************\n");
}
int check_year(int year) {//检查平年闰年函数

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

int week_f(int year)//返回xx年1月1日是星期几 
{

	if (year == 1)//第一年
	{
		return 1;
	}

	int sum = 0;
	for (int i = 1; i < year; i++) {
		if (check_year(i) == 1) {
			sum = sum + 366;
		}
		else {
			sum = sum + 365;
		}
	}

	

	return (sum + 1) % 7;//加一是因为第一年一月一日是星期一
}


int month_run(int n)//闰年月份
{
	switch (n)
	{
	case 1:return 31;
	case 2:return 29;
	case 3:return 31;
	case 4:return 30;
	case 5:return 31;
	case 6:return 30;
	case 7:return 31;
	case 8:return 31;
	case 9:return 30;
	case 10:return 31;
	case 11:return 30;
	case 12:return 31;
	}
}

int month_ping(int n)//平年月份
{
	switch (n) 
	{
	case 1:return 31;
	case 2:return 28;
	case 3:return 31;
	case 4:return 30;
	case 5:return 31;
	case 6:return 30;
	case 7:return 31;
	case 8:return 31;
	case 9:return 30;
	case 10:return 31;
	case 11:return 30;
	case 12:return 31;
	}
}




///闰年输出函数 
void run(int week) 
{
	int enter = 0;//记录换行 
	int wee = week;
	for (int i = 1; i <= 12; i++)//最外层遍历12个月
	{

		enter = 0;
		printf("------------->%d月<----------\n", i);//输出月份表头
		printf("日	一	二	三	四	五	六\n");//输出星期标头 


		for (int k = 0; k < wee; k++)//输出空格 
		{
			printf("	");//周日是0,所以周几就有几个空格
			enter++;
		}


		for (int j = 1; j <= month_run(i); j++) 
		{//输出每月的天 
			if (enter % 7 == 0) 
			{

				printf("\n");
			}
			printf("%d	", j);
			enter++;
		}

		wee = (wee + month_run(i)) % 7;
		//enter = enter - wee;

		printf("\n\n\n");

	}
}


///平年输出函数
void ping(int week)
{
	int enter = 0;//记录换行 
	int wee = week;
	for (int i = 1; i <= 12; i++) //最外层遍历12个月
	{
		printf("------------>%d月<------------\n", i);
		printf("日	一	二	三	四	五	六\n");//输出星期标头 


		for (int k = 0; k < wee; k++)//输出空格 
		{
			printf("	");
			enter++;
		}


		for (int j = 1; j <= month_ping(i); j++)//输出每月的天 
		{
			if (enter % 7 == 0) {
				printf("\n");
			}
			printf("%d	", j);
			enter++;
		}

		wee = (wee + month_ping(i)) % 7;
		enter = enter - wee;

		printf("\n\n\n");

	}
}
int main()

{



	while (1)
	{
		int input = 0;
		menu();
		printf("请选择:");
		scanf_s("%d", &input);
		switch (input)
		{
		case 1:
		{
			int n, y, r, s = 0;
			scanf_s("%d %d %d", &n, &y, &r);

			if (check_year(n) == 1)
			{
				for (int i = 1; i <= y - 1; i++)
				{
					s = s + month_run(i);
				}
			}
			else
			{
				for (int i = 1; i <= y - 1; i++)
				{
					s = s + month_ping(i);
				}
			}
			s = s + week_f(n) + r - 1;
			switch (s % 7)
			{
			case 1:printf("星期一\n");
				continue;
			case 2:printf("星期二\n");
				continue;
			case 3:printf("星期三\n");
				continue;
			case 4:printf("星期四\n");
				continue;
			case 5:printf("星期五\n");
				continue;
			case 6:printf("星期六\n");
				continue;
			case 0:printf("星期天\n");
				continue;
			}
			



		}
		case 2:
		{
			int year;
			printf("请输入年份:");
			scanf_s("%d", &year);
			if (check_year(year) == 1) //检查是否平年闰年
			{
				printf("%d是闰年\n", year);
			}
			else
			{
				printf("%d是平年\n", year);
			}
			continue;
		}
		case 3:
		{int year;
		int week = 0;
		int sum = 0;
		printf("请输入年份:");
		scanf_s("%d", &year);
		week = week_f(year);//返回xx年1月1日是星期几


		if (check_year(year) == 1) //检查是否平年闰年
		{
			run(week);
		}
		else {
			ping(week);
		}
		continue;
		}
		case 4:
		{
			int jiyue = 0;
			printf("请输入月份:\n");
			scanf_s("%d", &jiyue);
			if (jiyue >= 13 || jiyue <= 0)//1-12月分以外数据报错。
			{
				printf("不好意思,您输入的数据有误。");
				continue;
			}

			printf("%d月最多有%d天\n", jiyue, month_run(jiyue)); 
			continue;
		}
		case 5:
		{
			return 0; 
			continue;
		}

		default:printf("输入错误,请重新输入\n"); continue;

		system("pause");

		}

	}


	
	
	return 0;
}

运行环境为vs2022

主要问题如下
1,用户界面不够美观
2,存在一些输入性的bug需要修改

点击查看代码
{
			int jiyue = 0;
			printf("请输入月份:\n");
			scanf_s("%d", &jiyue);
			if (jiyue >= 13 || jiyue <= 0)//1-12月分以外数据报错。
			{
				printf("不好意思,您输入的数据有误。");
				continue;
			}

			printf("%d月最多有%d天\n", jiyue, month_run(jiyue)); 
			continue;
		}
增加了输入非正常年份的判定,提示输入错误 ![](https://img2024.cnblogs.com/blog/3398612/202403/3398612-20240305122552220-1781638399.png)

本次重构的软件,主要的难点在于读懂过去的代码,理解编写者的思维。明确进行逆向软件工程的目的和需求,是为了理解软件系统的功能、修复漏洞、提升性能还是进行其他目的。选择合适的工具和技术来进行逆向软件工程,例如反编译器、调试器、静态分析工具等,以帮助我们获取软件系统的源代码、数据结构和算法等信息。在进行逆向软件工程时,需要遵守相关的法律法规和道德准则,确保不侵犯知识产权和隐私等权益。在进行正式使用时,不得使用盗版软件。同时经常进行逆向软件工程也有利于编程经验的提升。
2024.3.5

posted @ 2024-03-05 12:38  Aegsis  阅读(9)  评论(0编辑  收藏  举报