当前位置:网站首页>20201108 programming exercise exercise 3

20201108 programming exercise exercise 3

2020-11-09 01:10:06 Time Stiller

2. Write a program , Ask for a prompt for ASCII Code value ( Such as ,66),  Then print the input characters .

#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);// adopt scanf() Function to read user input , And store it a variable 
	printf("%c\n", a);
	system("pause");
	return 0;
}

3. Write a program , Give an alarm , Then print the text below :
Startled by the sudden sound, Sally shouted,
"By the Great Pumpkin, what was that!"

#include<stdio.h>
// This is the code before looking at the answer 
#include<stdlib.h>
// It's not the same as the answer book, but the effect is the same /
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>
// This is the code modified according to the answer 
int main(void)
{
	char ch='\a';
	printf("%c",ch);
	// The output characters '\a' This character indicates an alarm , But some systems may not be able to produce sound 
	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. Write a program , Read a floating point number , First print it as a decimal point , And print it in exponential form . then , If the system supports , And print it as p Notation ( Hexadecimal notation ). Output... In the following format (  The actual number of index digits displayed varies by system ):
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>
// watermark 
#include<stdlib.h>
// watermark 
int main(void)
{
	float a;
	
	printf("Enter a floating-point value:");//wxy
	scanf("%f",&a);// Read user input , Store to a variable 
	printf("fixed-point notation:%f\n",a);// Print normal form 
	printf("exponential notation:%e\n",a);// Print index form 
	printf("p notation:%a\n",a);// Print p Counting form 
	system("pause");
	return 0;
}


5. About a year 3.156X10^7 second . Write a program ,  Prompt the user to enter the age , Then the number of seconds for that age is displayed .

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// watermark 
#include<stdlib.h>
// watermark 
#define SEC_PER_YEAR 3.156e7
//wxy
int main(void)
{
	float year,second;// Because the values need , Age also uses floating-point data 

	printf("please input your age:");
	scanf("%f", &year);// Read the age entered by the user 
	second = year * SEC_PER_YEAR;// Calculate the number of seconds for your age 
	printf("You are:%.1f years old.\n", year);
	printf("And you are %e seconds old,too.\n", second);
	system("pause");
	return 0;
}

6. 1 The mass of each water molecule is about 3.0X10^-23 g .1 Quart water is about 950 g . Write a program , Prompt the user to enter the quart number of water , And show the number of water molecules .

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// watermark 
#include<stdlib.h>
// watermark 
#define MASS 3.0e-23// Define the molecular mass of water 
#define MASS_PER_QUART 950// Definition 1 Quarts of water mass 
int main(void)
{
	float quart,quantity;

	printf(" Please enter the number of quarts of water :");
	scanf("%f",&quart);
	quantity=quart*MASS_PER_QUART/MASS;// Calculate the number of water molecules 
	printf(" The quart number of water is :%e\n",quantity);
	system("pause");
	return 0;
}

7. 1 Inches is equivalent to 2.54 centimeter . Write a program , Prompt user to enter height (/ Inch ), Then show height in centimeters .

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// watermark 
#include<stdlib.h>
// watermark 
#define INTOCM 2.54
// Inch to centimeter conversion factor 
int main(void)
{
	float inch,cm;

	printf("please input a inch:");
	scanf("%f", &inch);
	cm = INTOCM * inch;// Convert inches to centimeters 
	printf("%f inch equral to %f cm\n", inch, cm);// Print calculation results 
	system("pause");
	return 0;
}

8.  In the volume measurement system in the United States , 1 Pints are equal to 2 A cup of , 1 A cup is equal to 8 Oz. ,1 An ounce is equal to  2 Big spoon , 1 A big spoon is equal to 3 Teaspoon . Write a program , Prompt the user to enter the number of cups , And in pints 、 Oz. 、 a soup spoon 、 Teaspoons show equivalent capacity in units . Think about the program , Why floating point type is more suitable than integer type ?

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
// watermark 
#include<stdlib.h>
// watermark 
#define PINT_CUP 2
#define CUP_OUNCE 8
#define OUNCE_SPOON 2
#define SPOON_TEA 3
// A constant that defines the unit conversion 
int main(void)
{
	float pint,cup,ounce,spoon,tea;

	printf(" Please input the number of cups :");//wxy
	scanf("%f",&cup);
	pint=cup/PINT_CUP;
	ounce=cup*CUP_OUNCE;
	spoon=ounce*OUNCE_SPOON;
	tea=spoon*SPOON_TEA;
	printf("%.1f A cup is equal to %.1f pint , be equal to %.1f Oz. , be equal to %.1f a soup spoon , be equal to %.1f Teaspoon .\n",cup,pint,ounce,spoon,tea);
	system("pause");
	return 0;
}

Because the cup number conversion, the finished product takes off , It may produce 0.5 Pints , At this point, the value of pints is non integer , So it's better to use floating-point data than to use integer data .

版权声明
本文为[Time Stiller]所创,转载请带上原文链接,感谢