现在的位置: 首页 > 综合 > 正文

三个c#入门小程序

2012年09月26日 ⁄ 综合 ⁄ 共 5510字 ⁄ 字号 评论关闭
2008-03-27 14:47

1、邮包计费程序  

【说明】: 

1)假定向国外邮寄包裹计费如下计算:首先,包裹重量四舍五入到最近的15的倍数,然后按照表2.1收费。试编写一个程序是先邮包计费,要求编写为Windows应用程序。

2.1邮寄包裹收费表

重量/

收费/

0~14.9

5

15~29.9

9

30~44.9

12

45~59.9

14

60~74.9

15

75以上

每增加10,邮费增加1

2)提示:输入一个包裹的重量,根据邮包重量计算相应的邮费。这里采用多分支程序设计,可使用switch语句。switch后面的表达式不应为邮包的重量,而应是邮包的重量除以15后再取整。

using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
   private System.Windows.Forms.Label label1;
   private System.Windows.Forms.Label label2;
   private System.Windows.Forms.TextBox textBox1;
   private System.Windows.Forms.TextBox textBox2;
   private System.Windows.Forms.Button button1;
   private int weight;
   private int money;
   /// <summary>
   /// 必需的设计器变量。
   /// </summary>
   private System.ComponentModel.Container components = null;

   public Form1()
   {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent();

    //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
   }

   /// <summary>
   /// 清理所有正在使用的资源。
   /// </summary>
   protected override void Dispose( bool disposing )
   {
    if( disposing )
    {
     if (components != null)
     {
      components.Dispose();
     }
    }
    base.Dispose( disposing );
   }

   #region Windows 窗体设计器生成的代码
   /// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(16, 24);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(80, 16);
    this.label1.TabIndex = 0;
    this.label1.Text = "包裹重量(克)";
    //
    // label2
    //
    this.label2.Location = new System.Drawing.Point(8, 88);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(96, 23);
    this.label2.TabIndex = 1;
    this.label2.Text = "应收费用(元)";
    //
    // textBox1
    //
    this.textBox1.Location = new System.Drawing.Point(112, 24);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 2;
    this.textBox1.Text = "textBox1";
    //
    // textBox2
    //
    this.textBox2.Location = new System.Drawing.Point(112, 88);
    this.textBox2.Name = "textBox2";
    this.textBox2.TabIndex = 3;
    this.textBox2.Text = "textBox2";
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(280, 96);
    this.button1.Name = "button1";
    this.button1.TabIndex = 4;
    this.button1.Text = "结果";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(392, 273);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "邮资计算器";
    this.ResumeLayout(false);

   }
   #endregion

   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main()
   {
    Application.Run(new Form1());
   }

   private void button1_Click(object sender, System.EventArgs e)
   {
    weight = (int)(Convert.ToSingle(this.textBox1.Text)/15);
    switch(weight)
    {
     case 0:this.textBox2.Text = "5";break;
     case 1:this.textBox2.Text = "9";break;
                case 2:this.textBox2.Text = "12";break;
     case 3:this.textBox2.Text = "14";break;
     case 4:this.textBox2.Text = "15";break;
     default:this.textBox2.Text =Convert.ToString((int)(Convert.ToSingle(this.textBox1.Text)-74.9)/10+15);break;

    }
  
   }
}
}

2、捉拿肇事司机

【说明】:

1)一辆卡车违反交通规则,撞死行人,司机畏罪驾车逃跑。当时有三个人目击这一车祸的发生,但是都没有看清卡车的车牌号码,只记住车牌的部分特征:甲牌照号码的前两位数字相同,乙记住牌照号码的后两位数字也是相同的,丙是一位数学家,他说:“车牌号码肯定是四位数,而且恰好是一个整数的平方。”并且丙已经脱到处符合该条件的号码只可能有一个。根据这些信息来判断车牌号码并捉拿肇事司机。要求编写一个控制台应用程序。

2提示:假定车牌号码的前两位我们用a表示,后两位用b表示,这四位数可写成:a*1000+a*100+b*10+b。又知道这个四位数为某个数的平方,假定该数为i,则应该有如下等式成立:a*1000+a*100+b*10+b=i*i

本题的目的是求出abi。假设m为汽车车牌号码,i作为循环控制变量,则i= ;因为m为一个四位数,所以i的取值范围一定在3299之间。利用循环语句,判断由i所得到的mi*i)是否满足前两位相等,后两位相等。如果满足则m就是车牌号码,推出循环,否则继续循环。

3)参考运行结果:肇事车辆车牌号码为:7744

using System;

namespace zuonashiji
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
    int s,a1,a2,b1,b2;
    for(int i=32;i<=99;i++)
    {
     s = i*i;
     a1 = s/1000;
     a2 = (s-a1*1000)/100;
     b1 = (s-a1*1000-a2*100)/10;
     b2 = (s-a1*1000-a2*100-b1*10);
     if(a1==a2&b1==b2)
     {
      Console.WriteLine("偷车贼是{0}",s);
      break;
     }

    }
    Console.ReadLine();
   }
  
}
}

3、二分法查找

【提示】:

1)所谓二分法查找就是折半查找。定义一个具有是个元素的一位数组,为它赋一个有序正数序列,然后采用折半查找算法查找一个输入的数m在数组中的位置。如果找到,输出其下标;若找不到,则显示没有找到信息。

2)折半查找法是一种在有序数列中快速查找指定数据的一种方法,具体算法如下:每次查找前确定数组中待查找的下表范围lowhighlow<high),然后用m与中间位置(mid)处的元素进行比较。如果m的值大于中间位置处的元素值,则下一次查找的范围放在中间位置之后的元素中(设定low=mid+1);如果m的值小如中间位置的元素,则下一次查找的范围在中间元素之前(设定high=mid-1),如果中间位置的元素等于m,则找到。反复查找直到找到或者low<high为止。

using System;

namespace erfenchazhao
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
  
    int[]s = new int[10]{10,11,12,13,14,15,16,17,18,19};
    int m,low=0,high=9,mid=s.Length/2;
    Console.WriteLine("请输入您要查找的数,必须在10-19之间");
    m = Convert.ToInt32(Console.ReadLine());
    try
    {
     while(true)
     {
      if(s[mid]<m)
      {
       low = mid+1;
       mid = low+(high-low)/2;
      }
      else
       if(s[mid]>m)
      {
       high = mid-1;
       mid = high - (high-low)/2;
      }
      else
      {
       Console.WriteLine("找到了:在第{0}位",mid);
       break;
      }

     }
    }
    catch
    {
     Console.WriteLine("输入的数有误");
    }
    Console.ReadLine();
   }
}
}

抱歉!评论已关闭.