练习环境 windows 10
Visual Studio 2017
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
float a, b, max;
printf("please enter a and b:");
scanf("%f,%f", &a, &b);
if (a >= b)
max = a;
if (b > a)
max = b;
printf("max=%6.2f\n", max);
return 0;
}
优化程序
程序中用了两个if语句,先后进行了两次判断。其实可以只用一个if语句,在这个语句程序中用了两个if语句,先后进行了两次判断。其实可以只用一个if语句,在这个语句中包含一个else分支。可对程序修改如下:
这里顺带提及一下,为什么要减少if 语句,是因为到了数据结构那本书会提到算法的时间复杂度和空间复杂度,我们平时在写程序的时候也要时常考虑到这个问题,就是如何将程序在某个特定的环境下的算法最优。
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
float a, b, max;
printf("please enter a and b:");
scanf("%f,%f", &a, &b);
if (a >= b)
max = a;
else
max = b;
printf("max=%6.2f\n", max);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
float a, b, c, t;
print("please enter a,b,c:");
scanf("%f,%f,%f", &a, &b, &c);
if(a<b)
{
t = a; a = b; b = t;
}
if (a < c)
{
t = a; a = c; c = t;
}
if(b<c)
{
t = b; b = c; c = t;
}
printf("%6.2f,%6.2f,%6.2f\n", a, b, c);
return 0;
}
运行效果
#define _CRT_SECURE_NO_WARNINGS
#include
#include
int main()
{
double a, b, c, s, area;
printf("Please enter a,b,c:");
scanf("%lf,%lf,%lf", &a, &b, &c);
if (a + b > c && b + c > a && c + a > b)
{
s = 0.5*(a + b + c);
area = sqrt(s*(s - a)*(s - b)*(s - c));
printf("area=%6.2f\n", area);
}
else
printf("It is not a trilateral\n");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int number;
double cost, price, total;
printf("plesase enter number and price:");
scanf("%d,%lf", &number, &price);
if (number >= 500) //以下为6行嵌套语句
cost = 0.15;
else
if (number >= 300)
cost = 0.10;
else
if (number >= 100)
cost = 0.075;
else
if (number >= 50)
cost = 0.05;
else cost = 0;
total = number * price * (1 - cost);
printf("total=%10.2f\n", total);
return 0;
}
格式优化
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int number;
double cost, price, total;
printf("plesase enter number and price:");
scanf("%d,%lf", &number, &price);
if (number >= 500) cost = 0.15;
else if (number >= 300) cost = 0.10;
else if (number >= 100) cost = 0.075;
else if (number >= 50) cost = 0.05;
else cost = 0;
total = number * price * (1 - cost);
printf("total=%10.2f\n", total);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int year, leap;
printf("Please enter a year:");
scanf("%d", &year);
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
leap = 1;
else
leap = 0;
}
else
leap = 1;
}
else
leap = 0;
if (leap)
printf("%d is ", year);
else
printf("%d is not ", year);
printf("a leap year.\n");
return 0;
}
运行结果
代码优化
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int year, leap;
printf("Please enter a year:");
scanf("%d", &year);
if (year % 4 != 0) leap = 0;
else if (year % 100 != 0) leap = 1;
else if (year % 400 != 0)leap = 0;
else leap = 1;
if (leap)
printf("%d is ", year);
else
printf("%d is not ", year);
printf("a leap year.\n");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int year, leap;
printf("Please enter a year:");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
leap = 1;
else
leap = 0;
if (leap)
printf("%d is ", year);
else
printf("%d is not ", year);
printf("a leap year.\n");
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
int c, s;
double p, w, d, f;
printf("请输入单价、重量和距离:");
scanf("%lf,%lf,%d", &p, &w, &s);
if (s >= 3000) c = 12;
else c = s / 250;
switch (c)
{
case 0:d = 0; break;
case 1:d = 2; break;
case 2:
case 3:d = 5; break;
case 4:
case 5:
case 6:
case 7:d = 8; break;
case 8:
case 9:
case 10:
case 11:d = 10; break;
case 12:d = 15; break;
}
f = p * w * s *(1 - d / 100.0);
printf("运费:%10.2f元\n",f);
return 0;
}
运行结果
#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
char ch;
scanf("%c", &ch);
ch = (ch >= 'A'&&ch <= 'Z') ? (ch + 32) : ch;
printf("%c\n", ch);
return 0;
}
运行结果