2014年05月13日

practice 2-3:turn hexadecimal into decade

 

#include
#define MAXLINE 100
int ten_digit(char s[]);
int power(int m,int n);
int change(char c);

main()
{
 char a[MAXLINE];
 int result;
 gets(a);
 result=ten_digit(a);
 printf("%d",result);
 return 0;

}
int ten_digit(char s[])
{
 int len;
 int i,n;
 int sum=0;
 len=strlen(s);
 for(i=len-1,n=0;i>1;i--,n++)
  sum=sum+change(s[i])*power(16,n);
 
 return sum;

}
int change(char c)
{
 if(c<='9'&&c>='0')
   return (c-'0');
 if(c<='z'&&c>='a')
   return (c-'a')+10;
 if(c<='Z'&&c>='A')
   return (c-'A')+10;
}
int power(int m,int n)
{
 if(n==0)
  return 1;
 else
  return m*power(m,n-1);
}

以下是用Fortran语言编写的代码: (1) ``` program week_temperature implicit none integer :: i real :: temp(7), max_temp, avg_temp ! 循环输入一周最高气温 do i = 1, 7 write(*,*) "请输入第", i, "天的最高气温:" read(*,*) temp(i) end do ! 判断其中最高气温 max_temp = temp(1) do i = 2, 7 if (temp(i) > max_temp) then max_temp = temp(i) end if end do ! 计算一周平均最高气温 avg_temp = sum(temp) / 7 ! 输出结果 write(*,*) "一周中最高气温为:", max_temp, "℃" write(*,*) "一周平均最高气温为:", avg_temp, "℃" end program week_temperature ``` (2) ``` program month_temperature implicit none integer :: i, max_index, min_index real :: temp(28), max_temp, min_temp, avg_temp character(len=10) :: date(28) ! 使用数组输入一月最高气温 temp = [13.0, 4.0, 8.0, 10.0, 8.0, 8.0, 9.0, 7.0, 7.0, 6.0, 9.0, 10.0, 4.0, 7.0, 9.0, 10.0, 9.0, 13.0, 11.0, 14.0, 5.0, 6.0, 5.0, 10.0, 9.0, 10.0, 12.0, 13.0] ! 计算一月平均最高气温 avg_temp = sum(temp) / 28 ! 判断其中最高、最低气温 max_temp = temp(1) min_temp = temp(1) max_index = 1 min_index = 1 do i = 2, 28 if (temp(i) > max_temp) then max_temp = temp(i) max_index = i end if if (temp(i) < min_temp) then min_temp = temp(i) min_index = i end if end do ! 输出最高、最低气温值及其在该月中的天数(第几天)、期 date = ["2023/02/01", "2023/02/02", "2023/02/03", "2023/02/04", "2023/02/05", "2023/02/06", "2023/02/07", "2023/02/08", "2023/02/09", "2023/02/10", "2023/02/11", "2023/02/12", "2023/02/13", "2023/02/14", "2023/02/15", "2023/02/16", "2023/02/17", "2023/02/18", "2023/02/19", "2023/02/20", "2023/02/21", "2023/02/22", "2023/02/23", "2023/02/24", "2023/02/25", "2023/02/26", "2023/02/27", "2023/02/28"] write(*,*) "一月中最高气温为:", max_temp, "℃,出现在第", max_index, "天,即", date(max_index) write(*,*) "一月中最低气温为:", min_temp, "℃,出现在第", min_index, "天,即", date(min_index) end program month_temperature ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值