Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

版权申明
12
返回列表 发新帖
楼主: mutou

练习题:成绩统计(原来Lazarus的编程效率可以很高的)

[复制链接]

该用户从未签到

发表于 2012-5-9 20:42:47 | 显示全部楼层
学习学习!
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-8-12 00:09:49 | 显示全部楼层
告诉你吧,不是代码越少就意味着效率越高的,你们老师错的很严重。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-5-8 09:25:28 | 显示全部楼层
我用lazarus成功实现,但我的程序光读入处理就有20行左右,程序总长60行左右。或许我的读入处理有问题吧。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-5-30 23:48:32 | 显示全部楼层
本帖最后由 delphilisp 于 2014-5-31 01:51 编辑
  1. program noi;
  2. uses
  3.   Classes,Sysutils;
  4. var
  5.   I,J,K: Integer;ZF,FX: Currency; TxtList, strs, OutList: TStringList;ArrCurr: array of Currency;
  6. begin
  7.   TxtList := TStringList.Create;strs := TStringList.Create;OutList := TStringList.Create; TxtList.LoadFromFile('Score_In.txt');OutList.Append(TxtList[0]);SetLength(ArrCurr, TxtList.Count-1);for I := 0 to High(ArrCurr) do ArrCurr[I] := 0;strs.Delimiter:= #$9;
  8.   for I := 1 to TxtList.Count-1 do
  9.   begin
  10.     strs.DelimitedText:= TxtList[I]; ZF := 0;FX := 0;
  11.     for J := 2 to strs.Count-1 do
  12.       if J < 8 then
  13.         ZF := ZF + StrToCurr(strs[J])
  14.       else if J < 12 then
  15.         ZF := ZF + 0.5 * StrToCurr(strs[J])
  16.       else if Ord(Upcase(strs[J][1])) < Ord('C') then
  17.         FX := FX + (Ord('C') - Ord(Upcase(strs[J][1])))*5;
  18.     if FX > 20 then FX := 20;ZF := ZF + FX;Strs.Insert(0, Format('%.1f', [ZF]));K :=-1;if Length(Strs[2]) < 12 then Strs.Insert(3, '');
  19.     for J := 0 to I-1 do
  20.       if ZF > ArrCurr[J] then
  21.       begin
  22.         FX := ArrCurr[J];ArrCurr[J] := ZF;ZF := FX;if K<0 then K := J+1;
  23.       end;
  24.     OutList.Insert(K, Strs.DelimitedText);
  25.   end;
  26.   J := 0;ZF := 10000;
  27.   for I := 1 to OutList.Count-1 do
  28.   begin
  29.     if ArrCurr[I-1] < ZF then Inc(J);ZF := ArrCurr[I-1]; OutList[I] := IntToStr(J)+#$9+OutList[I];
  30.   end;
  31.   OutList.SaveToFile('Score_Out.txt');TxtList.Free;strs.Free;OutList.Free;
  32. end.
复制代码
这样不到40行,满足要求。不过这有什么意义?完全对编程的错误引导,没有实际工程经验人乱提要求,误人子弟,害人不浅。

回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2014-5-31 01:58:06 | 显示全部楼层
  1. #!/usr/local/bin/perl
  2. use strict;
  3. use warnings;
  4. my$infile=shift;
  5. my$outfile=shift;
  6. $infile = 'Score_In.txt' unless $infile;
  7. $outfile = 'Score_Out.txt' unless $outfile;
  8. my@data=();
  9. open INFILE, "<$infile" or die "读取文件[$infile]错误: $!";
  10. my$title = <INFILE>;
  11. while(<INFILE>) {
  12.         next unless /^\t\t(.*)/;
  13.         my%row=(zf => 0, txt => $1);
  14.         my@cs = split /\t/, $1;
  15.         my($i,$fx) = (0,0);       
  16.         foreach (@cs) {
  17.                 next unless $_;
  18.                 if($i > 1) {
  19.                         if($i < 8) {
  20.                                 $row{zf} += $_;
  21.                         } elsif($i < 12) {
  22.                                 $row{zf} += 0.5*$_;
  23.                         } elsif(uc($_) eq 'A' or uc($_) eq 'B') {
  24.                                 $fx += (ord('C') - ord(uc($_))) * 5;
  25.                         }
  26.                 }
  27.                 $i++;
  28.         }
  29.         $row{zf} += $fx>20?20:$fx;
  30.         push @data, \%row;
  31. }
  32. close INFILE;
  33. my@sdata = sort { $b->{zf} <=> $a->{zf} } @data;
  34. my($zf,$no) = (10000,0);
  35. open OUTFILE, ">$outfile" or die "打开输出文件[$outfile]错误: $!";
  36. print OUTFILE $title;
  37. foreach my$ptr(@sdata) {
  38.         $no++,$zf=$ptr->{zf} if $zf > $ptr->{zf};
  39.         print OUTFILE "$no\t",sprintf("%.1f",$ptr->{zf}),"\t$ptr->{txt}\n";
  40. }
  41. close OUTFILE;
复制代码
用Perl简单很多,思路一样,不同的语言相同的行数信息量完全不一样,不应该追求最小行数,而应该精炼算法。
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-1-14 06:34 , Processed in 0.027711 second(s), 6 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表