.C#认证考试试题汇编:第一单元:1,11 第二单元:1,11

第一单元1,11

好久没用异或都快忘了,就让我们一起来了解哈啥子事异或

说的这个,就不经意让我想起书上的几种交换值得方法了

我这儿说的交换的方法是,不使用第三个变量来交换,而是两个

实现条件是C

a=100,b=10;
第一种 缺点可能会如果超出范围溢出
a=a+b; a=110,b=10
b=a-b; b=100,a=110
a=a-b; a=-10,b=100
b=100,a=10;
第二种 缺点可能会如果超出范围溢出
a=a*b;a=1000,b=10
b=a/b;b=100,a=1000
a=a/b;a=10,b=100
第三种 最理想
例如:a=3,即11(2);b=4,即100(2)。
想将a和b的值互换,可以用以下赋值语句实现:
a=a∧b;
b=b∧a;
a=a∧b;
a=011(2)(∧)b=100(2)(a∧b的结果,a已变成7)
a=111(2)(∧)b=100(2)(b∧a的结果,b已变成3)
b=011(2)(∧)a=111(2)(a∧b的结果,a已变成4)
a=100(2)

a=10100001,   b=00000110

a=a^b //a=10100111

b=b^a //b=10100001

a=a^b //a=00000110

其实如果简单理解“相同为0”不同为1

如何还想在深入点,我只能说,小伙,咱家能力有限

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 习题1._11
{
class Program
{
static void Main(string[] args)
{
string s_text, s_key, s_result = null;
char ch;
Console.WriteLine("请输入原字符串:");
s_text = Console.ReadLine().ToString();
Console.WriteLine("请输入密钥字符串");
s_key = Console.ReadLine().ToString();
if(s_text.Length!=s_key.Length)
{
Console.WriteLine("密钥字符串与原字符串长度必须相等");
return; //提前结束程序
}
for(int i=0;i<s_key.Length;i++)
{
ch = s_text[i];
s_result += (char)(ch ^ s_key[i]);             //同志们注意哈强制转换为char类型不然得到的结果不对
}

Console.WriteLine("加密后的字符串为:");
Console.WriteLine(s_result.ToString());
Console.ReadKey();
}
}
}

第二单元1,11

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Animal
{
class Animal
{
private bool m_sex;
private string m_sound;
public Animal()
{
m_sound = "How...";
m_sex = false;
}

//前面一定不能打括号()

public bool Sex //用于子类设置基类的变量
{
get { return m_sex; }
set { m_sex = value; }
}
public string Sound
{
get { return m_sound; }
set { m_sound = value; }
}

public virtual string Roan() //虚方法
{
return "Animal"+m_sound;
}
}
class Dog:Animal
{
public Dog()
{
Sex = true;
Sound = "Wow...";
}
public override string Roan() //重写方法
{
return "Dog:" + Sound;
}
}
class Cat : Animal
{
public Cat()
{
Sound = "Miaow...";
}
public override string Roan()
{

return "Cat:"+Sound;

}
}
class Cow : Animal
{
public Cow()
{
Sound = "Moo...";
}
public override string Roan()
{
return "Cow:" + Sound;
}
}
class Program
{
static void Main(string[] args)
{
Animal animal;
animal = new Dog();
Console.WriteLine( animal.Roan());
animal = new Cat();
Console.WriteLine(animal.Roan());
animal = new Cow();
Console.WriteLine(animal.Roan());
Console.Read();
}
}
}

原文地址:https://www.cnblogs.com/XiaoGuanYu/p/7505691.html