找回密码
 会员注册
查看: 172|回复: 0

asp.net实现一个简单的网页计算器(能够实现加减乘除)

[复制链接]

250

主题

1

回帖

819

积分

管理员

积分
819
发表于 2024-2-29 08:53:10 | 显示全部楼层 |阅读模式

asp.net实现一个能够进行加减乘除)简单的网页计算器

  • 效果图

    在这里插入图片描述

  • 实现代码:
    aspx文件代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Calculator.aspx.cs" Inherits="WebApplication2.Computer2" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title>网页计算器</title>
  7. <style type="text/css">
  8. .tab {
  9. margin: 0 0; /*使整个计算器的界面位于网页中央 */
  10. Height: 290px;
  11. width: 220px;
  12. }
  13. .h{
  14. text-align: center;
  15. }
  16. .cellCss{
  17. Height: 40px;
  18. width: 50px;
  19. }
  20. .rowCss{
  21. Height: 40px;
  22. }
  23. .but_ac {
  24. width: 80px;
  25. height: 60px;
  26. font-size: 1.2em; /*设置字体大小*/
  27. }
  28. .but {
  29. width: 50px;
  30. height: 40px;
  31. font-size: 1.2em;
  32. background-color:#e0ebeb;
  33. }
  34. .butSign {
  35. width: 50px;
  36. height: 40px;
  37. font-size: 1.2em;
  38. background-color: #a3c2c2;
  39. }
  40. .butEqual {
  41. width: 50px;
  42. height: 80px;
  43. font-size: 1.2em;
  44. background-color: #a3c2c2;
  45. }
  46. .but0 {
  47. width: 100px;
  48. height: 40px;
  49. font-size: 1.2em;
  50. background-color:#e0ebeb;
  51. }
  52. .screen {
  53. width: 210px;
  54. height: 70px;
  55. font-size: 1.5em;
  56. text-align: right; /*使用户输入的表达数从屏幕的右边开始显示*/
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <form id="form1" runat="server">
  62. <div>
  63. <asp:Table ID="Table1" runat="server" CssClass="tab" CellPadding="2" CellSpacing="0" HorizontalAlign="Center" BorderStyle="Double" Caption="网页计算器">
  64. <asp:TableRow CssClass="rowCss" runat="server" HorizontalAlign="Center">
  65. <asp:TableCell CssClass="cellCss" runat="server" ColumnSpan="4">
  66. <asp:TextBox ID="TextBox1" runat="server" readOnly="true" CssClass="screen" OnTextChanged="TextBox1_TextChanged"></asp:TextBox><br />
  67. </asp:TableCell>
  68. </asp:TableRow>
  69. <asp:TableRow CssClass="rowCss" runat="server" HorizontalAlign="Center">
  70. <asp:TableCell CssClass="cellCss" runat="server">
  71. <asp:Button ID="Button1" CssClass="butSign" runat="server" Text="AC" OnClick="Button5_Click"/>
  72. </asp:TableCell>
  73. <asp:TableCell CssClass="cellCss" runat="server">
  74. <asp:Button ID="Button2" runat="server" CssClass="butSign" Text="×" OnClick="Button3_Click" />
  75. </asp:TableCell>
  76. <asp:TableCell CssClass="cellCss" runat="server">
  77. <asp:Button ID="Button3" runat="server" CssClass="butSign" Text="÷" OnClick="Button3_Click" />
  78. </asp:TableCell>
  79. <asp:TableCell CssClass="cellCss" runat="server">
  80. <asp:Button ID="Button4" runat="server" CssClass="butSign" Text="<-" Onclick="Button6_Click"/>
  81. </asp:TableCell>
  82. </asp:TableRow>
  83. <asp:TableRow CssClass="rowCss" runat="server" HorizontalAlign="Center">
  84. <asp:TableCell CssClass="cellCss" runat="server">
  85. <asp:Button ID="Button5" CssClass="but" runat="server" Text="7" OnClick="Button1_Click" />
  86. </asp:TableCell>
  87. <asp:TableCell CssClass="cellCss" runat="server">
  88. <asp:Button ID="Button6" CssClass="but" runat="server" Text="8" OnClick="Button1_Click" />
  89. </asp:TableCell>
  90. <asp:TableCell CssClass="cellCss" runat="server">
  91. <asp:Button ID="Button7" CssClass="but" runat="server" Text="9" OnClick="Button1_Click" />
  92. </asp:TableCell>
  93. <asp:TableCell CssClass="cellCss" runat="server">
  94. <asp:Button ID="Button8" CssClass="butSign" runat="server" Text="+" OnClick="Button3_Click" />
  95. </asp:TableCell>
  96. </asp:TableRow>
  97. <asp:TableRow CssClass="rowCss" runat="server" HorizontalAlign="Center">
  98. <asp:TableCell CssClass="cellCss" runat="server">
  99. <asp:Button ID="Button9" CssClass="but" runat="server" Text="4" OnClick="Button1_Click" />
  100. </asp:TableCell>
  101. <asp:TableCell CssClass="cellCss" runat="server">
  102. <asp:Button ID="Button10" CssClass="but" runat="server" Text="5" OnClick="Button1_Click" />
  103. </asp:TableCell>
  104. <asp:TableCell CssClass="cellCss" runat="server">
  105. <asp:Button ID="Button11" CssClass="but" runat="server" Text="6" OnClick="Button1_Click" />
  106. </asp:TableCell>
  107. <asp:TableCell CssClass="cellCss" runat="server">
  108. <asp:Button ID="Button12" CssClass="butSign" runat="server" Text="-" OnClick="Button3_Click" />
  109. </asp:TableCell>
  110. </asp:TableRow>
  111. <asp:TableRow CssClass="rowCss" runat="server" HorizontalAlign="Center">
  112. <asp:TableCell CssClass="cellCss" runat="server">
  113. <asp:Button ID="Button13" CssClass="but" runat="server" Text="1" OnClick="Button1_Click" />
  114. </asp:TableCell>
  115. <asp:TableCell CssClass="cellCss" runat="server">
  116. <asp:Button ID="Button14" CssClass="but" runat="server" Text="2" OnClick="Button1_Click" />
  117. </asp:TableCell>
  118. <asp:TableCell CssClass="cellCss" runat="server">
  119. <asp:Button ID="Button15" CssClass="but" runat="server" Text="3" OnClick="Button1_Click" />
  120. </asp:TableCell>
  121. <asp:TableCell CssClass="cellCss" runat="server" RowSpan="2">
  122. <asp:Button ID="Button20" CssClass="butEqual" runat="server" Text="=" OnClick="Button4_Click" />
  123. </asp:TableCell>
  124. </asp:TableRow>
  125. <asp:TableRow CssClass="rowCss" runat="server" HorizontalAlign="Center">
  126. <asp:TableCell CssClass="cellCss" runat="server" ColumnSpan="2">
  127. <asp:Button ID="Button17" CssClass="but0" runat="server" Text="0" OnClick="Button1_Click" />
  128. </asp:TableCell>
  129. <asp:TableCell CssClass="cellCss" runat="server">
  130. <asp:Button ID="Button18" CssClass="but" runat="server" Text="." OnClick="Button2_Click" />
  131. </asp:TableCell>
  132. </asp:TableRow>
  133. </asp:Table>
  134. <br />
  135. </div>
  136. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  137. <asp:Button ID="Button16" runat="server" Text="Button" OnClick="Button16_Click" />
  138. </form>
  139. </body>
  140. </html>
复制代码

aspx.cs文件代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. namespace WebApplication2
  8. {
  9. public partial class Comput : System.Web.UI.Page
  10. {
  11. double result;
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. /*实现数字运算符*/
  16. protected void Button1_Click(object sender, EventArgs e)
  17. {
  18. Button btn = (Button)sender;
  19. if (TextBox1.Text == "")
  20. {
  21. TextBox1.Text = btn.Text;
  22. }
  23. else
  24. {
  25. TextBox1.Text += btn.Text;
  26. }
  27. }
  28. protected void TextBox1_TextChanged(object sender, EventArgs e)
  29. {
  30. }
  31. /*实现结果运算符“=”功能*/
  32. protected void Button4_Click(object sender, EventArgs e)
  33. {
  34. string s_txt = TextBox1.Text;
  35. int space = s_txt.IndexOf(' ');
  36. if (space < 0) //如果只输入一个数值后直接按等于键‘=’
  37. {
  38. TextBox1.Text = TextBox1.Text;
  39. }
  40. else
  41. {
  42. string c1 = s_txt.Substring(0, space);//获取字符串s_txt中的从0开始的n个字符,其中space为想要提取的字符个数
  43. char operation = Convert.ToChar(s_txt.Substring((space + 1), 1));
  44. string c2 = s_txt.Substring(space + 3);
  45. double arg1 = Convert.ToDouble(c1);
  46. double arg2 = Convert.ToDouble(c2);
  47. TextBox1.Text = result.ToString();
  48. switch (operation)
  49. {
  50. case '+':
  51. result = arg1 + arg2;
  52. break;
  53. case '-':
  54. result = arg1 - arg2;
  55. break;
  56. case '×':
  57. result = arg1 * arg2;
  58. break;
  59. case '÷':
  60. result = arg1 / arg2;
  61. break;
  62. default:
  63. throw new ApplicationException();
  64. }
  65. TextBox1.Text = result.ToString();
  66. }
  67. }
  68. /*实现小数点*/
  69. protected void Button2_Click(object sender, EventArgs e)
  70. {
  71. Button btn = (Button)sender;
  72. string sc_txt = TextBox1.Text;
  73. int space = sc_txt.IndexOf(' ');
  74. if(TextBox1.Text == "") //屏幕中还没有输入其他数字就直接按小数点
  75. {
  76. TextBox1.Text = "0.";
  77. }
  78. else
  79. {
  80. string str_last = sc_txt.Substring(sc_txt.Length - 1, 1);
  81. if (str_last == " ") //判断小数点是否为该操作数的起始点
  82. {
  83. TextBox1.Text += "0.";
  84. }
  85. else
  86. {
  87. /*判断第二个数是否已有小数点,避免因为第一个数值有小数点而误判*/
  88. if (space < 0) //若space>0,说明已输入操作符
  89. {
  90. if (sc_txt.Contains(".")) // sc_txt.Contains(".")用于判断是否有指定字符
  91. {
  92. TextBox1.Text = TextBox1.Text;
  93. }
  94. else
  95. {
  96. TextBox1.Text += btn.Text;
  97. }
  98. }
  99. else
  100. {
  101. //string c1 = s_txt.Substring(0, space);//获取字符串s_txt中的从0开始的n个字符,其中space为个数
  102. //char operation = Convert.ToChar(s_txt.Substring((space + 1), 1));
  103. string s2 = sc_txt.Substring(space + 3);
  104. if (s2.Contains(".")) // sc_txt.Contains(".")用于判断是否有指定字符
  105. {
  106. TextBox1.Text = TextBox1.Text;
  107. }
  108. else
  109. {
  110. TextBox1.Text += btn.Text;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. /*实现加减乘除运算符*/
  117. protected void Button3_Click(object sender, EventArgs e)
  118. {
  119. Button btn = (Button)sender;
  120. //要求先输入数字后,才能输入运算符
  121. if (TextBox1.Text == "")
  122. {
  123. TextBox1.Text = "";
  124. }
  125. else
  126. {
  127. TextBox1.Text = TextBox1.Text + " " + btn.Text + " ";
  128. }
  129. }
  130. /*实现退格按钮,点击一次退格,清除最右边的一个字符*/
  131. protected void Button6_Click(object sender, EventArgs e)
  132. {
  133. string s = TextBox1.Text;
  134. string ss;
  135. string s_last= s.Substring(s.Length - 1,1);
  136. if(s_last==" ")
  137. {
  138. ss = s.Substring(0, s.Length - 3);
  139. }
  140. else
  141. {
  142. ss = s.Substring(0, s.Length - 1);
  143. }
  144. TextBox1.Text = ss;
  145. }
  146. //全部清空键
  147. protected void Button5_Click(object sender, EventArgs e)
  148. {
  149. TextBox1.Text = "";
  150. }
  151. protected void Button16_Click(object sender, EventArgs e)
  152. {
  153. string str1=TextBox2.Text;
  154. }
  155. }
  156. }
复制代码

该网页计算器的功能比较简单,目前只能两个操作数的加减乘除。


来源:https://blog.csdn.net/Relief_1619/article/details/105870968
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?会员注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-26 11:43 , Processed in 1.265353 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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