编程语言
首页 > 编程语言> > C++ stl 标准程序库实例源代码

C++ stl 标准程序库实例源代码

作者:互联网

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    cout<<"Welcome to the C ++ Standard Template Library!!!"<<endl;
    getchar();  //等待输入任意键,之后程序退出;
    return 0;
}

执行结果:
Welcome to the C ++ Standard Template Library!!!

 

#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;

int main()
{
    float pi=3.1415916;
    float radius=0;
    float area=0.0;
    float girth=0.0;
    cout<<"circle->radius:"<<endl;
    cin>>radius;
    area=pi*pow(radius,2);          //使用数学函数pow(),乘方
    girth=2*pi*radius;              //周长
    cout<<"area="<<area<<";girth="<<girth<<endl;

    getchar();
    getchar();      //等待程序退出

    return 0;
}

执行结果:
circle->radius:
10
area=314.159;girth=62.8318

root@mingdi-desktop:/home/share# ./stl1
circle->radius:
80
area=20106.2;girth=502.655

 

#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;

class Calculator
{
    public:
        Calculator();
        ~Calculator();
        float x;
        float y;
        float Adder(float x,float y);
        float Substration(float x,float y);
        float Multiplication(float x,float y);
    	float Division(float x,float y);
    	float CPow(float x,float y);
    	float CSqrt(float x);
};

Calculator::Calculator()
{
    x=0;
    y=0;
}
Calculator::~Calculator()
{

}
float Calculator::Adder(float x,float y)
{   
    float He=0;
    He=x+y;
    return He;
}

float Calculator::Substration(float x,float y)
{
	float Cha=0;
	Cha=x-y;
	return Cha;
}

float Calculator::Multiplication(float x,float y)
{
	float Ji=0;
	Ji=x*y;
	return Ji;
}
float Calculator::Division(float x,float y)
{
    float Shang=0;
    if(y==0.0)      //分母除数不能等于0
        return -1;
    Shang=x/y;
    return Shang;
}
float Calculator::CPow(float x,float y)   //x的Y次方
{
	float ChengFang=0;
	ChengFang=pow(x,y);
	return ChengFang;
}

float Calculator::CSqrt(float x)            //开平方
{
	float sqrtC=0.0;
	sqrtC=sqrt(x);
	return sqrtC;
}

int main()
{
    float x=0;
    float y=0;
    cout<<"Input x:";
    cin>>x;
    cout<<endl;

    cout<<"Input y:";
    cin>>y;
    cout<<endl;

    Calculator my;
    float He=my.Adder (x,y);
    cout<<"Adder:"<<He<<endl;
    
    float Cha=my.Substration(x,y);
	cout<<"Substration"<<Cha<<endl;
    
	float Ji=my.Multiplication(x,y);
	cout<<"Multiplication"<<Ji<<endl;
    
	float Shang=my.Division(x,y);
	cout<<"Division"<<Shang<<endl;
    
	float Pow=my.CPow(x,y);
	cout<<"CPow"<<Pow<<endl;
    
	float KaiFang=my.CSqrt(x);
	cout<<"CSqrt"<<KaiFang<<endl;
    
	cout<<"Any key program exit";
	getchar();
	getchar();
   
}

执行结果:
Input x:50

Input y:10

Adder:60
Substration40
Multiplication500
Division5
CPow9.76563e+16
CSqrt7.07107
Any key program exit

#include <iostream>
using namespace std;
void count(bool YN,int &counter)
{
    if(YN)
        {   
            counter++;
        }
}

int main()
{
    int counter=0;
    bool YN=1;
    while(YN)
        {
            count(YN,counter);
            cout<<" "<<counter<<endl;
            if(counter>5)
                {
                    YN=0;
                }
        }
}

执行结果:
1
2
3
4
5
6

#include <iostream>
#include <list>
#include <string.h>
using namespace std;
struct PERSON{
    int id;
    int sex;
    double core;
    void clear()
        {
            id=0;
            sex=0;
            core=0;
        }
};

int main()
{
    PERSON temp;
    list<PERSON>C1;
    int id_temp;
    int sex_temp;
    double core_temp;
    int size=0;
    C1.clear();
    int counter=0;
    cout<<"This is a simplest C++ Example!\n"<<endl;
    cout<<"Start with any key ...";
    cin.get();//按回车键等待

    while (counter<5)
        {
        cout<<"input ID:";
        cin>>id_temp;
        cout<<"input sex:";
        cin>>sex_temp;
        cout<<"input core:";
        cin>>core_temp;

        temp.id=id_temp;
        temp.sex=sex_temp;
		temp.core=core_temp;
        C1.push_back(temp);                     //尾部加入一个数据
        memset(&temp,0,sizeof(PERSON));
        counter++;  
        }
    cout<<"Enter to continue ...";

    cin.get();
    size=C1.size();
    cout<<endl;
    list<PERSON>::iterator Iter;
    for(Iter=C1.begin();Iter!=C1.end();Iter++)
        {
            temp.clear();
            temp=*Iter;
            cout<<"ID:"<<temp.id<<",SEX:"<<temp.sex<<",Core:"<<temp.core<<endl;
        }

    cout<<"Any key to exit the program ...";
    cin.get();
    return 0;
}

执行结果:
This is a simplest C++ Example!

Start with any key ...
input ID:001
input sex:0
input core:90
input ID:002
input sex:1
input core:80
input ID:003
input sex:0
input core:95
input ID:004
input sex:0
input core:95
input ID:005
input sex:1
input core:93
Enter to continue ...
ID:1,SEX:0,Core:90
ID:2,SEX:1,Core:80
ID:3,SEX:0,Core:95
ID:4,SEX:0,Core:95
ID:5,SEX:1,Core:93
Any key to exit the program ...

#include <iostream>
using namespace std;

int main()
{
    long Aa=11;
    long *B_Pointer=NULL;
    B_Pointer=&Aa;
    cout<<"Aa:"<<Aa<<" ,Aa Memory address: "<<B_Pointer<<endl;
    cin.get();

}

执行结果:
Aa:11 ,Aa Memory address: 0x7fffa190f1d8

#include <iostream>
#include <cmath>
using namespace std;


double pow2(float x);//声明
double pow3(float* y);//声明

double pow2(float x)        //定义
{
    double z=0;
    z=x*x;
    return z;
}

double pow3(float *y)      //定义
{
    double z=0;
    z=(*y)*(*y)*(*y);
    return z;
}

int main()
{
    double A=10;
    float B=20;
    double C=0;
    double D=0;
    C=pow2(A);
   //D=pow3(A);
	cout<<"A:  "<<A<<"  ; Pow2(A):  "<<C<<endl;
	D=pow3(&B);
	//D=pow3(&A);
	cout<<"B:  "<<B<<"  ; Pow3(B):  "<<D<<endl;
	cin.get();
}

 

执行结果:
A:  10  ; Pow2(A):  100
B:  20  ; Pow3(B):  8000

 

#include <iostream>
using namespace std;

int Factor(int n)
{
    int answer=0;
    if(n==1)
        {
            return (1);
        }
    answer=Factor(n-1)*n;
	return answer;
}

int main()
{
    int n=5;
    int result=0;
    result=Factor(n);
    cout<<"5!= 5x4x3x2x1 = "<<result<<" "<<endl;
	cin.get();
}

执行结果:
5!= 5x4x3x2x1 = 120

#include <iostream>
#include <stdio.h>
using namespace std;
int  main()
{
	int x;
	for(x=1;x<11;++x)
		printf("%d ",x*x);
	printf("\n");
	cin.get();
}

执行结果:
1 4 9 16 25 36 49 64 81 100

#include<iostream>
#include <fstream>
using namespace std;

int main(int argc,char *argv[])
{
    ifstream f1;
    ofstream f2;

    char filename1[256];
    char filename2[256];
    char content[256];
    cout<<"Please enter a file name (source):";
    cin>>filename1;
    cout<<"Please enter a file name (purpose):";
    cin>>filename2;

    f1.open(filename1,ios::in);
    f2.open(filename2,ios::out);

    while (!f1.eof())  //文字流"(stream)的结尾
        {
            f1.getline(content,128);
            f2<<content<<endl;
        }
        f1.close();
        f2.close();
}

 

#include <iostream>
#include <list>

#include <ctime>
using namespace std;

void mysleep(int second)
{
    clock_t st;
    st=clock();
    while (clock()-st<second*CLOCKS_PER_SEC);//CLOCKS_PER_SEC表示一秒钟内CPU运行的时钟周期数(时钟计时单元)
}
int main()
{
    int count=5;
    float number=0.0;
    list<int> mylist;
    cout<<"input 5 number:"<<endl;
    while (count--)
        {
            cin>>number;
            mylist.push_back(number);
        }
    list<int>::iterator iter;
    for(iter=mylist.begin();iter!=mylist.end();iter++)
        {
            cout<<*iter<<" , ";
        }
    cout<<endl;
    return 0;
}

执行结果:
input 5 number:
5
6
4
7
8
5 , 6 , 4 , 7 , 8 ,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

天巽夕 发布了151 篇原创文章 · 获赞 2 · 访问量 1万+ 私信 关注

标签:include,cout,temp,stl,float,int,input,程序库,源代码
来源: https://blog.csdn.net/aa804738534/article/details/104482457