C++实验--getline、文件、类模板、杨辉三角、运算符重载、复数操作

C++实验

前几天在电脑上发现自己大一的时候写的C++实验课的代码。舍不得删掉,那个时候写这么点代码都费劲呢。那就放一篇怀旧博客吧,用代码唤醒那些年刚学计算机时的记忆,希望自己永远保持热忱,保持好奇,你们也是~

1.分别用for和while循环计算50-100所有整数的和

#include 
using namespace std;

int main()
{
    int i;
    int sum = 0;
    for(i = 50;i <= 100;i++)
    {
        sum += i;
    }
    cout << sum;
    //这是for循环,下面的是while循环
    int i = 50,sum = 0;
    while(i <= 100)
    {
        sum = sum + i;
        i++;
    }
    cout << sum;
    return 0;
}

2.打开文件以及使用getline

#include
#include
#include
using namespace std;

int main()
{
    string buff;
    ifstream infile;
    ofstream outfile;
    
    cout << "input file name" << endl;
    cin >> buff;
    infile.open(buff.c_str());
    if(!infile)
    {
        cout << "error" << endl;
    }
    cout << "input file name" << endl;
    cin >> buff;
    outfile.open(buff.c_str());
    while(getline(infile,buff))
    {
        outfile << buff << endl;
    }
    infile.close();
    outfile.close();
    return 0;
}

3. 用类模板编译一个程序事之使用类模板分别计算整型、浮点型、字符型的两个常量之间的大小

#include 
using namespace std;

template <class numtype>
class compare
{
public:
    numtype x;
    numtype y;
    compare(numtype a,numtype b)
    {
        x = a;
        y = b;
    }
    numtype max()
    {
        return (x > y)?x:y;
    }
    numtype min()
    {
        return (x > y)?y:x;
    }
}int main()
{
    compare <int>   comp1(3,7);
    compare <float> comp2(2.1,5.2);
    compare <char>  comp3('d','e');
    cout << comp1.max() << comp1.min() << "for int" << '\n';
    cout << comp2.max() << comp2.min() << "for double" << '\n';
    cout << comp3.max() << comp3.min() << "for char" << '\n';
    return 0;
}

4.杨辉三角

#include 
#include 
#define N 10
using namespace std;

int main()
{
    int i,j;
    int a[N][N];
    int m;//杨辉三角的行数为m
    while(scanf("%d",&m)!=EOF)
    {
        for(i=0;i<m;i++)
        {
            a[i][0]=1;//第一竖行是全为1
            a[i][i]=1;//斜对角线全为1
        }
        for(i=2;i<m;i++)
        {//从第二行开始填充非1数
            for(j=1;j<i;j++)
            {/*此处下标从1开始*/
                a[i][j]=a[i-1][j-1]+a[i-1][j];
            }
        }
        for(i=0;i<m;i++)
        {
            for(j=0;j<=i;j++)
            {
                printf("%d ",a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

5.运算符重载的练习

#include 
using namespace std;

class Complex
{
public:
    Complex()
    {
        real = 0;
        imag = 0;
    }//构造函数
    Complex(double a,double b)
    {
        real = a;
        imag = b;
    }
    void input();
    Complex operator+(Complex &c2);//运算符重载的声明
private:
    double real;
    double imag;
};

void Complex::input()
{
    cout << "(" << real << "," << "i" << imag << ")" << endl;
}

Complex Complex::operator+(Complex &c2)
{
    Complex c;
    c.real = real + c2.real;
    c.imag = imag + c2.imag;
    return c;
}//运算符重载的定义
int main()
{
    Complex c1(3,4),c2(4,5),c3;
    c3 = c1 + c2;
    cout << "c1 = ";c1.input();
    cout << "c2 = ";c2.input();
    cout<< "c1 + c2 = "; c3.input();
    return 0;
}

6.实现复数的加减乘除,以及将实数转换为复数,并且将复数转换为复数的模

#include 
#include 
#include 
using namespace std;

class Complex
{
private:
    double real;
    double imag;
public:
    Complex()
    {
        real = 0;
        imag = 0;
    }//默认构造函数
    Complex(double a,double b)
    {
        real = a;
        imag = b;
    }//用于初始化的构造函数
    Complex(double r)
    {
        real = r;
        imag = 0;
    }// 转换型复制函数,将复数转换为实数
    void output()
    {
        cout << real << ' ' << imag <<'\n';
    }
    operator double()
    {
        return pow(real+imag,0.5);
    }//类型转换函数,把复数转换为浮点型复数的模
    void input();
    Complex operator+(Complex &c1);
    Complex operator-(Complex &c1);
    Complex operator*(Complex &c1);
    Complex operator/(Complex &c1);
};

void Complex::input()
{
    cin >> real >> imag;
}

Complex Complex::operator+(Complex &c1)
{
    Complex c;
    c.real = real + c1.real;
    c.imag = imag +c1.imag;
    return c;
}

Complex Complex::operator-(Complex &c1)
{
    Complex c;
    c.real = real - c1.real;
    c.imag = imag - c1.imag;
    return c;
}

Complex Complex::operator*(Complex &c1)
{
    Complex c;
    c.real = real * c1.real - imag * c1.imag;
    c.imag = real * c1.imag + imag * c1.real;
    return c;
}

Complex Complex::operator/(Complex &c1)
{
    Complex c;
    double d;
    d = pow(real,2) - pow(imag,2);
    c.real = (c1.real * real + c1.imag * imag) / d;
    c.imag = (c1.imag * real - c1.real * imag) / d;
    return c;
}

int main()
{
    Complex c1,c2;
    c1.input();
    c2.input();
    Complex c;
    c = c1 + c2;
    c.output();
    c = c1 - c2;
    c.output();
    c = c1 * c2;
    c.output();
    c = c1 / c2;
    c.output();//计算并输出复数之间的加减乘除
    c = Complex(2.0);
    c.output();//将C强制转换为实数并输出
    cout << (double)c;//转换成c的模并将其输出
    return 0;
}

7.输入输出本科生研究生的信息

#include 
#include 
using namespace std;

class student
{
public:
    //student(int a,float b,string m);
    int schnum;
    float score;
    string name;
    void input();
    void output();
};
student::input()
{
    cin >> schnum >> score >> name;
}
void student::output()
{
    cout << "本科生列表如下" << '\n';
    cout << schnum << " " << score << " " << name << '\n';
}
class graduate : public student
{
private:
    int pay;
public:
    void input();
    void output();
};
void graduate::input()
{
    cin >> schnum >> score >> name >> pay;
}
void graduate::output()
{
    cout << "研究生列表如下" << '\n';
    cout << schnum <<" " << score << " " << name << " " << pay << '\n';
}
int main()
{
    student a[1000];
    graduate b[500];
    int i;
    int j;
    int nums;//本科生人数,假设输入小于1000
    int numg;//研究生人数,假设输入小于500
    cin >> nums >> numg;
    for(i = 0;i < nums;i++)
    {
        a[i].student::input();
    }
    for(j = 0;j < numg;j++)
    {
        b[j].graduate::input();
    }
    while(--i>=0)
    {
        a[i].student::output();
    }
    while(--j>=0)
    {
        b[j].graduate::output();
    }
    return 0}

你可能感兴趣的:(c++,算法)