第三周作业
来源:互联网 发布:剑灵灵族男捏脸数据吧 编辑:程序博客网 时间:2024/06/01 20:26
一:书中例题
1.
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<"numerb of bytes in int is "<<sizeof(int)<<endl;
- cout<<"numerb of bytes in long int is "<<sizeof(long)<<endl;
- cout<<"numerb of bytes in shott int is "<<sizeof(short)<<endl;
- cout<<"numerb of bytes in long long int is "<<sizeof(longlong)<< endl;
- cout<<"numerb of bytes in float int is " <<sizeof(float) << endl;
- return 0;
- }
2.
- #include<iostream>
- #include<iomanip>
- using namespace std;
- int main()
- {
- bool flag=true;
- cout<<flag<<endl;
- cout<<boolalpha<<flag<<endl;
- cout<<flag+5<<endl;
- flag=0;
- cout<<"执行语句flag=0;后flag的值为:"<<boolalpha<<flag<<endl;
- flag=0.0;
- cout<<"执行语句a=0.0;后flag的值为:"<<boolalpha<<flag<<endl;
- cout <<noboolalpha<<flag <<endl;
- return 0;
- }
3.
- #include<iostream>
- using namespace std;
- int main()
- {
- int a,b,c,d;
- a=4;
- b=a;
- a=5;
- c=d=6;
- c*=a;
- d%=a+b;
- cout<<"a = "<<a<<endl;
- cout<<"b = "<<b<<endl;
- cout<<"c = "<<c<<endl;
- cout<<"d = "<<d<<endl;
- return 0;
- }
4.
- #include<iostream>
- using namespace std;
- int main()
- {
- short i,j,m,n;
- long a,b;
- i=1000;
- j=1000;
- m=i+j;
- n=i*j;
- cout<<"i="<<i<<endl;
- cout<<"j="<<j<<endl;
- cout<<"m="<<m<<endl;
- cout<<"n="<<n<<endl;
- a=i*j;
- cout<<"a="<<a<<endl;
- return 0;
- }
5.
- #include<iostream>
- using namespace std;
- int main()
- {
- int i = 6,j,k,l,m,temp;
- j=++i;
- k=i++;
- ++i=1;
- l=i++;
- --m=1;
- cout << " i = " <<i<< endl
- << " j = " <<j<< endl
- << " k = " <<k<< endl
- << " l = " <<l<< endl
- << " m = " <<m<< endl;
- return 0;
- }
6.
- #include<iostream>
- using namespace std;
- int main()
- {
- char ch;
- cout << "Please input a character:";
- cin >> ch;
- ch= ch >= 'a'&&ch<='z'?ch-'a'+'A':ch;
- cout << "The result is :"<< ch << endl;
- ch= ch >='A' &&ch<='Z'?ch+'a'-'A':ch;
- cout << "The result is :"<< ch << endl;
- return 0;
- }
7
- #include<iostream>
- using namespace std;
- int main()
- {
- int ab,ac;
- double b = 3.14;
- char c = 'A',ad;
- ab = int(b);
- ac = int(c);
- ad = char(int(b));
- cout << "b = "<<b<<endl;
- cout << "ab = "<<ab<<endl;
- cout << "c = "<< c << endl;
- cout << "ac = " << ac <<endl;
- cout << "ad = " << ad << endl;
- return 0;
- }
二:计算三角形的面积以及周长
- #include <iostream>
- using namespace std;
- int main()
- {
- float side_1, side_2,side_3,gap_1,gap_2,gap_3;
- cout << "Please enter the first side length:";
- cin >> side_1;
- cout << endl;
- cout << "Please enter the second side length:";
- cin >> side_2;
- cout << endl;
- cout << "Please enter the third side length:";
- cin >> side_3;
- cout << endl;
- gap_1=side_2+side_3-side_1;
- gap_2=side_1+side_3-side_2;
- gap_3=side_1+side_2-side_3;
- if(gap_1>0&&gap_2>0&&gap_3>0){
- cout << "Perimeter of the triangle is :" << side_1+side_2+side_3 << endl;//计算周长并输出
- cout << "Area of the triangle is :" << (side_1+side_2+side_3)/2 << endl;//计算面积并输出
- }
- else cout << "This is not a triangle" << endl;
- return 0;
- }
- //由于考虑到三角形边长可以为非整数,所以用float型,而且就算边长为整数,面积也可能得出非整数
- //为减少麻烦起见一律用float型,结果直接输出,不另用变量储存。
三:例题3_1
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main()
- {
- int e = 1, f = 4, g = 2;
- float m = 10.5 ,n = 4.0 ,k;
- k = (e+f)/g+sqrt((double)n)*1.2/g+m ;//求平方数学函数需要加上cmath头文件才能使用
- cout << "k = " << k << endl;
- return 0;
- }
- //e+f等于5,再处于g,由于是整型,得到结果为2
- //n开平方再乘以1.2才除以2再加上m再加上前面的结果,得到最后结果为13.7,根据类型转换规则,得到的类型为double型
例题3_2:
- #include <iostream>
- using namespace std;
- int main()
- {
- float x = 2.5, y = 4.7,z;
- int a = 7 ;
- z=x+a%3*(int(x+y)%2)/4;
- cout << "结果为: " << z << endl;
- return 0;
- }
- //a%3结果为1,int(x+y)%2结果也为1,两者相乘结果为1且为int型,除以4之后得到结果为0,最后加上x,所以最后结果为2.5
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- int main()
- {
- float a ,b ,c;
- double delta ;
- cout << "Please enter a :";
- cin >> a ;
- cout << endl;
- cout << "Please enter b :";
- cin >> b ;
- cout << endl;
- cout << "Please enter c :";
- cin >> c ;
- cout << endl;
- if(a==0&&b==0)
- {
- cout << "该方程无解。"<< endl;
- exit(0);
- }
- delta = (b*b)-(4*a*c) ; //求delta
- if (delta >= 0){
- if (delta > 0){
- cout << "方程的实数根 X1= " << (-b+sqrt(delta))/(2*a) << endl;
- cout << " 方程的实数根X2= " << (-b-sqrt(delta))/(2*a) << endl;
- }
- else cout << "方程的实数根 X1 = X2 = " << (-b)/(2*a) << endl;
- }
- else {
- delta=abs(delta);
- cout << "方程的虚根X1=" << -b/(2*a) <<"+"<<sqrt(delta)/(2*a) <<"j"<< endl;
- cout << "方程的虚根X2=" << -b/(2*a) <<"-"<<sqrt(delta)/(2*a) <<"j"<< endl;
- }
- return 0;
- }
五:加密算法
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- const int Size=20;
- string name;
- int num,i,name_num[Size];
- cout << "Pleast enter your name :";
- cin >> name;
- cout << endl;
- cout << "Please enter a number you like and don't forget: ";
- cin >> num;
- cout << endl;
- for (i=0;name[i]!='\0';i++)
- {
- name_num[i]=int(name[i]);
- name_num[i]=name_num[i] ^ num;
- }
- cout << "After the name Encryption :";
- for (i=0;name[i]!='\0';i++)
- {
- cout << name_num[i]<<" ";
- }
- cout << endl;
- return 0;
- }
- //输入音标姓名,先将每个字母转换为ASCII码,再与输入的数做异或运算,最后输出加密后的结果
六:
- #include<iostream>
- #include<bitset>
- using namespace std;
- int main()
- {
- bitset<16> ctrl_1;
- bitset<16> ctrl_2 = 1000000011111111;
- while (1)
- {
- cout << "请输入16位的二进制指令:";
- cin >>ctrl_1;
- ctrl_1=ctrl_1&ctrl_2;
- if(ctrl_1==0||ctrl_1==1000000000000000){
- if(ctrl_1==1000000000000000){
- cout << '\a'<<endl;
- break;
- }
- if(ctrl_1==0)break;
- }
- }
- return 0;
- }
错误:未出现。
问题:不明白第六题的要求是什么。只能照自己理解去编程。
0 0
- 第三周作业
- 第三周作业 2
- 第三周作业 3
- 第三周作业 4
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- c++第三周作业
- 作业(第三周)
- C++第三周作业
- 第三周C++作业
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- u-boot-2011.06启动流程分析
- 无名结构体
- 大整数C++类的实现
- k-近邻算法
- 利用jquery-latest.js实现合并table多列重复行
- 第三周作业
- js javascript:void(0) 真正含义(转)
- 一种nfc蓝牙设备及其配对方法和制造方法
- random.nextInt() random.nextDouble()
- 二叉树的非递归前序、中序、后序遍历
- FreeSwitch控制台常用命令(来自freeswitch中文wiki)
- HDOJ-1533 二分图最佳完美匹配通俗解释
- android中Activity之间跳转信息传递
- 学习JS不错的网站(纯收集)