版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、n 第1章 C语言概述1-1 编写程序,在屏幕上显示一个如下输出: - Programming in C is fun! I love C language.-Program#include <stdio.h>main()printf("-n"); printf("Programming in C is fun!n"); printf("I love C language.n");printf("-n");1-2 编写程序,在屏幕上显示一个如下图案: * * * * * * * * * *Program
2、 (1)#include <stdio.h>main()printf("* * * *n"); printf(" * * *n");printf(" * *n");printf(" *n ");Program (2) #include <stdio.h>main()printf("%c%4c%4c%4cn", '*', '*', '*', '*'); printf("%3c%4c%4cn"
3、, '*', '*', '*');printf("%5c%4cn", '*', '*');printf("%7cn ", '*');1-3 已知某个圆的半径,编写一个程序,用来计算并显示面积。要求:将定义为符号常量,并假设一个恰当的半径值。Program#include <stdio.h>#define PI 3.14main()float r=5, s;s = PI*r*r; printf("The area of circle is:
4、 %.2fn", s);Output The area of circle is: 78.501-4 已知两个整数20和10,编写程序,自定义函数add( )将这两个数相加,自定义函数sub( )计算这两个数的差,并按照下面形式显示计算结果:20+10=30 20-10=10Program#include <stdio.h>int add(int a, int b)return (a+b);int sub(int a, int b)return (a-b);main()int a=20, b=10;printf("%d + %d = %dn", a,
5、b, add(a, b);printf("%d - %d = %dn", a, b, sub(a, b);Output 20 + 10 = 3020 10 = 101-5 已知变量a、b和c的值,编写程序,用来计算并显示x的值,其中请分别用以下数值运行该程序(1)a=250,b=85,c=25(2)a=300,b=70,c=80Program (1)#include <stdio.h>main()int a=250, b=85, c=25;float x;x=1.0*a/(b-c);printf("x = %.2fn", x);Output
6、(1)x = 4.17Program (2)#include <stdio.h>main()int a=300, b=70, c=80;float x;x=1.0*a/(b-c); /*试写成x=a/(b-c); 得到什么运行结果?为什么?*/printf("x = %.2fn", x);Output (2)x = -30.00n 第2章 常量、变量及数据类型 & 第3章 运算符和表达式3-1 编写程序,求华氏温度100oF对应的摄氏温度。计算公式如下:式中:c表示摄氏温度,f表示华氏温度。(c定义为实型,f定义为整型)Program #include
7、<stdio.h>main()int f=100;float c;c=5.0*(f-32)/9;/*如果是c=5*(f-32)/9; 会是什么结果?为什么?*/printf("Celsius degree (corresponding to %d Fahrenheit) is: %.2f.n", f, c);Output Celsius degree (corresponding to 100 Fahrenheit) is: 37.78.3-2 一个物体从100m的高空自由落下,编写程序,求它在前3s内下落的垂直距离。设重力加速度为10m/s2。要求,将重力加速
8、度定义为符号常量,尝试将其改为9.8 m/s2,看结果有何不同?Program #include <stdio.h>#define G 10main()int t=3;float s;s=1.0/2*G*t*t; /*如果是s=1/2*G*t*t; 会是什么结果?为什么?*/printf("The falling vertical distance (in %d seconds) is: %.2f.n", t, s);Output The falling vertical distance (in 3 seconds) is:45.00.3-3 将球的半径R定义
9、为符号常量,计算球的表面积(4R2)和体积(4/3*R3)。Program #include <stdio.h>#define R 5.2#define PI 3.14main()float s, v;s=4*PI*R*R;v=4.0/3*PI*R*R*R;printf("The surface area of the ball (radius is %.2f) is: %.2f, and the volume is: %.2f.n", R, s, v);Output The surface area of the ball (radius is 5.20) i
10、s: 339.62, and the volume 588.68.3-4 给定x、y和z的值,编写程序,使x等于y的值,y等于z的值,z等于x的值。Program #include <stdio.h>main()int x=1, y=2, z=3, t;printf("Before swap: x=%d, y=%d, z=%d.n", x, y, z);t=x;x=y;y=z;z=t;/*变量t的作用是什么?*/printf("After swap: x=%d, y=%d, z=%d.n", x, y, z);Output Before sw
11、ap: x=1, y=2, z=3.After swap: x=2, y=3, z=1.3-5 编写一个程序,给定一个浮点数(例如456.78),显示该数的十位数字与个位数字之和(例如5+6=11)。Program (1)#include <stdio.h>main()float f=456.78;int n, a, b;n=f;a = n%10;/*赋值后,a是什么值?*/b = n/10%10;/*赋值后,b是什么值?*/printf("The sum of the tens digit and units digit of %.2f is: %d + %d = %d
12、.n", f, b, a, a+b);Program (2)#include <stdio.h>main()float f=456.78;int n, a, b;n=f;a = n%10;b = n%100/10;/*该语句与上面程序不同,看看有何区别?*/printf("The sum of the tens digit and units digit of %.2f is: %d + %d = %d.n", f, b, a, a+b);Output The sum of the tens digit and units digit of 456.7
13、8 is: 5+ 6 = 11.3-6 某种物品每年折旧费的计算方法如下:编写一个程序,当给定某物品的购买价格、使用年限和每年的折旧费时,计算出其废品价值。Program #include <stdio.h>main()float price=120.65, year=3, depreciation=10.2, value;value = price - year*depreciation;printf("The scrap value is %.2f.n", value);Output The scrap value is 90.05.3-7 在库存管理中,某
14、单个物品的经济定购数EOQ由下面等式给定:而最优的定购时间间隔TBO由下面等式给定:编写程序,给定需求率(单位时间内的物品数)、生产成本(每个定购)和储备成本(单位时间内每种物品),计算EOQ和TBO。Program #include <stdio.h>#include <math.h>main()int demand=1000;float product_cost=3.5, storage_cost=0.63, eoq, tbo;eoq=sqrt(2*demand*product_cost/storage_cost);tbo=sqrt(2*product_cost/d
15、emand/storage_cost);printf("EOQ is %.2f, and TBO is %.2f.n", eoq, tbo); Output EOQ is 105.41, and TBO is 0.11.n 第4章 输入输出操作管理4-1 输入两个数,将它们交换后输出。Program #include <stdio.h>main()int x, y, t;printf("Please input 2 numbers: ");scanf("%d%d", &x, &y);printf("
16、;Before swap, the 2 numbers are: %d, %dn", x, y);t=x;x=y;y=t;printf("After swap, the 2 numbers are: %d, %dn", x, y);Output Please input 2 numbers: 3 5¿/* Blue is input */Before swap, the 2 numbers are: 3, 5After swap, the 2 numbers are: 5, 34-2 输入一个十进制数,输出对应的八进制数和十六进制数。Program #i
17、nclude <stdio.h>main()int n;printf("Please input a decimal number: ");scanf("%d ", &n);printf("The octal is %o, and the hexadecimal is %x.n", n, n);Output Please input a decimal number: 10¿/* Blue is input */The octal is 12, and the hexadecimal is a.考虑:如何
18、得到下面的输出?Please input a decimal number: 10¿/* Blue is input */The octal is 012, and the hexadecimal is 0xa.4-3 编写程序,输入3个整数,计算并输出它们的平均值。Program #include <stdio.h>main()int a, b, c;printf("Please input 3 integers: ");scanf("%d%d%d", &a, &b, &c);printf("Th
19、e average is %.2f.n", (a+b+c)/3.0);Output Please input 3 integers: 4 7 -19¿/* Blue is input */The average is -2.67.4-4 编写一个程序,读取x和y的值,显示下面表达式的值:(1)(2)(3)Program #include <stdio.h>main()float x, y;printf("Please input x and y: ");scanf("%f%f", &x, &y);print
20、f("(1) (x+y)/(x-y) = %.2fn", (x+y)/(x-y);printf("(2) (x+y)/2 = %.2fn", (x+y)/2);printf("(3) (x+y)(x-y) = %.2fn", (x+y)*(x-y);Output Please input x and y: 3.5 4.1¿/* Blue is input */(1) (x+y)/(x-y) = -12.67(2) (x+y)/2 = 3.80(3) (x+y)(x-y) = -4.564-5 计算银行存款的本息。编写程序,输
21、入存款金额money、存期year和年利率rate,根据下列公式计算存款到期时的本息合计sum(税前),输出时保留两位小数。Program #include <stdio.h>#include <math.h>main()float money, rate, sum;int year;printf("Please input the deposit money: ");scanf("%f", &money);printf("Please input the deposit period: ");scan
22、f("%d", &year);printf("Please input the annual interest rate: ");scanf("%f", &rate);sum=money*pow(1+rate, year);printf("The total principal and interest is: %.2fn", sum);Output Please input the deposit money: 2500¿/* Blue is input */Please input t
23、he deposit period: 3¿/* Blue is input */Please input the annual interest rate: 0.023¿/* Blue is input */The total principal and interest is: 2676.504-6 输入圆柱的高h和半径r,求圆柱体积volume=*r2*h。Program #include <stdio.h>#define PI 3.14main()float volume, r, h;printf("Please input the radius
24、 of the cylinder: ");scanf("%f", &r);printf("Please input the height of the cylinder: ");scanf("%f", &h);volume=PI*r*r*h;printf("The volume of the cylinder is: %.2fn", volume);Output Please input the radius of the cylinder: 3.5¿/* Blue is inp
25、ut */Please input the height of the cylinder: 12.7¿/* Blue is input */The volume of the cylinder is: 488.514-7 编写一个程序,读取一个实数f,将其四舍五入的值赋值给整型变量n,输出n的值。(尝试不用if语句完成本程序,并考虑负数是否适用)Program (1)#include <stdio.h>main()float f;int n, m;printf("Please input a real number: ");scanf("%f
26、", &f);m=f*10;m=m%10/5;/*m是什么值?*/n=f;n=n+m;/*m有何作用?*/printf("The rounded integer is: %dn", n);Program (2)如果不明白if语句,可以在学完第5章后再看。#include <stdio.h>main()float f;int n, m;printf("Please input a real number: ");scanf("%f", &f);n=f+0.5;/*是否理解该语句?*/printf(&
27、quot;The rounded integer is: %dn", n);Output (1)Please input a real number: 3.6¿/* Blue is input */The rounded integer is: 4Output (2)Please input a real number: -13.2¿/* Blue is input */The rounded integer is: -134-8 编写程序,读入两个两位数字的整数,并按照如下形式输出这两个整数的乘积。4 5* 3 7 3 1 51 3 5 1 6 6 5提示:注意
28、格式!Program #include <stdio.h>main()int x, y, m, n;printf("Please input 2 integers: ");scanf("%d%d", &x, &y);printf("%5dn *%3dn-n", x, y);m=y%10;n=y/10;printf("%5dn%4dn-n%5dn", x*m, x*n, x*y);n 第5章 判断与分支5-1 输入一个字符ch,根据不同情况进行输出:(1)ch为小写字母,输出对应的大写字母
29、;(2)ch为大写字母,按照原样输出;(3)ch为数字,输出对应的数字;(4)ch为其他字符,输出“Other character.”。Program #include <stdio.h>main()char ch;printf("Please input a character: ");ch=getchar();if (ch>='a' && ch<='z')printf("%cn", ch-('a'-'A');elseif (ch>='
30、A' && ch<='Z')printf("%cn", ch);else if (ch>='0' && ch<='9')printf("%dn", ch-'0');elseprintf("Other character.n");Output (1)Please input a character: A¿/* Blue is input */AOutput (2)Please input a charact
31、er: b¿/* Blue is input */BOutput (3)Please input a character: 3¿/* Blue is input */3Output (4)Please input a character: ¿/* Blue is input */Other character.5-2 为鼓励居民节约用水,自来水公司采用按月用水量分段计费的办法,居民应交水费y元与月用水量x吨的函数关系式如下(设x³0)。编写程序,输入用户的月用水量x吨,计算并输出该用户应支付的水费y元(保留两位小数)。Program #include &
32、lt;stdio.h>main()float x, y;printf("Please input the water consumption: ");scanf("%f", &x);if(x<=15)y=4*x/3;/*是否可以写成y=4/3*x;?区别在哪里?*/elsey=2.5*x-10.5;printf("The water price is: %.2f. n", y);Output Please input the water consumption: 27.9¿/* Blue is input
33、 */The water price is: 59.255-3 输入一个年份year,判断year是否为闰年。year若为闰年,需要满足下列条件之一:(1)能被4整除,但不能被100整除(如2004年是闰年,2100年不是闰年)(2)能被400整除(如2000年是闰年)Program #include <stdio.h>main()int year;printf("Please input the year: ");scanf("%d", &year);if (year%4=0&&year%100!=0)|(year%
34、400=0)printf("%d is a leap year!n", year);elseprintf ("%d is not a leap year!n", year);Output Please input the year: 1900¿/* Blue is input */1900 is not a leap year!5-4 输入3个实数,将其按照降序输出(较大数在前),保留3位小数。Program (1)#include <stdio.h>main()float x, y, z;printf("Please i
35、nput 3 real numbers: ");scanf("%f%f%f", &x, &y, &z);if (x>y)if(y>z)printf("%.3f, %.3f, %.3fn", x, y, z);elseif(x>z)printf("%.3f, %.3f, %.3fn", x, z, y);elseprintf("%.3f, %.3f, %.3fn", z, x, y); elseif(x>z)printf("%.3f, %.3f, %
36、.3f", y, x, z);elseif(y>z)printf("%.3f, %.3f, %.3fn", y, z, x);elseprintf("%.3f, %.3f, %.3fn", z, y, x);/*程序结束时,x, y, z的值是否发生了变化?*/Program (2)#include <stdio.h>main()float x, y, z,t;printf("Please input 3 real numbers: ");scanf("%f%f%f", &x,
37、&y, &z);if (x>y)t=x; x=y; y=t;if (x>z)t=x; x=z; z=t;if (y>z)t=y; y=z; z=t;/*此程序与上面的程序有何不同?*/printf("%.3f, %.3f, %.3fn", x, y, z); /*程序结束时,x, y, z的值是否发生了变化?*/Output Please input 3 real numbers: 7.8 5 10¿/* Blue is input */10.000, 7.800, 5.0005-5 输入五级制成绩(AE),输出相应的百分制成绩(
38、0100)区间。五级制成绩对应百分制程序区间为:A(90100)、B(8089)、C(7079)、D(6069)和E(059)。例如:输入B,输出(8089)。(1)用switch语句实现;(2)用if语句实现。Program (1)#include <stdio.h>main()char grade;printf("Please input the grade: ");scanf("%c", &grade);switch (grade)case 'A': printf("90100n");brea
39、k;case 'B': printf("8089n");break;case 'C': printf("7079n");break;case 'D': printf("6069n");break;case 'E': printf("059n");break;default:printf("Wrong grade! n");Program (2)#include <stdio.h>main()char grade;prin
40、tf("Please input the grade: ");scanf("%c", &grade);if (grade='A')printf("90100n");else if (grade='B')printf("8089n");else if (grade='C')printf("7079n");else if (grade='D')printf("6069n");else if (grade=
41、39;E')printf("059n");elseprintf("Wrong grade! n");Output Please input the grade: B¿/* Blue is input */80895-6 用switch语句实现,输入数字17,输出对应的英文单词Monday, Tuesday, , Sunday。Program #include <stdio.h>main()int day;printf("Please input the day in a week: ");scanf(&q
42、uot;%d", &day);switch(day)case 1: printf("Mondayn"); break;case 2: printf("Tuesdayn");break;case 3: printf("Wednesdayn");break;case 4: printf("Thursdayn"); break;case 5: printf("Fridayn"); break;case 6: printf("Saturdayn");break;ca
43、se 7: printf("Sundayn"); break;default: printf("Wrong day! n");Output Please input the day in a week: 4¿/* Blue is input */Thursday5-7 某高校的某专业录取研究生的条件如下:(1)英语成绩³55(2)政治成绩³60(3)专业课成绩³60(4)以上三科的总数学成绩³200,或英语和专业课的总成绩³130。编写程序,读入一个学生的成绩(英语、政治和专业课),判断是否可以
44、被录取。Program #include <stdio.h>main()float english, politics, professional;printf("Please input score of English: ");scanf("%f", &english);printf("Please input score of Politics: ");scanf("%f", &politics);printf("Please input score of the pro
45、fessional course: ");scanf("%f", &professional);if(english>=55 && politics>=60 && professional>=60 && (english+politics+professional>=200 | english+professional>=130)printf("Congratulations! You have been admitted! n");elseprintf(&q
46、uot;Sorry, you have been rejected! n");Output Please input score of English: 60¿/* Blue is input */Please input score of Politics: 70¿/* Blue is input */Please input score of professional course: 60¿/* Blue is input */Sorry, you have been rejected!5-8 编写程序,计算下面二元方程的实数根。应用如下规则:(1)
47、如果a和b的值为零,则没有解;(2)如果a的值为零,则只有一个解(x=-c/b);(3)如果b2-4ac为负数,则没有实数根;(4)否则,有两个实数根:输入a, b和c的值,输出求根的情况,如有实数根,则输出实数根的值。Program #include <stdio.h>#include <math.h>main ( ) float a, b, c, f;printf ( "Please input the value of a, b and c in the quadratic equation:nax*x + bx + c = 0n" );pri
48、ntf ( "a = " );scanf ( "%f", &a );printf ( "b = " );scanf ( "%f", &b );printf ( "c = " );scanf ( "%f", &c );printf ( "About %.2fx*x + %.2fx + %.2f = 0: ", a, b, c );if ( a = 0 && b = 0 ) printf("There is n
49、o solution!n");elseif ( a = 0 ) printf("There is only one root: %.2f.n",- c/b);elsef = b * b - 4 * a * c;if ( f <0 ) printf("There are no real root!n");elseprintf("There are 2 real roots: %.2f, %.2f.n",(-b + sqrt(f)/2/a, (-b - sqrt(f)/2/a );Output Please input t
50、he value of a, b and c in the quadratic equation:ax*x + bx + c = 0a = -3¿/* Blue is input */b = 4¿/* Blue is input */c = 5¿/* Blue is input */About -3.00x*x + 4.00x+5.00 = 0: There are 2 real roots: -0.79, 2.12.5-9 编写程序,读取x的值,求下面函数的值(1)用else if语句实现(2)用嵌套if语句实现(3)用条件运算符 ? : 实现Program (
51、1)#include <stdio.h>main ( )float x;printf ( "Please input the value of x: " );scanf ( "%f", &x );if ( x > 0 )printf ( "y = 1n" );elseif ( x = 0 ) printf ( "y = 0n" );elseprintf ( "y = -1n" );Program (2)#include <stdio.h>main ( )fl
52、oat x;printf ( "Please input the value of x: " );scanf ( "%f", &x );if ( x != 0 )/*写成if(x)是否正确?*/if ( x > 0 )/*写成if(x)是否正确?*/printf ( "y = 1n" );elseprintf ( "y = -1n" );elseprintf ( "y = 0n" );Program (3)#include <stdio.h>main ( )float
53、x;printf ( "Please input the value of x: " );scanf ( "%f", &x );printf ( "y = %dn", x > 0 ? 1 : x < 0 ? -1 : 0 );Output Please input the value of x: 9¿/* Blue is input */y = 15-10 某个服装展示厅宣布以下所卖物品季节性打折:购买总额折扣机加工制品手工制品0100无折扣5%1012005%7.5%2013007.5%10.0%大于3
54、0010.0%15.0%编写程序,计算某顾客应付的款项。要求:同时用switch和if语句实现。Program #include <stdio.h>main ( )float price, discount;char category;printf ( "Category (m/h): " ); scanf ( "%c", &category );printf ( "Price: " ); scanf ( "%f", &price );if ( category = 'm'
55、; )switch ( (int) (price-1)/100 )case 0:discount = 0;break;case 1:discount = 0.05;break;case 2: discount = 0.075; break;default:discount = 0.1; else switch ( (int) (price-1)/100 )case 0:discount = 0.05; break;case 1:discount = 0.075; break;case 2:discount = 0.1; break;default:discount = 0.15;printf
56、("The net amount to be paid is %.2fn", price*(1-discount);Output Category (m/h): h¿/* Blue is input */Price: 250.60¿/* Blue is input */The net amount to be paid is 225.545-11 编写程序,输入x,计算并输出下列分段函数f(x)的值(保留2位小数)。提示:请调用sqrt( )函数求平方根,调用pow( )函数求幂。Program #include <stdio.h>#incl
57、ude <math.h>main()float x, y;printf("Please input the value of x: ");scanf("%f", &x);if(x<0)y=pow(x,5)+2*x+1/x;elsey=sqrt(x); printf("The value of y is: %.2f. n", y);Output Please input the value of x: -2¿/* Blue is input */The value of y is: -36.505-1
58、2 编写程序,输入一个实数(例如456.78),求不小于该数的最小整数和不大于该数的最大整数。(提示:注意负数)。 Program #include <stdio.h>main()float f;int n;printf("Please input a real number: ");scanf("%f", &f);n=f;if(f>=0)printf("The largest integer (not larger than %.2f) is: %dn", f, n);if (f-n>0)printf("The smallest integer (not less than %.2f) is:
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 语文苏教版五下教育课件
- 《护工舒缓疗护培训》课件
- 《石油储备》课件
- 《控制测量学教程》课件
- 机电安装安全培训
- 陕西省西安建筑科技大学附属中学2023-2024学年七年级上学期12月月考生物试题(无答案)
- 《See使用说明》课件
- 2024-2025学年四年级上册劳动与技能教学设计
- 《SOD生产汇报》课件
- 《创业基础》项目4经营企业测试题十一
- 绿盟极光漏洞扫描工具使用方法
- APQP-4-08产品质量策划总结和认定报告
- 五年级语文上学情分析(每一课都有,全)
- 四升五数学测试题(2022年整理)
- mom-knows-best-诗歌教学讲解课件
- 2022年征信知识竞赛基础题题库(含各题型)
- 新概念英语青少版入门级Starter A 结业测试 含答案
- 临床科室医疗质量管理与持续改进培训课件
- 六年级上册数学课件- 5.1扇形统计图 |北师大版 (共14张PPT)
- 门诊诊断证明书(模板)
- JJF1101-2019环境试验设备温度、湿度校准规范-(高清现行)
评论
0/150
提交评论