1用户输入一个正整数n,程序打印出该整数的所有约数(不包括1和该整数本身)。约数的求法是:若n为偶数,则出从2到n/2之间能被n整除的所有整数;若n为奇数,则出从3到(n-1)/2之间能被n整除的所有整数。
static void Main(string[] args)
{
int n, i = 0;
Console.WriteLine("请输入一个正整数:");
n = int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
for (i = 2; i <= n / 2; i++)
if (n % i == 0)
Console.WriteLine(i.ToString());
}
else
{
for (i = 3; i <= (n - 1) / 2; i++)
if (n % i == 0)
Console.WriteLine(i.ToString ());
}
Console.WriteLine("按回车键结束");
Console.ReadLine();
}
2. 用户输入一个正整数n,若n为奇数,程序计算出数列1+3+5+…+n之和;若n为偶数,则程序计算出数列2+4+6+…+n之和。
int n, i, result = 0;
Console.WriteLine("请输入一个正整数:");
n = int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
for (i = 0; i <= n; i += 2)
{
result += i;
}
Console.WriteLine(result.ToString());
}
else
{
for (i = 1; i <= n; i += 2)
{
result += i;
}
Console.WriteLine(result .ToString ());
}
Console.WriteLine("按回车键结束");
Console.ReadLine();
3.判断一个正整数是否为素数
int n, i;
bool prime = true;
Console.WriteLine("请输入一个正整数:");
n = int.Parse(Console.ReadLine());
for (i = 2; i <= n/2; i++)
{
if (n % i == 0)
{
prime = false;
Console.WriteLine("该数为非素数");
break;
}
else
Console.WriteLine("该数为素数");
break;
}
Console.WriteLine("按回车键结束");
Console.ReadLine();
4.最小公倍数
int a, b, i,temp;
int result=0;
Console.WriteLine("请输入第个正整数:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第个正整数:");
b = int.Parse(Console.ReadLine());
temp = Math.Min(a, b);
for (i=temp; i <= a * b;i++)
{
if ((i % a == 0) && (i % b == 0))
{
result=i;
break;
}
}
Console.WriteLine("最小公倍数为:");
Console.WriteLine(result.ToString());
Console.WriteLine("按回车键结束");
Console.ReadLine();
最大公约数
static void Main(string[] args)
{
int a, b, temp, result = 1;
Console.WriteLine("请输入第个正整数:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第个正整数:");
b = int.Parse(Console.ReadLine());
temp = Math.Min(a, b);
for (int i = temp; i >=1; i--)
{
if (a % i == 0 && b % i == 0)
{
result = i;
break;
}
}
Console.WriteLine("最大公约数为:");
Console.WriteLine(result.ToString());
Console.WriteLine("按回车键结束");
Console.ReadLine();
}
5.定义学生类Student,在类中定义字段、属性和虚拟方法;由基类Student创建派生类Undergraduate和Graduate,在派生类中实现方法重载;在程序中实例化类的对象并且调用类的方法。
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class Fruit
{
程序员和编程员的区别private float m_weight;
private string m_color;
public float Weight
{
get
{
return m_weight;
}
set
{
m_weight = value;
}
}
public string Color
{
get
{
return m_color;
}
set
{
m_color = value;
}
}
public virtual string  GetWeight()
{
return "The weight of fruit is "+Weight;
}
}
public class Apple : Fruit
{
public Apple()
{
Weight = 2;
}
public override string  GetWeight()
{
return "The weight of Apple is " + Weight;
}
}
public class Oranger : Fruit
{
public Oranger()
{
Weight = 1.5f;
}
public override string GetWeight()
{
return "The weight of Oranger is " + Weight;
}
}
}
6.模式对话框与非模式对话框,及字体,颜对话框的显示 。
private void button1_Click(object sender, EventArgs e)
{
FontDialog dlg = new FontDialog();
if (dlg.ShowDialog() != DialogResult.Cancel)
{
this.Font = dlg.Font;
}
ColorDialog dlg = new ColorDialog();
if (dlg.ShowDialog() != DialogResult.Cancel)
{
butt
on1.BackColor = dlg.Color;
this.BackColor = dlg.Color;
}
}
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}
7.透明度
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
progressBar1.Value = trackBar1.Value;
this.Opacity = 0.5 + (double)trackBar1.Value / 100;
}
8.红绿蓝
private void vScrollBar1_ValueChanged(object sender, EventArgs e)
{
this.ForeColor = Color.FromArgb(vScrollBar1.Value, vScrollBar2.Value,vScrollBar3.Value);
}
9.进制转换
static void Main(string[] args)
{
string s_octal = "", s_temp = "";
int number, i;
Console.WriteLine("输入一个十进制正整数");
number = int.Parse(Console.ReadLine());
while (number > 7)
{
s_temp += number % 8;
number /= 8;
}
s_temp += number;
for (i = s_temp.Length - 1; i >= 0; i--)
s_octal += s_temp[i];
Console.WriteLine("八进制为");
Console.WriteLine(s_octal);
Console.WriteLine("回车结束");
Console.ReadLine();
}
}
10.同心圆
private void Form1_Load(object sender, EventArgs e)
{
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
}
void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics gc = e.Graphics;
gc.DrawString("这是一组同心圆:", this.Font, Brushes.Black, 10, 20);
gc.DrawEllipse(new Pen(Brushes.Red, 40), GetEllopse(new Point(120, 120), 20));
gc.DrawEllipse(new Pen(Brushes.Yellow, 20), GetEllopse(new Point(120, 120), 40));
gc.DrawEllipse(new Pen(Brushes.Blue, 20), GetEllopse(new Point(120, 120), 60));
gc.DrawEllipse(new Pen(Brushes.Green, 20), GetEllopse(new Point(120, 120), 80));
}
public Rectangle GetEllopse(Point center, int radius)
{
int x = center.X - radius;
int y = center.Y - radius;
return new Rectangle(x, y, 2 * radius, 2 * radius);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics gc = e.Graphics;
gc.DrawString("这是一组同心圆:", this.Font, Brushes.Black, 10, 20);
gc.DrawEllipse(new Pen(Brushes.Red, 40), GetEllopse(new Point(120, 120), 20));
gc.DrawEllipse(new Pen(Brushes.Yellow,20), GetEllopse(new Point(12
0,120), 40));
gc.DrawEllipse(new Pen(Brushes.Blue,20), GetEllopse(new Point(120,120), 60));
gc.DrawEllipse(new Pen(Brushes.Green,20), GetEllopse(new Point(120,120), 80));
}
private void button1_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.Document = this.printDocument1;
if (pd.ShowDialog() != DialogResult.Cancel)
{
this.printDocument1.Print();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
this.pageSetupDialog1.Document = this.printDocument1;
this.pageSetupDialog1.ShowDialog();
}
}
11.二进制流保存,打开文件
public void SaveInfo(String Path)
{
FileStream fs = new FileStream(Path, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Box1 .Text .ToString ());
bw.Box2 .Text .ToString ());
bw.Write(this .checkBox1 .Checked.ToString ());
bw.Write(this.checkBox2.Checked.ToString());
bw.Write(this.checkBox3.Checked.ToString());
bw.Write(this.checkBox4.Checked.ToString());
bw.Write(this.checkBox5.Checked.ToString());
bw.Write(this.checkBox6.Checked.ToString());
bw.Close();
fs.Close ();
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "学生信息文件(*.s74)|*.s74";
dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;
if (dlg.ShowDialog() == DialogResult.OK)
{
SaveInfo(dlg.FileName);
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "学生信息文件(*.s74)|*.s74";
dlg.InitialDirectory = System.Windows.Forms.Application.StartupPath;
if(dlg.ShowDialog ()==DialogResult .OK)
{
LoadInfo(dlg.FileName);
}
DirectoryInfo info=new DirectoryInfo (dlg.FileName);
}
public void LoadInfo(string Path)
{
FileStream fs = new FileStream(Path, FileMode.Open );
BinaryReader br = new BinaryReader(fs);
this .textBox2 .Text =br.ReadString ();
this.checkBox1.Checked =
bool.Parse(br.ReadString());
this.checkBox2.Checked = bool.Parse(br.ReadString());
this.checkBox3.Checked = bool.Parse(br.ReadString());
this.checkBox4.Checked = bool.Parse(br.ReadString());
this.checkBox5.Checked = bool.Parse(br.ReadString());
this.checkBox6.Checked = bool.Parse(br.ReadString());
br.Close();
fs.Close();
}
}
12.
{
Int16 a = Int16.Parse(textBox1.Text);
float result = 1;
for (int i = 1; i <= 100; i += 2)
{
result -= 1.0f / i / a;
result += 1.0f / (i + 1) / a;
Debug.WriteLine(result);
}
textBox2.Text = result.ToString();
}
catch (FormatException e1)
{
MessageBox .Show ("请输入一个正整数");
}
catch (OverflowException e1)
{
MessageBox.Show("您的输入超出了有效范围");
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。