Output Format Control of Integer Four Arithmetic Expressions

Table of contents

Require:

related information

scanf function

Output format control for printf

the sign

domain width

precision

conversion specifier

programming requirements

program implementation


Require:

The user inputs two integers within four digits, please perform four arithmetic operations on the two integers (in order to ensure that the division can be calculated correctly, the second integer input cannot be 0), and the output of the four arithmetic expressions is required to be completely aligned .

For example, given the input of 1256 and 20, the output format of the four arithmetic operations that meet the above alignment requirements should be as follows (in order to make the display of spaces more intuitive, underscores are temporarily used to represent spaces here) _:

_1256 + 20___ = _____1276 _1256 - 20___ = _____1236 _1256 * 20___ = ____25120 _1256 / 20___ = _______62

related information

The output format of the program has a great impact on the user experience, and users often complain about poor software design because the output format is not beautiful.

So, how to control the output format of the program? Next, let's introduce formatted input and output. Formatted input and output can be stdio.hrealized by the scanf and printf functions in the function library.

scanf function

The scanf function inputs data from the keyboard according to a certain format. The function form is:

  1. scanf (<格式控制串> , <参数列表>) ;
  • <format control string> is a string that specifies the format to be followed by the input data;

  • <parameter list> is a list for storing input data addresses. When there are multiple input data, the parameters are ,separated. If you want to write the input data into a variable, you need to use the address of the variable in the parameter list. The method to obtain the variable address is: &变量名, where &is the address operator, and the operation result is the address of the following variable.

For example, read in an integer, a floating point number, and an integer from the command line, and assign them to num1, num2, and num3 respectively. The following code:

scanf(″%d%f%d″, &num1, &num2, &num3); //其中 %d 和 %f 均为转换说明符,分别表示读取的数据为整型和浮点型。

If the command line input is: 12 34.5 678, the effect of this function execution is: the system reads the integer 12 and assigns it to num1, reads the floating point number 34.5 and assigns it to num2, reads the integer 678 and assigns it to num3.

The conversion specifiers commonly used by the scanf function are as follows (omitted %):

type character meaning
d decimal integer
o octal integer
x hexadecimal integer
u unsigned decimal integer
i integer
f real decimal form
e real exponential form
g Shorter forms of f and e
c character
s string
l or h Placed before any integer conversion specifiers for entering short or long type data
l or L Before any floating-point conversion specifiers, it is used to input double or long double type data

Output format control for printf

The printf function outputs data to the screen in a specified format. The function form is:

  1. printf (<格式控制串>, <参数列表>) ;
  • <format control string> is generally a string describing the format of the output data. A format control string can contain three types of characters: format specifiers, escape characters, and ordinary characters (characters other than format specifiers and escape characters). in:

    • The function of the format indicator is to convert the corresponding output data in the parameter list to the specified format output

    • The escape character outputs the corresponding special symbol according to its meaning

    • Ordinary characters are output as-is

  • <parameter list> stores the output data list, and when there are multiple output data, separate them in the middle ,. The number and order of the format indicators correspond to the output data.

When the printf function is executed, according to the format indicator, escape character and ordinary character in the format control string, it will be processed sequentially from left to right: when it encounters an ordinary character, it will output as it is; if it encounters an escape character, it will follow its Meaning Output the corresponding symbol; when a format indicator is encountered, the corresponding output data will be output in the manner specified by the format indicator.

The general form of a format specifier is:

  1. % <标志> <域宽> <精度> <转换说明符>

Among them, <标志>, <域宽>and <精度>are all optional and may not appear.

the sign

The commonly used flags of the printf function are as follows:

the sign meaning
- Output is left aligned within the field width
+ Display a plus sign before positive values ​​and a minus sign before negative values
space display a space before positive values
# When used with the octal conversion specifier 0, the output value is preceded by 0;
when used with the hexadecimal conversion specifier x or X, the output value is preceded by 0x or 0X
0 pad the field width with 0

Note: Multiple flags can be used in combination.

domain width

The field width is generally an integer, indicating the width of the data output.

  • If the actual length of the data is smaller than the field width, the data output is right-aligned, that is, the data is still output according to the field width, and the left side of the data is filled with spaces;

  • If the actual length of the data is greater than the domain width, the system will automatically break through the limitation of the domain width and output according to the actual length of the data.

Note that the minus sign occupies a character position. If the field width is not specified, the system will output according to the actual length of the data.

precision

The precision is generally an integer, and the meaning of precision is different for different types of data.

  • For integers, precision means at least the number of digits to output.

If the actual length of the data is less than the precision, it will be filled with 0 on the left so that the data length is equal to the precision; if the actual length of the data is greater than the precision, it will automatically break through the precision limit and output according to the actual length of the data. Integers have a precision of 1 by default.

  • For floating point numbers, there are two cases.

① If the conversion specifiers are e, E, and f, the precision indicates the number of significant digits after the decimal point. If the actual length of the fractional part of the data is less than the precision, 0 is added on the right so that the length of the decimal part is equal to the precision; if the actual length of the decimal part of the data is greater than the precision, the data is rounded and output according to the precision;

②If the conversion specifier of the floating point number is g and G, the precision indicates the maximum length of the print data.

  • For string data, the precision indicates the maximum length of the string output.

If the actual length of the output string is less than the precision, output according to the actual length of the string; if the actual length of the string is greater than the precision, then intercept the n characters at the beginning of the output string according to the precision (assuming the precision is n) and output.

conversion specifier

The conversion specifier indicates the type information of the output data. Commonly used conversion specifiers for the printf function are as follows:

type character meaning
d decimal integer
o octal integer
x hexadecimal integer
u unsigned decimal integer
i integer
f real decimal form
e real exponential form
g Shorter forms of f and e
c character
s string
% output % itself
l or h Before any integer conversion specifiers, it is used to output short or long type data
L Before any floating-point conversion specifiers, it is used to output long double type data

For example, output 15 with a field width of 10 and right alignment, and then output a newline character ( \n):

printf("%10d\n",15);

Say the result is: ________15(Here, use _instead of spaces to highlight the result, that is, the output result is to add 8 spaces in front of 15.)

programming requirements

输入的两个非负整数(均小于 100000,两个数之间用一个空格隔开)进行四则运算,使得输出的四则运算表达式是完整对齐的。具体输出格式要求如下:

  1. 两个整数各占5个字符的位置,如果整数不足5位,则第一个整数在前面用空格补齐,第二个整数在后面用空格补齐,运算符(+、 -、 *、 /、 =都是运算符)占1个字符,且运算符前后各留一个空格位置;

  2. 输出结果占10个字符位置,不足10位时前面用空格补齐。

程序实现

//包含标准输入输出函数库
#include <stdio.h>

int main()
{
    //声明两个整型变量,用于存储输入的两个整数
    int x,y;
    //请在Begin-End之间添加你的代码,按要求格式输出四则运算式子
    /********** Begin *********/
    scanf("%d",&x);
    scanf("%d",&y);
	printf("%5d + %-5d = %10d\n",x,y,x+y);
    printf("%5d - %-5d = %10d\n",x,y,x-y);
    printf("%5d * %-5d = %10d\n",x,y,x*y);
    printf("%5d / %-5d = %10d\n",x,y,x/y);
    /********** End **********/
    return 0;
}
输入:9876 123 
输出:

_9876 + 123__ = ______9999

_9876 - 123__ = ______9753

_9876 * 123__ = ___1214748

_9876 / 123__ = ________80

输入:7777 12 
输出:

_7777 + 12___ = ______7789

_7777 - 12___ = ______7765

_7777 * 12___ = _____93324

_7777 / 12___ = _______648

Guess you like

Origin blog.csdn.net/m0_66411584/article/details/127498236