Liberty Basic Payroll Calculator IF ELSE - calculator

I'm just getting started with learning basic programming with Sams and already am having issues with the simple payroll calculator I've been trying to write. Could someone explain how I could better rewrite this? And can you only follow IF THEN statements with a print command? I think I'll have to invest in a newer book but I was hoping I could get this to run at least in the meantime.
Input "Please input payrate: "; Ans$
Print
Input "Please input hours worked: "; Hrs$
If (Hrs$ >= "40") Then
payRate = (Hrs$ * Ans$)
Else If (Hrs$ <= "41") Then
payRate = Hrs$ * (1.5 * Ans$)
End If
taxRate = payRate * .15
grossPay = payRate * Hrs$
netPay = payRate - taxRate
Print
Print: "Your net pay is: "; netPay

Also, I see you have an else if statement. One of the perplexing design decisions in Liberty Basic is that it doesn't support the else if/elseif statement that you might find in most BASIC language dialects.
From the online manual:
The elseif keyword is not supported. Here is an >example using elseif (QBasic):
'QBasic only
if sis boom bah then
print "Yippie!"
elseif la dee da then
print "So what!"
end if
Instead in Liberty BASIC, it looks like this:
'Liberty BASIC - no elseif
if sis boom bah then
print "Yippie!"
else
if la dee da then
print "So what!"
end if
end if
And to extend the above official documentation a little further with my own code sample in LB (Liberty Basic), you might just appreciate how deeply nested the if, else, end if can become.
'Liberty BASIC
grade = 55
if grade > 90 then
print "Excellent!"
else
if grade > 80 and grade < 90 then
print "Good"
else
if grade > 70 and grade < 80 then
print "We need to try a little harder."
else
if grade > 60 and grade < 70 then
print "We need to try a lot harder!"
else
print "Let's have a talk."
end if
end if
end if
end if
end
The equivalent in another dialect of BASIC, in this case FreeBasic, might look something like this which of course appears much tidier with only one end if vs a trail of end if's:
'FreeBasic
Dim grade as integer = 55
if grade > 90 then
print "Excellent!"
elseif grade > 80 and grade < 90 then
print "Good"
elseif grade > 70 and grade < 80 then
print "We need to try a little harder."
elseif grade > 60 and grade < 70 then
print "We need to try a lot harder!"
else
print "Let's have a talk."
end if
sleep
end
But all is not lost with LB because the language actually implements a fairly powerful select case statement which can include conditional statements which is not possible in certain dialects of BASIC (i.e. PureBasic)
'LB - Liberty Basic
grade = 55
select case grade
case grade > 90
print "Excellent!"
case (grade > 80) and (grade < 90)
print "Good"
case (grade > 70) and (grade < 80)
print "We need to try a little harder."
case (grade > 60) and (grade < 70)
print "We need to try a lot harder!"
case else
print "Let's have a talk."
end select

I don't use Liberty, but I imagine it's complaining when you're trying to do math on strings. Additionally, I find your misuse of the "payrate" name to be confusing. The logic of the overtime calc is also wrong. Here's my first attempt at a rewrite:
Input "Please input payrate: "; Rate$
Rate = val(Rate$)
Print
Input "Please input hours worked: "; Hrs$
Hrs = val(Hrs$)
Gross = Hrs * Rate
If Hrs > 40 Then Gross = Gross + ((Hrs - 40) * Rate / 2))
Taxes = Gross * .15
Net = Gross - Taxes
Print
Print: "Your gross pay is: "; Gross
Print: "Your net pay (after $";Taxes;" in taxes) is: "; Net

This code will work in Liberty BASIC:
INPUT "Please enter hours worked: "; hours 'you don't need a dollar sign at the end of the variable because it's a value not a string
INPUT "Please enter your hourly rate: "; hourlyRate
gross = hours * hourlyRate 'works out the gross amount
IF (hours > 40) THEN 'if the person worked more than 40 hours, then the following command is applicable...
gross = 40 * hourlyRate + ((hours - 40) * (hourlyRate * 1.5)) 'this line calculates the money earned for 40 hours, and adds the remaining hours at 1.5 times the hourly rate
END IF 'end the IF condition
tax = gross * 0.15 'calculates the tax at 15%
net = gross - tax 'calculates the net pay (i.e., gross pay after tax)
PRINT "-------------------------------"
PRINT "Your gross pay is: "; gross
PRINT "Your net pay is: "; net
As the previous poster suggested, you should use values instead of strings for maths (values don't have a dollar sign at the end), and when you label your variables they should have a meaningful name in relation to what you're trying to achieve.
Liberty BASIC has a tutorial under Help that you can use to teach yourself how to code. Otherwise Beginning Programming for Dummies is another useful guide - that's what I'm using :)

Related

How to add all or some conditions to alert ? - Pine script tradingview

I have a list of conditions for a candle. This is just an example:
aa = (close > open) and (low > low[1])
bb = (close[1] > open[1]) and (close[1] > 5)
cc = (close > ((high[1] - low[1])*23.6/100 + low[1]))
dd = (close > EMA34) and (close > ema(close, 10))
I set alert using the following code:
if aa
alert("A- " + tostring(close, "#.######"), alert.freq_once_per_bar)
else if bb
alert("B- " + tostring(close, "#.######"), alert.freq_once_per_bar)
else if cc
alert("C- " + tostring(close, "#.######"), alert.freq_once_per_bar)
else if dd
alert("D- " + tostring(close, "#.######"), alert.freq_once_per_bar)
With each condition, I will get an alert with a letter at the beginning of the message so I can know the priority of the conditions, ie A is the best, D is the last one.
I would like to know if there is any way to check all conditions at the same time, so I can set the priority like:
if the alert has all conditions fulfilled, so it's the best A
if the alert has at least 3 conditions fulfilled, then it's B
if the alert has at least 2 conditions, then it's C
and if there is only 1 condition fulfilled, then it will be D
The real list has more than 10 conditions so I cannot check them manually. Please give me some code to do it programmatically.
I think, it's something related to array but I don't know how to do it.
Thank you for helping me.
aa = (close > open) and (low > low[1]) ?1:0
bb = (close[1] > open[1]) and (close[1] > 5) ?1:0
cc = (close > ((high[1] - low[1])*23.6/100 + low[1])) ?1:0
dd = (close > EMA34) and (close > ema(close, 10)) ?1:0
number_of_condition_true=aa+bb+cc+dd
bool all_condition_true=na
bool three_condition_true=na
bool two_condition_true=na
bool one_condition_true=na
bool none_condition_true=na
if number_of_condition_true>=4
all_condition_true:=true
else if number_of_condition_true>=3
three_condition_true:=true
else if number_of_condition_true>=2
two_condition_true:=true
else if number_of_condition_true>=1
one_condition_true:=true
else if number_of_condition_true==0
none_condition_true:=true
this is one of the ways ,this code will help you in programming your logic. in the above code i have replaced it into numerical value if true , than i have added all , this will give you the overall how may are true at a time.

Why am I getting an additional riser/bar in my chart in crystal reports

So I have a chart that looks like this:
Here is what I have in the chart expert:
And here is the code for #ZZ_TAT_Name - A:
if {IB_MESSAGES.MSG_ID} = next({IB_MESSAGES.MSG_ID}) and {IB_MESSAGES.MSG_ID} = previous({IB_MESSAGES.MSG_ID}) then "na" else
IF {#Final Turnaround Minutes} > 0 and {#Final Turnaround Minutes} < 15 THEN "UNDER 15"
ELSE IF {#Final Turnaround Minutes} >= 15 and {#Final Turnaround Minutes} < 30 THEN "BETWEEN 15 & 30"
ELSE IF {#Final Turnaround Minutes} >= 30 and {#Final Turnaround Minutes} < 60 THEN "BETWEEN 30 & 60"
ELSE if {#Final Turnaround Minutes} > 60 then "OVER 60"
//else "*Total Messages"
I am wanting to get rid of the riser that shows 11. That riser is the total DistinctCount of IB_MESSAGES.MSG_ID. How would I be able to remove this? Why is this even showing up?
From what you've mentioned below, it seems 11 out of 22 records appear to fall into none of the if-else categories, leading to the unlabeled blue column. The easiest way to proceed is to intervene before the chart is rendered and reduce the incoming data to only values that belong in the other columns:
There's a few ways to handle this, but I would recommend adding an intermediary Formula that returns the exact same records, except any record that doesn't meet the if/else standards will be replaced with null. Something like:
IF (
{IB_MESSAGES.MSG_ID} = next({IB_MESSAGES.MSG_ID})
AND
{IB_MESSAGES.MSG_ID} = previous({IB_MESSAGES.MSG_ID})
)
OR (
{#Final Turnaround Minutes} > 0
AND
{#Final Turnaround Minutes} > 60
)
THEN
//repeat the original value here
ELSE
null
Done correctly, the intermediary formula will return 11 records and 11 nulls when evaluated in the Details section of the report. From here, you can change your chart to read the formula and it should evaluate only 11 non-null values.

Finding notes sounding at the same time in a different voice in a midi file

I have a midi file consisting of two parts. Now I need to print out, for each note in part 0 (including rests), which note sounds at the same time in part 1 (and the note that proceeds this).
I can go through all of the notes in part 0 with music21, but how do I find the note at that time in part 1. Will I need to use end times? Or is there a function for this?
for thisNote in s.parts[0].notes:
print thisNote.ps
Here is a very interesting function that let me solve this:
secondPart = s.parts[1]
for thisNote in s.parts[0].notes:
sys.stdout.write('at ' + str(thisNote.offset) + ' toppitch: ' + str(thisNote.ps))
soundingNotes = secondPart.getElementsByOffset(thisNote.offset, mustFinishInSpan=False, mustBeginInSpan=False)
try:
if soundingNotes > 0:
basslength = len(soundingNotes)
except:
basslength = 0
if basslength > 1:
print "oh ow, more then one note sounding in the bass."
if basslength > 0:
if soundingNotes[0].isRest:
sys.stdout.write(' bottompitch: R')
else:
sys.stdout.write(' bottompitch: ' + str(soundingNotes[0].ps)) # + ' from ' + str(soundingNotes[0].offset))
# print the previous note
soundingNotes2 = secondPart.getElementsByOffset(thisNote.offset-1, mustFinishInSpan=False, mustBeginInSpan=False)

Relative formatting for timestamps

I wrote the CS 462 Office Hours app over the past two days. The most recent iteration tells the user when the next office hour time block will be. Right now, it just formats it as "Thursday (February 3) at 3 PM." I'd like it to be a little smarter, though, and say something like "this afternoon at 3 PM" or "tomorrow at 10 AM".
This is similar to Twitter's relative timestamps on tweets (it says "3 minutes ago" or "23 hours ago;" beyond that it lists the date). In my case, though, it will be the opposite, since we're dealing with future times.
Basically, it needs to be smart enough to know that an event is today or tomorrow. Beyond that, I just want to display the date and day of the week.
Is there a way to do this with KRL? Do I just need to use logic like this and write a function (or module)?
These are the functions I have:
// First define some constants to measuring relative time
now = time:now({"tz": "America/Denver"});
midnight = time:new("23:59:59.000-07:00");
tomorrow_midnight = time:add(midnight, {"days": 1});
yesterday_midnight = time:add(midnight, {"days": -1});
// Functions for calculating relative time
relativefuturedate = function(d){
ispast(d) => "today (past) at " + justtime(d)
| istoday(d) => "today at " + justtime(d)
| istomorrow(d) => "tomorrow at " + justtime(d)
| datetime(d);
};
istoday = function(d){
d > now && d <= midnight;
};
istomorrow = function(d){
not istoday(d) && d <= tomorrow_midnight;
};
ispast = function(d){
d < now;
};
isfuture = function(d){
d > now;
};
justtime = function(d){
time:strftime(d, "%l:%M %p");
};
datetime = function(d){
time:strftime(d, "%A (%B %e) at %l:%M %p");
};
This should do the trick. Right now I'm testing it with this rule:
rule first_rule {
select when pageview ".*"
pre {
today_9 = time:new("2011-02-09T09:00:00.000-07:00");
today_12 = time:new("2011-02-09T12:00:00.000-07:00");
today_4 = time:new("2011-02-09T16:00:00.000-07:00");
tomorrow_9 = time:new("2011-02-10T09:00:00.000-07:00");
tomorrow_4 = time:new("2011-02-10T16:00:00.000-07:00");
nextday_9 = time:new("2011-02-11T09:00:00.000-07:00");
relative_now = relativefuturedate(now);
relative_today_9 = relativefuturedate(today_9);
relative_today_12 = relativefuturedate(today_12);
relative_today_4 = relativefuturedate(today_4);
relative_tomorrow_9 = relativefuturedate(tomorrow_9);
relative_tomorrow_4 = relativefuturedate(tomorrow_4);
relative_nextday_9 = relativefuturedate(nextday_9);
message = <<
Now: #{relative_now}<br />
Today at 9: #{relative_today_9}<br />
Today at 12: #{relative_today_12}<br />
Today at 4: #{relative_today_4}<br />
Tomorrow at 9: #{relative_tomorrow_9}<br />
Tomorrow at 4: #{relative_tomorrow_4}<br />
Day after tomorrow at 9: #{relative_nextday_9}<br />
>>;
}
notify("Time calculations:", message) with sticky=true;
}
However, that doesn't seem to be working yet. I get this output:
Can anyone see what's wrong?
There currently isn't built in functionality to do this in KRL. You will probably need to write a function or a module to do this and I would love to see it if/when you do.

Perl recursion help

I'm trying to write a program that will calculate the dividends of stocks. I did this without a subroutine. Right now, I'm trying to modify it so it can run using a recursive routine. Any help with this? Because I'm not so good at this.
Here's the original script + a pathetic attempt.
print "A stock xyz's price is now $100. It has 3.78% dividend. You have 1000 of it and reinvest the dividend into the stock.\n";
my %hash;
#stocknum = 1000;
#dividend = 6780;
while ($#dividend != 20) {
$a = $dividend[-1];
$stock = $stocknum[-1];
$div_total= $stock*100*0.0678;
$stock_total = $stock + int($a/100);
push (#stocknum, $stock_total);
push (#dividend, $div_total);
if ($#dividend == 20) {
last;
}
}
shift (#dividend);
$stock_num = $stocknum[-1];
$div = $stock_num*100*0.0678;
push (#dividend, $div);
#hash{#stocknum} = #dividend;
foreach $key(sort keys %hash) {
print "Stock number: $key\t"."Dividend: $hash{$key}\n";
}
$dividend=0.0378;
I don't think you want recursion. I think you just want to loop over the number of cycles of payouts that you're after. It looks like you're getting all mixed up with arrays for some reason.
print <<'HERE';
A stock xyz's price is now $100. It has 6.78% dividend.
You have 1000 of it and reinvest the dividend into the stock.
HERE
my $shares = 1000;
my $price = 100;
my $dividend = 6.78 / 100;
my $cycles = $ARGV[0] || 20;
foreach ( 1 .. $cycles ) {
local $cycle = $_;
local $payout = $shares * $dividend * $price;
local $new_shares = $payout / $price;
write();
$shares += $new_shares;
}
format STDOUT =
#### #####.###### ######.####### ###.###### #####.######
$cycle, $shares, $payout, $new_shares, $shares+$new_shares,
.
format STDOUT_TOP =
###.####%
$dividend
Cycle Shares Payout New Shares Total Shares
----------------------------------------------------------------
.
This gives me the output:
A stock xyz's price is now $100. It has 6.78% dividend.
You have 1000 of it and reinvest the dividend into the stock.
0.0678%
Cycle Shares Payout New Shares Total Shares
----------------------------------------------------------------
1 1000.000000 6780.0000000 67.800000 1067.800000
2 1067.800000 7239.6840000 72.396840 1140.196840
3 1140.196840 7730.5345752 77.305346 1217.502186
4 1217.502186 8254.6648194 82.546648 1300.048834
5 1300.048834 8814.3310942 88.143311 1388.192145
6 1388.192145 9411.9427423 94.119427 1482.311572
7 1482.311572 10050.0724603 100.500725 1582.812297
8 1582.812297 10731.4673731 107.314674 1690.126971
9 1690.126971 11459.0608610 114.590609 1804.717579
10 1804.717579 12235.9851873 122.359852 1927.077431
11 1927.077431 13065.5849830 130.655850 2057.733281
12 2057.733281 13951.4316449 139.514316 2197.247597
13 2197.247597 14897.3387104 148.973387 2346.220985
14 2346.220985 15907.3782750 159.073783 2505.294767
15 2505.294767 16985.8985220 169.858985 2675.153752
16 2675.153752 18137.5424418 181.375424 2856.529177
17 2856.529177 19367.2678194 193.672678 3050.201855
18 3050.201855 20680.3685775 206.803686 3257.005541
19 3257.005541 22082.4975671 220.824976 3477.830517
20 3477.830517 23579.6909021 235.796909 3713.627426
Don't worry about my use of format; I've had that on the brain this weekend since I rewrote some perlfaq stuff about it then also turned it into Use formats to create paginated, plaintext reports. You could just as easily created the output with printf:
print <<'HERE';
A stock xyz's price is now $100. It has 6.78% dividend.
You have 1000 of it and reinvest the dividend into the stock.
Cycle Shares Payout New Shares Total Shares
----------------------------------------------------------------
HERE
my $shares = 1000;
my $price = 100;
my $dividend = 6.78 / 100;
my $cycles = $ARGV[0] || 20;
foreach ( 1 .. $cycles ) {
my $payout = $shares * $dividend * $price;
my $new_shares = $payout / $price;
printf "%4d %12.6f %12.6f %10.6f %12.6f\n",
$_, $shares, $payout, $new_shares, $shares + $new_shares;
$shares += $new_shares;
}
As a side note, you really don't ever want recursion, and especially not in Perl if you can help it. Other languages get away with it because they know how to unroll your recursion to turn it into an iterative process. Perl, being a dynamic language, can't really do that because it doesn't know if the subroutine will have the same definition on the next go around. It's nice as a computer science topic because it makes the programming marginally easier and they know it all works out in the end. I think I talk about this in Mastering Perl somewhere, but Mark Jason Dominus covers it extensively in Higher-Order Perl. Basically, instead of recursion you use a queue, which is a better skill to practice anyway.