c++实验五

1.补足代码
#include <iostream> #include <vector> #include <string> using namespace std; // 函数声明 void output1(vector<string> &); void output2(vector<string> &); int main() { vector<string>likes, dislikes; // 创建vector<string>对象likes和dislikes likes.push_back("favorite book"); likes.push_back("music"); likes.push_back("film"); // 为vector<string>数组对象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) // 补足代码 // 。。。 cout << "-----I like these-----" << endl; output1(likes);// 调用子函数输出vector<string>数组对象likes的元素值 dislikes.push_back("sport");// 补足代码 dislikes.push_back("paintings");// 。。。 dislikes.push_back("sportsman"); // 为vector<string>数组对象dislikes添加元素值 // 补足代码 // 。。。 cout << "-----I dislike these-----" << endl; output2(dislikes);// 调用子函数输出vector<string>数组对象dislikes的元素值 // 补足代码 // 。。。 likes.swap(dislikes);// 交换vector<string>对象likes和dislikes的元素值 // 补足代码 // 。。。 cout << "-----I likes these-----" << endl; output1(likes);// 调用子函数输出vector<string>数组对象likes的元素值 // 补足代码 // 。。。 cout << "-----I dislikes these-----" << endl; output2(dislikes);// 调用子函数输出vector<string>数组对象dislikes的元素值 // 补足代码 // 。。。 return 0; } // 函数实现 // 以下标方式输出vector<string>数组对象v的元素值 void output1(vector<string> &v) { for(int i=0;i<v.size();++i){ cout<<v[i]<<endl; }// 补足程序 // 。。。 } // 函数实现 // 以迭代器方式输出vector<string>数组对象v的元素值 void output2(vector<string> &v) { for(int i=0;i<v.size();++i){ cout<<v[i]<<endl; }// 补足程序 // 。。。 }

  

2.习题6-17

#include<iostream>
using namespace std;
int main(){
	int i,*p;
	p=&i;
	*p=9;
	cout<<"The value at p:"<<*p;
	return 0;
}

  

原函数在不知道指针p指向哪里的时候就改了值,是不行的,应该先给指针p一个地址,然后才能改值。

习题6-18

#include<iostream>
using namespace std;
int *fn1(){
	int *p=new int(5);
	return p;	
}
int main(){
	int *a=fn1();
	cout<<"the value of a is"<<*a;
	delete a;
	return 0;
}

  

 需要用delete释放内存。

3.matrix类

 matrix.h

#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
	public:
		Matrix(int n); 
		Matrix(int n, int m); 
		Matrix(const Matrix &X); 
		~Matrix(); 
		void setMatrix(const float *pvalue); 
		void printMatrix() const; 
		inline float &element(int i, int j){
			return p[i*cols+j];
		}
		inline float element(int i, int j) const{
			return p[i*cols+j];
		}
		void setElement(int i, int j, int value); 
		inline int getLines() const; 
		inline int  getCols() const;
	private:
		int lines;    
		int cols; 	 
		float *p;   
};

#endif

  matrix.cpp

  

#include<iostream>
#include"matrix.h"
using namespace std;
  Matrix::Matrix(int n):lines(n),cols(n){
 	p=new float[lines*cols];
  }
  Matrix::Matrix(int n,int m):lines(n),cols(m){
 	p=new float[lines*cols];
  }
  Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols){
 	p=new float[lines*cols];
 	for(int i=0;i<lines*cols;++i){
 		p[i]=X.p[i];
	}
  }
  Matrix::~Matrix(){
  }
  void  Matrix::setMatrix(const float *pvalue){
  	for(int i=0;i<lines*cols;++i)
  	p[i]=pvalue[i];
  }
  void  Matrix::printMatrix() const{
  	for(int  i=0;i<lines;++i){
	  for(int j=0;j<cols;++j)
	  cout<<element(i,j)<<" ";
	  cout<<endl; }
  }
  void Matrix::setElement(int i,int j,int value){
    p[(i-1)*lines+j-1]=value;
  }
  inline int Matrix::getLines() const{
    cout<<lines<<endl; 
  } 
  inline int Matrix::getCols() const{
    cout<<cols<<endl;
  }

  main.cpp

#include <iostream>
#include"matrix.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	Matrix A(4);
	Matrix B(3,4);
	const float *p,*o;
    float s[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    p=s;
    A.setMatrix(p);
	float t[12]={1,2,3,4,5,6,7,8,9,10,11,12};
	o=t;
	B.setMatrix(o);
	A.printMatrix();
	B.printMatrix();
	return 0;
}

  写注释会乱码、、、、

3.期中考试第一题

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
	class Dice{
		public:
		    Dice(int n);
			int cast();
		private:
			int sides;
	};
	Dice::Dice(int n){
	    sides=n;
	}
	int Dice::cast(){
		return rand()%sides;
	}
	int main(){
	Dice myDice(40);
	float j=0;
	srand(time(NULL));
	for(int i=0;i<=500;++i){
		if(myDice.cast()==23)
		++j;
	}
    cout<<j/500<<endl;
	return 0;
} 

  

  

考试的时候一直不知道随机函数怎么用,一直没写出来

期中考试第2题

uer.h

#include<iostream>
#include<cstring>
using namespace std;
class User{
    public:
        User(string Name,string Password="111111");
        void changepassword();
        void printf();
    private:
        int id;
        string name,password;
        static int CurrentID,count;
        
};

user.cpp

#include<iostream>
#include"user.h"
#include<cstring>
using namespace std;
int User::CurrentID=999;
int User::count=0;
User::User(string Name,string Password){
        id=CurrentID++;
        name=Name;
        password=Password;
}
void User::printf(){
    cout<<id<<" "<<name<<" "<<password<<endl;
}
void User::changepassword(){
    cout<<"请输入原密码"<<endl;
    string m1,m2;
   while(cin>>m1){
    if(m1==password){
         cout<<"请输入修改的密码"<<endl;
        cin>>m2;
        password=m2;break;
    }
    else{
        cout<<"密码错误,请从新输入"<<endl;
        ++count;
        if(count==3){
            count=0;
            cout<<"请稍后再试"<<endl;
            break;
         }    
        changepassword(); 
    }
}
}

main.cpp

#include <iostream>
#include"User.h"
#include <string>
using namespace std;
int main() {
    User myuser("zq");
    myuser.printf();
    myuser.changepassword();
    myuser.printf();
    return 0;
}

 

期中考试第3题

book.h

 1 #ifndef BOOK_H
 2 #define BOOK_H
 3 #include <string>
 4 using std::string;
 5 
 6 class Book {
 7     public:
 8         Book(string isbnX, string titleX, float priceX);  //构造函数  
 9         void print(); // 打印图书信息 
10     private:
11         string isbn;
12         string title;
13         float price;
14 };
15 #endif

book.cpp

#include "book.h"
#include <iostream> 
#include <string>
using namespace std;
Book::Book(string isbnX, string titleX, float priceX):isbn(isbnX),title(titleX),price(priceX){}
// 构造函数
// 补足程序 
// ...
void Book::print(){
    cout<<isbn<<" "<<title<<" "<<price<<endl;
}
// 打印图书信息
// 补足程序 
// ...

main。cpp

#include <iostream>
#include"book.h"
#include<vector>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    vector<Book>mybook;
    // 定义一个vector<Book>类对象
    // 补足程序
    // ... 
     
    string isbn, title;
    float price;
    int i=0;
    cout<<"输入的书:"; 
    while(cin>>isbn>>title>>price){
        Book a(isbn,title,price);
        mybook.push_back(a);
        cout<<"输出:"; 
        mybook[i++].print();
    }
    // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中
    // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) 
    // 补足程序
    // ... 
    
    
    // 输出入库所有图书信息
    // 补足程序
    // ... 
    
    
    return 0;
}

 

 感想:好难呀~~~绝望~~~难受。

posted @ 2018-05-23 23:01  不见曦月  阅读(242)  评论(0编辑  收藏  举报