当前位置:网站首页>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
边栏推荐
猜你喜欢
A few lines of code can easily transfer traceid across systems, so you don't have to worry about losing the log!
How to make scripts compatible with both Python 2 and python 3?
Core knowledge of C + + 11-17 template (2) -- class template
Leetcode-15: sum of three numbers
How does FC game console work?
FC 游戏机的工作原理是怎样的?
云计算之路-出海记-小目标:Hello World from .NET 5.0 on AWS
基于链表的有界阻塞队列 —— LinkedBlockingQueue
LeetCode-15:三数之和
On buffer overflow
随机推荐
leetcode之反转字符串中的元音字母
Chapter 5 programming
Depth first search and breadth first search
23 pictures, take you to the recommended system
Linked blocking queue based on linked list
Common feature pyramid network FPN and its variants
Python应用场景多不多?
STC转STM32第一次开发
How does semaphore, a thread synchronization tool that uses an up counter, look like?
Realization of file copy
使用容器存储表格数据
Programmers should know the URI, a comprehensive understanding of the article
代码保存
Tips in Android Development: requires permission android.permission write_ Settings solution
实现图片的复制
Adding OpenGL form to MFC dialog
几行代码轻松实现跨系统传递 traceId,再也不用担心对不上日志了!
The vowels in the inverted string of leetcode
《MFC dialog中加入OpenGL窗体》
RabbitMQ快速入门详解