C语言编程问题解答Word文档格式.docx
- 文档编号:8108730
- 上传时间:2023-05-10
- 格式:DOCX
- 页数:16
- 大小:17.79KB
C语言编程问题解答Word文档格式.docx
《C语言编程问题解答Word文档格式.docx》由会员分享,可在线阅读,更多相关《C语言编程问题解答Word文档格式.docx(16页珍藏版)》请在冰点文库上搜索。
=9/2;
temp=inta[i];
inta[i]=inta[9-i];
inta[9-i]=temp;
Console.WriteLine("
第{0}个元素:
{1}"
i,inta[i]);
catch
请每行仅输入一个数字!
);
3.编写程序,统计4X5二维数组中奇数的个数和偶数的个数。
int[,]arr=newint[,]{{1,2,3,4,5},{6,7,8,92,10},{11,12,13,14,15},{16,17,18,19,20}};
intjs=0,os=0;
foreach(intiinarr)
if((i%2).Equals
(1))js++;
elseos++;
奇数:
{0}偶数{1}"
js,os);
4.编写一个求整数任意位数字的过程,过程的调用形式为:
digit(n,k),其功能是取出数n从右边起的第K位数字,例如:
digit(1234,3)=2,digit(1234,4)=1,digit(1234,6)=0。
publicstaticintdigit(intvalue,intk)
stringstr=value.ToString();
intivalue;
if(k>
=1&
&
k<
=str.Length)
ivalue=Convert.ToInt32(str[str.Length-k]-48);
elseivalue=0;
returnivalue;
5.编写一个应用程序用来输入的字符串进行加密,对于字母字符串加密规则如下:
‘a’→’d’‘b’→’e’‘w’→’z’……‘x’→’a’‘y’→’b’‘z’→’c’
‘A’→’D’‘B’→’E’‘W’→’Z’……‘X’→’A’‘Y’→’B’‘Z’→’C’
对于其他字符,不进行加密。
staticstringch
get;
set;
ch=Console.ReadLine();
chart;
ch.Length;
if(ch[i]>
='
a'
&
ch[i]<
z'
)
t=(char)(ch[i]+3);
if((int)t>
122)
t=(char)(t-26);
Console.Write(t);
elseif(ch[i]>
A'
Z'
{
90)
else
Console.Write(ch[i]);
6.定义一个车辆(Vehicle)基类,具有Run、Stop等方法,具有Speed(速度)、MaxSpeed(最大速度)、Weight(重量)等域。
然后以该类为基类,派生出bicycle、car等类。
并编程对该派生类的功能进行验证。
classvehicle
publicintspeed
publicintmaxspeed
publicintweight
publicvoidrun()
speed=60;
maxspeed=80;
vehiclerun{0}{1}"
speed,maxspeed);
publicvoidstop()
speed=0;
vehiclestop{0}{1}"
speed,maxspeed);
classbacycle:
vehicle
newpublicvoidrun()
speed=30;
maxspeed=40;
bacyclerun{0}{1}"
newpublicvoidstop()
bacyclestop{0}{1}"
classcar:
speed=70;
maxspeed=90;
carrun{0}{1}"
carstop{0}{1}"
classProgram
carc=newcar();
c.run();
c.stop();
vehiclev=c;
v.run();
v.stop();
bacycleb=newbacycle();
b.run();
b.stop();
7.编写出一个通用的人员类(Person),该类具有姓名(Name)、年龄(Age)、性别(Sex)等域。
然后对Person类的继承得到一个学生类(Student),该类能够存放学生的5门课的成绩,并能求出平均成绩,要求对该类的构造函数进行重载,至少给出三个形式。
最后编程对student类的功能进行验证。
students1=newstudent();
s1.calcu();
students2=newstudent("
robot"
s2.calcu();
students3=newstudent("
pkm"
21,"
man"
s3.calcu();
classperson
publicstringname
publicintage
publicstringsex
classstudent:
person
publicdouble[]mystr=newdouble[5];
publicdoublethis[intnindex]
get
returnmystr[nindex];
set
mystr[nindex]=value;
publicstudent()
name="
nobody"
;
age=20;
sex="
未知"
for(inti=0;
5;
mystr[i]=2*i+3;
publicstudent(stringName)
name=Name;
publicstudent(stringName,intAge)
name=Name;
age=Age;
publicstudent(stringName,intAge,stringSex)
sex=Sex;
mystr[i]=(2*i+3);
publicvoidcalcu()
此人的信息如下:
n姓名:
{0}年龄:
{1}性别:
{2}"
name,age,sex);
doubleavg=0;
5门课成绩分别是:
avg+=mystr[i];
{0}"
mystr[i]);
avg=avg/5;
n平均成绩是:
{0}n"
avg);
8.编写一个冒泡法排序程序,要求在程序中能够捕获到数组下标越界的异常。
i+1);
inttemp,j;
9;
for(j=i+1;
j<
=10;
j++)
if(inta[j]<
inta[i])
inta[i]=inta[j];
inta[j]=temp;
catch(Exceptione)
e.Message);
9.编写一个计算器程序,要求在程序中能够捕获到被0除的异常与算术运算溢出的异常。
doublea,b,c=0;
intt;
while(true)
Console.Clear();
请选择"
1-加2-减3-乘4-除5-取模"
t=int.Parse(Console.ReadLine());
//Console.WriteLine(t);
while(t>
5||t<
1)
请输入1~5选择功能!
请输入a的值:
a=Convert.ToDouble(Console.ReadLine());
请输入b的值:
b=Convert.ToDouble(Console.ReadLine());
switch(t)
case1:
c=a+b;
a+b的结果为:
c);
break;
case2:
c=a-b;
a-b的结果为:
case3:
c=a*b;
a*b的结果为:
case4:
if(b==0)thrownewDivideByZeroException();
c=a/b;
a/b的结果为:
catch(Exceptione)
e.Message);
case5:
if(a!
=(int)a||b!
=(int)b)thrownewArithmeticException();
c=a%b;
a%b结果为:
c);
按任意键继续!
Console.ReadKey();
10.编写程序在E盘下新建一文本文件,并对该文件进行复制,移动,写入,读出操作
//StreamWritersw=newStreamWriter("
E:
\test.txt"
true,Encoding.GetEncoding("
gb2312"
));
string[]data={"
helloworld,howareyou"
"
i'
mpkm"
};
//sw.WriteLine(data);
//sw.Close();
File.WriteAllLines("
data,Encoding.GetEncoding("
StreamReadersr=newStreamReader("
Encoding.GetEncoding("
stringline;
while((line=sr.ReadLine())!
=null)
Console.WriteLine(line);
sr.Close();
if(File.Exists("
\test11.txt"
))
File.Delete("
File.Copy("
\test22.txt"
File.Move("
11.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能:
1)输出字符串的长度。
2)输出字符串中第一个出现字母a的位置。
3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。
4)将字符串“hello”替换为“me”,输出新字符串。
5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。
stringch;
//ch="
bastmaxthing"
它的长度为{0}"
ch.Length);
第一个出现a的位置是:
{0}"
ch.IndexOf('
)+1);
ch=ch.Insert(3,"
hello"
插入后的字符串:
ch);
ch=ch.Replace("
me"
hello替换成me后:
string[]strs=ch.Split('
m'
分割后得到的字符串:
foreach(stringstrinstrs)
子串:
str);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 编程 问题解答