簡體   English   中英

如何在fprintf中調用外部函數

[英]How to call an external function inside fprintf

  1. 這是外部函數:用於將國際象棋棋盤的坐標更改為字母數字數據,而不是純數字。

     function [] = convertIntegerToCoordinates (i) First=floor(i/10); if First ==1 fprintf('A') elseif First ==5 fprintf('E') elseif First ==6 fprintf('F') elseif First ==7 fprintf('G') elseif First ==8 fprintf('H') end Second=i-(10*First); if Second ==1 fprintf('1') elseif Second ==2 fprintf('2') elseif Second ==3 fprintf('3') elseif Second ==4 fprintf('4') elseif Second ==5 fprintf('5') elseif Second ==6 fprintf('6') elseif Second ==7 fprintf('7') elseif Second ==8 fprintf('8') end endfunction 

2.這是主要功能:該功能用於檢測白王是否受到Rook,Bishop,Queen和Knight中的四個威脅。

    function [] = ChessThreatCheck (n,b,c)

    B = 'B';
    R = 'R';
    Q = 'Q';
    K = 'K'; 


    n= input("Please enter the coordinates of white king: ");
    whiteFirst=floor(n/10);
    whiteSecond=n-(10*whiteFirst);

    if (whiteFirst>8 || whiteFirst<1) && (whiteSecond>8 || whiteSecond<1)
    fprintf ('The x and y coordinates of the white king should be between 1 and 8\n');
    return;
    end 
    if whiteFirst>8 || whiteFirst<1
    fprintf('The x coordinate of white king should be between 1 and 8\n');
    elseif whiteSecond>8 || whiteSecond<1 
    fprintf('The y coordinate of white king should be between 1 and 8\n');
    return;
    end 


    b = input("Please enter the type of black chessman: ");
    if b != B && b!= R && b!= Q && b!= K 
    fprintf('The type of the black chessman can be (B)ishop, (R)ook, (Q)ueen or (K)night\n');
    return;
    end 


    c = input("Please enter the coordinates of black chessman: ");
    blackFirst = floor(c/10);
    blackSecond = c-(10*blackFirst);

    if (blackFirst<1 || blackFirst>8) && (blackSecond<1 || blackSecond>8)
    fprintf('The x and y coordinates of the black chessman should be between 1 and 8\n')
    return;
    end
    if blackFirst<1 || blackFirst>8 
    fprintf('The x coordinate of the black chessman should be between 1 and 8\n');
    elseif blackSecond<1 || blackSecond>8
    fprintf('The y coordinate of the black chessman should be between 1 and 8\n');
    return; 
    end


    if (b=='R')&&((blackFirst == whiteFirst) || (blackSecond == whiteSecond))
    fprintf('The White King at %s is threatened by black rook at %s \n',convertIntegerToCoordinates(n) ,convertIntegerToCoordinates(c));
    elseif fprintf('The White King at %s is not threatened by the black rook at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));

    end 
    return;

    if (b=='B') && (abs(blackFirst-whiteFirst) == abs(blackSecond-whiteSecond))
    fprintf('The white king at %s is threatened by the black bishop at %s \n', convertIntegerToCoordinates(n), convertIntegerToCoordinates(c));
    elseif fprintf('The white king at %s is not threatened by the black bishop at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
    end 

    if  (b=='Q')&&((abs(blackFirst-whiteFirst) == abs(blackSecond-whiteSecond)) || ((blackFirst == whiteFirst) || (blackSecond == whiteSecond)))
    fprintf ('The white king at %s is threatened by black queen at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
    else if fprintf('The white king at %s is not threatened by black queen at %s\n', convertIntegerToCoordinates(n), convertIntegerToCoordinates(c));
    end  

    if (b=='K')&& ((abs(blackFirst-whiteFirst == 2) && abs(blackSecond-whiteSecond==1)) || (abs(blackFirst-whiteFirst == 1) && abs(blackSecond-whiteSecond==2)))
    fprintf('The white king at %s is threatened by black knight at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
    elseif fprintf('The white king at %s is not threatened by black knight at %s \n', convertIntegerToCoordinates(n),convertIntegerToCoordinates(c));
    end 

    end
  1. 輸出以錯誤的方式打印。 有人可以幫我嗎? 我得到的輸出是

     E5E6The White King at >> 

但我期望

    The White king at E5 is threatened by the black rook at E6 >>

您的問題是fprintf已經在convertIntegerToCoordinates函數內部被調用。 因此,該函數在主腳本中的fprintf之前直接打印到命令窗口。 加上您的函數沒有輸出變量的事實,因此主腳本的fprintf不知道該用什么代替formatSpecs '%s'

這是您的函數的更緊湊的版本,應該可以解決此顯示問題:

function out = convertIntegerToCoordinates(i)

% Get column
C = char(floor(i/10)+64);

% Get raw 
R = num2str(mod(i,10));

% Output
out = [C R];

或者,相同的想法,但更緊湊:

function out = convertIntegerToCoordinates(i)

out = [char(floor(i/10)+64) num2str(mod(i,10))];

最好,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM