C++大学教程实验指导课后实验答案(共23页).docx
《C++大学教程实验指导课后实验答案(共23页).docx》由会员分享,可在线阅读,更多相关《C++大学教程实验指导课后实验答案(共23页).docx(23页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上/整数集/chart 7#include using std:cout;using std:endl;#include integerset.hint main()IntegerSet a( 101 ),b( 101 ),c( 101 ), d( 101 );cout Enter set A: n;a.inputSet();cout nEnter set B:n;b.inputSet(); c=b.unionOfIntegerSets(a); d=b.intersectionOfIntegerSets(a);cout nUnion of A and B is:n;c.
2、setPrint();cout Intersection of A and B is:n;d.setPrint(); if( a.isEqualTo( b ) )cout Set A is equal to set Bn;elsecout Set A is not equal to set Bn;cout nInserting 77 into set A.n;a.insertElement( 77 );cout Set A is now:n;a.setPrint();cout nDeleting 77 from set A.n;a.deleteElement( 77 );cout Set A
3、is now:n;a.setPrint();cout endl;return 0; #include using std:cout;using std:cin;#include #include using std:setw;/*white include derective for intrgerSet.h*/#include integerset.hIntegerSet:IntegerSet( int s )size = s;set = new int size;assert( set != 0);emptySet();/* white call to emptyset */Integer
4、Set:IntegerSet( const IntegerSet &init )size = init.size;/*whrit a statement to allocate sufficient memory*/set = new int size ;assert( set != 0);emptySet();/*whrite stament to copy elements of init*/for( int i = 0; i size; i+ )seti = init.seti;void IntegerSet:emptySet()for( int i=0; i size ; i+)set
5、i = 0;void IntegerSet:inputSet( void )int number;docout number;if(validEntry( number)setnumber = 1;/数和位置是相同的else if (number != -1)cout Invalid Elementn;while ( number != -1 );cout Entry completen;void IntegerSet:setPrint(void) constint x = 1;bool empty = true;cout ;for (int u =0; u size; +u)if ( set
6、u )cout setw( 4 ) u (x % 10 = 0 ? n : );empty = false;+x;if ( empty )cout setw( 4 ) -;cout setw ( 4 ) r.size ? size : r.size );temp.emptySet();int iterations = ( size r.size ? size : r.size);for ( int i = 0; i r.size ? size : r.size );temp.emptySet();int iterations = ( size r.size ? size : r.size);f
7、or ( int i = 0; i iterations; i+)if( set i = 1 & r.set i = 1)temp.set i = 1;return temp;void IntegerSet:insertElement( int k )if( validEntry(k) )set k = 1;elsecout Invalid insert attempted!n;void IntegerSet:deleteElement( int m )setm = 0;/*write definition for isEqualTo*/bool IntegerSet:isEqualTo( c
8、onst IntegerSet &r ) constif(size = r.size)for(int i=0; i = 0 & x size; ;#endif/计算机#include #include SimpleCalculator.husing std:cout;using std:endl;int main() double a = 10.0; double b = 20.0; SimpleCalculator sc; cout The value of a is: a n; cout The value of b is: b nn; double addition = sc.add(
9、a, b ); cout Adding a and b yields addition n; double subtraction = sc.subtract( a, b ); cout Subtracting b from a subtraction n; double multiplication = sc.multiply( a, b ); cout Multiplying a and b yields multiplication n;double division = sc.divide( a, b ); cout Dividing a by b yields division en
10、dl; return 0;#include SimpleCalculator.hdouble SimpleCalculator:add( double a, double b ) const return a +b;double SimpleCalculator:subtract( double a, double b ) const return a - b;double SimpleCalculator:multiply( double a, double b ) const return a * b;double SimpleCalculator:divide( double a, do
11、uble b ) const return a/b;#include SimpleCalculator.hdouble SimpleCalculator:add( double a, double b ) const return a +b;double SimpleCalculator:subtract( double a, double b ) const return a - b;double SimpleCalculator:multiply( double a, double b ) const return a * b;double SimpleCalculator:divide(
12、 double a, double b ) const return a/b;/学生#ifndef STUDENT_H#define STUDENT_Hclass Studentpublic:Student( const char *);Student();void displayGrades() const;Student &addGrade( int ) ;static int getNumStudents();private:int *grades;char *name;int numGrades;int idNum;static int numStudents; ;#endif#inc
13、lude using std:cout;using std:endl;#includeusing std:setw;#include #include student.hint Student:numStudents = 0;/初始化Student:Student( const char * n ) grades = new int 100 ; grades 0 = 0; name = new char strlen( n ) + 1 ; strcpy( name, n ); numGrades = 0; numStudents+; cout A student has been addedn
14、;Student:Student() cout name has been deletedn; delete grades; delete name; numStudents-;void Student:displayGrades() const cout Here are the grades for name endl; for ( int i = 0; i numGrades; i+ ) cout setw( 5 ) grades i ; cout endl endl; Student &Student: addGrade( int grade ) numGrades+; int * t
15、emp = new int numGrades + 1 ; for ( int i = 0; i numGrades; i+ ) temp i = grades i ; temp numGrades = grade; grades numGrades-1 = temp numGrades; return *this; int Student:getNumStudents() return numStudents; #include using std:cout;using std:endl;#include student.hint main() cout There are currentl
16、y Student:getNumStudents()addGrade( 100 ).addGrade( 75 ).addGrade( 89 );s1ptr-displayGrades();Student *s2ptr = new Student( Student 2 );s2ptr-addGrade( 83 ).addGrade( 92 );s2ptr-displayGrades(); Student s3( Student 3 );s3.addGrade( 62 ).addGrade( 91 ).displayGrades();cout There are currently s3.getN
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 大学 教程 实验 指导 课后 答案 23
限制150内