Chinaunix首页 | 论坛 | 博客
  • 博客访问: 169504
  • 博文数量: 84
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 940
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-12 20:30
文章分类

全部博文(84)

文章存档

2010年(18)

2009年(27)

2008年(39)

我的朋友
最近访客

分类:

2008-11-07 09:33:05

最近在学awk了,所以看完awk后想弄几个题实践一下,刚好找了个题,应该算解决了,但不知道有没有另外的简单办法。

题目是这样的:
Mike Harrington:(510) 548-1278:250:100:175
Christian Dobbins:(408) 538-2358:155:90:201
Susan Dalsass:(206) 654-6279:250:60:50
Archie McNichol:(206) 548-1348:250:100:175
Jody Savage:(206) 548-1278:15:188:150
Guy Quigley:(916) 343-6410:250:100:175
Dan Savage:(406) 298-7744:450:300:275
Nancy McNeil:(206) 548-1278:250:80:75
John Goldenrod:(916) 348-4278:250:100:175
Chet Main:(510) 548-5258:50:95:135
Tom Savage:(408) 926-3456:250:168:200
Elizabeth Stachelin:(916) 440-1763:175:75:300


要求输出成


                    ***CAMPAIGN 1998 CONTRIBUTIONS***

----------------------------------------------------------------------

NAME                 PHONE            Jan  |  Feb  |  Mar | Total Donated

----------------------------------------------------------------------

Mike Harrington     (510) 548-1278    250.00   100.00  175.00   525.00

Christian Dobbins   (408) 538-2358    155.00    90.00  201.00   446.00

Susan Dalsass       (206) 654-6279    250.00    60.00   50.00   360.00

Archie McNichol     (206) 548-1348    250.00   100.00  175.00   525.00

Jody Savage         (206) 548-1278     15.00   188.00  150.00   353.00

Guy Quigley         (916) 343-6410    250.00   100.00  175.00   525.00

Dan Savage          (406) 298-7744    450.00   300.00  275.00  1025.00

Nancy McNeil        (206) 548-1278    250.00    80.00   75.00   405.00

John Goldenrod      (916) 348-4278    250.00   100.00  175.00   525.00

Chet Main           (510) 548-5258     50.00    95.00  135.00   280.00

Tom Savage          (408) 926-3456    250.00   168.00  200.00   618.00

Elizabeth Stacheli  (916) 440-1763    175.00    75.00  300.00   550.00

----------------------------------------------------------------------

                              SUMMARY

----------------------------------------------------------------------

The campaign received a total of $6137.00 for this quarter.

The average donation for the 12 contributors was $511.42.

The highest contribution was $300.00.

The lowest contribution was $15.00.

****************************************
我一直不懂的是他的最高值我想的应该是450,他怎么是300,这个有点不太清楚,我自己写的程序里面
是找出这个季度里面的最大值和最小值。
程序如下:
#!/usr/bin/awk -f
#这是我的awk的路径,不一样的话可能要改一下
# 从网上找来的练习题
# 按它的要求输出,
BEGIN{
FS=":";
{print " ***CAMPAIGN 1998 CONTRIBUTIONS***";
print "----------------------------------------------------------------";
print "NAME PHONE Jan | Feb | Mar | Total Donated";
print "----------------------------------------------------------------"
#初始化最大值和最小值
min=10000;
max=0;
}}

#计算每行的总数变量:tot和tot的总数total;
{tot=$3+$4+$5;
total+=tot;
#找到每行数据的最大值和最小值放入min,max
{for(i=3;i<=5;i++)
{if($i if($i>max) max=$i;
}}
printf("%-20s %-15s\t %.2f\t %3.2f\t %3.2f\t %3.2f\n",$1,$2,$3,$4,$5,tot )}
END{
print "----------------------------------------------------------------";
print " SUMMERY"
print "----------------------------------------------------------------";
printf("The campaign received a total of $%.2f for this quarter.\n",total) ;
printf("The average donation for the %d contributors was $%.2f .\n",NR,total/NR);
printf("The MAX NUM is :%.2f ,the MIN NUM is :%.2f .\n",max,min);
}


阅读(787) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-05-19 21:13:24

BEGIN{ FS=":"; na="NAME"; ph="PHONE"; monat="Jan | Feb | Mar |"; to="Total Donated"; su="summe" leer=""; printf("\t\t***CAMPAIGN 1998 CONTRIBUTIONS***\n"); printf("-------------------------------------------------------------------------------------\n"); printf("%-25s%-20s%-27s%-13s\n",na,ph,monat,to); printf("-------------------------------------------------------------------------------------\n"); max=0; min=10000; } { max=($3 >max ?$3:max)