




免费预览已结束,剩余48页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
大学C+程序设计教程第二版例题源码- 53 -第1章 C+语言简介例1-1 第一个C+程序, 在计算机屏幕上显示:Hello World!/ Example 1-1: 屏幕上显示: Hello World!#include /包含基本输入输出库文件using namespace std;/使用名字空间stdint main()/主函数名cout Hello World! endl;/屏幕显示语句return 0;/表示程序顺利结束例1-2 使用欧几里德算法,编写一程序求解任意两个正整数的最大公因数。/ Example 1-2: 计算两个正整数的最大公因数#include /包含基本输入输出库文件using namespace std;/使用命名空间stdint main()/ 说明三个整型变量 p,q,rint p, q, r;/ 提示用户由键盘输入两个正整数cout Please input two integers: p q;/ 如果 p q,交换 p 和 qif(pq)r = p;p = q;q = r;/ 计算 p 除以 q 的余数 rr = p%q;/ 只要 r 不等于 0,重复进行下列计算while(r != 0)p = q;q = r;r = p%q;/ 输出结果cout The maximum common divisor is q . endl;return 0;例1-3 计算星球之间的万有引力。/ Example 1-3:计算星球之间的万有引力#include using namespace std;double grav(double m1, double m2, double distance)double g, G = 6.67E-11;g = G*m1*m2/(distance*distance); /计算万有引力return g;int main()double Gse, Gme, Msun, Mearth, Mmoon, Dme;Msun = 1.987E30;/太阳质量1.9871030千克Mearth = 5.975E24;/地球质量5.9751024千克Gse = grav(Msun, Mearth, 1.495E11);/太阳与地球两者间距1.4951011米cout The gravitation between sun and earth is Gse N. endl;Mmoon = 7.348E22; /月亮质量7.3481022千克Dme = 3.844E5;/月亮与地球两者间距3.844105米Gme=grav(Mmoon, Mearth, Dme);cout The gravitation between moon and earth is Gme N. endl;return 0;例1-4 加法计算器程序。/ Example 1-4:加法计算器程序#include using namespace std;int main()double a, b, c;coutab;c = a+b;cout a + b = c endl;return 0;例1-5 显示生日卡。该程序首先要求输入收信人和发信人的姓名,然后在屏幕上显示出完整的生日卡来。/ Example 1-5:显示生日卡#include using namespace std;int main()char name141, name241;cout endl name1;cout endl name2;cout endl = endl;cout My dear name1 , endl;cout Happy birthday to you! endl;cout yours, endl;cout name2 endl;cout = endl;return 0;例1-6 使用梯形法计算定积分,其中a = 0,b = 1,被积函数为sin(x),取积分区间等分数为 1000。/ Example 1-6:用梯形法计算定积分#include #include /包含标准数学函数库using namespace std;int main()double a, b;/ 双精度类型变量: 积分的下限和上限double h;/ 双精度类型变量: 积分步长double sum;/ 双精度类型变量: 工作变量,最后为积分值int n;/ 整型变量 : 积分区间等分数int i;/ 整型变量 : 循环工作变量/ 根据题意确定积分的下限、上限和积分区间等分数a = 0.0;b = 1.0;n = 1000;h = (b-a)/n;/ 计算小区间长度/ 为工作变量赋初值 : 先计算无需循环运算的部分sum = (sin (a)+ sin (b)/2;/ 循环计算公式中的和式for(i=1;in;i=i+1)sum = sum+ sin (a+i*h);/ 完成计算,变量 sum 中存放积分结果sum = sum*h;/ 输出计算结果coutThe result is sum endl;return 0;例1-7计算保险经纪人月薪:假定每一名保险经纪人的每月工资都由三部分组成:底薪奖金业务提成其中奖金的颁发方法为:如果经纪人已经在公司工作7年以下(含7年),奖金为每年10元;如果经纪人已经在公司工作7年以上,奖金为每年20元。业务提成的颁发方法为:如果经纪人该月销售额在1000050000元之间,可以得到3的提成;如果经纪人该月销售额超过50000元,可以得到5的提成。/ Example 1-7:计算保险经纪人月薪#include using namespace std;int main() double baseSalary, bonus, totalSale, additionalBonus, Salary;int noOfServiceYears;coutbaseSalary;coutnoOfServiceYears;/计算奖金bonusif(noOfServiceYears = 7)bonus=10*noOfServiceYears;elsebonus=20*noOfServiceYears;couttotalSale;/计算提成additionalBonusif(totalSale = 10000 & totalSale 50000)additionalBonus=totalSale*(0.03);elseadditionalBonus=totalSale*(0.05);/计算经纪人的月薪Salary Salary = baseSalary+ bonus+ additionalBonus;cout该经纪人的月薪为:Salaryendl;return 0;第2章 基本数据类型与表达式例2-1取一个整型变量的最低4位。/ Example 2-1:宏 tran16(): 取整型量的最低4位#include using namespace std;#define tran16(x) (x)&0x0f)int main() int x;coutx;coutx的最低4位是:tran16(x)endl;return 0;例2-2 根据三边长求三角形面积。/ Example 2-2:求三角形面积#include #include using namespace std;int main()double a, b, c, s, area;cout a b c;s = (a+b+c)/2;area = sqrt(s*(s-a)*(s-b)*(s-c);cout area = area endl;return 0;例2-3 求一元二次方程ax2+bx+c=0的根,其中系数a, b, c为实数,由键盘输入。/ Example 2-3:解一元二次方程#include #include using namespace std;int main()double a, b, c, delta, p, q;cout a b c;delta = b*b-4*a*c;p = -b/(2*a);q = sqrt(fabs(delta)/(2*a);if(delta = 0)cout x1 = p+q endl x2 = p-q endl;elsecout x1 = p + j q;cout endl x2 = p - j q endl;return 0;例2-4温度转换:输入一个华氏温度,计算并输出对应的摄氏温度值。/ Example 2-4:温度转换#include using namespace std;int main()double c, f;coutf;c=(f-32)*5.0/9.0;cout对应于华氏温度f的摄氏温度为cendl;return 0;例2-5输入一个四位无符号整数,反序输出这四位数。/ Example 2-5:反序输出#include using namespace std;int main() unsigned int n;char c1, c2, c3, c4;coutn;cout反序输出前的数为: n endl;c1=n%10+0; /分离个位数字c2=n/10%10+0;/分离十位数字c3=n/100%10+0;/分离百位数字c4=n/1000+0; /分离千位数字cout反序输出后的数为:c1c2c3c4endl;return 0;例2-6大小写转换:输入一个字符,判断它是否为大写字母,如是,将其转换为对应的小写字母输出;否则,不用转换直接输出。/ Example 2-6:大小写转换#include using namespace std;int main() char ch;coutch;if(ch=A & ch=Z)ch=ch-A+a;cout将大写转换为小写后,该字母为:chendl;return 0;例2-7找零钱问题:假定有伍角、壹角、伍分、贰分和壹分共五种硬币,在给顾客找硬币时,一般都会尽可能的选用硬币个数最小的方法。例如,当要给某顾客找七角二分钱时,会给他一个伍角,2个壹角和1个贰分的硬币。请编写一个程序,输入的是要找给顾客的零钱(以分为单位),输出的是应该找回的各种硬币数目,并保证找回的硬币数最少。/ Example 2-7:找零钱问题#include using namespace std;int main()int change;/存放零钱的变量coutchange;cout找给顾客的五角硬币个数为:change/50endl;change=change%50;cout找给顾客的壹角硬币个数为:change/10endl;change=change%10;cout找给顾客的伍分硬币个数为:change/5endl;change=change%5;cout找给顾客的贰分硬币个数为:change/2endl;change=change%2;cout找给顾客的壹分硬币个数为:changeendl;return 0;第3章 控制结构例3-1 编程实现如图所示的分段函数。图 分段函数/ Example 3-1:分段函数#include using namespace std;int main( ) double x, y;coutx;if(x0)y=x+1;coutx=x, y=x+1=yendl;else if(x1)y=1;coutx=x, y=yendl;elsey=x*x*x;coutx=x, y=x*x*x=yendl;return 0;例3-2编写一个程序,将百分制的学生成绩转换为优秀、良好、中等、及格和不及格的5级制成绩。标准为: 优秀: 100-90分; 良好: 80-89分; 中等: 70-79分; 及格: 60-69分; 不及格: 60分以下。/ Example 3-2:将百分制的分数转换为5级制分数#include using namespace std;int main()int old_grade, new_grade;coutold_grade;switch (old_grade/10)case 10:case 9:new_grade = 5;break;case 8:new_grade = 4;break;case 7: new_grade = 3;break;case 6:new_grade = 2;break;default:new_grade = 1;cout”Before transformed, the score is ”old_gradeendl;cout”After transformed, the score is ”new_gradeendl;return 0;例3-3计算,当通项时停止计算。/ Example 3-3:计算常数e的值#include using namespace std;int main()double e = 1.0;double u = 1.0;int n = 1;while(u = 1.0E-7)u = u/n;e = e+u;n = n+1;cout e = e ( n = n ) endl;return 0;例3-4使用do-while结构重新编写例3-3的程序。/ Example 3-4:计算常数e的值#include using namespace std;int main()double e = 1.0;double u = 1.0;int n= 1;dou = u/n;e = e+u;n = n+1;while(u=1.0E-7);cout e = e ( n = n ) endl;return 0;例3-5编写程序制作九九乘法表。/ Example 3-5:制作乘法表#include using namespace std;int main()int i, j;for(i=1; i10; i+)for(j=1; j=i; j+)cout j * i =i*j t;cout 0?x:-x;例3-7利用迭代公式求平方根。设 , 则迭代公式为 迭代结束条件取相对误差。/ Example 3-7:用迭代公式求平方根#include #include using namespace std;#define EPS 1.0e-10int main()double a, x;cout a;double x0, x1;x1 = 1.0;if(a0.0)dox0 = x1;x1 = (x0+a/x0)/2;while(fabs(x0-x1)/x1)=EPS);/fabs()函数为求绝对值的库函数x= x1;elsex= a;if(x0)cout The negative does not have square root ! endl;elsecout The square root of a is x endl;return 0;例3-8求 p 的近似值。/ Example 3-8:求的近似值#include #include using namespace std;int main()int s = 1;double n = 1.0, u = 1.0, pi = 0.0;while(fabs(u)=1.0e-4)pi = pi+u;n = n+2;s = -s;/符号位的生成u = s/n;cout pi = 4*pi endl;return 0;例3-9求水仙花数。如果一个三位数的个位数、十位数和百位数的立方和等于该数自身,则称该数为水仙花数。编一程序求出所有的水仙花数。/ Example 3-9:打印所有的水仙花数#include using namespace std;int main()int n, i, j, k;for(n=100; n=999; n+)i = n/100; / 取出n的百位数j = (n/10)%10; / 取数n的十位数k = n%10; / 取出n的个位数if(n=i*i*i+j*j*j+k*k*k)cout n = i3 + j3 + k3endl;return 0;例3-10对于任意给定的一个正整数n,统计其阶乘n!的末尾中0的个数。/ Example 3-10:统计阶乘n!的末尾中0的个数#include using namespace std;int main( ) int n; int sum=0; int i, k;/循环控制变量coutn; for(i=5; i=n; i=i+5) /只有5的倍数才含5的因子 int m=i;for(k=0; m%5=0; k+)m=m/5;sum=sum+k;coutThe number of zero in n! is: sumendl; return 0;第4章 数组与字符串例4-1 给一维数组输入7个整数,找出该数组中的最大数。/ Example 4-1: 求数组中的最大元素#include using namespace std; int main() int a7;coutPlease input an array with seven elements: endl;for(int i=0; iai;int big = a0;for(int j=0; jbig)big = aj;coutmax=bigendl;return 0;例4-2将矩阵M置成单位阵/ Example 4-2:生成单位阵#include using namespace std; int main() int M55;int i, j;for(i = 0; i 5; i+) for(j = 0; j 5; j+)Mij = 0;Mii = 1;for(i = 0; i 5; i+)for(j = 0; j 5; j+)coutMijt;coutendl;return 0;例4-3编写一个用来计算字符串长度的函数mystrlen(),并用主函数验证。 / Example 4-3:求字符串的长度 #include using namespace std; /计算字符串的长度的函数int mystrlen(char string) int len = 0; while(stringlen!=0) len = len+1; return len; /测试计算字符串长度的主函数int main()char string100; coutPlease input a string (within 99 characters): string;coutThe length of the string is:mystrlen(string)endl;return 0;例4-4string类的运算符操作/ Example 4-4 string类的运算符操作#include #include using namespace std;int main()string str1(Alpha);string str2(Beta);string str3(Omega);string str4;/ 字符串赋值str4 = str1; cout str1 n str4 n;/ 字符串连接str4 = str1 + str2; cout str4 n;str4 = str1 + to + str3;cout str4 str1) cout str1n;if(str3 = str1+str2)cout str3 = str1+str2n;/ 使用null结束的字符串赋值str1 = This is a NULL-terminated string.n;cout str1;/ 使用字符串对象构造另一个字符串string str5(str1);cout str5;/ 输入字符串cout str5;cout str5;return 0;例4-5编写程序用于计算如下两个矩阵之和。 = ?/ Example 4-5-1:矩阵相加(使用二维数组来存储矩阵)#include using namespace std; int main()const int M=3;const int N=4;double aMN=1, 2, 3, 4,5, 6, 7, 8,9, 10, 11,12;double bMN=1, 4, 7,10,2, 5, 8, 11,3, 6, 9,12;double cMN;cout 矩阵a和矩阵b的和的矩阵c为: endl;for(int i=0; iM; i=i+1)for(int j=0; jN; j=j+1)cij=aij+bij;cout cij t;cout endl;return 0;/ Example 4-5-2:矩阵相加(使用一维数组来存储矩阵)#include using namespace std; int main()const int M=3;const int N=4;double aM*N=1, 2, 3, 4,5, 6, 7, 8,9, 10, 11,12;double bM*N=1, 4, 7,10,2, 5, 8, 11,3, 6, 9,12;double cM*N;cout 矩阵a和矩阵b的和的矩阵c为: endl;for(int i=0; iM; i=i+1)for(int j=0; jN; j=j+1)ci*N+j=ai*N+j+bi*N+j;cout ci*N+j t;cout endl;return 0;例4-6编写一个用于对整型数组进行排序的程序,排序方法使用简单的交换排序法。/ Example 4-6:交换排序#include using namespace std; int main()const int COUNT=16;int listCOUNT=503, 87, 512, 61, 908, 170, 897, 275,653, 426, 154, 509, 612, 677, 765, 703;for(int i=0; ii; j-)if(listj-1listj)int tmp = listj-1;listj-1 = listj;listj = tmp;cout The result is : endl;for(int k=0; k16; k+)cout listk ;coutendl;return 0;例4-7计算50!/ Example 4-7:利用数组计算阶乘#include using namespace std; int main()const int MAXSIZE=100;int arrayMAXSIZE;int n; coutn;int sum, sc;int i, j;for(i=0; iMAXSIZE; i+)arrayi=0;array0=1;for(i=2; i=n; i+)sc=0;for(j=0; jMAXSIZE; j+)sum=arrayj*i+sc;/上一次进位值和当前计算结果求和sc=sum/10; /存放进位数值arrayj=sum%10;/将余数存入数组coutn=0; i-) coutarrayi;coutendl;return 0;例4-8编写一个字符串处理程序,将一个字符串之中的所有小写字母转换为相应的大写字母。/ Example 4-8:将字符串中所有的小写字母转换为大写字母#include using namespace std; int main() char str=This is a sample;coutThe original string is: str=a & stri=z)stri = stri-a+A;i = i+1;coutAfter transform: strendl;return 0;例4-9字符串连接。/ Example 4-9:连接两个字符串#include #include using namespace std; int main()char destination81 = abcdefghijklmnopqrstuvwxyz;char source = ABCDEFGHIJKLMNOPQRSTUVWXYZ;int i = strlen(destination);int j = 0;while(sourcej!=0)destinationi+ = sourcej+;destinationi = 0;coutThe result is: destinationendl;return 0;第5章 函 数例5-1编写一个求阶乘n!的函数。/ Example 5-1:函数 fac()计算阶乘n!int fac(int n)int result = 1;if(n1)result *= n;n-;return result;例5-2阶乘函数的调用。/ Example 5-2:测试阶乘计算函数的主程序#include using namespace std; int main()int n;cout n;cout n ! = fac(n) endl;return 0;例5-3交换两个变量的值。/ Example 5-3:交换两个变量的值 (不成功)#include using namespace std; void swap(int x, int y)int tmp;tmp = x;x = y;y = tmp;/ 测试函数 swap() 用的主函数int main( )int a = 1, b = 2;cout Before exchange:a= a ,b= b endl;swap(a, b);cout After exchange:a= a ,b= b endl;return 0;例5-4利用引用编写交换函数swap( )。/ Example 5-4:交换两个整形变量的值#include using namespace std; void swap(int &x, int &y)int tmp = x;x = y;y = tmp;/ 测试函数 swap() 用的主函数int main()int a = 1,b = 2;cout Before exchange:a= a ,b= b endl;swap(a, b);cout After exchange:a= a ,b= b endl;return 0;例5-5编写一个用于对整型数组进行排序的函数,排序方法使用例4-6的简单交换排序法。/ Example 5-5: 交换排序#include using namespace std; /函数 bubble_up(): 冒泡法排序void bubble_up(int list, int count)for(int i=0; ii; j=j-1)if(listj-1listj)int tmp = listj-1;listj-1 = listj;listj = tmp;/测试冒泡法排序的主程序int main()int i;int array16=503, 87, 512, 61, 908, 170, 897, 275,653, 426, 154, 509, 612, 677, 765, 703;cout 原数组是: endl;for(i=0; i16; i+)cout array i ;coutendl;bubble_up(array, 5);cout 对数组前5项进行排序后的结果是: endl;for(i=0; i16; i+)cout array i ;coutendl;bubble_up(array, 16);cout 对整个数组排序后的结果是: endl;for(i=0; i16; i+)cout array i ;coutendl;return 0;例5-6全局变量和局部变量。/ Example 5-6:全局变量和局部变量#include using namespace std; int x;/ 声明全局变量int func1(int x) / 函数func1()有一个名为x的参数return (x+5)*(x+5);int func2(int y)int x=y+5;/ 函数func2()中声明了一个名为x的局部变量return x*x;int main()x = 0;/ 在主函数中为全局变量x赋值coutThe result in func1 :func1(5)endl;coutThe result in func2 :func2(2)endl;coutx= xendl;return 0;例5-7一元作用域运算符: 的使用/ Example 5-7:一元作用域运算符:的使用#include using namespace std; int x=0; / 声明全局变量int main()int x = 5; / 声明局部变量coutglobal variable: :xendl;coutlocal variable: xendl;return 0;例5-8求任意两个整数的最大数(函数
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 泉州店面出租合同范本
- 民间厨房团购合同范本
- 租赁高端设备合同范本
- 活动策划会务合同范本
- 自如管家合同免责协议
- 配送搬运安装合同范本
- 租赁露营场地合同范本
- 物品购买定金合同范本
- 研究协议签不签署合同
- 物品长期供应合同范本
- 厨房管理知识第一章厨房管理概述
- 四川2022年1月自考00285《中国福利思想》真题
- 《GMP实务教程》 完整全套教学课件 项目1-14 GMP基础知识-药品生产行政检查
- 第二学期六年级家长会PPT名师优质课获奖市赛课一等奖课件
- 房屋租赁交接家私清单
- 公对私转账借款协议书
- 人教鄂教版六年级科学上册知识点总结
- 宇宙中的地球 1.3地球的历史(第1课时)课件
- 静脉治疗现状与发展趋势
- 如何书写个案护理报告
- 一线医务人员登记表(模板)
评论
0/150
提交评论