zoukankan      html  css  js  c++  java
  • 面向对象程序设计-C++ Steam & Vector 【第三次上课笔记】

    大家可以下载后用Vim 或者 Sublime Text等文本编辑器查看

    Conference: http://blog.csdn.net/candy1232009/article/details/7032526

    //ofstream fout;	//C style
    //fout.open("fout.txt");
    
    ofstream fout ("fout.txt");	//C++ out stream recommend
    ifstream fin ("fio.cpp");	
    
    //fout.close()	//Not necessary in C++, because C++ do it automatically
    
    //Copy the whole file
    
    int main(){
    	ofstream fout ("SourceCodeCopy.cpp");
    	ifstream fin ("SourceCode.cpp");
    	string str;
    	while(getline(fin, str)){	//Discards newline char
    		fout << str << endl;	//... must add it back
    	}
    	return 0;
    }
    
    //calculate the average number
    
    int main(){
    	int i, num, cur;
    	cin >> num;
    	
    	//double* array = (double*) malloc(num * sizeof(double));	//C style
    	double* array = new double[num];	//C++ style
    	double ave = 0.0;
    	for(i = 0; i < num; ++i){
    		cin >> array[i];
    		ave += array[i];
    	}
    	cout << "Ave is the " << ave / num << endl;
    	//free(array);	//C style
    	delete[] array;	//[] means delete all the Array, if "delete array" means delete only the array[0]
    
    	return 0;
    }
    
    //one double number array Example
    
    int main(){
    	double* pd = new double;
    
    	cin >> *pd;
    	cout << *pd;
    	delete pd;
    
    	return 0;
    }
    
    //Introduce Vector
    
    int main(){
    	vector <double> vc;		//init a vector
    	vc.push_back(27.8);		//insert element to its tail
    	vc.push_back(54.2);
    
    	//vc[2] = 89.3	//Don't do in this way, no such spacez
    
    	for(i = 0; i < vc.size(); ++i){
    		cout << vc[i] << endl;
    	}
    	return 0;
    }
    
    //Answer is 0 0 89.3 27.8 54.2  (5 elements)
    
    int main(){
    	int i;
    	vector <double> vc(3);	//init a space long for 3 
    	vc.push_back(27.8);
    	vc.push_back(54.2);
    
    	vc[2] = 89.3;//
    
    	for(i = 0; i < vc.size(); ++i){
    		cout << vc[i] << endl;
    	}
    
    	return 0;
    }
    
    //Copy an entire file into a vector of string
    
    int main(){
    	vector <string> v;
    	ofstream out ("SourceCodeCopy.cpp");
    	ifstream in ("SourceCode.cpp");
    	string line;
    	while(getline(in, line)){
    		v.push_back(line);
    	}
    	for(int i = 0; i < v.size(); ++i){
    		out << 1 + i << ": " << v[i] << endl;
    	}
    
    	return 0;
    }
    
    //Class work
    //give a number N, and make n random numbers into a file
    
    int main(){
    	srand((int)time(NULL));
    	int i, n;
    	vector <int> v;
    	ofstream out ("rand_num.txt");
    	
    	cin >> n;
    	while(n--){
    		v.push_back(rand() % 65536);
    	}
    	for(i = 0; i < v.size(); ++i){
    		out << v[i] << endl;
    	}
    
    	return 0;
    }
    
    //make n numbers in the range [0, 1)
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <cmath>
    #include <algorithm>
    
    using namespace std;
    
    int main(){
    	srand((int)time(NULL));
    	int i, n;
    	vector <double> v;
    	ofstream out ("rand_num.txt");
    	
    	cin >> n;
    	while(n--){
    		v.push_back((double)rand() / (double)RAND_MAX);
    	}
    	for(i = 0; i < v.size(); ++i){
    		out << v[i] << endl;
    	}
    
    	return 0;
    }
    

      

  • 相关阅读:
    电路的耦合方式
    PCBA与PCB的区别
    vue记住密码功能
    数组变异
    element时间选择器插件转化为YYYY-MM-DD的形式
    box-shadow
    从后台传select的值
    jQuery事件(持续更新中)
    JavaScript对象(持续更新中)
    15分XX秒后订单自动关闭(倒计时)
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4341446.html
Copyright © 2011-2022 走看看