当前位置:网站首页>20201108编程练习——练习3
20201108编程练习——练习3
2020-11-09 01:10:00 【时间静止器】
2.编写一个程序,要求提示输入一个ASCII码值(如,66), 然后打印输入的字符。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
//wxy
int main(void)
{
int a;
printf("please input ascii number:");
scanf("%d", &a);//通过scanf()函数读取用户输入,并储存在a变量中
printf("%c\n", a);
system("pause");
return 0;
}

3.编写一个程序,发出一声警报,然后打印下面的文本:
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"
#include<stdio.h>
//这是看了答案之前的代码
#include<stdlib.h>
//和答案课本不一样但是效果相同/
int main(void)
{
printf("\aStartled by the sudden sound,Sally shoutde,\n");/*wxy*/
printf("\"By the Great Pumpkin,what was that!\"");/*wxy*/
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
//这是根据答案修改的代码
int main(void)
{
char ch='\a';
printf("%c",ch);
//输出字符'\a'该字符表示警报,但部分系统可能无法发声
printf("Startled by the sudden sound,Sally shoutde,\n");//wxy
printf("\"By the Great Pumpkin,what was that!\"\n");//wxy
system("pause");
return 0;
}

4.编写一个程序,读取一个浮点数,先打印成小数点形式,再打印成指数形式。然后,如果系统支持,再打印成p记数法(即十六进制记数法)。按以下格式输出( 实际显示的指数位数因系统而异):
Enter a floating-point value: 64.25
fixed-point notation: 64. 250000
exponential notation: 6. 425000e+01
p notation: 0x1.01p+6
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//水印
#include<stdlib.h>
//水印
int main(void)
{
float a;
printf("Enter a floating-point value:");//wxy
scanf("%f",&a);//读取用户的输入,储存至a变量中
printf("fixed-point notation:%f\n",a);//打印普通形式
printf("exponential notation:%e\n",a);//打印指数形式
printf("p notation:%a\n",a);//打印p计数法形式
system("pause");
return 0;
}

5.一年大约有3.156X10^7秒。编写个程序, 提示用户输入年龄,然后显示该年龄对应的秒数。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//水印
#include<stdlib.h>
//水印
#define SEC_PER_YEAR 3.156e7
//wxy
int main(void)
{
float year,second;//由于数值需要,年龄也使用浮点型数据
printf("please input your age:");
scanf("%f", &year);//读取用户输入的年龄
second = year * SEC_PER_YEAR;//计算年龄对应的秒数
printf("You are:%.1f years old.\n", year);
printf("And you are %e seconds old,too.\n", second);
system("pause");
return 0;
}

6. 1个水分子的质量约为3.0X10^-23克。1夸脱水大约是950克。编写个程序,提示用户输入水的夸脱数,并显示水分子的数量。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//水印
#include<stdlib.h>
//水印
#define MASS 3.0e-23//定义水分子质量
#define MASS_PER_QUART 950//定义1夸脱水质量
int main(void)
{
float quart,quantity;
printf("请输入水的夸脱数:");
scanf("%f",&quart);
quantity=quart*MASS_PER_QUART/MASS;//计算水分子数量
printf("水的夸脱数为:%e\n",quantity);
system("pause");
return 0;
}

7. 1英寸相当于2.54厘米。编写一个程序,提示用户输入身高(/英寸),然后以厘米为单位显示身高。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//水印
#include<stdlib.h>
//水印
#define INTOCM 2.54
//英寸到厘米的转换系数
int main(void)
{
float inch,cm;
printf("please input a inch:");
scanf("%f", &inch);
cm = INTOCM * inch;//将英寸转换成厘米
printf("%f inch equral to %f cm\n", inch, cm);//打印计算结果
system("pause");
return 0;
}

8. 在美国的体积测量系统中, 1品脱等于2杯, 1杯等于8盎司,1盎司等于 2大汤勺, 1大汤勺等于3茶勺。编写一个程序,提示用户输入杯数,并以品脱、盎司、汤勺、茶勺为单位显示等价容量。思考对于该程序,为何使用浮点类型比整数类型更合适?
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
//水印
#include<stdlib.h>
//水印
#define PINT_CUP 2
#define CUP_OUNCE 8
#define OUNCE_SPOON 2
#define SPOON_TEA 3
//定义单位转换的常量
int main(void)
{
float pint,cup,ounce,spoon,tea;
printf("请输入杯数:");//wxy
scanf("%f",&cup);
pint=cup/PINT_CUP;
ounce=cup*CUP_OUNCE;
spoon=ounce*OUNCE_SPOON;
tea=spoon*SPOON_TEA;
printf("%.1f杯等于%.1f品脱,等于%.1f盎司,等于%.1f汤勺,等于%.1f茶勺。\n",cup,pint,ounce,spoon,tea);
system("pause");
return 0;
}
因为杯数转换成品脱时,有可能会产生0.5个品脱,此时品脱的值为非整数,所以使用浮点型数据比使用整型数据更合数。
版权声明
本文为[时间静止器]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4741736/blog/4708461
边栏推荐
猜你喜欢

Introduction to nmon

File queue in Bifrost (1)

AI人工智能编程培训学什么课程?

上线1周,B.Protocal已有7000ETH资产!

Huawei HCIA notes

c++11-17 模板核心知识(二)—— 类模板

Have you ever thought about why the transaction and refund have to be split into different tables

分库分表的几种常见玩法及如何解决跨库查询等问题

SAP S/4HANA 2020安装实录

Combine theory with practice to understand CORS thoroughly
随机推荐
How does semaphore, a thread synchronization tool that uses an up counter, look like?
上线1周,B.Protocal已有7000ETH资产!
Operation 2020.11.7-8
华为HCIA笔记
作业2020.11.7-8
How to make scripts compatible with both Python 2 and python 3?
平台商业化能力的另一种表现形式SAAS
你有没有想过为什么交易和退款要拆开不同的表
老大问我:“建表为啥还设置个自增 id ?用流水号当主键不正好么?”
Decorator (2)
Dynamic relu: Microsoft's refreshing device may be the best relu improvement | ECCV 2020
SAP S/4HANA 2020安装实录
Linked list
Test comparison of three domestic cloud databases
Introduction skills of big data software learning
centos7下安装iperf时出现 make: *** No targets specified and no makefile found. Stop.的解决方案
Have you ever thought about why the transaction and refund have to be split into different tables
C / C + + Programming Notes: pointer! Understand pointer from memory, let you understand pointer completely
Leetcode-11: container with the most water
Flink's datasource Trilogy 3: customization