c语言印章制作视频,90-本章练习-C语言教程2020年 - 视频教程 - 北盟网校 - 专注原创教学第一站...

1 编写程序,首先给用户以下两种选择:

(1)将温度从摄氏度转换为华氏度

(2)将温度从华氏度转换为温度。

接着,程序提示用户输入温度值,病输出转换后的值。

摄氏度转华氏度,可以乘以1.8 再加上32.

华氏度转摄氏度,可以减去32再乘以5除以9#include 

int main(void)

{

printf("请选择要选择的转换:1是将温度从摄氏度转换为华氏度,2是将温度从华氏度转换为温度。\n");

int choice = 0;

scanf("%d",&choice);

if(choice!=1 && choice !=2)

{

printf("请选择正确的选项\n");

}

else

{

printf("请输入要转换的温度值\n");

double tem = 0.0;

scanf("%lf",&tem);

switch(choice)

{

case 1:

printf("转换为华氏度是%lf\n",tem*1.8+32);

break;

case 2:

printf("转换为摄氏度是%lf\n",(tem-32)/1.8);

break;

}

}

return 0;

}

ffdf51f64fb7cd3a8b578252ed79bb4d.png

2 编写一个程序,提示用户输入3个整数值,分别代表月、日、年。例如用户输入了12、31、2003

程序就以31st December 2003的格式输出该日期

必须在日期后面加上 th nd st rd

例如1st  2nd  3rd  4th 11th 12th  13 th 14th

3 编写一个程序,根据从键盘输入的一个数值,计算总价,单价是5元,数值超过30的折扣是10%,数值超过50的折扣是15%#include 

int main(void)

{

int count = 0;

const int singlePrice = 5;

printf("请输入要购买的数量:\n");

scanf("%d",&count);

if(count>50)

{

printf("需要支付总额%lf",count * singlePrice * (1-0.15) );

}

else if(count>30)

{

printf("需要支付总额%lf",count * singlePrice * (1-0.1) );

}

else

{

printf("需要支付总额%lf",count * singlePrice );

}

return 0;

}

d4fd41271c0d1369b49f0fee82619cb7.png

4 修改本章最后的计算器例子,让用户选择输入y或者Y 以执行另外一个计算,输入n或者N则结束程序

(提示,这里可以使用goto语句,下章会介绍一个更好的方法)#include 

#include 

int main(void)

{

double number1 = 0.0;          /* 第一个操作数值十进制数  */

double number2 = 0.0;          /* 第二个操作数值十进制数 */

char operation = 0;            /* 操作符必须是 +, -, *, /, or %  */

char reply = 0;                /* 表示另一个计算-或不*/

tdad:

printf("\n输入操作数\n");

scanf("%lf %c %lf", &number1, &operation, &number2);

/* 这里是检查输入的代码 */

switch(operation)

{

case '+':                    /* 加法时不需要检查      */

printf("= %lf\n", number1 + number2);

break;

case '-':                    /* 减法时不需要检查 */

printf("= %lf\n", number1 - number2);

break;

case '*':                    /* 乘法时不需要检查 */

printf("= %lf\n", number1 * number2);

break;

case '/':

if(number2 == 0)           /* 需检查第二个数不为0    */

printf("\n\n\aDivision by zero error!\n");

else

printf("= %lf\n", number1 / number2);

break;

case '%':                    /* 需检查第二个数不为0    */

if((long)number2 == 0)

printf("\n\n\a除数不能为空!\n");

else

printf("= %ld\n", (long)number1 % (long)number2);

break;

default:                     /* 如果我们到了这里,操作无效 */

printf("\n\n\a无效操作!\n");

break;

}

char c = 0;

printf("继续使用计数器Y/N\n");

scanf("%c",&c);

if(toupper(c)=='Y')

{

goto tdad;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值