第三周作业
来源:互联网 发布:爱哭知乎 编辑:程序博客网 时间:2024/05/21 10:13
实验作业
1.输入课本各个例题,调试运行程序,并分析程序,将每一个程序改写2到3个版本,自己分析程序结果,然后再调试运行,核对分析结果的对错。
2.编写程序输入一个三角形的三条边,计算其面积和周长;
3.编写程序计算并输出课本本章习题3表达式的值并分析结果。
4.编写一个程序,输入一个一元二次方程的三个系数,并计算其方程的解,然后输出。
5.编写程序,自己确定一个加密算法,将自己的音标姓名(英文)加密,并输出加密后结果,请注释你的加密算法。
6.在一个自动控制设备中,控制字位数16位,控制设备产生机械动作(如削,压等)的是指令字的低8位,其中保护强制停机动作的控制命令是低8位是全为0,控制报警声音是指令的高第1位,0为报警,1为不报警。请编写程序,在紧急状况启动时,向控制器输入控制指令。
7.积累调试程序经验,收集错误信息原因(每个同学收集3-5条错误信息原因,并输入电脑形成文字)。
课本例题1:
// Thomework2.cpp : Defines the entry point for the console application.////********功能:显示输出各种整型变量所占的字节数********//#include "stdafx.h"#include<iostream> //编译预处理命令using namespace std; //使用标准名空间 stdint main() //主函数{ cout<<"number of bytes in int is: "<<sizeof(int)<<endl; cout<<"number of bytes in long int is: "<<sizeof(long)<<endl; cout<<"number of bytes in short int is: "<<sizeof(short)<<endl; cout<<"number of bytes in signed int is: "<<sizeof(signed)<<endl; cout<<"number of bytes in unsigned int is: "<<sizeof(unsigned)<<endl; cout<<"number of bytes in unsigned long int is: "<<sizeof(unsigned long)<<endl; cout<<"number of bytes in unsigned short int is: "<<sizeof(unsigned short)<<endl; return 0;}输出整型数分别是4、4、2、4、4、4、2。
课本例题2:
// Thomework.cpp : Defines the entry point for the console application.//**************功能:布尔类型使用举例***********//#include "stdafx.h"#include<iostream> //编译预处理命令#include<iomanip> //使用控制符boolalpha需使用此头文件using namespace std; //使用标准名空间stdint main() //主函数{ bool flag =true; //定义布尔型变量flag,并初始化为truecout<<flag<<endl; //默认情况下为非bool字母(noboolapha),输出布尔型值cout<<boolalpha<<flag<<endl; //使用输出格式控制符boolalpha,输出布尔型值cout<<flag + 5<<endl; //在算数运算中,把布尔数据当整型数据,输出6flag= 0; //可以给bool类型的变量赋任意类型的值cout<<"执行语句flag =0;后flag的值为:"<<boolalpha<<flag<<endl;flag= 0.0; //0.0为double类型的数值cout<<"执行语句flag =0.0;后flag的值为:"<<boolalpha<<flag<<endl;flag= 1.1;cout<<"执行语句flag =1.1;后flag的值为:"<<boolalpha<<flag<<endl;flag= 1.0;cout<<"执行语句flag =1.0;后flag的值为:"<<boolalpha<<flag<<endl;flag= 0.1;cout<<"执行语句flag =0.1;后flag的值为:"<<boolalpha<<flag<<endl;return 0;}
分析:布尔类型就是用来判断正确与否,默认情况下为非bool字母(noboolalpha),输出数字。
课本例题3:
int main(){ int a,b,c,d;a=4;b=a;a=5;c=d=6;c*=a;cout<<"a="<<a<<endl<<"b="<<b<<endl<<"c="<<c<<endl<<"d="<<d<<endl;return 0;}
分析:我把d%=a+b;删去了,结果没有变化,这个验证了“=”有变的表达式未求值之前是无法对其左边的变量赋值的。
// homework.cpp : Defines the entry point for the console application.//********功能:赋值表达式语句的使用*************//#include "stdafx.h"#include<iostream>using namespace std;int main(){ int a,b,c,d;a=10;a=5;b=a;c=d=6;c*=a;cout<<"a="<<a<<endl<<"b="<<b<<endl<<"c="<<c<<endl<<"d="<<d<<endl;return 0;}
分析:我把a=5,与a=b交换位置,结果取后面运算的值。
课本例题4:
// Thomework.cpp : Defines the entry point for the console application.//**************功能:溢出举例***********//#include "stdafx.h"#include<iostream> //编译预处理命令using namespace std; //使用标准名空间stdint main() //主函数{ long i,j,m,n;i=1000000;j=1000000;m=i+j;n=i*j;cout<<"m="<<m<<endl;cout<<"n="<<n<<endl;return 0;}
分析:把short 型变成long型,只要超过了其字节大小就会随便输出一个数。
课本例题5:
// homework3.cpp : Defines the entry point for the console application.//***********++运算符号使用举例*******//#include "stdafx.h"#include<iostream>using namespace std;int main(){int i=6,j,k,temp;j=++i;k=i++;i++=1;cout<<" i= "<<i<< endl<<" j= "<<j<< endl<<" k= "<<k<< endl;return 0;}
程序中的语句++i=1;改为i++=1;则编译时回出错,说明“=”的左操作数必须为左值,而i++为右值,所以会出错。
课本例题6:
// homework3.cpp : Defines the entry point for the console application.//***********++运算符号使用举例*******//#include "stdafx.h"#include<iostream>using namespace std;int main(){ char ch; //定义字符型变量ch//cout<<"please input a character:"; //显示“please input a character”关键符cin>>ch; //输入chch=ch>='1'&&ch<='10'?ch-'1'+'2':ch; //运算符号cout<<"The result is:"<<ch<<endl; //输出结果为chreturn 0;}
即该式为:若数值在1到10之间。则若值为1到8之间的数,则输出为数+1.若数为0则为0,为9则为:。超过10则都为2
课本例题7:
// homework3.cpp : Defines the entry point for the console application.//***********++强制类型转换使用举例*******//#include "stdafx.h"#include<iostream>using namespace std;int main(){ int ab,ac;double b=3.14;char c='A';ab=int(b);ac=int(c);cout<<"b= "<<b<<endl;cout<<"ab= "<<ab<<endl;cout<<"c= "<<c<<endl;cout<<"ac= "<<ac<<endl;return 0;
强制类型转换的精度会有些损失。
作业二:编写程序输入一个三角形的三条边,计算其面积和周长;
// HOMEWORK03.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include<iostream>#include<cmath>using namespace std;int main(){double a,b,c,S,C,P;cout<<"请输入三角形的三边长:";cin>>a>>b>>c;P=(a+b+c)/2;if (a+b>c&a-b<c){C=a+b+c;S=sqrt(P*(P-a)*(P-b)*(P-c));cout<<"C="<<C<<endl;cout<<"S="<<S<<endl;}else{cout<<"输入有误,无法求出!"<<endl;}return 0;}
作业三:
#include <iostream>#include<math.h>using namespace std;int main(){double a,b,c,x1,x2,D,Re,Im;cout<<"请输入一元二次方程的系数a,b,c:";cin>>a>>b>>c;D=b*b-4*a*c;if(D>=0){x1=(-b+sqrt(D))/2*a;x2=(-b-sqrt(D))/2*a;cout<<"x1="<<x1<<endl;cout<<"x2="<<x2<<endl;}else{cout<<"该方程有虚根!";Re=(-b)/(2*a); Im=sqrt(-D)/(2*a); cout<<"X1="<<Re<<"+"<<Im<<"i"<<endl; cout<<"X2="<<Re<<"-"<<Im<<"i"<<endl; }return 0;}
作业四(自动控制):
#include<iostream> #include <bitset> using namespace std; int main(){long a,b;cout<<"输入信号(数字或者字母):";cin>>a;cout<<bitset<16>(a)<<endl;//看看a的二进制数是什么样的,方便对照结果 b=0xff00; //1111111100000000 a=a&b; //按位与之后a的高8位不变,低8位全为0,能急停,是否报警 //还得看输入的信号,若急停时必须报警,只需将b的高1位改为0,即0xfe00 //若不需要报警,即高一位必须为1,只需将结果与0x0100按位或再赋值给a //b=0x0100; //a=a|b; cout<<"a="<<a<<endl; //这是十进制数 cout<<bitset<16>(a)<<endl; //输出二进制看看是否符合要求 return 0; }
感觉这题挺难的参考了好几位同学的才写出来,主要是进制的转换不熟练,以后练习多些应该能改善。
作业五:错误集锦:
1、在输入的时候如果英文字符和中文字符没有转换过来,容易导致建立不了程序,出错了还挺难找错的,所以每次都得注意。
2、在写程序的时候容易少写了头文件,以后要注意把程序中对应的头文件写上。
3、cout对应的是<<;cin对应的是>>。
- 第三周作业
- 第三周作业 2
- 第三周作业 3
- 第三周作业 4
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- c++第三周作业
- 作业(第三周)
- C++第三周作业
- 第三周C++作业
- 第三周作业
- 第三周作业
- 第三周作业
- 第三周作业
- Software AG Installer Command line options
- 黑马程序员_java基础_java环境搭建及环境变量的配置(第1天)
- 第三周作业
- MFC修改list某一行
- echo
- 第三周作业
- zxing 的常见错误
- 给Python初学者的一些技巧
- 百度Bae云平台安装dedecms图文教程
- IOS开发之不同版本适配问题3(#ifdef __IPHONE_7_0 BaseSDK Development Target)
- 在VS2010中使用SVN
- 让BI在CRM云上起舞——Oracle Cloud CRM整合德昂BI解决方案推介会
- WGS1984 -UTM投影问题
- iOS开发记录