/******************************************************************************
按年份算生肖-多种语言的代码实现
author:chinayaosir
QQ:44633197
blog http://blog.csdn.net/chinayaosir
********************************************************************************/
1.计算生肖算法的实现(C语言)2.计算生肖算法的实现(C++语言)
3.计算生肖算法的实现(PHP语言)
4.计算生肖算法的实现(Java语言)
5.计算生肖算法的实现(C#语言)
6.计算生肖算法的实现(vb.net语言)
/**************************************************************************
chinayaosir用多个计算机语言实现按年份计算生肖功能代码,
是我们认识各个语言一个非常好的方法!
希望对广大程序爱好者能提供一个学习和分析的方法,tks!
计算生肖算法的不同语言实现的想法是chinayaosir在11/09/2007晚上想到的!
/*************************************************************************/
1.计算生肖算法的实现(C语言)
//filename calxx.c 编译通过vc++6.0
#include <stdio.h>
char* calxs(int year){
int i=year%12;
char* name[12]={"猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊"};
return name[i];
}
int main(){
int birth=300;
printf("出生年份:");
scanf("%d",&birth);
printf("%d年的生肖是:%s \n",birth,calxs(birth));
return 0;
}
---------------------------------------------------------------------------
2.计算生肖算法的实现(C++语言)
//filename calxx.cpp 编译通过vc++6.0
#include <string>
#include <iostream>
using namespace std;
string calxs(int year){
int xx=year%12;
string name[12]={"猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊"};
return name[xx];
}
int main(int argc, char *argv[]){
int birthday=3000;
cout<<"出生年号[3000]:";
cin>>birthday;
cout<<endl<<birthday<<"年的生肖是: "<<calxs(birthday)<<"\n";
return 0;
}
---------------------------------------------------------------------------
3.计算生肖算法的实现(PHP语言)
3.1 主程序index.php
<!--file name is index.php-->
<!--edit tools is Dreamweaver 8.0-->
<!--在windows+apache+mysql+php运行环境测试OK-->
<html><head><meta charset=gb2312" />
<title>年份计算出生肖</title>
</head>
<body>
<form action="calxs.php" method="post">
<table border="1" align="center" bordercolor="#000066" bgcolor="#66CC66">
<tr bgcolor="#666666">
<td width="128" bgcolor="#FFCCCC">
<div align="center">年份计算生肖</div></td>
<td width="80" bgcolor="#FFCCCC"><div align="center">输入年份</div></td>
<td align="center"><input name="birth" type="text" value="2000" size="8" maxlength="4"/></td>
<td colspan="2"><input type="submit" name="submit" value="计算" /></td>
</tr>
</table>
</form>
</body>
</html>
3.2处理程序calxs.php
<!--file name is calxs.php-->
<?phpfunction calxs($y){
$name=array('猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊');
$i=$y%12;
return $name[$i];
}
$birthyear=$_POST['birth'];
calxs($birthyear);
echo $birthyear.'年的生肖是:'.calxs($birthyear).'</br>';?>
---------------------------------------------------------------------------
4计算生肖算法的实现(Java语言)
//file name is abc.java
//编译javac abc.java
//调用java abc 2007
//结果:在命令行把年号以参数形式读入 java abc 2007,结果为猪
//开发环境:j2sdk1.4.2+editplus
//生肖类
calxsclass calxs{
private String name[]=new String[12];
private int year;
private String xs;
//构造函数初始化数据
public calxs(){
name[0]="猴";name[1]="鸡";name[2]="狗";
name[3]="猪";name[4]="鼠";name[5]="牛";
name[6]="虎";name[7]="兔";name[8]="龙";
name[9]="蛇";name[10]="马";name[11]="羊";
xs="";year=3000;
}
//得到年号的方法readyear()
public void setyear(int y){year=y;}
//年号转换成生肖的方法cal()
public void cal(){int y; y=year%12; xs=name[y];}
//打印结果的方法print()
public void print(){System.out.println(year+"年的生肖是:"+xs);}}
//调用生肖类calxs的主类abc,包含main方法
class abc{
public static void main ( String args[] ){
int year=2000;
year=Integer.parseInt(args[0]);
calxs a=new calxs();
a.setyear(year);
a.cal();
a.print();
}
}
---------------------------------------------------------------------------
5.计算生肖算法的实现(C#语言)
//filename calxs.cs
using System;
namespace ConsoleApplication1{
class Program {
static void Main(string[] args) {
Console.Write("出生年份:");
int birth= 2010;
birth = int.Parse(Console.ReadLine());
Console.WriteLine(birth+"年的生肖是:" +calxs(birth));
Console.ReadLine();
}
static public String calxs(int year) {
int i = year % 12;
String[] name = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊" };
return name[i];
}
}
}
---------------------------------------------------------------------------
6.计算生肖算法的的实现(vb.net语言)
//filename Module1.vb
Imports System
Module Module1
Sub Main()
Dim birth As Integer
Dim sbirth As String
Console.Write("出生年份:")
sbirth = Console.ReadLine()
birth = CInt(sbirth)
Console.WriteLine(sbirth + "年的生肖是:" + calxs(birth))
Console.ReadLine()
End Sub
Public Function calxs(ByVal year As Integer) As String
Dim i As Integer = CInt(year Mod 12)
Dim name As String() = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"}
Return name(i)
End Function
End Module
---------------------------------------------------------------------------