[C++]实验记录2018

实验1 输出个人信息

一、问题描述
二、实验输出
三、实验思路
四、代码

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

int main()
{
    string cla,id,name;
    cout << "请输入班级学号姓名:";
    cin >> cla >> id >> name;
    cout << "班级:" << cla << endl;
    cout << "学号:" << id << endl;
    cout << "姓名:" << name << endl;
    return 0;
}

实验2 求两点间的距离
一、 问题描述
若以屏幕左上角为原点,可将屏幕视为坐标系。从键盘输入任意两点P1、P2的坐标值,求两点间的距离。
二、 实验输出

三、实验思路
四、代码

#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
    void setCoordinate(int X,int Y);
    void setX(int X);
    void setY(int Y);

    int getX();
    int getY();

private:
    int x;
    int y;
};

void Point::setCoordinate(int X,int Y)
{
    setX(X);
    setY(Y);
}

void Point::setX(int X)
{
    x=X;
}

void Point::setY(int Y)
{
    y=Y;
}

int Point::getX()
{
    return x;
}

int Point::getY()
{
    return y;
}

int main()
{
    Point p1,p2;
    double distance;
    int x,y;
    cout<<"请输入横纵坐标值:" ;
    cin>>x>>y;
    p1.setX(x);
    p1.setY(y);
    cout<<"请输入横纵坐标值:" ;
    cin>>x>>y;
    p2.setX(x);
    p2.setY(y);
    double m=(p1.getX()-p2.getX() )*(p1.getX()-p2.getX())+(p1.getY()-p2.getY())*(p1.getY()-p2.getY());
    distance = sqrt(m);
    cout << "两点间距离为:" << distance<<endl;
    return 0;
}

实验3模拟存钱罐(p70)
一、问题描述
二、实验输出

三、实验思路
四、代码模板

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string.h>
using namespace std;
                /*CLASS MONEY*/
class Money
{
public:
    Money();                        //产生的零钱面额是随机的!
    double getValue() const;
private:
    double value;
};

                /*CLASS SAVINGBOX*/
class SavingBox
{
public:
    SavingBox();
    ~SavingBox();                   //释放随机生成的零钱
    void PutIntoMoney(Money* mon);  //投入一张零钱
    void Count() const;             //零钱分类统计
private:
    Money *items[100];
    int count;
};

                /*MONEY*/
Money :: Money()
{
    static double val[] = {0.1,0.5,1,5,10};
    int magic = rand() % 5;
    value = val[magic];
}

double Money :: getValue() const
{
    return value;
}


                /*SAVINGBOX*/
SavingBox::SavingBox()
{
    count = 0;
}

SavingBox::~SavingBox()
{
    for(int i = 0;i < count;i++)
        delete items[i];
}

void SavingBox :: PutIntoMoney(Money *mon)
{
    items[count++] = mon;
    cout << "投入" << mon->getValue() << "元币." << endl;
}

void SavingBox::Count() const
{
    double sum=0;
    int i, kind[5]={0,0,0,0,0};
    for(i=0;i<=count;i++)
        {
            if(items[i]->getValue()==0.1)kind[0]++;
            else if(items[i]->getValue()==0.5)kind[1]++;
            else if(items[i]->getValue()==1)kind[2]++;
            else if(items[i]->getValue()==5)kind[3]++;
            else if(items[i]->getValue()==10)kind[4]++;
        }
    sum=kind[0]*0.1+kind[1]*0.5+kind[2]*1.0+kind[3]*5.0+kind[4]*10.0;
    cout<<"存钱罐共有钱"<<sum<<"元。\n其中,1角币"<<kind[0]<<"个,5角币"<<kind[0]<<"个,5角币"
    <<kind[1]<<"个,1元币"<<kind[2]<<"个,5元币"<<kind[3]<<"个,10元币"<<kind[4]<<"个。"<<endl;
}


                /*MAIN*/
int main()
{
    SavingBox piggybank;
    srand(time(NULL));          //设置随机数种子
    while(cin.get() == '\n')    //输入的键是回车键时保持循环
    {
        piggybank.PutIntoMoney(new Money());
    }
    piggybank.Count();
    return 0;
}

实验4 积分返券
三、 问题描述
某商场年终举行“积分返券”活动,其规则为:每张会员卡积满1000分返礼券10元,满3000分返礼券30元,积满5000分返礼券100元。返券同时消除卡上礼券对应的积分。
四、 实验输出

三、实验思路
四、代码

#include <string.h>
#include <iostream>
#include <cstring>
#ifndef MEMBERCARD_H
#define MEMBERCARD_H
using namespace std;

class MemberCard
{
public :
MemberCard(char * pid,char * pname,double s);
    ~MemberCard();
    void Reward();
    void print();
    void reward0();
    char *id;
    char *name;
    double score;
    double reward;
};
#endif

MemberCard::MemberCard(char *pid,char *pname,double s )
{
    id = new char[strlen(pid) + 1];
    strcpy(id,pid);

    name = new char[strlen(pname) + 1];
    strcpy(name,pname);

    score = s;
    reward = 0;
}

MemberCard::~MemberCard()
{
    delete [] id;
    delete [] name;
}

void MemberCard::Reward()
{
    if(score>5000)
    {
        score = score - 5000 ;
        reward+=100;
    }
    else if(score>3000)
    {
        score = score - 3000 ;
        reward+=30;
    }
    else if(score>1000)
    {
        score = score - 1000 ;
        reward+=10;
    }
    else
    {
        score=score;
        reward = reward;
    }
}
void MemberCard:: print()
{
cout<< "卡号:" <<id    <<endl;
cout<< "姓名:" <<name  <<endl;
cout<< "积分:" <<score <<endl;
cout<< "返券:" <<reward<<endl;
}

void MemberCard::reward0()
{
    reward = 0;
}

intmain()
{
MemberCard m("10239965","刘明",4211);
cout<< "******返券前******" <<endl;
    m.reward0();
m.print();
while(m.score>= 1000)
    {
m.Reward();
    }
cout<< "******返券后******" <<endl;
m.print();
    return 0;
}

实验5 时间
一、 问题描述
时间
二、 实验输出

三、实验思路
四、代码

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <conio.h>
using namespace std;

class Time
{
public:
Time();
Time(inth,intm,int s);
    void tick();
    void printUniversal();
    void printStandard();

intgetHour(){ return hour;}
intgetMinute() { return minute;}
intgetSecond() {return second;}

    void setTime(inth,intm,int s);
    void setHour(int h);
    void setMinute(int m);
    void setSecond(int s);

private:
int hour;
int minute;
int second;
};

Time::Time()
{
    long now = time(NULL);
    tm local = *localtime(&now);
    hour = local.tm_hour;
    minute = local.tm_min;
    second = local.tm_sec;
}

Time::Time(inth,intm,int s)
{
    hour = h;
    minute = m;
    second = s;
}

void Time::tick()
{
    second++;
if(second >= 60)
    {
        minute++;
        second = second % 60;
    }
if(minute >=60)
    {
        hour++;
        minute = minute % 60;
    }
if(hour > 24)
    {
        hour = hour % 24;
    }
}

void Time :: printUniversal()
{
cout<<setfill('0') <<setw(2) << hour << ":" <<setw(2) << minute << ":" <<setw(2) << second <<endl;
}

void Time::printStandard()
{
cout<<setfill('0') <<setw(2) << hour%12 << ":" <<setw(2) << minute << ":" <<setw(2) << second <<endl;
}

void Time::setTime(inth,intm,int s)
{
    hour = h;
    minute = m;
    second = s;
}

void Time::setHour(int h)
{
    hour = h;
}

void Time::setMinute(int m)
{
    minute = m;
}

void Time::setSecond(int s)
{
    second = s;
}


intmain()
{
    Time now;
    long t1 = time(NULL),t2;
now.printUniversal();
    while(!-kbhit())
    {
        t2 = time(NULL);
if(t2-t1 == 1)
        {
            system("cls");
            t1 = t2;
now.tick();
now.printUniversal();
        }
    }
    return 0;
}

实验6 智能电表
一、 问题描述
模拟智能电表
二、 实验输出
三、实验思路
四、代码

#include <iostream>

using namespace std;

class Ammeter
{
public:
    Ammeter(double r = 0);
    void print() const;
    void setReading(double amount);         //计电流量
protected:
    double reading;                         //电表度数
    static double price;                    //电费单价
};

class Smartmeter:public Ammeter
{
public:
    Smartmeter(double r=0);
    void setPrepaid(double pre);            //存入预付费
    double CalcCharge();                    //计算剩余款
    void print() const;
private:
    double prepaid;                         //预付费
    double balance;                         //剩余款
};

Ammeter::Ammeter(double r)
{
    reading = r;
}

Smartmeter::Smartmeter(double r)
{
    reading = r;
}

void Ammeter::print() const
{
    cout << "度数:" << reading << "     单价:" << price ;
}

void Ammeter::setReading(double amount)
{
    reading ++;
}

void Smartmeter::setPrepaid(double pre)
{
    prepaid = pre;
}

double Smartmeter::CalcCharge()
{
    balance = prepaid - reading*price;
    return balance;
}

void Smartmeter::print() const
{
    cout << "度数:" << reading << "    单价:" << price << "    预付款:" << prepaid << "    剩余数:" << balance << endl;
}

double Ammeter::price = 0.48;

int main()
{
    Smartmeter meter;
    int val = 1;
    double balance;
    meter.setPrepaid(50);
    balance = meter.CalcCharge();
    meter.print();
    do
    {
        meter.setReading(val++);
        balance = meter.CalcCharge();
    }while(balance > 1e-1);
    meter.print();
    return 0;
}

实验7 设计使用整型数组
设计实用整型数组
一、问题描述
数组
二、实验输出

三、实验思路
四、代码

#include <iostream>
#include<stdlib.h>
using namespace std;

template <class T>
int getArrayLen(T& array) //使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
{
 return (sizeof(array) / sizeof(array[0]));
}
class Array
{
public:
    Array(unsigned l=0, int *elems=NULL)
    {
        len=l;
        this->elems=(int*)malloc(sizeof(int));
        this->elems=elems;
    }
    int  length()const
    {
        return len ;
    }
    int at(int i)const
    {
        return *(elems+i);
    }
    int &at(int i)
    {
        return *(elems+i);
    }
    void print()const
    {
        int i;
        for(i=0;i<len-1;i++)
        {
            cout<<*(elems+i)<<" ";
        }
        cout<<*(elems+len-1)<<endl;
    }
protected:
    int len;
    int *elems;
};


class ChkArray:virtual public Array
{
public:
    ChkArray(unsigned l=0,int *elems=NULL):Array(l,elems){}
    int at(int i)const
    {
        if(BorderCheck(i)){cout<<"下标越界!"<<endl;}
        else {cout<<*(elems+i)<<endl;}
    }
    int & at(int i)
    {
        if(BorderCheck(i)){cout<<"下标越界!"<<endl;}
        else {cout<<*(elems+i)<<endl;}
    }
protected:
    bool BorderCheck(int i)const
    {
        if(i>len-1)return 1;
        else  return 0;}
};
class SortArray:virtual public Array
{
public:
    SortArray(unsigned l=0, int *elems=NULL):Array(l,elems){}
    void sort(bool asc=1)
    {
        int i,j,m;
        for(i=0;i<len-1;i++)
        {
            for(j=i+1;j<len;j++)
            {
                if(*(elems+j)<*(elems+i))
                {
                    m=*(elems+i);
                    *(elems+i)=*(elems+j);
                *(elems+j)=m;
                }
            }
        }
    }
};
class ChkSrtArray:public ChkArray,public SortArray
{
public :
    ChkSrtArray(unsigned l=0, int *elems=NULL):Array(l,elems),SortArray(l,elems),ChkArray(l,elems){}
};
int main()
{
    int Arr[]={9,23,7,5,13,6,8,12,17};
    cout<<"排序前:";
    ChkSrtArray arr(9,Arr);
    arr.print();
    cout<<"排序后";
    arr.sort();
    arr.print();
    int i;
    cout<<"输入要访问的元素下标:";
    cin>>i;
    arr.at(i);
    return 0;
}

实验8 计算工作量
一、问题描述
计算工作量
二、实验输出

三、实验思路
四、代码

#include <iostream>
#include <iomanip>
#include <string>
//#include <ctime>
using namespace std;

class Facu{
public:
    Facu(const char * n,double t):name(n),tchDay(t){}
    virtual double wl() const{return 0;}
    virtual void print() const{cout<<setw(8)<<name;}
protected:
    string name;
    double tchDay;
};

class Seni:public Facu{
public:
    Seni(const char * n,double t,double pg):Facu(n,t),pgTD(pg){}
    double wl() const{return pgCo*pgTD+tchDay*coef;}
    void print() const;
private:
    static double pgCo;
    static double coef;
    double pgTD;
};
double Seni::pgCo=95;
double Seni::coef=80;
void Seni::print()const{
    Facu::print();
    cout<<setw(8)<<"高级"<<setw(6)<<tchDay+pgTD<<setw(6)<<wl()<<endl;
}

class Midd:public Facu{
public:
    Midd(const char * n,double t):Facu(n,t){}
    double wl() const{return tchDay*coef;}
    void print() const;
private:
    static double coef;
};
double Midd::coef=60;
void Midd::print()const{
    Facu::print();
    cout<<setw(8)<<"中级"<<setw(6)<<tchDay<<setw(6)<<wl()<<endl;
}
class Prim:public Facu{
public:
    Prim(const char * n,double t):Facu(n,t){}
    double wl() const{return pgCo*tchDay+tchDay*coef;}
    void print() const;
private:
    static double pgCo;
    static double coef;
};
double Prim::pgCo=40;
double Prim::coef=10;
void Prim::print()const{
    Facu::print();
    cout<<setw(8)<<"初级"<<setw(6)<<tchDay<<setw(6)<<wl()<<endl;
}

int main(){
    const int SIZE=3;
    Prim ma("玛丽",90);
    Midd li("陈颖",90);
    Seni da("郑申",45,45);
    Facu *ptr[SIZE]={&da,&li,&ma};
    cout<<setiosflags(ios::left);
    cout<<setw(8)<<"姓名"<<setw(8)<<"职称"<<setw(6)<<"教日"<<setw(6)<<"工作量"<<endl;
    cout<<"-------------------------------"<<endl;
    for(int i=0;i<SIZE;i++)
        ptr[i]->print();

}

实验9在任意类型数组中查找某数是否存在
一、问题描述
实验6 在任意类型数组中查找某数是否存在
二、实验输出

三、实验思路
四、代码

#include <iostream>

using namespace std;
template<class T>
void find (T *p,int size,T s);
class Complex;
bool operator==(const Complex &c1,const Complex &c2);
class Complex
{
    friend bool operator==(const Complex &c1,const Complex &c2);
    public:
    Complex(float r =0,float i=0):real(r),image(i){}
    private:
        float real,image;
};
bool operator==(const Complex &c1,const Complex &c2)
{
    if(c1.image==c2.image&&c1.real==c2.real)
        return 1;
    return 0;
};
int main()
{
    Complex t;
    int a[3]={33,56,99};
    Complex b[3]={Complex(1,2),Complex(5.4,6.6),Complex(56,5)};
    t=Complex(1,2);
    find(a,3,33);
    find(b,3,t);
    return 0;
}
template<class T>
void find(T *p,int size,T s)
{

    int i;
    for(i=0;i<size;i++)
    {
        if(*(p+i)==s)
            {cout<<"YES!"<<endl;break;}
    }
}

实验10 图书目录
打印图书目录实验报告
一、问题描述
实验7-1 打印图书目录实验报告
二、实验输出

三、实验思路
四、代码

#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int main()
{
    cout << "第一章" << endl;
    cout << "    ";
    cout<<left<<setw(7)<<"1.1";
    cout.fill('*');
    cout<<left<<setw(30)<<"什么是C++语言";
    cout<< setfill('*')<<right<<setw(10)<<"1"<<endl;
    cout << "    ";
    cout.fill(' ');
    cout<<left<<setw(7) << "1.1.1";
    cout.fill('*');
    cout<<left<<setw(30)<<"C++语言的发展史";
    cout<< setfill('*')<<right<<setw(10)<<"58"<<endl;
    cout << "第二章" << endl;
    system("pause");
    return 0;
}

实验11 凯撒密码
一、问题描述
凯撒密码
二、实验输出

三、实验思路
四、代码

#include <iostream>
#include <cmath>
#include <string.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
void Decrypt(char *p,int key)
{
    while(*p)
    {
        if(isupper(*p))
            *p=(*p-'A'-key+26)%26+'A';
        else if(islower(*p))
            *p=(*p-'a'-key+26)%26+'a';
        else
            *p-=key;
        p++;
    }
}
void Encrypt(char *p,int key)
{
    while(*p)
    {
        if(isupper(*p))
            *p=(*p-'A'+key)%26+'A';
        else if(islower(*p))
            *p=(*p-'a'+key)%26+'a';
        else
            *p-=key;
        p++;
    }
}
int main()
{
    const int SIZE =256;
    char str[SIZE],asd[SIZE];
    int key;
    int i,j;
    ofstream WriteToFile;
    ifstream ReadFromFile;
    cout<<"请输入明文:";
    cin.getline(str,SIZE);
    cout<<"请输入偏移量:";
    cin>>key;
    Encrypt(str,key);
    cout<<"加密锁的密文:"<<str<<endl;
    WriteToFile.open("e:\\stufile1.dat",ios::out|ios::trunc);
    if(!WriteToFile)
    {
        cerr<<"file打开失败!"<<endl;
        exit(EXIT_FAILURE);
    }
    for( i=0;str[i]!='\0';i++)
    {
        WriteToFile<<str[i];
    }
    cout<<"向文件写入完毕."<<endl;
    WriteToFile.close();
    ReadFromFile.open("e:\\stufile1.dat",ios::in);
    if(!ReadFromFile)
    {
        cerr<<"file打开失败!"<<endl;
        exit(EXIT_FAILURE);
    }
    for( j=0;j<i;j++)
    {
        ReadFromFile>>asd[j];
    }
    cout<<"从文件读出完毕."<<endl;
    ReadFromFile.close();
    Decrypt(asd,key);
    cout<<"解密所得密文:"<<asd<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯糖不加咖啡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值