【问题标题】:How to call an external function inside fprintf如何在 fprintf 中调用外部函数
【发布时间】:2015-04-01 22:18:34
【问题描述】:
  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 >>

【问题讨论】:

  • “输出打印方式错误”信息不足。请提供Minimal, Complete, and Verifiable example。这包括: 可运行 代码减少到有问题的部分。您期望的输出。以及您实际得到的输出。
  • 您的代码中有几个不一致的地方。您指定 nbc 作为输入,但稍后使用 input 请求它们。您使用了哪些值作为输入?打印结果有什么问题?我希望我能读懂你的想法,但不幸的是我不能;)
  • @Ratbert 我使用 n 作为 55, b 作为 'R' 和 c 作为 56 。我还在那里添加了我的预期输出

标签: matlab function printf octave


【解决方案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))];

最好的,

【讨论】:

  • 不,只是一个有经验的用户;) 如果是这种情况,请不要犹豫,将问题标记为 已解决,这样社区就知道您不再需要帮助这件事。
  • 我看到你在看到elseifs 时也发抖了。 ;-)
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 2023-01-21
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多