请设计输出实数的格式,包括:(1)一行输出一个实数;(2)一行输出两个实数;(3)一行输出3个实数。实数用“6.2f”格式输出。
解:程序如下:(xt9-6.c)
#include <stdio.h>
#define PR printf
#define NL "\n"
#define Fs "%f"
#define F "%6.2f"
#define F1 F NL
#define F2 F "\t" F NL
#define F3 F "\t" F "\t" F NL
void main()
{ float a,b,c;
PR("Input three floating numbers a,b,c:\n");
scanf(Fs,&a);
scanf(Fs,&b);
scanf(Fs,&c);
PR(NL);
PR("Output one floating number each line:\n");
PR(F1,a);
PR(F1,b);
PR(F1,c);
PR(NL);
PR("Output two floating numbers:\n");
PR(F2,a,b);
PR(F1,c);
PR(NL);
PR("Output three floating numbers:\n");
PR(F3,a,b,c);
}
运行情况如下:
Input three floating numbers a,b,c:
2.4 5.9 9.1↙
Output one floating number each line:
2.40
5.90
9.10
Output two floating numbers:
2.40 5.90
9.10
Output three floating numbers:
2.40 5.90 9.10
9.7 设计所需的各种各样的输出格式(包括整数、实数、字符串等),用一个文件名"format.h",把这些信息都放到此文件内,另编一个程序文件,用#include“format.h”命令以确保能使用这些格式。
解:程序如下:(format.h)
/*format.h文件*/
#define INTEGER(d) printf("%d\n",d) /* 输出整数*/
#define FLOAT(f) printf("%8.2f\n",f) /* 输出实数*/
#define STRING(s) printf("%s\n",s) /* 输出字符串*/
以下为用户自己编写的程序,其中需要用到“format.h”文件中的输出格式
程序如下:(xt9-7.c)
#include <stdio.h>
#include "format.h"
void main()
{ int d,num;
float f;
char s[80];
printf("Choice data format:1-integer,2-float,3-string:");
scanf("%d",&num);
switch(num)
{ case 1: printf("input integer:");
scanf("%d",&d);
INTEGER(d);
break;
case 2: printf("Input float:");
scanf("%f",&f);
 %
阅读(1095) | 评论(0) | 转发(0) |