Loading...
Elena123456 200 Точки

## 08. Letters Change Numbers*- (Text Processing - Exercise) 40/100

I need some help for this exercise. I will apreciate it a lot if somebody takes out of their time to check my rounded method.

With this input "A12b s17G" my output is correct- "330.00" and with this input "a1A" my output is correct too, but with " P34562Z q2576f H456z" my output is "46015.00", not "46015.13".

https://softuni.bg/trainings/resources/officedocument/49591/text-processing-exercise-csharp-fundamentals-may-2020/2830

https://judge.softuni.bg/Contests/Compete/Index/1217#7

using System;
using System.Text;
using System.Linq;

namespace text
{
class MainClass
{
public static void Main(string[] args)
{
string[] inputArray = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries);
string alphabetUpperCase = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
string alphabetLowerCase = "abcdefghigklmnopqrstuvwxyz";
int allSum = 0;

foreach (var text in inputArray)
{
int firstCurrentSum = 0;
int lastCurrentSum = 0;
char firstLetter = text[0];
char lastLetter = text[text.Length - 1];
int numberLength = text.Length - 2;
string numberLikeString = text.Substring(1, numberLength);
int number = int.Parse(numberLikeString);

if (alphabetUpperCase.Contains(firstLetter))
{
int firstLetterPositionInAlphabet = alphabetUpperCase.IndexOf(firstLetter)+1;
firstCurrentSum=number / firstLetterPositionInAlphabet;
}

else if (alphabetLowerCase.Contains(firstLetter))
{
int firstLetterPositionInAlphabet = alphabetLowerCase.IndexOf(firstLetter)+1;
firstCurrentSum =number * firstLetterPositionInAlphabet;
}

if (alphabetUpperCase.Contains(lastLetter))
{
int lastLetterPositionInAlphabet = alphabetUpperCase.IndexOf(lastLetter)+1;
lastCurrentSum = firstCurrentSum - lastLetterPositionInAlphabet;
}

else if (alphabetLowerCase.Contains(lastLetter))
{
int lastLetterPositionInAlphabet = alphabetLowerCase.IndexOf(lastLetter)+1;
lastCurrentSum =firstCurrentSum + lastLetterPositionInAlphabet;
}

allSum += lastCurrentSum;
}

Console.WriteLine(\$"{allSum:f2}");
}
}
}

Тагове:
0
14/11/2020 19:25:15
Axiomatik 1170 Точки
Best Answer

Hi Elena,

The exercise can only be solve when using: totalSum = Math.Round(totalSum, 2, MidpointRounding.AwayFromZero);

Strangely enough, Midpoint Rounding is never used again in the latter exercises, so I guess it is solely used for some demo-purpose. Otherwise your code seems OK so that should be the only problem.

``````using System;
using System.Text;

namespace lettersChangeNumbers
{
class Program
{
static void Main(string[] args)
{
string[] input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
double totalSum = 0;

for (int i = 0; i < input.Length; i++)
{
StringBuilder sb = new StringBuilder(input[i]);
char firstLetter = sb[0];
char lastLetter = sb[^1];
string numbers = input[i].Substring(1, input[i].Length - 2);
double num = int.Parse(numbers);
if ((int)firstLetter >= 65 && (int)firstLetter <= 90)
{
num = FirstCapitalLetter(firstLetter, num);
}
else if ((int)firstLetter >= 97 && (int)firstLetter <= 122)
{
num = FirstLowerLetter(firstLetter, num);
}
if ((int)lastLetter >= 65 && (int)lastLetter <= 90)
{
num = SecondCapitalLetter(lastLetter, num);
}
else if ((int)lastLetter >= 97 && (int)lastLetter <= 122)
{
num = SecondLowerLetter(lastLetter, num);
}

totalSum += num;

}
totalSum = Math.Round(totalSum, 2, MidpointRounding.AwayFromZero);
Console.WriteLine(\$"{totalSum:F2}");
}

static double FirstCapitalLetter(char firstLetter, double num)
{
int firstL = (int)firstLetter - 64;
num = num / firstL;
return num;
}

static double FirstLowerLetter(char firstLetter, double num)
{
int firstL = (int)firstLetter - 96;
num = num * firstL;
return num;
}

static double SecondCapitalLetter(char lastLetter, double num)
{
int lastL = (int)lastLetter - 64;
num = num - lastL;
return num;
}

static double SecondLowerLetter(char lastLetter, double num)
{
int lastL = (int)lastLetter - 96;
num = num + lastL;
return num;
}
}
}``````

0
Elena123456 200 Точки

Hi Axiomatik,

thanks for your code. As far as I remember about  MidpointRounding.AwayFromZero from one lecture in Basic this rounding is used in some bank operations.. and for this letter exercise .

On my Monodevelop this rounding isn't working and my code is still 40/100. It wouldn't matter and I can go to another exercise, because acording to you my code seems Ok.

Thanks a lot for all your help!

Best regards!

0