1. ios::binary可以顯示指定它的打開方式為ios::binary,也就是以二進制方式打開。
2. 結構與聯合的差別:
聯合類型和結構類型的變量都有不同類型的成員,但是聯合類型的變量系統僅按占空間最大的成員配置設定空間,幾個成員共用同一塊記憶體空間,即在某一時刻隻能存放其中一種而不是幾種。而對于結構類型的變量,系統将為其配置設定所有成員占用空間的總和大小的空間,即各成員同時存在,是以聯合類型變量不能用作函數參數或傳回值,而結構類型變量都可以。
3. 對于一個枚舉類型
enum color{WHITE,BLACK,BLUE,PINK}
不定值的話那麼它的成員的值依次為0,1,2…
如果指定其中一個值,那就從那個值後依次加
舉個?:enum color{WHITE,BLACK,BLUE,PINK}
WHITE=0,BLACK=1,BLUE=2,PINK=3
enum color{WHITE,BLACK=100,BLUE,PINK}
而對于上式,WHITE=0,BLACK=100,BLUE=101,PINK=102
4. 用rand()函數生成随機數要包含頭檔案stdlb.h
使用time()要包含頭檔案ctime
srand((unsigned int)time(NULL));這條語句是給rand()一個種子值,其中time(NULL)是取系統目前時間值,為了每次調用rand()函數時産生一系列不同的随機數。如果不加此句,則rand()函數對于同一個程式每次運作生成的随機數是一樣的。
練習題和代碼嗷
// character4.cpp: 定義控制台應用程式的入口點。
//
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<cmath>
#include<fstream>
#include<stdlib.h>
#include<ctime>
using namespace std;
void exp3()
/*40個學生用1-10分評價食堂品質,
将40個分數放在整形數組中并彙總調查結果*/
{
const int cStuNum = 40;
const int cScoreNum = 11;
int score[cStuNum] = {
1,2,6,4,8,5,9,7,8,10,
1,2,3,8,6,4,9,4,7,8,
3,6,5,9,8,10,5,1,5,4,
2,6,9,3,8,1,5,3,8,9
};//全部分數
int counts[cScoreNum] = { 0 };
int k;
for (int k = 0; k < cStuNum; k++)
{
counts[score[k]]++;
}
cout << "score" << setw(10) << "counts" << endl;
for (int k = 1; k < cScoreNum; k++)
{
cout << setw(3) << k << setw(10) << counts[k] << endl;
}
}
void exp5()
//将正弦函數的一個周期分成N等分,定義數組顯示自變量值和函數值
{
const int N = 8;
double x[N];
double y[N];
const double PI2 = 3.1415926 * 2;
double delta = PI2 / N;
cout << setw(8) << "弧度" << setw(12) << "sin(x)" << endl;
for (int k = 0; k < N; k++)
{
x[k] = k * delta;
y[k] = sin(x[k]);
cout << setw(10) << x[k] << ", " << y[k] << endl;
}
}
void exp6()//存儲一個周期正餘弦函數值并存到test.txt
{
const int N = 16;
double x[N];
double y_sin[N], y_cos[N];
const double pi2 = 3.14159 * 2;
double delta = pi2 / N;
ofstream ofile("test.txt");
for (int k = 0; k < N; k++)
{
x[k] = k * delta;
y_sin[k] = sin(x[k]);
y_cos[k] = cos(x[k]);
cout << setw(10) << x[k] << " " << setw(10) << setprecision(4) << y_sin[k] << " "
<< setw(10) << setprecision(4) << y_cos[k] << endl;
ofile<< setw(10) << x[k] << " " << setw(10) << setprecision(4) << y_sin[k] << " "
<< setw(10) << setprecision(4) << y_cos[k] << endl;
}
ofile.close();
}
void exp7()//把上題存的再讀出來
{
ifstream ifile("test.txt");
const int N = 16;
double x[N], y_sin[N], y_cos[N];
for (int k = 0; k < N; k++)
{
ifile >> x[k] >> y_sin[k] >> y_cos[k];
cout << setw(10) << x[k] << "," << setw(10) << setprecision(4)
<< y_sin[k] << "," << setw(10) << setprecision(4) << y_cos[k] << endl;
}
ifile.close();
}
void exp8()//輸出進行36000次投擲兩次骰子之和實驗的結果
{
const int N = 11;
const int Dice = 6;
const int EXP_TIMES = 36000;
int result_times[N] = { 0 };
int add_result;
int i, j;
srand((unsigned int)time(NULL));
for (i = 0; i < EXP_TIMES; i++)
{
add_result = rand() % Dice + 1;
add_result += (rand() % Dice + 1);
result_times[add_result - 2]++;
}
for (int i = 0; i < N; i++)
cout << setw(10) << i + 2 << setw(10) << result_times[i] << endl;
}
int main()
{
exp8();
system("pause");
return 0;
}