标签:bubuko protect code ice float while 修改 显示 oid
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 string myfavorite[7]={"book", "music", "film", "paintings","anime","sport","sportsman"};//函数声明 6 void output1(vector<string> &); 7 void output2(vector<string> &); 8 void swapl(string s1,string s2); 9 int main() 10 { 11 vector<string> likes, dislikes; // 创建vector<string>对象likes和dislikes 12 likes.push_back("《高等数学》"); 13 likes.push_back("navada"); 14 likes.push_back("《复仇者联盟》"); 15 likes.push_back("向日葵"); 16 likes.push_back("《哆啦A梦》"); 17 likes.push_back("羽毛球"); 18 likes.push_back("梅西"); 19 cout << "-----I like these-----" << endl; 20 output1(likes);//调用函数输出vector数组对象like的元素值 21 dislikes.push_back("语文"); 22 dislikes.push_back("低沉音乐"); 23 dislikes.push_back("烂片"); 24 dislikes.push_back("无"); 25 dislikes.push_back("大胃王"); 26 dislikes.push_back("冰球"); 27 dislikes.push_back("无"); 28 cout << "-----I dislike these-----" << endl; 29 output2(dislikes);//调用函数输出vector数组对象dislike的元素值 30 for(unsigned int i=0;i<likes.size();i++) 31 { 32 swapl(likes[i],dislikes[i]); 33 } 34 cout << "-----I like these-----" << endl; 35 output1(likes); 36 cout << "-----I dislike these-----" << endl; 37 output2(dislikes); 38 return 0; 39 40 } 41 //以下标的方式输出 42 void output1(vector<string> &v) 43 { 44 for(unsigned int i=0;i<v.size();i++) 45 { 46 cout<<myfavorite[i]<<":"<<v[i]<<" "<<endl; 47 } 48 } 49 //以迭代器的方式输出 50 void output2(vector<string> &v) 51 { 52 int a=0; 53 for(auto i=v.begin();i<v.end();i++) 54 { 55 cout<<myfavorite[a++]<<":"<<*i<<" "<<endl; 56 } 57 } 58 void swapl(string s1,string s2){ 59 string temp=s1; 60 s1=s2; 61 s2=temp; 62 }
1 #include<iostream> 2 using namespace std; 3 int fn1(){ 4 int *p=new int(5); 5 6 return *p; 7 delete p;//在定义了new后没有使用delete进行释放内存,回导致问题的出现 8 } 9 int main(){ 10 int a=fn1(); 11 cout<<"the value of a is:"<<a; 12 return 0; 13 14 }
1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int *p; 6 int a; 7 p=&a; 8 p=8 9 cout<<"The value at p: "<<*p; 10 return 0; 11 }
main
#include <iostream> #include"Matrix.h" using namespace std; int main() { Matrix A(2); Matrix B(2,3); float a[4]={1,2,3,4}; A.setMatrix(a); A.printMatrix(); return 0; }
Matrix.h
#ifndef MATRIX_H_INCLUDED #define MATRIX_H_INCLUDED 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;//显示举证 inline 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;//返回矩阵行数 { return lines; } inline int getCols()const;//返回矩阵列数 { return cols; } private: int lines; int cols; float *p;//存放矩阵数据内存的首地址 }; inline float &element(int i,int j) { float &r=p[(i-1)*cols+j-1]; return r; } inline float element(int i, int j) const { float r=p[(i-1)*cols+j-1]; return r; } #endif // MATRIX_H_INCLUDED
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() { delete p; } void Matrix::printMatrix()const { for(int i=0;i<lines;i++) { for(int j=0;j<cols;j++) cout<<p[i*cols+j]<<" "; cout<<endl; } } void Matrix::setMatrix(const float *pvalue) { for(int i=0;i<lines;i++) { for(int j=0;j<cols;j++) p[i*cols+j]=pvalue[i*cols+j]; } }
期中考试
#include <iostream> #include<cstdlib> #include<cmath> #include<ctime> using namespace std; class Dice { public: Dice(int n); int cast(); protected: int sides; }; Dice::Dice(int n):sides(n){} Dice::cast() { struct timeb timeSeed; ftime(&timeSeed); srand(timeSeed.time * 1000 + timeSeed.millitm); int result; result=1+(rand()%sides-1); return result; } int main() { double times,probability; Dice a(40); for(int i=0;i<500;i++){ int eachtime=a.cast(); cout<<eachtime<<" "; if(eachtime==21) times++; } probability=times/500; cout<<"概率是"<< probability<<endl; return 0; }
main
#include <iostream> #include"user.h" #include <string> #include <vector> 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; }
user。h
#ifndef USER_H_INCLUDED #define USER_H_INCLUDED #include <string> #include <vector> #include <iostream> using namespace std; class user { public: user(); ~user(); void print(); void changepassword(); void printcurrentid(); private: int id; string name; string password; static int currentid; }; #endif // USER_H_INCLUDED
user。cpp
#include<iostream> #include"user.h" #include <string> #include <vector> #include <iostream> using namespace std; int user::currentid = 999; user::user() { cout << "请输入用户名:"; cin >> name; password = "111111"; currentid++; id = currentid; } user::~user(){} void user::print() { cout << "用户ID为:" << id << endl; cout << "用户名为:" << name << endl; cout << "密码为:" << password << endl; } void user::changepassword(){ int count=0; while(count<3) { string a; cout<<"plesse input psssword"<<endl; cin>>a; if(a==password) {cout<<right; cin>>a; password=a; break; } else cout<<"错了请重输"<<endl; count++; } if (count == 3) { cout << "请稍后再试" << endl; } } void user::printcurrentid(){ cout << "currentID:" << currentid << endl; }
main
#include <iostream> #include<vector> #include"Book.h" using namespace std; int main() { vector<Book> books; string cx,cv; float pl; while(cin>>cx>>cv>>pl) { Book a(cx,cv,pl); books.push_back(a); } for(int i=0;i<books.size();i++) { books[i].print(); } return 0; }
book.h
#ifndef BOOK_H_INCLUDED #define BOOK_H_INCLUDED #include<iostream> #include<string> #include<cstring> using namespace std; class Book { public: Book(string a,string c,float v); void print(); private: string isbn; string title; double price; }; #endif // BOOK_H_INCLUDED
book.cpp
#include "Book.h" Book::Book(string a,string b,float c):isbn(a),title(b),price(c){} void Book::print() { cout<<"出版编号:"<<isbn<<" "<<"书名:"<<title<<" "<<"定价:"<<price<<" RMB"<<endl; }
标签:bubuko protect code ice float while 修改 显示 oid
原文地址:https://www.cnblogs.com/fffffaaaaaa/p/9080006.html