实用标准文案
C 语言程序设计 实验报告
班级 CS1108 日期 2012/5/26
学生姓名 王一微 学号 U201114404
第 3 次上机
实验 1 编译预处理
( 一) 实验目的
(1)掌握文件包含、宏定义、条件编译、 assert 宏的使用;
(2 )练习带参数的宏定义、条件编译的使用;
(3)练习 assert 宏的使用;
(4 )使用集成开发环境中的调试功能:单步执行、设置断点、观察变量值。
( 二) 实验内容及要求
1.源程序改错
下面是用宏来计算平方差和交换两数的源程序。在这个源程序中 , 存在若干语法和逻辑
错误 , 要求在计算机上对这个源程序进行调试修改,使之能够正确完成计算平方差和交换两
数的任务。
源程序:
#include
#define SUM a+b
#define DIF a-b
#define SWAP(a,b) a=b,b=a
void main(void)
{
int b, t;
printf("Input two integers a, b:");
scanf("%d,%d", &a,&b);
printf("\nSUM=%d\n the difference between square of a and square of b
is:%d",SUM, SUM*DIF);
SWAP(a,b);
Printf("\nNow a=%d,b=%d\n",a,b);
}
源程序在 code ::blocks 上运行后出现
精彩文档
实用标准文案
修改后为:
运行测试为:
精彩文档
实用标准文案
2 .源程序修改替换
下面是用函数实现求三个数中最大数、 计算两数之和的源程序。 在这个源程序中存在若
干语法和逻辑错误 , 要求:
(1) 对这个例子程序进行调试修改,使之能够正确完成指定任务。
(2) 用带参数的宏替换函数 max,来实现求最大数的功能。
源程序:
void main(void)
{
int a, b, c;
float d, e;
printf("Enter three integers:");
scanf("%d,%d,%d",&a,&b,&c);
printf("\nthe maximum of them is %d\n",max(a,b,c));
printf("Enter two floating point numbers:");
scanf("%f,%f",&d,&e);
printf("\nthe sum of them is %f\n",sum(d,e));
}
int max(int x, int y, int z)
{
int t;
if (x>y)
t=x;
else
t=y;
if (t
t=z;
return t;
}
float sum