【程式設計-C#】電費計算器(非時間電價,非營業用)

  • 台電 – 電價表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double D = Convert.ToDouble(textBox1.Text); //將輸入的度數字串轉成浮點數double
            double MS = 0, MW = 0; //MS夏日電費, MW非夏日電費;
            int L120 = 120, L330 = 330, L500 = 500, L700 = 700, L1000 = 1000;
            double s163 = 1.63, s238 = 2.38, s352 = 3.52, s461 = 4.61, s542 = 5.42, s613 = 6.13; //夏日費率
            double w163 = 1.63, w210 = 2.10, w289 = 2.89, w379 = 3.79, w442 = 4.42, w483 = 4.83; //非夏日費率

            //計算夏日電費
            if (D <= L120) MS = D * s163;
            else if (D <= L330)  MS = L120 * s163 + (D - L120) * s238 ;
            else if (D <= L500)  MS = L120 * s163 + (L330 - L120) * s238 + (D - L330) * s352;
            else if (D <= L700)  MS = L120 * s163 + (L330 - L120) * s238 + (L500 - L330) * s352 + (D - L500) * s461;
            else if (D <= L1000) MS = L120 * s163 + (L330 - L120) * s238 + (L500 - L330) * s352 + (L700 - L500) * s461 + (D - L700) * s542; 
            else                 MS = L120 * s163 + (L330 - L120) * s238 + (L500 - L330) * s352 + (L700 - L500) * s461 + (L700 - L1000) * s542 + (D - L1000) * s613; ;

            //計算非夏日電費
            if (D <= L120) MW = D * w163;
            else if (D <= L330) MW = L120 * w163 + (D - L120) * w210;
            else if (D <= L500) MW = L120 * w163 + (L330 - L120) * w210 + (D - L330) * w289;
            else if (D <= L700) MW = L120 * w163 + (L330 - L120) * w210 + (L500 - L330) * w289 + (D - L500) * w379;
            else if (D <= L1000) MW = L120 * w163 + (L330 - L120) * w210 + (L500 - L330) * w289 + (L700 - L500) * w379 + (D - L700) * w442;
            else MW = L120 * w163 + (L330 - L120) * w210 + (L500 - L330) * w289 + (L700 - L500) * w379 + (L700 - L1000) * w442 + (D - L1000) * w483; ;

            if (radioButton1.Checked) label4.Text = MS.ToString();
            else label4.Text = MW.ToString();
        }

        private void S2NS(object sender, EventArgs e) //2個RadioBuuton 的 共同 Click事件函式
        {
            RadioButton rb = (RadioButton)sender;
            if (rb.Checked) button1_Click(rb, e);
        }
    }
}

 

【程式設計-C#】將字串轉換為數值 Parse (Convert) & TryParse

從視窗文字方塊取得的輸入,雖然”看”起來是數值,本質是字串,需要進一步地將輸入的字串轉換成對應的數值型態,才能進行數值型態的運算(例:加減乘除等…)

 

Parse方法:

在C#中,每個基本資料型態(char, byte, short, int, long, float, double等)都會對應一個類別型態(Char, Byte, Short, Int32, Long, Float, Double等),提供該數值型態的方法,其中Parse方法是將傳入的字串轉成對應的數值,Parse或Convert方法在轉換字串時,發生問題會產生例外,例如有小數點的字串數值轉換成整數就會發生格式錯誤的例外,此時,你最好寫一個catch補捉例外的區塊,對產生的例外加以處理。

TryParse執行和Parse一樣的功能,不過TryParse發生錯誤時不會拋出例外,回傳一個布林值,成功回傳true,轉型失敗會轉成初始值,並回傳false。

Parse例:

int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
// 輸出: -105

TryParse例:

// TryParse 回傳 true,如果轉換是成功的話
// 並且將結果儲存到 j 變數
int j;
if (Int32.TryParse("-105", out j))
    Console.WriteLine(j);
else
    Console.WriteLine("字串無法解析");
// 輸出: -105

Pasre with Try-catch

try
{
    int m = Int32.Parse("abc");
}
catch (FormatException e)
{
    Console.WriteLine(e.Message);
}
// 輸出: Input string was not in a correct format.

TryParse例:

string inputString = "abc";
int numValue;
bool parsed = Int32.TryParse(inputString, out numValue);

if (!parsed)
    Console.WriteLine("Int32.TryParse 無法轉換 '{0}' 成為一個 int.\n", inputString);

// 輸出: Int32.TryParse 無法轉換 'abc' 成為一個 int.

 

資本資料型態:

 
數值型態 數值類別 型態 寬度 數值範圍(bits)
byte Byte Unsigned integer 8 0 to 255
sbyte SByte Signed integer 8 -128 to 127
int Int32 Signed integer 32 -2,147,483,648 to 2,147,483,647
uint UInt32 Unsigned integer 32 0 to 4294967295
short Int16 Signed integer 16 -32,768 to 32,767
ushort UInt16 Unsigned integer 16 0 to 65535
long Int64 Signed integer 64 -9223372036854775808 to 9223372036854775807
ulong UInt64 Unsigned integer 64 0 to 18446744073709551615
float Single Single-precision floating point type 32 -3.402823e38 to 3.402823e38
double Double Double-precision floating point type 64 -1.79769313486232e308 to 1.79769313486232e308
char Char A single Unicode character 16 Unicode symbols used in text
bool Boolean Logical Boolean type 8 True or false
object Object Base type of all other types
string String A sequence of characters
decimal Decimal Precise fractional or integral type that can represent decimal numbers with 29 significant digits 128 ±1.0 × 10e−28 to ±7.9 × 10e28

美國大學如何招攬頂尖人才:我在懷俄明大學當新聘教師委員的經驗 – 報導者

徵聘委員的角色有點像星探,不是高高在上要別人去討好或跪求,反而要跪求最佳人才。

來源: 美國大學如何招攬頂尖人才:我在懷俄明大學當新聘教師委員的經驗 – 報導者

Xueii 學藝育成中心 │ 專業師資、業界經驗、實務導向

學藝育成中心是藝智數位旗下的教育品牌,我們致力於平面設計,網頁設計及系統規劃服務,因此,我們想將業界所遇到的問題及經驗分享給各位。我們的老師都來自於業界,我們用歷年來積累的接案經驗,將這過程中所遇到的問題及解決方案提供給您。當然我們的老師也具備一定的教學時數,可以透過淺顯易懂的教學,讓您輕鬆上手。-design by 雅藝視覺設計

來源: Xueii 學藝育成中心 │ 專業師資、業界經驗、實務導向