实验五
#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"); likes.push_back("paintings"); likes.push_back("anime"); likes.push_back("sport"); likes.push_back("sportsman"); likes.push_back("etc"); // 为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(" ") ; // 为vector<string>数组对象dislikes添加元素值 cout << "-----I dislike these-----" << endl; output1(dislikes); // 调用子函数输出vector<string>数组对象dislikes的元素值 likes.swap(dislikes); // 交换vector<string>对象likes和dislikes的元素值 // 补足代码 cout << "-----I likes these-----" << endl; output2(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 ( vector<string>::iterator it = v.begin(); it != v.end(); it++ ) cout << *it << ' '<<endl; // 补足程序 }
6-17
#include<iostream> using namespace std; int main(){ int a,*p=&a; *p=9; cout<<"the value of a is :"<<*p; return 0; }
6-18
#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<<endl; return 0; }
1
#include<iostream> #include<stdlib.h> #include<time.h> using namespace std; class Dice { public: Dice(int n); int cast(); private: int sides; }; Dice::Dice(int n) { sides=n; } int Dice::cast() { int val; val=(rand() % (sides + 1) + 1); return val; } int main() { srand((unsigned)time(NULL)); int a,b,c,d; cout<<"请输入班级人数及你的学号"<<endl; cin>>a>>b; Dice x1(a); for(int i=0;i<500;i++){ c=x1.cast(); if(c==b){ d++;} } cout<<"学号被抽中的概率为"<<endl; cout<<d<<"/500"<<endl; return 0; }
2
#include<iostream> #include<string> using namespace std; static int n=999; class User { private: int id; string name; string password; static int CurrentID; public: User(string s1,string s2="111111") { n++; id = n; name = s1; password = s2; CurrentID++; } User(User &u1) { id = u1.id; name = u1.name; password = u1.password; CurrentID = u1.CurrentID; } void show1() { cout << "该用户信息为 id:" << id << " name:" << name << " password:" << password << endl; } static void show2(User &u1) { //输出CurrentID cout << "CurrentID:" << CurrentID << endl; cout << "最后一个新增用户为:"; u1.show1(); } void change() { //修改密码 string s3, s4; int j; j = 3; while (j) { cout << "请输入原有密码:"; cin >> s3; if (s3 == password) { cout << "请输入新密码:"; cin >> s4; password = s4; break; } if (s3 != password) { j--; cout << "输入原密码错误!" << endl; } } if (j == 0)cout << "错误次数过多,请稍后再试" << endl; } }; int User::CurrentID = 999;//给CurrentID赋初值 int main() { User u1("user1", "123456"); User u2("user2"); User u3("user3", "234567"); u1.show1(); u2.show1(); u3.show1(); User u(u3); User::show2(u3); u1.change(); u1.show1(); u2.change(); return 0; }
3
#include "book.h" #include <vector> #include <iostream> using namespace std; int main() { // 定义一个vector<Book>类对象 // 补足程序 // ... vector<Book>book; string isbn, title; float price; // 录入图书信息,构造图书对象,并添加到前面定义的vector<Book>类对象中 // 循环录入,直到按下Ctrl+Z时为止 (也可以自行定义录入结束方式) // 补足程序 while(cin>>isbn>>title>>price) { Book b(isbn,title,price); book.push_back(b); } // 输出入库所有图书信息 // 补足程序 for(int i=0;i<book.size();i++) book[i].print(); return 0; }