NIIT1秋重修复习阅读程序流程图等.docx
《NIIT1秋重修复习阅读程序流程图等.docx》由会员分享,可在线阅读,更多相关《NIIT1秋重修复习阅读程序流程图等.docx(39页珍藏版)》请在冰豆网上搜索。
NIIT1秋重修复习阅读程序流程图等
一、阅读程序
1.
usingSystem;
classClass1
{
privatestringstr="Class1.str";
privateinti=0;
staticvoidStringConvert(stringstr)
{str="stringbeingconverted.";}
staticvoidStringConvert(Class1c)
{c.str="stringbeingconverted.";}
staticvoidAdd(inti)
{i++;}
staticvoidAddWithRef(refinti)
{i++;}
staticvoidMain()
{inti1=5;
inti2=10;
stringstr="str";
Class1c=newClass1();
Add(i1);
AddWithRef(refi2);
Add(c.i);
StringConvert(str);
StringConvert(c);
Console.WriteLine(i1);
Console.WriteLine(i2);
Console.WriteLine(c.i);
Console.WriteLine(str);
Console.WriteLine(c.str);
}
}
参考解答:
5
11
0
Str
stringbeingconverted
2.
usingSystem;
namespaceCalculateNumber
{
classCalculate
{
staticintnumber1=10;
staticintcount1;
publicvoidDisplay(intnumber)
{Console.WriteLine(number);}
Calculate()
{count1++;
number1++;
Display(number1);
}
staticCalculate()
{Console.WriteLine("StaticConstructorInvoked");}
~Calculate()
{Console.WriteLine("Destructor{0}Invoked",count1);}
staticvoidMain()
{
CalculateCal1=newCalculate();
CalculateCal2=newCalculate();
}
}
}
参考解答:
StaticConstructorInvoked
11
12
Destructor2Invoked
Destructor2Invoked
3.
usingSystem;
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;
Console.Write("={0}",rowsum);
Console.WriteLine();
}
Console.WriteLine("n={0}",n);
Console.WriteLine("sum={0}",sum);
Console.ReadLine();
}
}
}
参考解答:
-237-3=5
3-633=3
n=5
sum=8
4.
usingSystem;
namespaceCalculateNumber
{
classCalculate
{
staticintnumber1;
publicvoidDisplay(intnumber)
{
Console.WriteLine(number);
}
Calculate()
{
Console.WriteLine("InstantceConstructorinvoked");
number1++;
Display(number1);
}
staticCalculate()
{
Console.WriteLine("StaticConstructorinvoked");
number1=10;
number1++;
}
staticvoidMain(string[]args)
{
CalculateCal1=newCalculate();
CalculateCal2=newCalculate();
Console.ReadLine();
}
}
}
参考解答:
StaticConstructorinvoked
InstanceConstructorinvoked
12
InstanceConstructorinvoked
13
5.
usingSystem;
namespaceSquare_Number
{
classCalculateSquare
{
intnumber;
publicvoidSquare(intnumber)
{Console.WriteLine(number);
number*=number;
Console.WriteLine(number);
}
CalculateSquare()
{number=10;
Square(number);
}
CalculateSquare(refinta)
{number=a++;
Square(number);
}
staticvoidMain(string[]args)
{CalculateSquarecal1=newCalculateSquare();
inta=5;
CalculateSquareCal2=newCalculateSquare(refa);
Console.WriteLine(a);
Console.ReadLine();
}
}
}
参考解答:
10
100
5
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);
Console.ReadLine();
}
}
参考解答:
2
5
1
6
7.usingSystem;
namespaceCalculateNumber
{
classCalculate
{
staticintnumber1;
Calculate()
{
Console.WriteLine("InstantceConstructorinvoked");
number1++;
Console.WriteLine(number1);
}
staticCalculate()
{
Console.WriteLine("StaticConstructorinvoked");
number1=5;
}
staticvoidMain(string[]args)
{
CalculateCal1=newCalculate();
CalculateCal2=newCalculate();
Console.ReadLine();
}
}
}
参考解答:
StaticConstructorinvoked
InstantceConstructorinvoked
6
InstantceConstructorinvoked
7
8.usingSystem;
namespaceParamArrays
{publicclassParamExample
{
publicintfun1(paramsint[]List)
{
intTotal=0;
intnumber1=0;
foreach(intIinList)
{if(I%2==1)
number1++;
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));
Console.ReadLine();}
}
}
参考解答:
4
54
54
9.usingSystem;
namespaceObjects
{
classTestcalculator
{
Testcalculator()
{Console.WriteLine("InstanceConstructorInvoked");}
staticTestcalculator()
{Console.WriteLine("StaticConstructorInvoked");}
~Testcalculator()
{Console.WriteLine("DestructorInvoked");}
publicstaticvoidMain(string[]args)
{Testcalculatorcalc1=newTestcalculator();
Testcalculatorcalc2=newTestcalculator();}
}
}
参考解答:
StaticConstructorinvoked
InstantceConstructorinvoked
InstantceConstructorinvoked
Destructorinvoked
Destructorinvoked
10.usingSystem;
namespaceObjects
{ClassTestCalculator
{
publicstaticints;
TestCalculator()
{Console.WriteLine(“InstanceConstructorInvoked”);
s++;}
staticTestCalculator()
{Console.WriteLine(“StaticConstructorInvoked”);}
publicstaticvoidMain(string[]args)
{Testcalculatorcalc1=newTestCalculator();
TestCalculatorcalc2=newTestCalculator();
Console.WriteLine(TestCalculator.s);}
}
}
参考解答:
StaticConstructorinvoked
InstantceConstructorinvoked
InstantceConstructorinvoked
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")
{Console.WriteLine("ChildConstructor.");}
publicvoidprint()
{base.print();
Console.WriteLine("I'maChildClass.");}
publicstaticvoidMain()
{
Childchild=newChild();
child.print();
((Parent)child).print();
}
}
参考解答:
FromDerived
ChildConstructor
I'maParentClass
I'maChildClass
I'maParentClass
12.usingSystem;
namespaceParamATest
{
publicclassParamExample
{
staticvoidSomeFunction(int[]ints,inti)
{
ints[0]=100;
i=100;
}
publicstaticintMain()
{
inti=0;
int[]ints={0,1,2,4,8};
Console.WriteLine("i="+i);
Console.WriteLine("ints[0]="+ints[0]);
SomeFunction(ints,i);
Console.WriteLine("i="+i);
Console.WriteLine("ints[0]="+ints[0]);
return0;
}
}
}
参考解答:
i=0
ints[0]=0
i=0
ints[0]=100
二、程序填空
1.
/*ThefollowingCodeimplementstheStreamWriterclasstoacceptdatafromauserandwritethesameintothefile*/
usingSystem;
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()
2.
/*Thefunctionoffollowingprogramisthatcheckapalindrome(回文)characterarray*/
usingSystem
namespaceManipulateArray
{
classpalindrome
{
staticvoidMain(string[]args)
{
char[]str=newchar[10];
Console.WriteLine(“EnteraPalindromestring(Max10char):
”);
;
//CallingtheCheckPalidromemethod
Console.WriteLine(CheckPalindrome(str));
Console.ReadLine();
}
privatestaticboolCheckPalindrome(char[]myString)
{
intstartChar;
intlastChar;
;
;
while(startchar{
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
{
staticvoidMain(string[]args)
{intn;
floatlarge,small;
int[]a=newint[50];
Console.WriteLine("EnterthesizeofArray");
strings=Console.ReadLine();
;
for(inti=0;i{strings1=Console.ReadLine();
a[i]=Int32.Parse(s1);
}
;
for(inti=1;i{if(a[i]>large)
;
elseif(a[i]
;
}
Console.WriteLine("Largestelementinthearrayis{0}",large);
Console.WriteLine("Smallestelementinthearrayis{0}",small);
}
}
}
参考解答:
usingSystem
n=Int32.Parse(s)
large=small=a[0]
large=a[i]
small=a[i]
4./*ThefunctionoffollowingprogramisthatCalculatethefactorialofthenumberenteredbyauser.(Thelimitofusersinputshouldbe1-20)*/
usingSystem;
classFactorial
{
staticlongFac1;
publicstaticboolFac(longn,outlonganswer)
{
longk;
boolok=true;
if()//userinputshouldbe1-20
{ok=false;}
else
{
;
for(k=2;k<=n;++k)
{
;
}
}
;
returnok;
}
}
classFactorial1
{
staticvoidMain(string[]args)
{longFac1;
boolok;
longNumber;
Console.WriteLine("EntertheNumberforcalculatingfactorial(1-20):
");
Number=long.Parse(Console.ReadLine());
ok=;//functioncall
if(ok)
Console.WriteLine("Factoria