C/C++例程

1.前n项求和?

#include<stdio.h>

int main()

{   

    int n,i,sum;   

    sum=0;    

    while((scanf("%d",&n)!=EOF))    

    {        

        sum=0;        

        for(i=0;i<=n;i++)            

            sum+=i;      

        printf("%d\n\n",sum);    

    }

}

 

2.输入一个小于或等于九位数的数,输出它是几位数,然后单独顺序输出它的位,最后逆序输出它的位

#include<stdio.h>

 

int digtal(int n)

{

    return (n > 0)? 1 + digtal(n/10) : 0;

}

int single(int n)

{

    return (n > 0)? single(n/10),printf("%d ",n%10):0;      

}

int niverted(int n)

{

    return (n > 0)? (printf("%d",n%10),niverted(n/10)):0;

}

int main()

{  

    int n;      

    while((scanf("%d",&n)!=EOF))    

    {        

        printf("%d has %d digtals\n",n,digtal(n)); 

        printf("single out is:");

        single(n);

        printf("\nInverted order output IS:");

        niverted(n);

        printf("\n\n");

    }

}

3.给定一个半径求其周长及面积

#include <iostream>

#include <stdio.h>

#include<iomanip>

using namespace std;

#define PI  3.1416

float girth(float r)

{

    float l;

    if (r > 0)

        l = 2 * PI * r;

    return l;

}

 

float area(float r)

{

    float s;

    if (r > 0)

        s = PI * r * r;

    return s;

}

 

int main()

{

    float r;

    cout << "please enter raduis:";

    while(cin >> r)

    {

         cout << "girth:l=" << girth(r) << endl;

         cout << "area:s=" << area(r) << endl << '\n';

         cout << "please enter raduis:";

    }

    return 0;

}

 

4.打印九九乘法表

#include <iostream>

#include <stdio.h>

#include<iomanip>

using namespace std;

void main()

{

    int i,j;

 

    for (i = 1; i <= 9; i++)

        for(j = 1; j <= i; j++)

        {

            cout << j << '*' << i << '=' << i * j << '\t';

            if (j == i)cout << '\n';

        }  

}

4.1、程序启动后,在控制台中显示“请输入一个数字(10-20):”的提示信息,并等待用户输入内容

      2、读取用户输入的内容,根据用户输入的内容做以下处理:

    A、如果用户输入的数字不在 10到 20之间,显示提示“输入不正确,请重新输入数字”

    B、如果用户输入的数字正确,依次输出 1到用户输入的数字范围内的数字(使用循环语句输出数字)。如:用户输入 18,在控制台上依次输出 1,2,3..18

   3、 C、用户输入“0”,退出程序

#include <stdio.h>

#include <iostream>

using namespace std;

void main()

{

    int  digit;

    cout << "请输入一个数字(10-20):";

    while(cin >>  digit)

    {

        if (digit == 0)

            break;

        else if ((10 <= digit )&&(digit <= 20))

        {

            for (int i =1; i <= digit; i++)

            {

                cout << i;

                if (i != digit)

                    cout << ',';

            }

            cout << endl;

        }

        else

            cout << "输入不正确,请重新输入数字" << '\n';

        cout << '\n';

    }

}

 

 

 

 

5.求任意一点到一条直线的距离

#include <stdio.h>

#include <math.h>

#include <iostream>

using namespace std;

class line;

class point

{

public:

    int x,y;

public:

    point(int x1,int y1)

    {

        x=x1;

        y=y1;

    };

    friend double dist(line l,point p);

};

 

class line

{

public:

    int a,b,c;

public:

    line(int a1,int b1,int c1)

    {

        a=a1;b=b1;c=c1;

    };

    friend double dist(line l,point p);

};

 

double dist(line l,point p)

{

    double d;

    d = abs(l.a*p.x + l.b*p.y + l.c)/(sqrt(l.a*l.a + l.b*l.b));

    return d;

}

 

void main()

{

    int x,y;

    int a,b,c;

    cout << "请输入一个点:";

    while(cin >> x >> y)

    {

       

        cout << "点:" << "(" << x << "," << y << ")" << endl;

        cout << "请输入直线的三个参数:";

        cin >> a >> b >> c;

        printf("%d,%d,%d\n",a,b,c);

        printf("直线:%d*x+%d*y+%d=0\n",a,b,c);//cout << "直线:" << "(" << a << "," << b << "," << c << ")" << endl;

        point p1(x,y);

        line l1(a,b,c);

        cout << "他们间的距离为:distance=" << dist(l1,p1) << endl << endl;

        cout << "请输入一个点:";

    }

    system("pause");

}

6.给定两三角形边长,求其面积和

#include <stdio.h>

#include <math.h>

#include <iostream>

using namespace std;

class trig

{

    double x,y,z;

    double area()

    {

        double d = (x+y+z)/2;

        return sqrt(d*(d-x)*(d-y)*(d-z));

    }

public:

    trig(int i,int j,int k)

    {

        x = i;

        y = j;

        z = k;

    }

    int judge()

    {

        if ((z < x+y)&&(x < z+y)&&(y < z+x))

            return 1;

        else

            return 0;

    };

    friend double twoarea(trig a1,trig a2)

    {

        return (a1.area() + a2.area());

    }

};

 

void main()

{

    trig t1(1,4,5),t2(3,4,5);

    if (t1.judge()&&t2.judge())

        cout << "两三角形的面积和为:area=" << twoarea(t1,t2) << endl;

    else

        cout << "不能构成三角形" << endl;

}

 

7.计算储户三家银行的存款数

#include <stdio.h>

#include <iostream>

using namespace std;

 

class CBank;

class BBank;

class GBank;

 

class CBank

{

    int balance;

public:

    CBank(){balance = 0;}

    CBank(int i){balance = i;}

    void getblance()

    {

        cout << "输入中国银行存款数:";

        cin >> balance;

    }

    void disp()

    {

        cout << "中国银行存款数为:" << balance << endl;

    }

    friend void total(CBank,BBank,GBank);

};

 

class BBank

{

    int balance;

public:

    BBank(){balance = 0;}

    BBank(int i){balance = i;}

    void getblance()

    {

        cout << "输入工商银行存款数:";

        cin >> balance;

    }

    void disp()

    {

        cout << "工商银行存款数为:" << balance << endl;

    }

    friend void total(CBank,BBank,GBank);

};

 

class GBank

{

    int balance;

public:

    GBank(){balance = 0;}

    GBank(int i){balance = i;}

    void getblance()

    {

        cout << "输入农业银行存款数:";

        cin >> balance;

    }

    void disp()

    {

        cout << "农业银行存款数为:" << balance << endl;

    }

    friend void total(CBank,BBank,GBank);

};

 

void total(CBank A,BBank B,GBank C)

{

    cout << "总款数:" << A.balance + B.balance + C.balance << endl;

}

 

void main()

{

    CBank A;

    BBank B;

    GBank C;

    A.disp();

    B.disp();

    C.disp();

    while(1)

    {

        A.getblance();

        B.getblance();

        C.getblance();

        total(A,B,C);

        cout << endl;

    }

  

}

8.经典测试题,强有力的说明了定义类时、构造函数、析构函数等发生的操作,相当经典,有相当的震撼力

#include <stdio.h>

#include<math.h>

#include<string>

#include <iostream>

using namespace std;

class B

{

    int x,y;

public:

    B() { x=y=0;cout << "Construcor1" << endl; }

    B(int i) { x=i;y=0;cout << "Construcor2" << endl;}

    B(int i,int j) { x=i; y=j; cout << "Construcor3" << endl;}

    ~B() { cout << "Construcor" << endl; }

    void print()

    {

        cout << "x=" << x << ",y=" << y << endl;

    }

};

void main()

{

    B *ptr;

    ptr=new B[3];

    ptr[1]=B(5);

    ptr[2]=B(2,3);

    for (int i = 0; i < 3; i++)

        ptr[i].print();

    delete[] ptr;

}

执行结果:

 

 

9.简单时钟设计

#include <stdio.h>

#include <windows.h>

#include <iostream>

using namespace std;

class CDate

{

    int m_nDay;

    int m_nMonth;

    int m_nYear;//定义年月日的数据成员

    int sec,min,hour;//定义时间数据成员

public:

    CDate();

    CDate(int day,int month,int year);

    CTime(int s,int m,int h);

    void Display();

    void AddDay();

    void AddTime();

    void SetDate(int day,int month,int year);

    void SetTime(int s,int m,int h);

    ~CDate();

private:

    bool IsLeapYear();

};

 

CDate::CDate(){}

CDate::CDate(int year,int month,int day)

{

    m_nDay = day;

    m_nMonth = month;

    m_nYear = year;

}

 

CDate::CTime(int s,int m,int h)

{

    sec = s;

    min = m;

    hour = h;

}

void CDate::Display()

{

    char day[3];

    char month[3];

    char year[5];

    char secd[3],mind[3],hourd[3];

    _itoa(m_nDay,day,10);

    _itoa(m_nMonth,month,10);

    _itoa(m_nYear,year,10);

    _itoa(sec,secd,10);

    _itoa(min,mind,10);

    _itoa(hour,hourd,10);

    printf("当前日期是:%s/%s/%s\n",day,month,year);

    if ((sec < 10)&&(min < 10)&&(hour < 10))//为了时间前面加个0,我容易吗我?

        printf("当前时间是:0%s:0%s:0%s",hourd,mind,secd);

    else if ((sec < 10)&&(min < 10)&&(hour >= 10))

        printf("当前时间是:%s:0%s:0%s",hourd,mind,secd);

    else if ((sec < 10)&&(min >= 10)&&(hour < 10))

        printf("当前时间是:0%s:%s:0%s",hourd,mind,secd);

    else if ((sec >= 10)&&(min < 10)&&(hour < 10))

        printf("当前时间是:0%s:0%s:%s",hourd,mind,secd);

    else if ((sec < 10)&&(min >= 10)&&(hour >= 10))

        printf("当前时间是:%s:%s:0%s",hourd,mind,secd);

    else if ((sec >= 10)&&(min < 10)&&(hour >= 10))

        printf("当前时间是:%s:0%s:%s",hourd,mind,secd);

    else if ((sec >= 10)&&(min >= 10)&&(hour < 10))

        printf("当前时间是:0%s:%s:%s",hourd,mind,secd);

    else

        printf("当前时间是:%s:%s:%s",hourd,mind,secd);

}

 

void CDate::AddTime()

{

    sec++;

    if (sec > 59)

    {

        sec = 00;

        min++;

        if (min > 59)

        {

            min = 00;

            hour++;

            if (hour > 23)

            {

                hour = 00;

                AddDay();

            }

        }

    }

    return;

}

void CDate::AddDay()

{

    m_nDay++;

    if (IsLeapYear())

    {

        if ((m_nMonth == 2)&&(m_nDay == 30))

        {

            m_nMonth++;

            m_nDay = 1;

            return;

        }

    }

    else

    {

        if ((m_nMonth == 2)&&(m_nDay == 29))

        {

            m_nMonth++;

            m_nDay = 1;

            return;

        }

    }

    if (m_nDay > 31)

    {

        if (m_nMonth == 12)

        {

            m_nYear++;

            m_nMonth = 1;

            m_nDay = 1;

        }

        else

        {

            m_nMonth++;

            m_nDay = 1;

        }

    }

}

void CDate::SetDate(int year,int month,int day)

{

    m_nDay = day;

    m_nMonth = month;

    m_nYear = year;

}

void CDate::SetTime(int h,int m,int s)

{

    sec = s;

    min = m;

    hour = h;

}

CDate::~CDate(){ }

bool CDate::IsLeapYear()

{

    bool bLeap;

    if ((m_nYear % 4 == 0)&&(m_nYear % 100 != 0)||(m_nYear % 400 == 0))

        bLeap = 1;

    else

        bLeap = 0;

    return bLeap;

}

void main()

{

    CDate d;

    d.SetDate(2012,10,20);//设置日期

    d.SetTime(15,05,00);//设置时间

    while(1)

    {

        d.Display();//显示

        d.AddTime();//时间计数

        Sleep(1000);//延时一秒钟

        system("cls");//刷屏

    }

}

10.求两个复数的算术运算(利用运算符重载)

#include <stdio.h>

//#include <iomanip>

#include <iostream.h>

//using namespace std;

 

class complex

{

    float real,imag;

public:

    complex(float r,float i) {real = r;imag = i;}

    complex() {real = 0;imag = 0;}

    void print();

    friend complex operator+(complex a,complex b);

    friend complex operator-(complex a,complex b);

    friend complex operator*(complex a,complex b);

    friend complex operator/(complex a,complex b);

};

void complex::print()

{

    cout << real;

    if (imag > 0) cout << "+";

    if (imag != 0) cout << imag << "i" << endl;

}

complex operator+(complex a,complex b)

{

    complex temp;

    temp.real = a.real + b.real;

    temp.imag = a.imag + b.imag;

    return temp;

}

complex operator-(complex a,complex b)

{

    complex temp;

    temp.real = a.real - b.real;

    temp.imag = a.imag - b.imag;

    return temp;

}

complex operator*(complex a,complex b)

{

    complex temp;

    temp.real = a.real * b.real - b.imag*b.imag;

    temp.imag = a.real * b.imag - a.imag*b.real;

    return temp;

}

complex operator/(complex a,complex b)

{

    complex temp;

    float tt;

    tt = 1/(b.real*b.real + b.imag*b.imag);

    temp.real = (a.real*b.real + a.imag*b.imag)*tt;

    temp.imag = (b.real*a.imag - a.real*b.imag)*tt;

    return temp;

}

 

void main()

{

    complex c1(2.3,4.6),c2(3.6,2.8),c3;

    c1.print();

    c2.print();

    c3 = c1 + c2;

    c3.print();

    c3 = c1 - c2;

    c3.print();

    c3 = c1 * c2;

    c3.print();

    c3 = c1 / c2;

    c3.print();

}

11.编写一个学生和教师的数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类

#include <stdio.h>

#include <iomanip>

#include <iostream.h>

using namespace std;

 

class person

{

    int number;

    char name[10];

public:

    void input()

    {

        cout << "输入编号:";cin >> number;

        cout << "输入姓名:";cin >> name;

    }

    void disp()

    {

        cout << " 编号" << number << endl;

        cout << " 姓名" << name << endl;

    }

};

 

class student:public person

{

    char clas[6];

    int score;

public:

    void input()

    {

        person::input();

        cout << " 班号:"; cin >> clas;

        cout << " 成绩:"; cin >> score;

    }

    void disp()

    {

        person::disp();

        cout << " 班号:" << clas << endl;

        cout << " 成绩:" << score << endl;

    }

};

 

class teacher:public person

{

    char title[10];

    char branch[10];

public:

    void input()

    {

        person::input();

        cout << " 职称:"; cin >> title;

        cout << " 部门:"; cin >> branch;

    }

    void disp()

    {

        person::disp();

        cout << " 职称:" << title << endl;

        cout << " 部门:" << branch << endl;

    }

};

 

void main()

{

    student s1;

    teacher t1;

    cout << "输入一个学生数据:\n";s1.input();

    cout << "输入一个教师数据:\n";t1.input();

    cout << "显示一个学生数据:\n";s1.disp();

    cout << "显示一个教师数据:\n";t1.disp();

    system("pause");

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东皇※太一

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

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

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

打赏作者

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

抵扣说明:

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

余额充值