1.ex3.cpp
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 using namespace std; 5 6 // 函數聲明 7 void output1(vector<string> &); 8 void output2(vector<string> &); 9 10 int main() 11 { 12 vector<string>likes, dislikes; // 創建vector<string>對象likes和dislikes 13 14 // 為vector<string>數組對象likes添加元素值 ( favorite book, music, film, paintings,anime,sport,sportsman,etc) 15 // 補足代碼 16 // 。。。 17 likes.push_back("favorite book"); 18 likes.push_back("music"); 19 likes.push_back("film"); 20 likes.push_back("paintings"); 21 22 23 cout << "-----I like these-----" << endl; 24 // 調用子函數輸出vector<string>數組對象likes的元素值 25 // 補足代碼 26 // 。。。 27 output1(likes); 28 29 // 為vector<string>數組對象dislikes添加元素值 30 // 補足代碼 31 // 。。。 32 dislikes.push_back("anime"); 33 dislikes.push_back("sport"); 34 dislikes.push_back("sportsman"); 35 36 cout << "-----I dislike these-----" << endl; 37 // 調用子函數輸出vector<string>數組對象dislikes的元素值 38 // 補足代碼 39 // 。。。 40 output2(dislikes); 41 42 43 // 交換vector<string>對象likes和dislikes的元素值 44 // 補足代碼 45 // 。。。 46 likes.swap(dislikes); 47 48 49 cout << "-----I likes these-----" << endl; 50 // 調用子函數輸出vector<string>數組對象likes的元素值 51 // 補足代碼 52 // 。。。 53 output1(likes); 54 cout << "-----I dislikes these-----" << endl; 55 // 調用子函數輸出vector<string>數組對象dislikes的元素值 56 // 補足代碼 57 // 。。。 58 output2(dislikes); 59 60 return 0; 61 } 62 63 64 // 函數實現 65 // 以下標方式輸出vector<string>數組對象v的元素值 66 void output1(vector<string> &v) { 67 // 補足程序 68 // 。。。 69 for(int i=0;i<v.size();i++) 70 cout<<v[i]<<","; 71 cout<<endl; 72 } 73 74 // 函數實現 75 // 以迭代器方式輸出vector<string>數組對象v的元素值 76 void output2(vector<string> &v) { 77 // 補足程序 78 // 。。。 79 vector<string>::iterator iter; 80 for(iter=v.begin() ;iter!=v.end() ;iter++) 81 cout<<*iter<<","; 82 cout<<endl; 83 }
6-17
1 #include <iostream> 2 #include <stdlib.h> 3 using namespace std; 4 int main() 5 { // int *p;定義了一個指針p,然而p并沒有指向任何地址,所以當使用*p時是沒有任何地址空間對應的,所以 *p=9 就會導致,不知道把這個1賦值給哪個地址空間了 6 int *p=NULL; 7 int a = 9; 8 p =&a; 9 cout << "The value at 不加上p: " << *p; 10 system("pause"); 11 return 0; 12 } 13 /*不加上#include <stdlib.h>和system("pause"); 我vs2017的cmd框會閃退,求解惑.*/
6-18
1 #include <iostream> 2 #include "stdlib.h " 3 using namespace std; 4 int fn1() { 5 int *p = new int(5);//new的值一定要delete 6 return *p ; 7 delete p; 8 } 9 int main() 10 { 11 int a = fn1(); 12 cout << "the value of a is: " << a; 13 system("pause"); 14 return 0; 15 }
期中考試題
第一題
1 Dice.h 2 3 class Dice { 4 public: 5 Dice(int n) :sides(n) { 6 7 } 8 int cast() 9 { 10 return rand() % sides + 1; 11 } 12 13 private: 14 int sides; 15 16 }; 17 18 main.cpp 19 #include<iostream> 20 #include<cstdlib> 21 #include<ctime> 22 #include"Dice.h" 23 24 using namespace std; 25 26 int main() { 27 28 srand(time(NULL)); 29 30 Dice A(40); 31 32 int num = 0; 33 34 for (int j = 0; j<500; ++j) { 35 36 if (A.cast() == 19)num++; 37 38 } 39 40 double p = 0; 41 42 p = (double)num / 500.0; 43 44 cout << "學號 20178303019 被抽中的概率是:" << p << endl; 45 46 return 0; 47 48 }
第三題
book.h
1 #ifndef BOOK_H 2 #define BOOK_H 3 4 #include <string> 5 using std::string; 6 7 class Book { 8 public: 9 Book(string isbnX, string titleX, float priceX); //構造函數 10 void print(); // 打印圖書信息 11 private: 12 string isbn; 13 string title; 14 float price; 15 }; 16 #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 << " Title: " << title << " Price: " << price << endl; }
main.cpp
#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時為止 (也可以自行定義錄入結束方式) // 補足程序// ... cout << "請依次錄入圖書信息 出版編號(isbn), 書名(title), 定價(price)" << endl;do{cin >> isbn >> title >> price;} while (cin >> isbn);// 輸出入庫所有圖書信息// 補足程序// ... for (int i = 0; i<BOOK.size(); i++)BOOK[i].print();return 0; }
//可能是我停止錄入那邊的代碼寫錯了。。不會寫,求指點
動態矩陣
matrix.h
1 #ifndef MATRIX_H 2 #define MATRIX_H 3 class Matrix { 4 public: 5 Matrix(int n); // 構造函數,構造一個n*n的矩陣 6 Matrix(int n, int m); // 構造函數,構造一個n*m的矩陣 7 Matrix(const Matrix &X); // 復制構造函數,使用已有的矩陣X構造 8 ~Matrix(); //析構函數 9 void setMatrix(const float *pvalue); // 矩陣賦初值,用pvalue指向的內存塊數據為矩陣賦值 10 void printMatrix() const; // 顯示矩陣 11 inline float &element(int i, int j); //返回矩陣第i行第j列元素的引用 12 inline float element(int i, int j) const;// 返回矩陣第i行第j列元素的值 13 void setElement(int i, int j, int value); //設置矩陣第i行第j列元素值為value 14 inline int getLines() const; //返回矩陣行數 15 inline int getCols() const; //返回矩陣列數 16 private: 17 int lines; // 矩陣行數 18 int cols; // 矩陣列數 19 float *p; // 指向存放矩陣數據的內存塊的首地址 20 }; 21 #endif
matrix.cpp
#include<iostream> #include"matrix.h" using namespace std; int i, j; 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 (i = 0; i<lines*cols; i++)p[i] = x.p[i]; } Matrix::~Matrix() {delete[] p; } void Matrix::setMatrix(const float *pvalue) {for (i = 0; i<lines*cols; i++)p[i] = pvalue[i]; } void Matrix::printMatrix() const {for (i = 0; i<lines; i++) {for (j = 0; j<cols; j++)cout << p[i*lines + j] << " ";cout << endl;} } inline float Matrix::element(int i, int j) const {cout << p[(i - 1)*lines + j - 1] << 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" using namespace std; int main() {int l, c, i, j, n;cout << "輸入行數和列數" << endl;cin >> l >> c;float a[l*c];cout << "初始化矩陣" << endl;for (i = 0; i<l*c; i++)cin >> a[i];Matrix A(l, c);A.setMatrix(a);cout << "輸出矩陣" << endl;A.printMatrix();cout << "輸入行數和列數輸出該數" << endl;cin >> i >> j;A.element(i, j);cout << "改變第幾行第幾列的數" << endl;cin >> n;A.setElement(i, j, n);cout << "輸出這個數" << endl;A.element(i, j);cout << "返回矩陣行數與列數" << endl;A.getLines();A.getCols();return 0; }
真的 看了很多人很多人的代碼,這行都是這么寫的。
但我確實編不出來啊。。。
期中考試 第二條 還不會寫
就不把代碼拿出來丟人了。。