第五次实验
#include <iostream> #include <vector> #include <string> #include <iomanip> using namespace std; // 函数声明 void output1(vector<string> &); void output2(vector<string> &); int main() { vector<string>love, hate; // 创建vector<string>对象love和hate // 为vector<string>数组对象love添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) love.push_back("favorite book"); love.push_back("music"); love.push_back("film"); love.push_back("paintings"); love.push_back("anime"); love.push_back("sport"); love.push_back("sportsman"); love.push_back("etc"); cout << "-----I love these-----" << endl; // 调用子函数输出vector<string>数组对象love的元素值 output1(love); // 为vector<string>数组对象hate添加元素值 hate.push_back("favorite book"); hate.push_back("sport"); hate.push_back("sportsman"); hate.push_back("etc"); cout << "-----I hate these-----" << endl; // 调用子函数输出vector<string>数组对象hate的元素值 output1(hate); // 交换vector<string>对象love和hate的元素值 swap(hate,love); cout << "-----I love these-----" << endl; // 调用子函数输出vector<string>数组对象love的元素值 output1(love); cout << "-----I hate these-----" << endl; // 调用子函数输出vector<string>数组对象hate的元素值 output1(hate); return 0; }
定义指针只是分了一片内存用于储存地址,但该地址需要指向内存单元, 原题中指针没有指定内存单元,会随机指向某处内存, 直接赋值就是越界了,导致程序崩溃 #include<iostream> using namespace std; int main(){ int a; int *p = &a; *p = 23; cout << "The value at p :"<< *p; return 0; } // 函数实现 // 以下标方式输出vector<string>数组对象v的元素值 void output1(vector<string> &v) { for(int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } // 函数实现 // 以迭代器方式输出vector<string>数组对象v的元素值 void output2(vector<string> &v) { }
原题中用new申请了空间,但是没有释放它 #include<iostream> using namespace std; int fn1(){ int *p = new int (5); return *p; delete p; } int main(){ int a = fn1(); cout << "the value of a is:" << a; return 0; }
#include <iostream> #include "matrix.h" 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) { float a[9]; //以数组作为矩阵赋值用的内存块 for(int n1=0;n1<9;n1++) { cin>>a[n1]; } Matrix mat_one(3,3); mat_one.setMatrix(a); Matrix mat_two(mat_one); mat_one.printMatrix(); cout<<endl; mat_two.setElement(1,1,666); mat_two.setElement(3,3,233); mat_two.printMatrix(); cout<<endl; float a9=mat_two.element(1,1); cout<<"矩阵2 第一行第一列的数字为"<<a9<<endl; return 0; }
#ifndef MATRIX_H #define MATRIX_H class Matrix { public: Matrix(int n); // 构造函数,构造一个n*n的矩阵 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 ~Matrix(); //析构函数 void setMatrix(const float *pvalue); // 矩阵赋初值,用pvalue指向的内存块数据为矩阵赋值 void printMatrix() const; // 显示矩阵 float &element(int i, int j); //返回矩阵第i行第j列元素的引用 inline float element(int i, int j) const;// 返回矩阵第i行第j列元素的值 void setElement(int i, int j, int value); //设置矩阵第i行第j列元素值为value inline int getLines() const; //返回矩阵行数 inline int getCols() const; //返回矩阵列数 private: int lines; // 矩阵行数 int cols; // 矩阵列数 float *p; // 指向存放矩阵数据的内存块的首地址 }; #endif
#include<iostream> #include"matrix.h" using namespace std; Matrix::Matrix(int n):lines(n),cols(n){ int total=lines*cols; p=new float[total]; } Matrix::Matrix(int n,int m):lines(n),cols(m){ int total=lines*cols; p=new float[total]; } Matrix::Matrix(const Matrix &X){ lines=X.lines; cols=X.cols; int total=lines*cols; p=new float[total]; for(int num=0;num<total;num++) //赋值 { *(p+num)=*(X.p+num); } } Matrix::~Matrix(){ delete p; } void Matrix::setMatrix(const float *pvalue){ int total=lines*cols; for(int num=0;num<total;num++) { *(p+num)=*(pvalue+num); } } void Matrix::printMatrix() const{ float *p1=p; //p1用于输出值 for(int l1=0;l1<lines;l1++) { for(int c1=0;c1<cols;c1++) { cout<<*p1; p1++; cout<<' '; } cout<<endl; } } inline float Matrix::element(int i, int j) const { float return_this; return_this=*(p+(i-1)*j+(j-1)); return return_this; } void Matrix::setElement(int i, int j, int value){ *(p+(i-1)*j+(j-1))=value; } inline int Matrix::getLines() const{ return lines; } inline int Matrix::getCols() const{ return cols; }
#include<iostream> #include<cstdlib> #include<cmath> #include<ctime> using namespace std; class Dice{ protected: int sides; public: Dice(int n);//构造函数 int cast(); }; Dice::Dice(int n):sides(n){ //构造函数 } Dice::cast(){ struct timeb timeSeed; ftime(&timeSeed); srand(timeSeed.time * 1000 + timeSeed.millitm); // milli time int result; result=1+(rand()%sides-1); return result; } int main(){ double alltime=0;//统计自己的学号被抽中的总次数 double gailv=0.0;//概率 Dice myclass(40); for(int i=0;i<500;i++){ int eachtime=myclass.cast(); cout<<eachtime<<" "; if(eachtime==2)//学号为2号 alltime++; } gailv=alltime/500.0; cout<<endl<<gailv<<endl;//输出概率 return 0; }
user.h文件 #pragma once #ifndef USER_H #define USER_H #include<string> using namespace std; class user { private: int id; string name; string password; static int currentid; public: user(); void print(); void changepassword(); void printcurrentid(); }; #endif // !USER_H user.cpp文件 #include"user.h" #include<iostream> #include<string> using namespace std; int user::currentid = 999; user::user() { cout << "请输入用户名:"; cin >> name; password = "111111"; currentid++; id = currentid; } void user::print() { cout << "用户ID为:" << id << endl; cout << "用户名为:" << name << endl; cout << "密码为:" << password << endl; } void user::changepassword() {//正常应该先验证用户名再输密码,这里从简 int count = 0; while (count < 3) { cout << "请输入旧密码:"; string temp; cin >> temp; cout << endl; if (temp == password) { cout << "密码正确,请输入新密码:"; cin >> temp; password = temp; break; } else { cout << "密码错误,请重输:"; count++; } } if (count == 3) { cout << "请稍后再试" << endl; } } void user::printcurrentid() { cout << "currentID:" << currentid << endl; } main.cpp文件 // 用户管理.cpp: 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <vector> #include "user.h" using namespace std; int main() { vector<user>users; int count = 0; while (true) { cout << "创建用户输1,修改新用户密码输2,打印用户信息输3,打印当前ID输4,退出输0" << endl; int number; cin >> number; if (number == 1) { user u1; users.push_back(u1); count++; } else if (number == 2) { users[count - 1].changepassword(); } else if (number == 3) { users[count - 1].print(); } else if (number == 4) { users[count - 1].printcurrentid(); users[count - 1].print(); } else if (number == 0) { break; } } return 0; }
book.h文件 #ifndef BOOK_H #define BOOK_H #include <string> using std::string; class Book { public: Book(string isbnX, string titleX, float priceX); //构造函数 void print(); // 打印图书信息 private: string isbn; string title; float price; }; #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:" << isbn << endl; cout << "title:" << title << endl; cout << "price:" << price << endl; } // ... main.cpp文件 #include "book.h" #include <vector> #include <iostream> using namespace std; int main() { // 定义一个vector<Book>类对象 // 补足程序 vector<Book>books; // ... string isbn, title; float price; // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中 // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) // 补足程序 int count = 0; while (true) { cin >> isbn; if ((isbn == "stop"))//输入stop则停止输入 break; cin >> title >> price; Book tempt(isbn, title, price); books.push_back(tempt); count++; } // ... // 输出入库所有图书信息 // 补足程序 for (int i = 0; i < count; i++) { books[i].print(); } // ... return 0; }