实验2 数据类型与表达式实验目的●掌握C++语言数据类型,熟悉如何定义一个整型、字符型、实型变量,以及对它们赋值的方法;●懂得转义字符的使用;●了解基本数据类型的字节宽度;●进一步熟悉C++程序的编辑、编译、连接和运行的过程;●掌握数据的输入输出的方法;●掌握运算符的优先级和结合性,能够正确书写表达式;●掌握不同的类型数据之间运算的类型转换规则,理解自动类型转换和会使用强制类型转换。
实验内容:1、基本数据类型的长度验证你的C++编译环境中每个基本数据类型的长度(即在内存空间所占的字节个数)。
用sizeof运算符获取类型或数据的所占字节数。
# include<iostream>using namespace std;int main(){cout << "字符'a' 所占字节数: " << sizeof('a') << endl;cout << "字符\"a\"所占字节数: " << sizeof("a") << endl;cout << "字符串\"China\" 所占字节数: " << sizeof("China") << endl;cout << "整型常量默认为int,int型所占字节数: " << sizeof(136) << endl;cout << "短整型short类型所占字节数: " << sizeof(short) << endl;cout << "实型常量.23456默认为double类型,double字节数:" << sizeof(1.23456) << endl; cout << "float型数据所占字节数:" << sizeof(float) << endl;cout << "double型数据所占字节数:" << sizeof(double) << endl;cout << "bool型数据所占的字节数:" << sizeof(bool) << endl;return 0;}1)人工分析程序,写出应得结果,上机后将二者对照。
2)仿照上述程序输出基本数据类型char, short, int, long double, float,double,bool的数据类型长度。
3)为什么字符串长度比总字符个数还要多1?2、先阅读下列程序,写出执行结果(有些程序可能存在错误,请调试、改正)(1)#include<iostream >using namespace std;int main(){char c1 = ‘’;char c2 = ‘’;c1 = 'a';c2 = 'b';cout << c1 << endl; cout << c2 << endl; return 0;}(2)#include<iostream> using namespace std; int main(){char c1 = 0 ;char c2 = 0;c1 = 102;c2 = 400;cout << c1 << endl; cout << c2 << endl; return 0;}(3)#include<iostream> using namespace std; int main(){char c1 = ‘’;char c2 = ‘’;c1 = a;c2 = b;cout << c1 << endl; cout << c2 << endl; return 0;}(4)#include<iostream>using namespace std;int main(){char c1 = “”;char c2 = “”;c1 = ”a”;c2 = ”b”;cout << c1 << endl;cout << c2 << endl;return 0;}(5)#include<iostream>using namespace std;int main(){cout << "Prints the \'\\a\' character: " << 'a' << endl;cout << "------------------------------------" << endl;cout << "Prints the \"\\t\" character:" << '\t' << "test" << endl;cout << "------------------------------------" << endl;cout << "Prints the \"\\r\" character: " << '\r' << "test\n\n\n";cout << "------------------------------------" << endl;return 0;}3、运行程序,观察实验结果。
//该程序已知圆的半径R,输出圆的周长P和面积S#include <iostream>using namespace std;#define PI 3.14159const double R = 10;int main(){double peremiter = 0;double area = 0;peremiter = 2 * R * PI; //周长 = 2πRarea = R * R * PI; //面积 = πR2cout << "周长:" << peremiter << endl;cout << "面积:" << area << endl;return 0;}1)将上述用蓝色字显示的语句改为“ int peremiter;int area;”,比较两次实验结果有什么不同,为什么?2)比较用define和const定义符号变量的格式和优缺点4、阅读下面的程序,写出执行结果。
然后上机调试,比较结果的正确性。
(有错误改正后运行)(1)#include<iostream>using namespace std;int main(){int a = 40;int b = 4;int c = 4;a = (b == c);//一个 = 号表示赋值,两个 = = 表示判断两面的值是否相等,是的话为1,否的话为0cout << "( 前面的 ) a = " << a << endl;a = a == (b = c);cout << "( 经过改变后的 ) a = " << a << endl;return 0;}(2)#include<iostream>using namespace std;int main(){int a = 0;int b = 0;int c = 0;a =b = 2;c = 3;b = a++ - 1;cout << "(第一次) a = " << a << ",b = " << b << endl;b = ++a - 1;cout << "(第二次) a = " << a << ",b = " << b << endl;b = c-- + 1;cout << "(这时的) b = " << b << ",第一次:c = " << c << endl;b = --c + 1;cout << "(这时的) b = " << b << ",第二次:c = " << c << endl;return 0;}(3)#include<iostream>using namespace std;int main(){int a = 1 ;int b = 2 ;int x = 0 ;int y = 0;cout << (a++ + ++b) << endl;//a = 2,b = 3cout << (a % b) << endl;x = ! a > b; //!表示“非”的意思//x = 0y = x-- && b;//两个&&表示“与”(而且)的意思,这里用来判断,两边都同时为真(或假)的时候为1,否则就为0cout << x << endl;cout << y << endl;return 0;}(4)#include <iostream>using namespace std;int main(){ int i = 0;int j = 0;int m = 0;int n = 0;i = 8;j = 10;m = ++i;n = j++;n = (++i) + (++j) + m;cout << i << '\t' << j << '\n';cout << m << '\t' << n << '\n';return 0;}(5)#include <iostream>#include <string>using namespace std;int main(){{string s = "a string";{string x = s + ", really";cout << s << endl;}cout << x << endl;}return 0;}5、求出下列算术表达式的值,并上机验证结果。