برنامه نویسی به زبان ارلنگ

ارلنگ زبان برنامه نویسی تابعی و همروند است

برنامه نویسی به زبان ارلنگ

ارلنگ زبان برنامه نویسی تابعی و همروند است

برنامه نویسی به زبان ارلنگ

در این وبلاگ مطالب مربوط به ارلنگ و الیکسیر منتشر خواهد شد

  • ۰
  • ۰

این مثال عینا شبیه مثال قبل است با این تفاوت که فرمول محاسبه مبلغ نهایی تفاوت دارد.

A = P (1 + nr )^nt

در این مثال با توجه به اینکه تنها به دو رقم اعشار در جواب نیاز دارم از .2f استفاده کردم که در تابع io:format مشهود است. 

-module(ex13).
-export([main/0]).
-compile([debug_info]).

main() ->
	try run()
	catch
		error:_Error -> io:format(
			"Please Enter only Numeric type~n");
		throw:negativeNumber -> io:format(
			"Please Enter only non-zero positive number~n")
	end.

run() ->
	{P,R,T,N} = readItems(),
	A = calc_Investment({P,R,T,N}),
	io:fwrite(
		"$~w invested at ~w% for ~w years compounded ~w times per year is $~.2f",
		[P,R,T,N,A]).

calc_Investment({P,R,T,N}) -> 
	P*(math:pow(1+R/(N*100),N*T)).
	
readItems() ->
	{ok,[P]} = io:fread(
		"Enter the principal: ","~d"),
	{R,_} = string:to_float(string:strip(
			io:get_line("Enter the rate of interest: "), right, $\n)),
	{ok,[T]} = io:fread(
		"Enter the number of years: ","~d"),
	{ok,[N]} = io:fread(
		"What is the number of times the interest is compounded per year? ","~d"),
	if
		P =< 0 orelse R =< 0 orelse T =< 0 orelse N =< 0 -> 
			throw(negativeNumber);
		true -> ok
	end,
	{P,R,T,N}.
  • ۹۷/۰۲/۲۸
  • مهدی حسینی مقدم

ارلنگ

مثال هایی در ارلنگ

نظرات (۰)

هیچ نظری هنوز ثبت نشده است

ارسال نظر

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی