如何在fprintf中调用外部函数

时间:2015-04-01 22:18:35

标签: matlab function printf octave

  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. 2.这是主要功能:用于检测白人国王是否受到白羊,主教,女王和骑士四人威胁的功能。

        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 >>
      
    2. 但我希望

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

1 个答案:

答案 0 :(得分:1)

您的问题是您的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))];

最佳,

相关问题