C#考试题第一波
第一章
1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1_1
{
class Program
{
static void Main(string[] args)
{
int n, i, result = 0;
Console.WriteLine("请输入一个正整数");
n = int.Parse(Console.ReadLine());
if (n % 2 == 0)
{
for (i = 2; i <= n; i++)
{
if (i % 2 == 0)
result += i;
}
}
else
{
for (i = 1; i <= n; i++)
{
if (i % 2 != 0)
result += i;
}
}
Console.WriteLine(result);
Console.ReadKey();
}
}
}
11.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test1_10
{
class Program
{
static void Main(string[] args)
{
int i;
string s_text, s_key, s_result=null;
char ch;
Console.WriteLine("请输入原字符串:");
s_text = Console.ReadLine();
Console.WriteLine("请输入密钥字符串:");
s_key = Console.ReadLine();
if (s_text.Length != s_key.Length)
{
Console.WriteLine("密钥字符串与原字符串长度必须相等");
return ;
}
for(i=0;i<s_text.Length-1;i++)
{
ch =Convert.ToChar(s_text[i]^s_key[i]);
s_result+=ch;
}
Console.WriteLine("加密后的字符串为:");
Console.WriteLine(s_result);
Console.ReadKey();
}
}
}
第二章
1.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_1
{
class Program
{
static void Main(string[] args)
{
Animal ani = new Animal();
Console.WriteLine(ani.Introduce());
Dog dog = new Dog();
Console.WriteLine(dog.Introduce());
Cat cat = new Cat();
Console.WriteLine(cat.Introduce());
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_1
{
class Animal
{
bool m_sex;
int m_age;
public Animal()
{
Sex = false;
}
public bool Sex
{
get { return m_sex; }
set { m_sex = value; }
}
public int Age
{
get { return m_age; }
set { m_age = value; }
}
public virtual string Introduce()
{
if (Sex)
return "This is a male Animal!";
else
return "This is a female Animal!";
}
}
class Dog:Animal
{
public Dog()
{
Sex = true;
}
public override string Introduce()
{
if (Sex)
return "This is a male Dog!";
else
return "This is a female Dog!";
}
}
class Cat : Animal
{
public Cat()
{
Sex = false;
}
public override string Introduce()
{
if (Sex)
return "This is a male Cat!";
else
return "This is a female Cat!";
}
}
}
11.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_11
{
class Program
{
static void Main(string[] args)
{
Animal animal = new Animal();
animal = new Dog();
Console.WriteLine( animal.Roar());
animal = new Cat();
Console.WriteLine(animal.Roar());
animal = new Cow();
Console.WriteLine(animal.Roar());
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test2_11
{
class Animal
{
private bool m_sex;
private string m_sound;
public bool Sex
{
get { return m_sex; }
set { m_sex = value; }
}
public Animal()
{
Sex = false;
Sound = "Howl...";
}
public string Sound
{
get { return m_sound; }
set { m_sound = value; }
}
public virtual string Roar()
{
return m_sound;
}
}
class Dog:Animal
{
public Dog()
{
Sex = true;
Sound = "Wow...";
}
public override string Roar()
{
return "Dog:"+ Sound;
}
}
class Cat : Animal
{
public Cat()
{
Sex = true;
Sound = "Miaow...";
}
public override string Roar()
{
return "Cat:" + Sound;
}
}
class Cow : Animal
{
public Cow()
{
Sex = true;
Sound = "Moo...";
}
public override string Roar()
{
return "Cow:" + Sound;
}
}
}