习题3.1
Paste_Image.png编写一个程序,首先给用户以下两种选择:
(1)将温度从摄氏度转换为华氏度。
(2)将温度从华氏度转化为摄氏度。
接着,程序提示用户输入温度值,并输出转换后的数值。
方法一:
# include <stdio.h>
int main (void)
{
float Fahrenheit_temperate = 0;
float Celsius_temperate =0;
float temperate = 0;
printf(" Enter the number of temperater: ");
scanf("%.2f", &temperater);
Fahrenheit_temperate = temperater*1.8+32
Celsius_temperate = (temperater-32)*5%9
printf("%.f 摄氏度= %.f华氏度:",temperater,Fahrenheit_temperate);
scanf("%.2f", &temperater);
printf("%.f华氏度=%.f摄氏度: ", temperater,Celsius_temperate);
scanf("%.2f",&temperater);
return 0;
}
方法二:
#include <stdio.h>
int main()
{
float temperature_F = 0; //华氏度
float temperature_C = 0; //摄氏度
printf("请输入温度值: \n");
scanf("%f",&temperature_C);
temperature_F = temperature_C *1.8 + 32;
temperature_C = (temperature_F - 32)/1.8;
printf("%.2f\n", temperature_F);
printf("%.2f\n",temperature_C);
return 0;
}
习题 3.3
编写一个程序,根据从键盘输入的一个数值,计算总价(单价是$5),数值超过30的折扣是10%, 数值超过50的折扣是15%。
- 使用多项选择结构 else-if
#include <stdio.h>
int main(void)
{
const int unit_price = 5;
int quantity = 0;
float total_price = 0;
float discont = 0;
printf("Enter the number of quantity: ");
scanf("%d",&quantity);
if(quantity <=30)
{
printf("The total of price is %.2f\n",quantity*unit_price);
scanf("%.2f",&total_price);
}
else if(quantity> 30)
{
printf("The total of price is %.2f\n", quantity*unit_price*0.9);
scanf("%.2f",&total_price);
}
else (quantity >50)
{
printf("The total of price is %.2f\n",quantity*unit_price*0.85);
scanf("%.2f",&total_price);
}
return 0;
}
- 使用条件运算符
在你不知道为什么的时候,不要把过的时间放在为什么上,去想办法解决问题,先记住是什么,再问为什么 有时候不需要问为什么,规范就是这样的,你只需要记住是什么就好了
#include <stdio.h>
int main(void)
{
const int unit_price = 5;
const double discount1 = 0.1;
const double discount2 = 0.15
double total price = 0.0;
int quantity = 0;
printf("Enter the number that you want to buy: ");
scanf(" %d", &quantity);
total_price = quantity*unit_price*(1.0 -
(quantity > 50 ? discount2 : (
( quantity > 30 ?discount1: ))));
printf("The price for %d is $%.2f\n", quantity, total_price);
return 0;
}
习题3.2 编写一个程序,提示用户输入3个整数值,分别代表月,日,年。例如用户输入了12、31、2003,程序就以31stDecember 2003的格式输出该日期。
必须在日期值得后面加上th 、nd、st和rd,例如1st、2nd、3rd、4th、11th、12th、13th、14th、21st、22nd、23rd、24th
当时学的时候你不懂,你记下来,但是你有时间回头再来看看吗?
人家说对于初学者而言,C语言入门经典这本书对于初学者并不简单,但是回头再看这些书的时候,你会有不一样的领悟,所以我浪费时间把它记录下来,希望我不是真的浪费时间吧!
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
int year = 0;
int month = 0;
int day = 0;
printf_s("please enter the year:");
scanf_s("%d", &year);
printf_s("please enter the month:");
scanf_s("%d", &month);
printf_s("please enter the day:");
scanf_s("%d", &day);
switch (day) {
case 1:
case 21:
case 31:
printf_s("%dst ", day);
break;
case 2:
case 22:
printf_s("%dnd ", day);
break;
case 3:
case 23:
printf_s("%drd ", day);
break;
default:
printf_s("%dth ", day);
break;
}
switch (month) {
case 1:
printf_s("January ");
break;
case 2:
printf_s("February ");
break;
case 3:
printf_s("March ");
break;
case 4:
printf_s("April ");
break;
case 5:
printf_s("May ");
break;
case 6:
printf_s("June ");
break;
case 7:
printf_s("July ");
break;
case 8:
printf_s("August ");
break;
case 9:
printf_s("September ");
break;
case 10:
printf_s("October ");
break;
case 11:
printf_s("November ");
break;
case 12:
printf_s("December ");
break;
default:
break;
}
printf_s("%d\n", year);
return 0;
}
#include<stdio.h>
int main(void)
{
int month = 0;
int day = 0;
int year = 0;
printf("please enter the number of integer: ");//提示用户输入三个整数
scanf_s("%d %d %d", &month, &day, &year);//读取三个整数
switch (day)
{
case 1:
case 21:
printf("%dst",day);
break;
case 2:
case 22:
printf("%dnd",day);
break;
case 3:
case 23:
printf("%drd",day);
break;
case 4:
case 11:
case 12:
case 13:
case 14:
case 24:
printf("%dth",day);
break;
default:
break;
}
switch (month)
{
case 1:
printf("January");
break;
case 2:
printf("Feburday");
break;
case 3:
printf("March");
break;
case 4:
printf("May");
break;
case 5:
printf("June");
break;
case 6:
printf("July");
break;
case 7:
printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
default:
break;
}
printf("%d", year);
return 0;
```
```
#include <stdio.h>
int main()
{
int year = 0;
int month = 0;
int day = 0;
printf("请输入日期 :\n");
scanf("%d、%d、%d",&month,&day,&year);
switch(month)
{
case 1:
printf("January");
break;
case 2:
printf("February");
break;
case 3:
printf("March");
break;
case 4:
printf("April");
break;
case 5:
printf("May");
break;
case 6:
printf("June");
break;
case 7:
printf("July");
break;
case 8:
printf("August");
break;
case 9:
printf("September");
break;
case 10:
printf("October");
break;
case 11:
printf("November");
break;
case 12:
printf("December");
break;
default:
break;
}
switch(day)
{
case 1:
case 21:
case 31:
printf("%dnd",day);
break;
case 22:
case 2:
printf("%dnd",day);
break;
case 3:
case 23:
printf("%drd",day);
break;
default:
printf("%dth",day);
break;
}
printf("%d\n",year);
return 0;
}
```
网友评论
scanf() 用错了地方