c++题目总结

 一

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string user_name;
    cout<<"Please enter your first name:";
    cin>>user_name;
    cout<<'\n'<<Hello, "<<user_name<<"...and goodbye!\n;
    return 0;
}

#include"iostream"
using namespace std;
int main()
{
    int i;float g;
    char s[80];
    cout<<"Enter an integer,float,and string:";
    cin>>i>>f>>s;
    cout<<"Here's your data:"<<i<<' '<<f<<endl<<s<<'\n';
    return 0;
}

#include"iostream"
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b;
    if(b==0) return -1;
    c=a/b;
    cout<<"=c"<<c<<'\n';
    return 0;

#include"iostream"
using namespace std;
int max(int x,int y);
int main()
{
    int a,b,c;
    cin>>a>>b;
    c=max(a,b);
    cout<<"max="<<c<<'\n';
}
int max(int x,int y)
{
    int z;
    if(x>y) z=x;
    else z=y;
    return(z);
}

#include"iostream"
using namespace std;
void sqr_it(int *i);
int main()
{
    int x;
    x=10;
    sqr_it(&x);
    cout<<"The square of x is "<<x<<'\n';
    return 0;
}
void sqrt_it(int *i)
{
    *i=(*i)*(*i);
}

#include"iostream"
using namespace std;
//例1.10
int max(int x,int y);
long max(long x,long y);
double max(double x,double y);
int main()
{
    int x1=-6;int x2=5;
    long x3=-61;long x4=-99;
    double x5=6.6;double x6=88.55;
    cout<<"x="<<max(x1,x2)<<endl;
    cout<<"y="<<max(x3,x4)<<endl;
    cout<<"z="<<max(x5,x6)<<endl;
    return 0;
}
int max(int x,int y)
{
    int z;
    if(x>y) z=x;
    else z=y;
    return(z);
}
long max(long x,long y)
{
    long z;
    if(x>y) z=x;
    else z=y;
    return(z);
}
double max(double x,double y)
{
    double z;
    if(x>y) z=x;
    else z=y;
    return(z);
}

 

#include"iostream"
using namespace std;
#define _CRT_SECURE_NO_WARNING
//例1.11
int main()
{
    char *name;
    name=new char[10];
    if(!name)
    {
        cout<<"Allocation error\n";
        return 1;
    }
    strcpy(name,"wuyufei");
    cout<<"Here is my name:";
    for(int i=0;i<8;i++)
    {
        cout<<name[i];
        if(name[i]=='/0') break;
    }
    delete name;
    return 0;
}

 

#include"iostream"
using namespace std;
#define _CRT_SECURE_NO_WARNING
void f(int a[],int n,int &max,int &min)
{
    max=min=a[0];
    for(int i=1;i<n;i++)
    {
        if(max<a[i]) max=a[i];
        if(min>a[i]) min=a[i];
    }
}
void main()
{
    int a[10]={2,5,3,9,0,8,1,7,6,4};
    int max,min;
    f(a,10,max,min);
    cout<<"Max: "<<max<<endl;
    cout<<"Min: "<<min<<endl;
}

#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNING
class counter{
private:
    int value;
public:
    counter(int number):value(number){}
    void increment()
    {
        value++;
    }
    void decrement()
    {    
        value--;
    }
    int getvalue()
    {
        return value;
    }
    void print()
    {
        cout<<"计数器的值为:"<<value<<endl;
    }
};

int main()
{
    counter myCounter(10);
    myCounter.print();
    myCounter.increment();
    myCounter.print();
    myCounter.decrement();
    myCounter.print();
    return 0;
}

//student.h
class Student
{
private:
    int number;
    char* name;
    float score;
public:
    Student(int number1,char* name1,float score1);
    ~Student();
    void modify(float score1)
    {
        score=score1;
    }
    void print();
};


#include"student.h"
#include<iostream>
#include<string>
using namesapce std;
#define _CRT_SECURE_NO_WARNING
//构造函数
Student::Student(int number1,char* name1,float score1)
{
    number=number1;
    name=new char[srelen(name1)+1];
    strcpy(name,name1);
    score=score1;
}
//析构函数
Student::~Student()
{
    delete[]name;
}
void Student::print()
{
    cout<<"number:"<<number<<"name:"<<name<<"score:"<<score<<'\n';
}
int main()
{
    int numberi;
    char namei[15];
    float scorei;

    cout<<"Enter number:\n";cin>>numberi;
    cout<<"Enter name:\n";cin>>namei;
    cout<<"Enter score:\n";cin>>scorei;
    Student stu1(numberi,namei,scorei);
    
    cout<<"Enter number:\n:;cin>>numberi;
    cout<<"Enter name:\n";cin>>namei;
    Student stu2(numberi,namei,scorei);
    stu2.modify(87);

    stu1.print();
    stu2.print();
    return 0;
}

#include<iosream>
using namespace std;
#define _CRT_SRCURE_NO_WARNIG
class Rectangle
{
    friend float area(Rectangle T);
    friend float cret(Rectangle T);
public:
    Rectangle(int a,int b)
    {
        len=a;
        wid=b;
    }
private:
    int len;
    int wid;
};
float area(Rectangle T)
{
    cout<<"area="<<T.len*T.wid<<endl;
    return 2*(T.len+T.wid);
}
int main()
{
    Rectangle t1(2,3);
    area(t1);
    cret(t1);
}

#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNING
class Data
{
    int d,m,y;
public:
    int day() const {return d;}
    int month() const {return m;}
    int year() const;
};
inline int Data::year() const
{
    return y;
}

 

#include<iostream>
#include<string>
using namesapce std;
#define _CRT_SECURE_NO_WARNING
//构造函数
class Score{
    float computer;
    float english;
    float mathematics;
public:
    Score(float x,float y,float z):computer(x),english(y),mathematics(z)  {}
    Score() {}
    void print() {
        cout<<"Computer: "<<computer<<" ,English: "<<english<<",Mathematics: "<<mathematics<<endl;
    }
};

class Student
{
private:
    int number;
    char* name;
    Score score;
public:
    Student(int number1,char* name1,float com,float eng,float math);
    ~Student();
    void print();
};
Student::Student(int number1,char* name1,float com,float math):score(com,eng,math)
{
    number=number1;
    name=new char[strlen(name1)+1];
    strcpy(name,name1);
}
//析构函数
Student::~Student()
{
    delete[]name;
}
void Student::print()
{
    cout<<"number:"<<number<<" name:"<<name<<'\n';
    score.print();
}
int main()
{
    int number;
    char name[50];
    float computer,english,mathematics;
    cout<<"请输入学号:”;
    cin>>number;
    cout<<"请输入姓名:”;
    cin>>name;
    cout<<"请输入计算机成绩:”;
    cin>>computer;
    cout<<"请输入英语成绩:”;
    cin>>english;
    cout<<"请输入数学成绩:”;
    cin>>mathematics;
    
    Student stu(number,name.computer,english,mathematics);
    stu.print();
    return 0;
}

#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
class Student{
public:
    string name;
    int age;
    Student(string n,int a):name(n),age(a) {}
};

int main()
{
    Student students[5]={
        {"Alice",20},
        {"Bob",21},
        {Charlie",22},
        {David",23},
        {Eve",24}
    };

    for(int i,i<5;++i){
        cout<<"Student "<<i+1<<": "<<student[i].name<<", Age: "<<students[i].age<<endl;
    }
    cout<<"\n"<<endl;
    for(int i=0;i<5;++i){
        cout<<"Student "<<i+1<<": "<<students[i].name<<", Age: "<<students[i].age<<endl;
    }
    
    return 0;
}

#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
class samp
{
    int i;
public:
    samp(int n) {i=n;}
    void set_i(int n) {i=n;}
    int get_i() {return i;}
};

void sqr_it(samp &o)
{
    o.set_i(o.get_i()*o.get_i());
    cout<<"Copy of a has i value of "<<o.get_i();
    cout<<"\n";
}
int main()
{
    samp a(10);
    sqr_it(a);
    cout<<"Copy of a has i value of "<<a.get_i();
    return 0;
}

#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
class Circle
{
public:
    Circle(float r)
    {
        radius=r;
        ++count;
    }
    ~Circle()  {--count;}
    static int num() {return count; }
private:
    float radius;
    static int count;
};
int Circle::count=0;
int main()
{
    Circle c1(5);
    Circle c2(7);
    Circle c3(9);
    
    cout<<"对象的个数: ”<<Circle::num()<<endl;
    return 0;
}

#include<iostream>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
class Point
{
public:
    Point(double xi,double yi) {X=xi,Y=yi;}
    double GetX() {return X;}
    double GetY() {return Y;}
    friend double Distance(Point &a,Point &b);
private:
    double X,Y;
};
double Distance(Point &a,Point &b)
{
    double dx=a.X-b.X;
    doubel dy=a.Y-b.Y;
    return sqrt(dx*dx+dy+dy);
}
int main()
{
    Point p1(3.0,5.0),p2(4.0,6.0);
    double d=Distance(p1,p2);
    cout<<"The distance is "<<d<<endl;
    return 0;
}

一个是普通函数,一个是成员函数

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值