im struggling with this one guys like the oddsum and the rest is undefeatable with me

조회 수: 2 (최근 30일)
its kinda fading
  댓글 수: 4

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2023년 6월 15일
편집: Stephen23 2023년 6월 15일
N = 'S2307605'; % must be character
V = N(2:end)-'0'
V = 1×7
2 3 0 7 6 0 5
R = rem(sum(3*V(1:2:end))+sum(V(2:2:end)),11)
R = 5
S = struct('S','A':'K','G','K':'U');
Z = [N,S.(N(1))(1+R)]
Z = 'S2307605F'
  댓글 수: 1
Stephen23
Stephen23 2023년 6월 15일
Lets fix your code too:
nric = 'S2307605';
assert(ischar(nric)&&numel(nric)==8,'The NRIC must have 8 characters.')
% Check if the first character is 'S' or 'G'
assert(any(strncmp(nric,{'S','G'},1)),'The first character must be "S" or "G".')
% Check if there are any non-digit characters in the numeric portion of the NRIC
numericPortion = nric(2:end);
assert(all(isstrprop(numericPortion, 'digit')),'The numeric portion of the NRIC should only contain digits.')
% Extract the first character to determine the status
status = nric(1);
% Convert the numeric portion to a numeric array
numericArray = sscanf(numericPortion,'%1d',[1,Inf]);
% Calculate the sum of digits in odd-numbered positions
oddSum = sum(3*numericArray(1:2:end));
% Sum the digits in even positions
evenSum = sum(numericArray(2:2:end));
% Calculate the total sum
totalSum = oddSum + evenSum;
% Calculate the remainder when divided by 11
remainder = mod(totalSum, 11);
% Display the intermediate calculations
fprintf('Odd Sum: %d\n', oddSum);
Odd Sum: 39
fprintf('Even Sum: %d\n', evenSum);
Even Sum: 10
fprintf('Total Sum: %d\n', totalSum);
Total Sum: 49
fprintf('Remainder: %d\n', remainder);
Remainder: 5
% Determine the checksum letter based on the status
if status == 'S'
checksumLetter = getChecksumLetterS(remainder);
elseif status == 'G'
checksumLetter = getChecksumLetterG(remainder);
else
error('Invalid NRIC format.');
end
% Add the checksum letter to the NRIC
nricWithChecksum = [nric, checksumLetter];
fprintf('NRIC with checksum: %s\n', nricWithChecksum);
NRIC with checksum: S2307605F
function letter = getChecksumLetterS(remainder)
switch remainder
case 0
letter = 'A';
case 1
letter = 'B';
case 2
letter = 'C';
case 3
letter = 'D';
case 4
letter = 'E';
case 5
letter = 'F';
case 6
letter = 'G';
case 7
letter = 'H';
case 8
letter = 'I';
case 9
letter = 'J';
case 10
letter = 'K';
otherwise
error('Invalid remainder.');
end
end
function letter = getChecksumLetterG(remainder)
switch remainder
case 0
letter = 'K';
case 1
letter = 'L';
case 2
letter = 'M';
case 3
letter = 'N';
case 4
letter = 'O';
case 5
letter = 'P';
case 6
letter = 'Q';
case 7
letter = 'R';
case 8
letter = 'S';
case 9
letter = 'T';
case 10
letter = 'U';
otherwise
error('Invalid remainder.');
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Signal Attributes and Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by