2014年05月13日

practice 1-18:delete blank or'\t' at the end of one line of words,also before the \n

#include
#define MAXWORD 1000

int getline(char line[],int maxline);
int removes(char s[]);
int main()
{
 char line[MAXWORD];
 int len;
 while((len=getline(line,MAXWORD))>0)
  if(removes(line)>0)
   printf("%s",line);
 return 0;
}

int getline(char s[],int lim)
{
 int c,i;

 for(i=0;i
 {
  s[i]=c;
  
 }
 if(c=='\n')
 {
  s[i]=c;
  
  ++i;
 }
 s[i]='\0';
 return i;
}
int removes(char s[])
{
 int i=0;
 while(s[i]!='\n')
  ++i;
 --i;
 while(i>=0&&(s[i]==' '||s[i]=='\t'))
  --i;
 if(i>=0)
 {
  ++i;
  s[i]='\n';
  ++i;
  s[i]='\0';
 }
 return i;
}

以下是用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、付费专栏及课程。

余额充值