NIIT1秋重修复习阅读程序流程图等Word文档格式.docx
《NIIT1秋重修复习阅读程序流程图等Word文档格式.docx》由会员分享,可在线阅读,更多相关《NIIT1秋重修复习阅读程序流程图等Word文档格式.docx(39页珍藏版)》请在冰豆网上搜索。
}
参考解答:
5
11
0
Str
stringbeingconverted
2.
namespaceCalculateNumber
{
classCalculate
staticintnumber1=10;
staticintcount1;
publicvoidDisplay(intnumber)
{Console.WriteLine(number);
Calculate()
{count1++;
number1++;
Display(number1);
staticCalculate()
{Console.WriteLine("
StaticConstructorInvoked"
);
~Calculate()
Destructor{0}Invoked"
count1);
CalculateCal1=newCalculate();
CalculateCal2=newCalculate();
StaticConstructorInvoked
12
Destructor2Invoked
Destructor2Invoked
3.
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceMultiArray
classMultiArryExample
{staticvoidMain(string[]args)
{intn=0;
intsum=0;
introwsum;
int[,]mArray=newint[2,4]{{-2,3,7,-3},{3,-6,3,3}};
for(introw=0;
row<
2;
row++)
{rowsum=0;
for(intcol=0;
col<
4;
col++)
{if(mArray[row,col]>
0)n++;
Console.Write("
{0}\t"
mArray[row,col]);
rowsum=rowsum+mArray[row,col];
sum=sum+rowsum;
={0}"
rowsum);
Console.WriteLine();
Console.WriteLine("
n={0}"
n);
sum={0}"
sum);
Console.ReadLine();
-237-3=5
3-633=3
n=5
sum=8
4.
staticintnumber1;
Console.WriteLine(number);
InstantceConstructorinvoked"
StaticConstructorinvoked"
number1=10;
staticvoidMain(string[]args)
StaticConstructorinvoked
InstanceConstructorinvoked
13
5.
namespaceSquare_Number
classCalculateSquare
intnumber;
publicvoidSquare(intnumber)
{Console.WriteLine(number);
number*=number;
CalculateSquare()
{number=10;
Square(number);
CalculateSquare(refinta)
{number=a++;
{CalculateSquarecal1=newCalculateSquare();
inta=5;
CalculateSquareCal2=newCalculateSquare(refa);
Console.WriteLine(a);
10
100
25
6
6.usingSystem;
publicclassA
publicvirtualvoidFun1(inti)
Console.WriteLine(i);
publicvoidFun2(Aa)
a.Fun1
(1);
Fun1(5);
publicclassB:
A
publicoverridevoidFun1(inti)
base.Fun1(i+1);
publicstaticvoidMain()
Bb=newB();
Aa=newA();
a.Fun2(b);
b.Fun2(a);
2
5
1
7.usingSystem;
Console.WriteLine(number1);
number1=5;
StaticConstructorinvoked
InstantceConstructorinvoked
6
7
8.usingSystem;
namespaceParamArrays
{publicclassParamExample
publicintfun1(paramsint[]List)
intTotal=0;
intnumber1=0;
foreach(intIinList)
{if(I%2==1)
Total+=I;
Console.WriteLine(number1);
Console.WriteLine(Total);
returnTotal;
staticvoidMain(string[]srgs)
{ParamExampleparam_Exam=newParamExample();
Console.WriteLine(param_Exam.fun1(2,3,4,5,6,7,8,9,10));
4
54
9.usingSystem;
namespaceObjects
classTestcalculator
Testcalculator()
{Console.WriteLine("
InstanceConstructorInvoked"
staticTestcalculator()
~Testcalculator()
DestructorInvoked"
publicstaticvoidMain(string[]args)
{Testcalculatorcalc1=newTestcalculator();
Testcalculatorcalc2=newTestcalculator();
Destructorinvoked
10.usingSystem;
{ClassTestCalculator
publicstaticints;
TestCalculator()
{Console.WriteLine(“InstanceConstructorInvoked”);
s++;
staticTestCalculator()
{Console.WriteLine(“StaticConstructorInvoked”);
{Testcalculatorcalc1=newTestCalculator();
TestCalculatorcalc2=newTestCalculator();
Console.WriteLine(TestCalculator.s);
2
11.usingSystem;
publicclassParent
stringparentString;
publicParent()
{Console.WriteLine("
ParentConstructor."
);
publicParent(stringmyString)
{parentString=myString;
Console.WriteLine(parentString);
publicvoidprint()
{Console.WriteLine("
I'
maParentClass."
publicclassChild:
Parent
publicChild():
base("
FromDerived"
)
ChildConstructor."
publicvoidprint()
{base.print();
Console.WriteLine("
maChildClass."
publicstaticvoidMain()
Childchild=newChild();
child.print();
((Parent)child).print();
FromDerived
ChildConstructor
maParentClass
maChildClass
12.usingSystem;
namespaceParamATest
publicclassParamExample
staticvoidSomeFunction(int[]ints,inti)
ints[0]=100;
i=100;
publicstaticintMain()
inti=0;
int[]ints={0,1,2,4,8};
i="
+i);
ints[0]="
+ints[0]);
SomeFunction(ints,i);
return0;
i=0
ints[0]=0
ints[0]=100
二、程序填空
/*ThefollowingCodeimplementstheStreamWriterclasstoacceptdatafromauserandwritethesameintothefile*/
using;
ClassFileWrite
publicvoidWriteData()
FileStreamfs=
newFileStream(“Myfile.txt”,FileMode.Append,FileAcess.Write);
StreamWritew=;
//Promptingusertoenterthestringwhichneedstobestoredinthefile.
Console.WriteLine(“Enterastring”);
stringstr=//Enterastringbyuser
w.Write(str);
//clearsallbufferofthecurrentwriter
;
//CosesthecurrentStreamWriterobject.
w.Close();
publicstaticvoidMain(String[]args)
FileWritefw=newFileWrite();
fw.WriteData();
System.IO
newStreamWriter(fs)
Console.ReadLine()
w.Flush()
fs.Close()
/*Thefunctionoffollowingprogramisthatcheckapalindrome(回文)characterarray*/
usingSystem
namespaceManipulateArray
classpalindrome
{
char[]str=newchar[10];
Console.WriteLine(“EnteraPalindromestring(Max10char):
”);
;
//CallingtheCheckPalidromemethod
Console.WriteLine(CheckPalindrome(str));
privatestaticboolCheckPalindrome(char[]myString)
intstartChar;
intlastChar;
while(startchar<
lastchar)
if(myString[startChar]==myString[lastChar])
else
returnfalse;
returntrue;
Str=Console.ReadLine().ToCharArray()
startChar=0
lastChar=myString.Length-1
startChar++
lastChar--
3./*Thefunctionoffollowingprogramistoacceptthesizeofanarrayanditsvalues.Heneedstoidentifythelargestandsmallestnumberinthearrayandbasedonthatheneedstodisplaytheresult.
namespaceLargestSmallest
classProgram
{intn;
floatlarge,small;
int[]a=newint[50];
EnterthesizeofArray"
strings=Console.ReadLine();
for(inti=0;
i<
n;
i++)
{strings1=Console.ReadLine();
a[i]=Int32.Parse(s1);
}
;
for(inti=1;
{if(a[i]>
large)
elseif(a[i]<
small)
Largestelementinthearrayis{0}"
large);
Console.WriteLine("
Smallestelementinthearrayis{0}"
small);
}
n=Int32.Parse(s)
large=small=a[0]
large=a[i]
small=a[i]
4./*ThefunctionoffollowingprogramisthatCalculatethefactorialofthenumberenteredbyauser.(Thelimitofusersinputshouldbe1-20)*/
classFactorial
staticlongFac1;
publicstaticboolFac(longn,outlonganswer)
longk;
boolok=true;
if()//userinputshouldbe1-20
{ok=false;
else
for(k=2;
k<
=n;
++k)
returnok;
classFactorial1
{longFac1;
boolok;
longNumber;
EntertheNumberforcalculatingfactorial(1-20):
"
Number=long.Parse(Console.ReadLine());
ok=;
//functioncall
if(ok)
Factoria