计算机程序设计基础——精讲多练C/C++语言程序的基本结构1、在屏幕上显示如下图形:***************** Hello World *****************#include<iostream>using namespace std;int main() {cout<<"****************"<<endl;cout<<"* Hello World *"<<endl;cout<<"****************"<<endl;return 0;}2、编写一个通用计算器程序,当用户输入两个数后,可以计算并输出这两个数的和、差、积、商。
#include<iostream>using namespace std;int main() {double x,y,sum,sub,mult,div;cout<<"Please enter the two Numbers for calculation:";cin>>x>>y;sum=x+y;sub=x-y;mult=x*y;div=x/y;cout<<x<<"+"<<y<<"="<<sum<<endl;cout<<x<<"-"<<y<<"="<<sub<<endl;cout<<x<<"*"<<y<<"="<<mult<<endl;cout<<x<<"/"<<y<<"="<<div<<endl;return 0;}3、编写一个计算直角三角形斜边边长的程序,要求当输入直角三角形的两个直角边边长后,能够计算出第三边(即斜边)的边长。
#include<iostream>#include<cmath>using namespace std;int main(){double a,b,c;cout<<"Please enter two right edge of the right triangle:";cin>>a>>b;c=sqrt (a*a+b*b);cout<<"Calculation results:hypotenuse c="<<c<<endl;return 0;} 5、编写程序,计算32)7.9/()2.3()1(6+-++-=x x xx x y 在X=3时的值。
#include <iostream>#include <cmath>using namespace std;int main () {double x,y;cout<<"Please enter the one Number for calculation:";cin>>x;y=sqrt (x)-6*(x+1/x)+((x-3.2)*(x-3.2))/((x+7.7)*(x+7.7)*(x+7.7));cout<<"Calculation results: y="<<y<<endl;return 0;}6、编写一个程序,当输入某学生“英语”课程的平时成绩、期中考试和期末考试成绩及各项占总成绩百分比后,可以计算出该生的“英语”课程的最终成绩。
#include <iostream>using namespace std;int main (){double peacetime,midterm, terminal,total,rate1,rate2,rate3; char p1,p2,p3;cout<<"Please input peacetime result:"<<endl;cin>>peacetime;cout<<"Please input peacetime result accounts for the percentage of total grade:"<<endl;cin>>rate1>>p1;cout<<"Please input the mid-term examination results:"<<endl; cin>>midterm;cout<<"Please input midterm examination result accounts for the percentage of total grade:"<<endl;cin>>rate2>>p2;cout<<"Please input peacetime result:"<<endl;cin>>terminal;cout<<"Please input midterm examination result accounts for the percentage of total grade:"<<endl;cin>>rate3>>p3;total=(peacetime*rate1+midterm*rate2+terminal*rate3)/100;cout<<"The students for the final result:"<<total<<endl;return 0;}数据类型与表达式1、编写一个程序,当输入某一个整数i,输出i/3的值。
变换不同的i,观察输出结果的变化。
#include<iostream>#include<cmath>using namespace std;int main() {int i;cout<<"Please enter the one number: i=";cin>>i;cout<<"i/3="<<i/3<<endl;return 0;}2、编写一个程序,当用户输入一个四位无符号整数后,程序能够反序输出这个四位数。
#include<iostream>using namespace std;int main(){int a,b=0;cout<<"Please enter a need to reverse order number:";cin>>a;while(1>0){b*=10;b+=a%10;a/=10;if(a==0) break;}cout<<b;return 0;}3、编写一个程序,要求完成以下要求。
(1)提示用户输入任意的三个小数;(2)显示这个三个小数;(3)将这三个小数相加,并显示其结果;(4)将结果按四舍五入方法转换成整数并显示。
#include<iostream>using namespace std;int main(){double a,b,c;cout<<"Please input three decimals:";cin>>a>>b>>c;cout<<"The three decimals is:"<<endl;cout<<a<<"\t"<<b<<"\t"<<c<<endl;cout<<"The result is:"<<endl;cout<<"a+b+c="<<a+b+c<<endl;cout<<"After rounding:"<<endl;cout<<int(a+b+c+0.5);return 0;}4、编写一个程序,将字符串love译成密码。
加密规则是:将原来的字母用字母表中其后面的第3个字母的来替换,如字母c就用f来替换,字母y用b来代替。
#include<iostream>using namespace std;char encryption(char ch){if(ch>='a'&&ch<='z')ch=((int)(ch-94))%26+'a';else if(ch>='A'&&ch<='Z')ch=((int)(ch-62))%26+'A';return ch;}int main(){char ch1,ch2,ch3,ch4;ch1='L';ch2='o';ch3='v';ch4='e';cout<<"string to be unencrypted is:"<<ch1<<ch2<<ch3<<ch4<<endl; cout<<"string to encrypteis:"<<encryption(ch1)<<encryption(ch2)<<encryption(ch3)<<encrypti on(ch4)<<endl;return 0;}5、编写一个程序,当用户输入某一天的两个任意时刻以后,求出这两个时刻的时间差(按秒计算)并输出。