Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

print "My name is Lw, No.9700001.

\n";
print "This is my homework #3.\n";
$|=1;
while (1){
print "Which work do you want to run? \n(1,2,3,4,5=homework1,2,3,4,5 lea
ve=0): ";
chomp($k=<STDIN>);
if ($k==0){
last; #0NX
}
print "\n============================= Homework ${k} ===================
===============\n";
if ($k==1){
print "This is the homework1.\n";
print "Please enter 3 numbers(A, B and C) for the quadratic equa
tions.\n\n";
my @Input = ("a","b","c");
for($i=0; $i<=$#Input; $i++){
print "The input number \'$Input[$i]\' is: ";
chomp($Input[$i]=<STDIN>);
}
#Input[0-2]Oa, b, c
$Denom = 2*$Input[0];
#(2a)
$Check = $Input[1]**2-4*$Input[0]*$Input[2];

#P_ (b^2-4ac)

print "\nThe equation you typed in is \" $Input[0](x^2)+$Input[1


](x)+$Input[2]=0 \"\n";
if($Input[0] == 0){
#a=0
if($Input[1] == 0){
#a=0&b=0
print "This equation has no answer.\n";
next;
}
else{
#a=0&b!=0
$x = -$Input[2]/$Input[1];
#-c/b
}
}
elsif($Check < 0){
#b^2-4ac < 0
$Real = -$Input[1]/$Denom;
#(-b/2a)
$Imag = sqrt(-$Check)/$Denom; #(sqrt(b^2-4ac) / 2a)
$x = $Real."+".$Imag."i or ".$Real."-".$Imag."i";
}
else{
#b^2-4ac > 0
$x1=(-$Input[1]+sqrt($Check))/$Denom;
$x2=(-$Input[1]-sqrt($Check))/$Denom;
$x = $x1." or ".$x2;
}
print "The answer is \"x = ${x}\"\n";
}
elsif ($k==2){
print "This is the homework2.\n";
for ($i=1;$i<16;$i++){
$sum=$sum+(1/$i);
}
print "The sum is \"$sum\".\n";
}

elsif ($k==3){
print "This is the homework3.\n";
for($i=1;$i<601;$i+=4){
$sum2=$sum2+$i**3;
}
print "The sum is \"$sum2\".\n";
}

){

elsif($k==4){
print "This is the homework4.\n";
for($i=0;$i<=8000;$i++){
if ($i%2!=0 or $i%3!=0 or $i%5!=0 or $i%7!=0 or $i%11!=0
}

$sum3=$sum3+$i;

}
print "The sum is \"$sum3\".\n";

elsif($k==5){
print "This is the homework5.\n";
for ($i=1;$i<10;$i++){
for($j=1;$j<10;$j++){
$x=$i*$j;
print "$x ";
}
print "\n";
}
}
print "\nPress Enter to continue...";
chomp($o=<STDIN>);
system("cls");
}#end of while

You might also like