Home > Net >  Dynamic array and the arithmetic of the class
Dynamic array and the arithmetic of the class

Time:05-09


C # is a great god save the children's mid-term exam

CodePudding user response:

The first question:
 
Int studentCount=0;
Int menCount=0;
Int womenCount=0;

@ Total:
Console. WriteLine (" please enter the total number of class: ");
Var result=Console. ReadLine ();
if (! Int. J TryParse (result, out studentCount))
{
Console. WriteLine (" the number of input error, please enter an integer!" );
Goto @ Total;
}

@ Men:
Console. WriteLine (" please enter the number of boys: ");
Result=Console. ReadLine ();
if (! Int. J TryParse (result, out menCount))
{
Console. WriteLine (" boy number: input error, please enter an integer!" );
Goto @ Men;
}

@ "WoMen:
Console. WriteLine (" please enter the number of girls: ");
Result=Console. ReadLine ();
if (! Int. J TryParse (result, out womenCount))
{
Console. WriteLine (" girls: input error, please enter an integer!" );
Goto @ "WoMen;
}

Var stuArr=new List (a);
for (int i=0; I & lt; MenCount; I++)
{
StuArr. Add (" M ");
}

for (int i=0; I & lt; WomenCount; I++)
{
StuArr. Add (" F ");
}

Console. WriteLine (" the number of boys is: "+ stuArr. The.findall (p=& gt; P=="M"). The Count);
Console. WriteLine (" girls is: "+ stuArr. The.findall (p=& gt; P=="F"). The Count);

CodePudding user response:

The second question:
 
A decimal l=0.00 m;
A decimal r=0.00 m;
@ GetNum:
Console. WriteLine (" please enter the first number: ");
Var result=Console. ReadLine ();
if (! A decimal. TryParse (result, out l))
{
Console. WriteLine (" input error, please enter a valid number!" );
Goto @ GetNum;
}

@ GetNum2:
Console. WriteLine (" please enter the second number: ");
Result=Console. ReadLine ();
if (! A decimal. TryParse (result, out r))
{
Console. WriteLine (" input error, please enter a valid number!" );
Goto @ GetNum2;
}


@ Sym:
Var symNum=0;
Console. WriteLine (" please input to the operation method of (1=2=3=except for) : ");
Result=Console. ReadLine ();
if (! Int. J TryParse (result, out symNum))
{
Console. WriteLine (" input error, please enter a valid integer!" );
Goto @ Sym;
}

The switch (symNum)
{
Case 1:
Result=Add (l, r);
break;
Case 2:
Result=Sub (l, r);
break;
Case 3:
Result=Ride (l, r);
break;
Default:
Console. WriteLine (" input error, please enter a valid integer!" );
Goto @ Sym;
}

Console. WriteLine (" result is: "+ result);

CodePudding user response:

 
Class Program
{
The static void Main (string [] args)
{
Operator oper=OperatorFactory. CreateOperator (5, 4, OperatorEnum. I.e.);
Console. WriteLine (oper. GetResult (). The ToString ());
Console.ReadKey();
}
}

///& lt; Summary>
///operation class
///& lt;/summary>
Public class Operator
{
///& lt; Summary>
///the first digit
///& lt;/summary>
Public int Num1 {get; set; }

///& lt; Summary>
The second digit///
///& lt;/summary>
Public int Num2 {get; set; }

///& lt; Summary>
///to get result
///& lt;/summary>
///& lt; Returns>
Public virtual int GetResult ()
{
return 0;
}
}

///& lt; Summary>
///addition
///& lt;/summary>
Public class OperatorAdd: Operator
{
Public override int GetResult ()
{
Return this.. This Num1 + Num2;
}
}

///& lt; Summary>
///subtraction
///& lt;/summary>
Public class OperatorSub: Operator
{
Public override int GetResult ()
{
Return this. Num1 - this. Num2;
}
}

///& lt; Summary>
///multiplication
///& lt;/summary>
Public class OperatorMul: Operator
{
Public override int GetResult ()
{
Return this. Num1. * this Num2;
}
}

///& lt; Summary>
///algorithms factory
///& lt;/summary>
Public class OperatorFactory
{
Public static Operator CreateOperator (int num1, int num2, OperatorEnum oper)
{
Operator opera=null;
The switch (oper)
{
Case OperatorEnum. I.e. the:
Opera=new OperatorAdd ();
break;
Case OperatorEnum. Subduction:
Opera=new OperatorSub ();
break;
Case OperatorEnum. Multiplication:
Opera=new OperatorMul ();
break;
Default:
Throw new ArgumentException (" operation method type exception!" );
}
Opera. Num1=Num1;
Opera. Num2=Num2;

The return of opera;
}
}

///& lt; Summary>
///operator enumeration
///& lt;/summary>
Public enum OperatorEnum
{
///& lt; Summary>
///addition
///& lt;/summary>
I.e.=1,
///& lt; Summary>
///subtraction
///& lt;/summary>
Subduction=2,
///& lt; Summary>
///multiplication
///& lt;/summary>
Multiplication=3
}

CodePudding user response:

 
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related