求教,下面一段代码定义常量为什么只能用const关键字而不能用#define
//一个对于长袍运动员的程序#include<stdio.h>constintS_PER_M=60;//每分钟的秒数constintS_PER_H=3600;constd...
//一个对于长袍运动员的程序
#include<stdio.h>
const int S_PER_M=60;//每分钟的秒数
const int S_PER_H=3600;
const double M_PER_K=0.62137;
int main(void)
{
double distk,distm;
double rate;
int min,sec;
int time;
double mtime;
int mmin,msec;
printf("This program converts your time for a metric race\n");
printf("to a time for running a mile andto your average\n");
printf("speed in miles per hour.\n");
printf("Please enter,in kilometers,the distance run.\n");
scanf("%lf",&distk);//lf表示读取一个double类型的数值
printf("Next enter the time in minutes and seconds.\n");
printf("Begin by entering the minutes.\n");
scanf("%d",&min);
printf("Now enter the seconds.\n");
scanf("%d",&sec);
//吧时间转换为全部用秒表示
time=S_PER_M*min+sec;
//把公里转换为英里
distm=M_PER_K*distk;
//英里/秒*秒/小时=英里/小时
rate=distm/time*S_PER_H;
//时间/距离=跑完没盈利的勇士
mtime=(double)time/distm;
mmin=(int)mtime/S_PER_M;//求出分钟数
msec=(int)mtime%S_PER_M;//求出剩余的秒数
printf("You ran %1.2f km(%1.2f miles) in %d min,%d sec.\n",
distk,distm,min,sec);
printf("That pace corresponds to running a mile in %d min,",
mmin);
printf("%d sec.\nYour average speed was %1.2f mph.\n",msec,
rate);
getchar();
getchar();
return 0;
}
当我用#define定义时无法正常编译,还望指教一下,谢谢 展开
#include<stdio.h>
const int S_PER_M=60;//每分钟的秒数
const int S_PER_H=3600;
const double M_PER_K=0.62137;
int main(void)
{
double distk,distm;
double rate;
int min,sec;
int time;
double mtime;
int mmin,msec;
printf("This program converts your time for a metric race\n");
printf("to a time for running a mile andto your average\n");
printf("speed in miles per hour.\n");
printf("Please enter,in kilometers,the distance run.\n");
scanf("%lf",&distk);//lf表示读取一个double类型的数值
printf("Next enter the time in minutes and seconds.\n");
printf("Begin by entering the minutes.\n");
scanf("%d",&min);
printf("Now enter the seconds.\n");
scanf("%d",&sec);
//吧时间转换为全部用秒表示
time=S_PER_M*min+sec;
//把公里转换为英里
distm=M_PER_K*distk;
//英里/秒*秒/小时=英里/小时
rate=distm/time*S_PER_H;
//时间/距离=跑完没盈利的勇士
mtime=(double)time/distm;
mmin=(int)mtime/S_PER_M;//求出分钟数
msec=(int)mtime%S_PER_M;//求出剩余的秒数
printf("You ran %1.2f km(%1.2f miles) in %d min,%d sec.\n",
distk,distm,min,sec);
printf("That pace corresponds to running a mile in %d min,",
mmin);
printf("%d sec.\nYour average speed was %1.2f mph.\n",msec,
rate);
getchar();
getchar();
return 0;
}
当我用#define定义时无法正常编译,还望指教一下,谢谢 展开
3个回答
展开全部
#define S_PER_M 60//每分钟的秒数
#define S_PER_H 3600
#define M_PER_K 0.62137
只能和上面那样定义,因为#define只能替换成常量,比如,#define PI 3.14,用PI代替了3.14而已,#define可以起到一个全换的作用,大大提高效率喔!
#define S_PER_H 3600
#define M_PER_K 0.62137
只能和上面那样定义,因为#define只能替换成常量,比如,#define PI 3.14,用PI代替了3.14而已,#define可以起到一个全换的作用,大大提高效率喔!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
原因应该是因为const 定义的常量是有数据类型的,而宏定义的没有数据类型,所以出错。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用#define定义
#define S_PER_M 60
#define S_PER_H 3600
#define M_PER_K 0.62137
是OK的,可能是你有地方写错了
#define S_PER_M 60
#define S_PER_H 3600
#define M_PER_K 0.62137
是OK的,可能是你有地方写错了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询