第一章
//修改例1-1的Hello World 程序,使其能够在计算机屏幕上显示\"I am a student,and I like programming!“。
#include using namespace std; int main() { cout<<\"I am a student,and I like programming!\"< return 0; } //修改例1-3的加法计算器程序,编写一个乘法计算器程序 #include using namespace std; int main() { double a,b,c; cout<<\"请输入两个数字\"< c=a*b; cout<system(\"pause>nul\"); return 0; } //修改例1-4的生日卡程序,使其能够输入和显示日期 #include using namespace std; int main() { char name1[41],name2[41],date[41]; cout<<\"请输入你的朋友的名字:\"< cout<<\"请输入你的名字:\"< cout<<\"请输入日期\"< system(\"cls\"); cout<<\"==========================================\"< return 0; } /*参考例1-5,使用梯形法计算下列定积分的值 (sinx+e^x)dx[上限1,下限-1] 其中,积分区域等分数取为200,并比较计算结果和手算结果的。*/ #include #include using namespace std; int main() { double a,b,h,sum; int n,i; a=1.00; //积分上限为1 b=-1.00; //积分下限为-1 n=200; //积分区间等分为200份 h=(a-b)/n; //小区间长度 sum=(exp(a)+exp(b))/2; //exp()为计算e^x的函数库 for(i=1;i sum=sum*h; cout<<\"结果是\"< return 0; } //仿照例1-6,编写一个计算矩形面积的程序 #include using namespace std; double square(double a,double b) //{ double s; s=a*b; return s; } int main() { 定义square函数 double length,wideth,mianji; cout<<\"请输入长度:\"< cout<<\"请输入宽度:\"< mianji=square(length,wideth); system(\"cls\"); cout<<\"面积为:\"< return 0; } 第二章 //为例2-2添加数据检验部分。给出三边长,检验其是否能构成一个三角形。如果检验不合格,输入信息“Error data!”。 #include #include using namespace std; int main() { double a,b,c,s,area; system(\"title 三角形面积计算程序-by lyz810\"); system(\"color 1e\"); cout <<\"请分别输入三边长a,b,c的值:\"; cin>>a>>b>>c; if (a + b >c && b + c > a && a + c > b) { s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); system(\"cls\"); cout<<\"面积为:\"<system(\"pause>nul\"); return 0; } else cout<<\"Error data!\"; system(\"pause>nul\"); return 0; } //输入两个角度值x,y,计算式子[sin(|x|+|y|)]/[√cos(|x+y|)] #include #include using namespace std; int main() { system(\"title 计算式子的值-by lyz810\"); system(\"color 1e\"); double x,y,z,pi=3.1415926536,a,b,c,d; cout<<\"请分别输入x和y的值(角度):\"; cin>>x>>y; x=pi*x/180; y=pi*y/180; c=fabs(x)+fabs(y); d=cos(fabs(x+y)); a=sin(c); b=sqrt(d); z=a/b; system(\"cls\"); cout<<\"原式=\"< return 0; } /*编写一个程序,要求完成以下要求: 1.提示用户输入3个小数 2.显示这3个小数 3.将这三个小数相加并显示其结果 4.将结果按四舍五入发转换成整数并显示*/ #include using namespace std; int main() { long double a,b,c,d; system(\"color 1e\"); cout<<\"请输入3个小数:\"< system(\"cls\"); cout<<\"你输入的是\"<system(\"echo 按任意键继续&pause>nul\"); d=a+b+c; system(\"cls\"); cout<system(\"echo 按任意键继续&pause>nul\"); if(a-int(a)<0.5) a=int(a); else a=int(a)+1; if(b-int(b)<0.5) b=int(b); else b=int(b)+1; if(c-int(c)<0.5) c=int(c); else c=int(c)+1; system(\"cls\"); cout<<\"你输入的数进行四舍五入后是\"<system(\"echo 按任意键结束&pause>nul\"); return 0; } //从键盘输入任意3个整数,然后输入这3个数并计算平均值 #include using namespace std; int main() { double a,b,c,d; system(\"color 1e\"); cout<<\"请输入三个整数:\"< system(\"cls\"); cout<<\"这三个整数是:\"<system(\"pause>nul\"); d=(a+b+c)/3; system(\"cls\"); cout<<\"这三个整数的平均数为:\"< return 0; } //编写一个程序,将字符串\"LOVE\"译成密码,采用替换加密法,加密规则为原来的字母用字母表中其后面的第三个字母替换,例如c用f替换,y用b替换 #include using namespace std; int main() { char a='L',b='O',c='V',d='E'; a=a+3; b=b+3; c=c+3; d=d+3; system(\"color 1e\"); cout<<\"翻译成密文为:\"<system(\"pause>nul\"); return 0; } 第三章 //编写计算n!的程序 //仅提示使用for循环的方法,使用do-while,while,甚至if 加goto都可以实现,篇幅有限,就不介绍了 #include using namespace std; void main() { start: double n,i,a; system(\"title 阶乘计算器-by lyz810\"); system(\"color 1e\"); system(\"mode con cols=26 lines=4\"); cout<<\"请输入n的值(有效值0~170):\"< a=1; for (i=1;i a=a*i; } system(\"cls\"); cout< goto start; } //计算1!+2!+...+10! #include using namespace std; void main() { double n,i,a,s=0; for (n=1;n<11;n++) { a=1; for (i=1;i a=a*i; } s=s+a; } cout<<\"1!+2!+...+10!=\"< } /*编写程序求斐波那契数列的第n项和前n项和。 斐波那契数列是形如0,1,1,2,3,5,8,13...的数列 其通项为: F0=0 F1=1 Fn=F(n-1)+F(n-2) */ #include using namespace std; void main() { int n,a=0,b=1,c=0,i=0,d=0,j=0; cout<<\"请输入n值:\"< if (n==2) { c=1; goto end; } loop: while(i c=a+b; a=b; b=c; i=i+1; while (j d=c+d; j=j+1; goto loop; } } end: system(\"cls\"); cout<<\"第\"< } //编程求 arcsinx≈x+x^3/(2*3)+1*3*x^5/(2*4*5)+...+(2n)!x^(2n+1)/(2^2n*(n!)^2*(2n+1)+...,其中|x|<1 #include #include using namespace std; double jc(int m) { double i=0,p=m,k,t=1; do{ i=i+1; p=p+1; k=p/i; t=t*k; }while(i } int main() { double sum=0; double n=0; double x,a,b,c,d; cout<<\"请输入x的值(|x|<1):\"< do{ b=2*n+1; a=pow(x,b); c=pow(2,b-1); d=jc(n)*a/c/b; sum=sum+d; n=n+1; }while(fabs(d)>0.0000001); system(\"cls\"); cout<<\"arcsin\"< return 0; } //用牛顿迭代法求方程2x^3-4x^2+3x-6=0在1.5附近的根 #include #include using namespace std; double fangcheng(double n) { double y; y=2*pow(n,3)-4*pow(n,2)+3*n-6; return y; } double daoshu(double m) { double w; w=6*pow(m,2)-8*m+3; return w; } double main() { double x=1.5,z=1.5,a; do{ z=x; a=fangcheng(x)/daoshu(x); x=x-a; }while(fabs(fangcheng(x))>=0.001||fabs(x-z)>=0.001); cout<<\"x=\"< return 0; } /*求解猴子吃桃问题。猴子在第一天摘下若干个桃子,当即就吃掉了一半,有感觉不过瘾,于是就多吃了一个。以后每天如此,到第10天时,只剩下1个桃子。 试编程计算第一天猴子摘的桃子的个数。*/ #include using namespace std; int main() { int day=9,num=1; do{ num=(num+1)*2; day=day-1; }while(day>=1); cout<<\"第一天摘了\"< return 0; } /*编写一个程序,需找用户输入的几个整数中的最小值。 用户输入的第一个数值指定后面要输入的数值个数。*/ #include using namespace std; int main() { int num,x,i=1,min; cout<<\"请输入需要判断的数的个数:\"; cin>>num; system(\"cls\"); cout<<\"\\n请在输入所有的整数:\\n\"; cin>>x; min=x; do{ i=i+1; cin>>x; if(x min=x; } }while(i return 0; } //有一个分数序列:2/1,3/2,5/3,8/5,13/8,21/13......(即后一项的分母为前一项的分子,后一项的分子为前一项分子与分母之和),求其前n项和 #include using namespace std; void main() { double num1=2.0,num2=1.0,num3; double num=2.0; double sum=0; int i=1,n; cout<<\"请输入项数n:\"< do{ num=num1/num2; sum+=num; num3=num1; num1=num1+num2; num2=num3; i=i+1; }while(i<=n); cout<<\"原式=\"< } //求a+aa+aaa+aaaa+...+aaa...a(n个),其中a为1~9之间的整数 #include using namespace std; void main() { int a,n,sum=0,num,i=1; cout<<\"请输入a的值(1~9):\"; cin>>a; system(\"cls\"); cout<<\"请输入n的值:\"; cin>>n; num=a; do{ sum+=num; num=num*10+a; i+=1; }while(i<=n); cout<<\"原式=\"< } //猜幻数游戏。由系统随机给出一个数字(即幻数),让游戏者去猜,如果猜对,则打印成功提示;否则打印出错提示,并提示游戏者选择下一步动作,最多可以猜5次 #include #include using namespace std; int main() { start:int num=rand()%100; int user,count=0; char again; system(\"cls\"); do{ system(\"title 猜数游戏,你共有5次机会!\"); cout<<\"你已经猜了\"< cin>>user; if(num==user) { system(\"cls\"); cout<<\"恭喜你,猜对了!\"; system(\"pause>nul\"); cout<<\"是否继续?(Y/N)\"; cin>>again; if(again=='Y'||again=='y') goto start; else return 0; } else if(num >user) { system(\"msg %username% /time:2 猜小了,再猜!\"); cout<<\" \"<<\"第\"< else { system(\"msg %username% /time:2 猜大了,再猜!\"); 次猜的是\"< count+=1; }while(count<5); cout<<\"真遗憾,没猜对!这个数是\"< cout<<\"\\n是否继续?(Y/N)\"; cin>>again; if(again=='Y'||again=='y') goto start; else return 0; } 第四章 //用数组来求斐波那契数列的第n项和前n项和 #include using namespace std; int main() { int array[47]; array[0]=0; array[1]=1; int i=2,sum=1,n,j,k; cout<<\"请输入项数n(最大值为47):\"; cin>>n; do{ j=i-1; k=i-2; array[i]=array[j]+array[k]; sum+=array[i]; i+=1; }while(i sum=0; if(n==2) sum=1; cout<<\"第\"< return 0; } //编写程序,将4阶方阵转置,原方阵如下 // ┌ ┐ // │ 4 6 8 9 │ // │ 2 7 4 5 │ // │ 3 8 16 15│ // │ 1 5 7 11│ // └ ┘ #include using namespace std; int main() { int array[4][4]={ {4,6,8,9}, {2,7,4,5}, {3,8,16,15}, {1,5,7,11} }; int i=0,j=0; cout<<\"原方阵为\"<<\"┌ do{ cout<<\" │\\"; do{ cout< }while(j<4); i=i+1; j=0; ┐\\n\"; cout<<\"│\\n\"; }while(i<4); cout<<\" \"<<\"└ ┘\\n\"; cout<<\"\\n\"; cout<<\"转置后的矩阵为\"<<\"┌ i=0; j=0; do{ cout<<\" │\\"; do{ cout< }while(i<4); ┐\\n\"; j=j+1; i=0; cout<<\"│\\n\"; }while(j<4); cout<<\" \"<<\"└ ┘\\n\"; cout<<\"\\n\"; system(\"pause>nul\"); return 0; } //使用数组编写一个统计学生课程平均分的程序: //输入6个学生的学号和3门课程的成绩(整数),统计每个学生3门课程的平均分(整数),最后输出统计结果。输出格式如下: //学号 高数 英语 体育 平均分 //----------------------------------------------------- #include #include using namespace std; void main() { int array[6][5]; int n=1; int i=0; int j=0; do{ array[i][4]=0; do{ system(\"cls\"); if(j==0) cout<<\"请输入第\"< cout<<\"请输入第\"< cout<<\"请输入第\"< cout<<\"请输入第\"< if(j>0) { array[i][4]+=array[i][j]; } j=j+1; }while(j<4); array[i][4]/=3; j=0; i+=1; n+=1; }while(n<=6); cout<<\"学号\\高数成绩\英语成绩\体育成绩\平均分\\n\"; cout<<\"--------------------------------------------------------------------------\\n\"; i=0; j=0; do{ do{ cout< }while(j<=4); j=0; cout<<\"\\n\"; i+=1; }while(i<=4); system(\"pause>nul\"); } //编写一个程序,要求用户输入一个十进制的正整数,然后分别转换成二进制数、八进制数和十六进制数输出 #include using namespace std; int main() { int dec,bin,oct,hex; int n=0; bool stop=0; int b[31],o[11]; char h[8]; cout<<\"请输入一个十进制正整数(不大于21474837)\"; cin>>dec; system(\"cls\"); cout< do{ do{ if (stop!=0) break; b[n]=bin%2; bin/=2; n+=1; }while(bin>0); stop+=1; n=n-1; cout<}while(n>0); cout<<\八进制是0\"; stop=0; oct=dec; n=0; do{ do{ if (stop!=0) break; o[n]=oct%8; oct/=8; n+=1; }while(oct>0); stop+=1; n=n-1; cout< cout<<\十六进制是0x\"; stop=0; hex=dec; n=0; do{ do{ if (stop!=0) break; h[n]=hex%16; if(hex%16<10) h[n]=h[n]+48; else h[n]=h[n]+55; hex/=16; n+=1; }while(hex>0); stop+=1; n=n-1; cout< cout<<\"\\n\"; system(\"pause>nul\"); return 0; } //输入10个字符到一维字符数组s中,将字符串置逆。即s[0]与s[9]互换,s[1]与s[8]互换,...,s[4]与s[5]互换,输出置逆后的数组 #include using namespace std; void main() { system(\"msg %username% /time:1 你将输入10个字符\"); int n=0; char s[11]; do{ cout<<\"请输入第\"< n+=1; system(\"cls\"); }while(n<10); cout<<\"原字符数组为:\"; n=0; do{ cout< }while(n<10); n=n-1; do{ s[10]=s[n]; s[n]=s[9-n]; s[9-n]=s[10]; n-=1; }while(n>4); cout<<\"\\n新字符数组为:\"; n=0; do{ cout< }while(n<10); system(\"pause>nul\"); } /*替换加密(凯撒加密法) 加密规则是:将原来的小写字母用字母表中其后面的3个字母的大写形式来替换,大写字母按同样规则用小写字母替换。 对于字母表最后的3个字母,可将字母表看成是首末衔接的。 例如字母c就用F来替换,字母y用B来替换。 试将字符串“I love you”译成密码*/ #include using namespace std; void main() { char array[11]=\"I love you\"; int n=0; do{ if (array[n]==0) array[n]=32; else if (array[n]>&&array[n]<88) array[n]=array[n]+35; else if (array[n]>87&&array[n]<91) array[n]=array[n]+11; else if (array[n]>96&&array[n]<120) array[n]=array[n]-29; else if (array[n]>119) array[n]=array[n]-55; cout< }while(n<11); system(\"pause>nul\"); } 第五章 //编写字符串反转函数mystrrev(),该函数的功能为将指定字符串中的字符顺序颠倒排列。然后,在编写主函数验证之,函数声明如下 //void mystrrev(char string[]) //该函数无需返回值 //提示:求该字符串长度可直接调用库函数strlen(),但在程序首部应加上#include #include #include using namespace std; char array[100]; int l; void mystrrev(char string[]) { int i=0; l=strlen(string); do{ array[l-i]=string[i]; i++; }while(i do{ string[i]=array[i]; i--; }while(i>0); } char main() { int k=0; char a[100]; cout<<\"请输入字符串(100个字符以内)\"; cin>>a; system(\"cls\"); cout<<\"原字符串 \"<mystrrev(a); cout<<\"转换后的字符串\"; do{ cout< }while(k<=l); system(\"pause>nul\"); return 0; } //编写一组求数组中最大、最小元素的函数。该组函数的原型如下: //int imax(int array[],int count) //int imin(int array[],int count) //其中,参数count为待考察的数组中的元素个数,函数的返回值即为求得的最大或最小元素值。 //要求同时编写出主函数进行验证 #include using namespace std; int imax(int array[], int count) { int i=0,num=1; array[count]=array[0]; do{ if (array[i]>array[count]) array[count]=array[i]; i++; num++; }while(num<=count); return array[count]; } int imin(int array[], int count) { int i=0,num=1; array[count]=array[0]; do{ if (array[i] i++; num++; }while(num<=count); return array[count]; } int main() { int n,array[10000]; cout<<\"请输入需要输入的数据个数:\"; cin>>n; cout<<\"\\n请分别输入这些数,用空格连接或回车连接:\"; int m=0; do{ cin>>array[m]; m++; }while(m cout<<\"你输入的\"< do{ cout< }while(m return 0; } //编写函数isprime(int a)用来判断变量a是否为素数,若是素数,函数返回1,否则返回0 //调用该函数找出任意给定的n个整数中的素数 #include using namespace std; bool isprime(int a) { if(a<=1) return 0; if(a==2) return 1; int i=2; do{ if (a%i==0) return 0; i++;system(\"pause>nul\");n+=1;n+=1;
Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务