作业2 根据订票的张数和月份决定优惠折扣。
写法一:用switch()语句
#include<stdio.h>
void main()
{
int month,tickets;/*变量命名要见名知义,不要都用a,b,c之类的*/
printf("please input month:");
scanf("%d",&month);
printf("how many tickets?"); /*输入前的提示要人性化,不要写please input a这样的字样*/
scanf("%d",& tickets);
if(tickets<0||month>12||month<0)
printf(“input error!”);
else
switch (month)/*()中只能是变量或者变量构成的算术表达式,而且它们的结果必须是整型或字符型,这样才能与case后比对*/
{
case 7,/*case与后边的数据之间要用空格分开*/
case 8,/*case后边只能是结果,不能再是条件,例如:case (7<=a&&a<=9)&&b>=20 这类的写法是错误的。*/
case 9,/*多种case是一种情况,只在最后的case后写明即可,其它可不写。另外,月份不必按顺序排,一般将出现次数多的case排在前边,好处是比较的效率高*/
if (tickets >=20)
printf("10%%off \n");
else
printf("5%%off \n");
break;
default:/*其它多种情况可以放在default里,就不用一一列举了*/
if (tickets >=20)
printf("20%%off \n");/*输出%,用printf(“20%%”); */
else
printf("10%%off \n");
break;
}
}
也可以写成
#include<stdio.h>
void main()
{
int month,tickets;/*变量命名要见名知义,不要都用a,b,c之类的*/
printf("please input month:");
scanf("%d",&month);
printf("how many tickets?"); /*输入前的提示要人性化,不要写please input a这样的字样*/
scanf("%d",& tickets);
if(tickets<0||month>12||month<0)
printf(“input error!”);
else
switch (tickets/20)
{
case 1,/*20张票以上的情况*/
switch(month)
{
case 7:
case 8,
case 9,
printf("10%%off\n");break;
default,
printf("5%%off \n");break;
}
case 0,/*20张票以下的情况*/
switch(month)
{
case 7:
case 8,
case 9,
printf("20%%off \n");break;
default,
printf("10%%off \n");break;
}
}
}
写法二:用if-else语句
#include<stdio.h>
void main()
{
int month,tickets;
printf("please input month:");
scanf("%d",&month);
printf("how many tickets?");
scanf("%d",& tickets);
if(tickets<0||month>12||month<0)
printf(“input error!”);
else
{
if (month>=7&&month<=9)/*注意多层嵌套时,最好用{}明确明确彼此的逻辑关系*/
{
if (tickets>=20)
printf("10%%off ");
else
printf("5%%off ");
}
else/*其它月份*/
{
if (tickets>=20)
printf("20%%off ");
else
printf("10%%off ");/*对于if-else嵌套,尽量用不同的缩进,这样容易看清楚结构。*/
}
}
写法一:用switch()语句
#include<stdio.h>
void main()
{
int month,tickets;/*变量命名要见名知义,不要都用a,b,c之类的*/
printf("please input month:");
scanf("%d",&month);
printf("how many tickets?"); /*输入前的提示要人性化,不要写please input a这样的字样*/
scanf("%d",& tickets);
if(tickets<0||month>12||month<0)
printf(“input error!”);
else
switch (month)/*()中只能是变量或者变量构成的算术表达式,而且它们的结果必须是整型或字符型,这样才能与case后比对*/
{
case 7,/*case与后边的数据之间要用空格分开*/
case 8,/*case后边只能是结果,不能再是条件,例如:case (7<=a&&a<=9)&&b>=20 这类的写法是错误的。*/
case 9,/*多种case是一种情况,只在最后的case后写明即可,其它可不写。另外,月份不必按顺序排,一般将出现次数多的case排在前边,好处是比较的效率高*/
if (tickets >=20)
printf("10%%off \n");
else
printf("5%%off \n");
break;
default:/*其它多种情况可以放在default里,就不用一一列举了*/
if (tickets >=20)
printf("20%%off \n");/*输出%,用printf(“20%%”); */
else
printf("10%%off \n");
break;
}
}
也可以写成
#include<stdio.h>
void main()
{
int month,tickets;/*变量命名要见名知义,不要都用a,b,c之类的*/
printf("please input month:");
scanf("%d",&month);
printf("how many tickets?"); /*输入前的提示要人性化,不要写please input a这样的字样*/
scanf("%d",& tickets);
if(tickets<0||month>12||month<0)
printf(“input error!”);
else
switch (tickets/20)
{
case 1,/*20张票以上的情况*/
switch(month)
{
case 7:
case 8,
case 9,
printf("10%%off\n");break;
default,
printf("5%%off \n");break;
}
case 0,/*20张票以下的情况*/
switch(month)
{
case 7:
case 8,
case 9,
printf("20%%off \n");break;
default,
printf("10%%off \n");break;
}
}
}
写法二:用if-else语句
#include<stdio.h>
void main()
{
int month,tickets;
printf("please input month:");
scanf("%d",&month);
printf("how many tickets?");
scanf("%d",& tickets);
if(tickets<0||month>12||month<0)
printf(“input error!”);
else
{
if (month>=7&&month<=9)/*注意多层嵌套时,最好用{}明确明确彼此的逻辑关系*/
{
if (tickets>=20)
printf("10%%off ");
else
printf("5%%off ");
}
else/*其它月份*/
{
if (tickets>=20)
printf("20%%off ");
else
printf("10%%off ");/*对于if-else嵌套,尽量用不同的缩进,这样容易看清楚结构。*/
}
}