簡體   English   中英

正/負號被卡在前導零和數字之間。 我如何使標志開始?

[英]Positive/Negative signs are stuck between leading zeros and the number. How do I make the signs move to the beginning?

現在,我正在使用MPLAB X IDE 3.45,並且要進行分配,我需要打印出具有所需條件的溫度表。 除了一個我都能滿足。 當我嘗試用0填充數字時, 加號會覆蓋零 例如,它們看起來像00-70 + 123 但是,我希望能夠將它們打印為-007+0123。

這是我目前擁有的代碼:

// **** Include libraries here ****
// Standard libraries
#include <stdio.h>

//Class specific libraries
#include "BOARD.h"

// Microchip libraries
#include <xc.h>

// User libraries
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    BOARD_Init();
/***************************************************************************************************
 * Your code goes in between this comment and the following one with asterisks.
 **************************************************************************************************/
    // Declare Variables
    float fahr, celcius;
    int lower, upper, step;

    // Initialize Variables
    lower = 0; // lower limit of temperature
    upper = 300; // upper limit
    step = 20; // step size
    fahr = lower;

    // Print out table
    while (fahr <= upper) {
        celcius = (5.0 / 9.0) * (fahr - 32.0);
        printf("%7.1f %04.0f\n", (double)fahr, (double)celcius);
        fahr = fahr + step;
    }
/***************************************************************************************************
 * Your code goes in between this comment and the preceding one with asterisks.
 **************************************************************************************************/

    // Returning from main() is bad form in embedded environments. So we sit and spin.
    while (1);
}

功能輸出

Fahr      Celc
0.0 0-18
20.0 00-7
40.0 0004
60.0 0016
80.0 0027
100.0 0038
120.0 0049
140.0 0060
160.0 0071
180.0 0082
200.0 0093
220.0 0104
240.0 0116
260.0 0127
280.0 0138
300.0 0149

注意:這部分我正在使用模擬器。

您的系統上的printf()實現中似乎存在一個錯誤:

7.21.6.1:在fprintf功能

6標記字符及其含義是:

+有符號轉換的結果始終以加號或減號開頭。 (僅當未指定此標志時轉換負值時,才以符號開頭。)

0對於d,i,o,u,x,X,a,A,e,E,f,F,g和G轉換,前導零(跟隨符號或基數的任何指示)用於填充該字段寬度,而不是執行空格填充,除非轉換無窮大或NaN。 如果同時出現0-標志,則忽略0標志。

%04.0f應該在負號后用0 %04.0f加上數字,並且除非您還通過如下方式傳遞+標志,否則不應輸出+

printf("%7.1f %+04.0f\n", (double) fahr, (double) celcius);

OP的printf()無法以所需格式打印浮點數。

有時每個規范@chqrlie的編譯器/二進制文件有故障

下面是一個僅整數解決方案。

  // Declare Variables
  int fahr;
  int milli_celcius;
  int lower, upper, step;

  // Initialize Variables
  lower = 0; // lower limit of temperature
  upper = 300; // upper limit
  step = 20; // step size
  fahr = lower;

  // Print out table
  while (fahr <= upper) {
    printf("%5d.0 ", fahr);
    milli_celcius = ((fahr - 32) * 5 * 1000) / 9;
    // round to nearest whole degree;
    int celcius = milli_celcius < 0 ? 
        (milli_celcius - 1000 / 2) / 1000 :  
        (milli_celcius + 1000 / 2) / 1000;
    printf("%+04d  Ref:%f\n", celcius, (fahr - 32.0) * 5 / 9.0);
    fahr = fahr + step;
  }

產量

    0.0 -018  Ref:-17.777778 
   20.0 -007  Ref:-6.666667 
   ...
  280.0 +138  Ref:137.777778 
  300.0 +149  Ref:148.888889 

暫無
暫無

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

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