请设计输出实数的格式,包括:(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);
FLOAT(f);
break;
case 3: printf("Input string:");
scanf("%s",s);
STRING(s);
break;
default: printf("input error!\n");
}
}
运行结果如下:
①Choice data format:1-integer,2-float,3-string:1↙
Input integer:20↙
20 (输出整数 20)
②Choice data format:1-integer,2-float,3-string:2↙
Input float:3.79↙
3.79 (输出实数 3.79)
③Choice data format:1-integer,2-float,3-string:3↙
Input string:student↙
student (输出字符串student)
④Choice data format:1-integer,2-float,3-string:4↙
Input error!
本题参考解答只是示意性的,表明如何利用“format.h”文件中的输出格式。读者应在此基础上编出能供实际使用的各种各样的输出格式(例如,一行中输出一个、两个或若干个实数,有的使用%6.2f格式,有的使用%10.2f格式,等等)。
9.8 分别用函数和带参的宏,从3个数中找出最大数。
解:(1)用函数实现。程序如下:(xt9-8-1.c)
#include <stdio.h>
void main()
{ int a,b,c;
printf("Enter three integers:");
scanf("%d,%d,%d",&a,&b,&c);
printf("max=%d\n",max(a,b,c));
}
max(int x,int y,int z)
{ int t;
t=(x>y?x:y);
return(t>z?t:z);
}
运行结果:
Input three integers:12,34,9↙
max=34
(2)用带参的宏实现。程序如下:(xt9-8-2.c)
#include <stdio.h>
#define MAX(a,b) ((a)>(b)?(a)
b))
void main()
{ int a,b,c;
printf("Input three integers:");
scanf("%d,%d,%d",&a,&b,&c);
printf("max=%d\n",MAX(MAX(a,b),c));
}
运行结果:
Input three integers:12,34,9↙
max=34
9.9 试述"文件包含"和程序文件的连接(link)的概念,二者有何不同?
解:“文件包含”是事先将程序中需要用到的信息分别存放在不同的“头文件” (文件后缀为.h) 中,用户在编写程序时,利用#include命令将该头文件的内容包含进来,成为程序中的一部分。特别应当注意的是,该头文件与它所在的源文件共同组成一个文件模块(而不是两个文件模块)。在编译时它是作为一个文件进行编译的。连接则与此不同,它的作用是将多个目标文件连接起来。如果有两个或多个源程序文件,应先对它们分别进行编译,得到两个或多个目标文件(后缀为.obj),在连接阶段,把这些目标文件与系统提供的函数库等文件连接成一个可执行的文件(后辍为.exe)。
9.10 用条件编译方法实现以下功能:
输入一行电报文字,可以任选两种输出:一为原文输出;一为将字母变成其下一字母(如'a'变成'b'……'z'变成'a'。其他字符不变)。用#define命令来控制是否要译成密码。例如:
#define CHANGE 1
则输出密码。若
#define CHANGE 0
则不译成密码,按原码输出。
解:程序如下:(xt9-10.c)
#include <stdio.h>
#define MAX 80
#define CHANGE 1
void main()
{ char str[MAX];
int i;
printf("Input text:\n");
gets(str);
#if(CHANGE)
{ for(i=0;i<MAX;i++)
{ if(str
!='\0')
if(str>=’a'&&str<'z'||str>='A'&&str<'Z') str+=1;
else if(str=='z'||str=='Z') str-=25;
}
}
#endif
printf("Output:\n%s\n",str);
}
运行结果:
Input text:
A Lazy Brown Fox Jumps Over A Dog.
Output:
B Mbaz Cspxo Gpy Kvnqt Pwfs B Eph.
如果想输出原文,可以将程序第3行改为:
#define CHANGE O
修改后再运行:
Input text:
A Lazy Brown Fox Jumps Over A Dog.
Output:
A Lazy Brown Fox Jumps Over A Dog.