C_机试题目.doc_第1页
C_机试题目.doc_第2页
C_机试题目.doc_第3页
C_机试题目.doc_第4页
C_机试题目.doc_第5页
已阅读5页,还剩5页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.编写一个简单的计算器实现加减乘除:输入两个数字和一个运算符号,输出结果(注意容错性) begin: try Console.WriteLine(请输入1个数字); double db = Convert.ToDouble(Console.ReadLine(); Console.WriteLine(请输入+,-,*,/任意1种运算符); string ss = Console.ReadLine(); Console.WriteLine(请再次输入1个数字:); double db1 = Convert.ToDouble(Console.ReadLine(); switch (ss) case +: double db2 = db + db1; Console.WriteLine(进行加法运算的结果为: + db2); goto begin; case -: double db3 = db - db1; Console.WriteLine(进行减法运算的结果为: + db3); goto begin; case *: double db4 = db * db1; Console.WriteLine(进行乘法运算的结果为: + db4); goto begin; case /: double db5 = db / db1; Console.WriteLine(进行乘法运算的结果为: + db5); goto begin; default: Console.WriteLine(您输入的运算符有误!); goto begin; catch Console.WriteLine(您输入有误:); goto begin; 2.编写代码,实现反转一个四位数的功能 int intNumber = 2306; int intThousand, intHundred, intTen, intIndiv; /反转一个四位数 intThousand = intNumber / 1000; intNumber %= 1000; intHundred = intNumber / 100; intNumber %= 100; intTen = intNumber / 10; intIndiv = intNumber % 10; intNumber = intIndiv * 1000 + intTen * 100 + intHundred * 10 + intThousand; Console.WriteLine(反转后的数是0, intNumber);3.编写代码,由用户输入一个字符串,使用foreach循环遍历出此字符串中有多个字母,多少个数字,多少个标点 /存放字母个数 int countLetters = 0; /存放数字的个数 int countDigits = 0; /存放标点符号的个数 int countPunctuation = 0; /用户提供的的输入 string input; Console.WriteLine(请输入一个字符串); input = Console.ReadLine(); /声明foreach循环以遍历输入的字符串中的每个字符 foreach (char chr in input) /检查字母 if (char.IsLetter(chr) countLetters+; /检查数字 if (char.IsDigit(chr) countDigits+; /检查标点 if (char.IsPunctuation(chr) countPunctuation+; Console.WriteLine(字母的个数为:0, countLetters); Console.WriteLine(数字的个数为:0, countDigits); Console.WriteLine(标点符号的个数为:0, countPunctuation);4.输入一段字符,将字符串各字母转换成ASCII数字码进行输出,并且控制每行只显示5个 static void Main(string args) string strContent = Console.ReadLine(); PrintT(strContent); / / 将字符串各字母转换成ASCII数字码进行输出 / / 将要被转换的字符串 static void PrintT(string strContent) /x为每5个字符输出后,进行换行的标识 int x = 0; /定义数组,数组长度是字符串的字符数 string strArray = new stringstrContent.Length; /填充数组,将字符串的每个字符都进行单个填充到数组中 for (int i = 0; i strContent.Length; i+) strArrayi = strContent.Substring(i, 1); /转换成ASCII数字码进行输出,首先将字符转换成Char类型,然后在转换成Int类型,最后输出 for (int i = 0; i strArray.Length; i+) /换行的标识每次输出一个字符时进行加一 x = x + 1; Console.Write(strArrayi + : + Convert.ToInt32(Convert.ToChar(strArrayi).ToString() + t); /当换行标识能够整除5的时候说明已经到5次 if (x % 5 = 0) Console.Write(n); Console.Write(n); 7.冒泡排序/ / 1、有一个班级,共有10人,姓名分别为赵甲、钱乙、孙丙、李丁、周戊、吴已、郑庚、王辛、冯壬、陈癸, 进行了两门课程的考试,/ 语文分数分别为98,80,78,62,84,77,59.5,66,55,92,/ 数学分数分别为92,55,66,59.5,77,84,62,78,80,98/ 请完成以下工作:/ i.创建一个结构数组用于存储以上所有的个人信息,并从键盘录入这些信息。(5分)/ ii.输出每位同学的信息和平均分、班级每门课程的平均分和班级每门课程的合格率(60分及以上为合格);(10分)/ iii.请按个人平均分数对以上人员进行名次排列,并输出姓名、名次和分数这三个信息;(15分)/ /定义学生的结构struct Studentpublic string name;public float chinese; public float math;public float average;/平均分class Class1/排名(冒泡排序法)public void Sort(Student list) int i,j;Student temp; bool done=false; j=1; while(jlist.Length)&(!done) done=true; for(i=0;ilist.Length-j;i+) if(listi.average listi+1.average) done=false; temp=listi; listi=listi+1; listi+1=temp; j+; static void Main(string args)Class1 c1 = new Class1();/定义结构数组和结构int rs;/人数Console.WriteLine(请输入要录入的人数:);rs = Int32.Parse(Console.ReadLine();Student student = new Studentrs;Student stu = new Student();/从键盘录入这些数据;for (int i = 0 ; i =60)chinesepass+;if (s.math = 60)mathpass+;/求平均分并输出chinese = (float)Math.Round(chinese / student.Length, 2);math = (float)Math.Round(math / student.Length, 2); Console.WriteLine(语文课程的平均分是:0,chinese);Console.WriteLine(数学课程的平均分是:0,math);chinesepass = (float)Math.Round(chinesepass/student.Length),4) * 100;Console.WriteLine(班级语文合格率:0/100,chinesepass);mathpass = (float)Math.Round(mathpass/student.Length),4) * 100;Console.WriteLine(班级数学合格率:0/100,mathpass);/排名c1.Sort(student);int k = 0;foreach(Student s in student)k+;Console.WriteLine(第0名是:1,平均分是:2,k,,s.average); 8.猴子吃桃class Class1/ / 桃林的桃子成熟了,小猴子摘了好多的桃,小猴子心想:/ 我要是一次就把所有的桃子吃光了,以后就没有桃子吃了,/ 所以,每天吃一半,这样可以多吃几天。小猴子就这样开始吃了/ 但是,每天吃完了一半,小猴子的馋嘴还没止住,/ 就想:我再吃一颗,再吃一颗就不吃了。/ 于是,小猴子每天吃了剩余桃子一半多一颗,吃到第十天,就只剩一颗桃子了,/ 请问,小猴子第一天总共有多少颗桃子呢?/ / sum(day) = sum(day + 1) *2/ STAThreadstatic void Main(string args)Console.WriteLine(猴子在第一天共有0颗桃子!,peach(10);/ / 求桃的个数/ 参数为桃子吃了的天数/ static int peach(int day)if (day = 1)return 1;elsereturn (peach(day - 1) + 1 ) * 2;9.阶乘public static void Main()for(int i=1; i=10; i+)Console.WriteLine(0 的阶乘是 1,i, Factorial(i);public static long Factorial(long n)if(n = 1)return 1;elsereturn n * Factorial(n-1);1 定义一个委托,定义两个方法,分别实现任意多个参数的相乘和相除,用委托调用这两个方法执行class Delegates/ 委托定义public delegate int Call(int num1, int num2);class Math/ 乘法方法public int Multiply(int num1, int num2)return num1*num2;/ 除法方法public int Divide(int num1, int num2)return num1/num2;static void Main(string args)/ 委托的对象Call objCall; / Math 类的对象Math objMath = new Math();/ 将方法与委托关联起来objCall = new Call(objMath.Multiply);/objCall = new Call(objMath.Divide);/ 将委托实例化int result; result = objCall(5, 3);System.Console.WriteLine(结果为 0,result);2 程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。要求:要有联动性,老鼠和主人的行为是被动的。考虑可扩展性,猫的叫声可能引起其它联动效应。namespace Test public interface Observer void Response(); /观察者的响应,如是老鼠见到猫的反映 public interface Subject void AimAt(Observer obs); /针对哪些观察者,这里指猫的要扑捉的对象-老鼠 public class Mouse : Observer private string name; public Mouse(string name, Subject subj) = name; subj.AimAt(this); public void Response() Console.WriteLine(name + attempt to escape!); public class Master : Observer public Master(Subject subj) subj.AimAt(this); public void Response() Console.WriteLine(Host waken!); public class Cat : Subject private ArrayList observers; public Cat() this.observers = new ArrayList(); public void AimAt(Observer obs) this.observers.Add(obs);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论