程序设计下课堂记录

对缓考的复验:

 

#include<stdio.h>
int func(int b);
int main()
{
	int a=100;
	for(int i=0;i<3;i++)
	{
		a++;
		func(a);
		printf("%d\n",a);
	}

	
	
}
	
int func(int b)
	{
		int a;
		a=20;
		a++;
		b=b+a;
		printf("%d",b);
	}
	

//122101
//123102
//124103

引用的学习

int &r=i;

即r声明为i的引用

注意!不能只声明Int &r;

引用自身没有储存单位

引用的值就是被引用的变量(或对象)的值

对引用的操作就是对变量的操作,反之亦然

引用和被引用的实体有相同的地址

区别引用和指针

1、指针是变量,引用不是

2、引用必须初始化

3、指针可以作为数组元素,引用不能

#include<iostream>
using namespace std;
void test(int &a)
{
	a+=3;
	cout<<"a:"<<a<<"\n"<<"a.address:"<<&a<<"\n";
	
}

int main()
{
	int b=10;
	test(b);
	cout<<"b:"<<b<<"\n"<<"b.address:"<<&b<<"\n";
	return 0;
}

//a:13
//a.address:0x70fe0c
//b:13
//b.address:0x70fe0c

 对比指针

#include<iostream>
using namespace std;
int test(int *a)
{
	*a+=3;
	cout<<"a:"<<*a<<"\n"<<"a.address:"<<a<<"\n";
}

int main()
{
	int b;
	test(&b);
	cout<<"b:"<<b<<"\n"<<"b.address:"<<&b<<"\n";
	return 0;
}

//a:3
//a.address:0x70fe0c
//b:3
//b.address:0x70fe0c

#include<iostream>
using namespace std;
int swap(char*x,char*y)
{
	char*temp;
	temp=x;
	x=y;
	y=temp;
}
//交换了指针变量指向的地址
//但没有交换地址存储的值 
int main()
{
	char a[20]="hello",b[20]="how are you?";
	char *ap=a;
	char *bp=b;
	cout<<"ap:"<<ap<<endl;
	cout<<"bp:"<<bp<<endl;
	swap(ap,bp);
	cout<<"ap:"<<ap<<endl;
	cout<<"bp:"<<bp<<endl;
	return 0;
}

//ap:hello
//bp:how are you?
//ap:hello
//bp:how are you?

#include<iostream>
using namespace std;
int swap(char *&x,char*&y)
{
	char*temp;
	temp=x;
	x=y;
	y=temp;
}

int main()
{
	char a[20]="hello",b[20]="how are you?";
	char *ap=a;
	char *bp=b;
	cout<<"ap:"<<ap<<endl;
	cout<<"bp:"<<bp<<endl;
	swap(ap,bp);
	cout<<"ap:"<<ap<<endl;
	cout<<"bp:"<<bp<<endl;
	return 0;
}

 //成功交换
//ap:hello
//bp:how are you?
//ap:how are you?
//bp:hello 

 内联函数inline

使用情况

1.函数功能简单

2.代码短

3.使用频率高

为了协调效率和可读性之间的矛盾,采用内联函数

注意:

1、内联函数体内不能有循环语句和开关语句

(如果有,都按普通函数处理,相当于内联函数没用了)

2、语句不能多于10行

3、类内部定义的成员函数我们都看作内联函数处理,所以不需要再用inline声明了

4、类外部一类的成员函数,如果要说明为内联函数,要加上Inline

宏定义

#include<iostream>
using namespace std;
#define SUM(x) x*x
inline int sum(int a)     //内联函数的声明格式 
{
	return a*a;
}

int main()
{
	cout<<sum(1+3)<<endl;
	cout<<SUM(1+3)<<endl;
	return 0;
}

//16
//7 

注意宏定义的运算规则
相当于1+3*1+3=7

作业

#include<iostream>
#include<math.h>
using namespace std;
void sum1(double &sum,unsigned int N)
{
    double k;
	k=1.0;
	
	while(N>0)
	{
     	k=k*4/3;        //注意这里不能是(4/3),也不能是k*=4/3,不然就相当于一直乘以1了 
		N--;
	}
	
	sum=3*k;
}
double sum2(unsigned int N)
{
	double a,b,s=1;
	int k,j; 
	
	for(int i=1;i<N;i++)
	{
		j=i-1;
		k=2*i-1;
		a=pow(4.0,j);
		b=pow(3.0,k);
		s+=a/b;
		
	}
	return s;
	
}
 
 
int main()
{
	double a,b;
	unsigned int n;
	cout<<"输入学号后三位:"<<endl;
	cin>>n;
	
	sum1(a,n);
	b=sum2(n);
	cout<<"输出a为"<<a<<endl;
	cout<<"输出b为"<<b<<endl;
	return 0;
}

输入学号后三位
28
输出a为9449.44
输出b为1.6

#include<iostream>
using namespace std;
class rect
{
	private:
		int length;
		int width;
		
	public:
		void Set(int len,int wid);
		int area();
};                                            //定义完类后面要记得加; 

void rect :: Set(int len,int wid)
{
	length=len;
	width=wid;
}

int area()
{
	return length*width;
}

int main()
{

	rect aa,bb;
	aa.Set(20,30);
	cout<<"面积:"<<aa.area()<<endl;
	bb.Set(40,50);
	cout<<"面积:"<<bb.area()<<endl;
}

22    9    C:\Users\Administrator\Desktop\未命名1.cpp    [Error] 'length' was not declared in this scope

22    16    C:\Users\Administrator\Desktop\未命名1.cpp    [Error] 'width' was not declared in this scope

局部变量、静态局部变量、全局变量

#include<iostream>
using namespace std;
void func();
int n=1;
int main()
{
	static int a;
	int b=-10;
	cout<<"a:"<<a<<"b:"<<b<<"n:"<<n<<endl;
	b+=4;
	func();
	cout<<"a:"<<a<<"b:"<<b<<"n:"<<n<<endl;
	n+=10;
	func();
	return 0;
}

void func()
{
	static int a=2;
	int b=5;
}

a:0b:-10n:1
a:0b:-6n:1


```cpp
#include<iostream>
using namespace std;
class Date {
public:
	void Set(int m, int d, int y);   // 传入 时间
	int dayMonth(int month);         // 计算某个月的天数
	int Judge_date();				// 判断日期是否合法
	void Print();					// 打印当前日期
	void Nextday();					// 打印明天日期
private :
	int day, month, year;
};


#include "date.h"
void Date::Set(int d, int m, int y) {    //置日期值
    month = m; day = d; year = y;
}

// 计算某个月份的天数
int Date::dayMonth(int month) {
    if (month == 4 || month == 5 || month == 7 || month == 9 || month == 11) {
        return 30;
    }
    else if (month == 2) // 如果是2月 分 闰年、非闰年讨论
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            return 29;    // 闰年 29 天
        }
        else
            return 28;    // 非 闰年 29 天
    }
    else {
        return 31;         // 1、2、3、6、8、10、12 返回31天
    }
}
int  Date::Judge_date() {               //判日期是否合法
    if (year >= 1 && (month > 0 || month < 13)) {   // 判断年和月合法之后,我们来判断日与月是否合法
        int Emonth_day = dayMonth(month);
        if (day<1 || day > Emonth_day) {
            cout << "日期有误!";
            return 0; // 有误返回0
        }
        else
            return 1;
    }
}




void Date::Print() {                   //输出日期值
    cout << "当前日期为:" << day << "/" << month << "/" << year << endl;

}


void Date::Nextday() {
    //if (month == 12 && day == 31) 
    //{ year++; 
    //month = 1;
    //}
    day++;
    if (day > dayMonth(month))              // 如果大于当前日期的月,则指向下一个月的第一天(因为输出的是明天)
    {
        if (month == 12) {
            year++;
            month = 1;     // 如果比12天的最大天数还大,则跳转下月
        }
        else
            month++;
        day = 1;
    }
    cout << "明天日期为:" << day << "/" << month << "/" << year << endl;
}



```cpp
#include<iostream>
#include"date.h"
using namespace std;
//---------------------




int main() 
{
    Date time_1;
    int day, month, year;
    cout << "请输入日期(日 月 年)" << endl ;
    cin >> day >> month >> year;    // 输入时间
    time_1.Set(day, month, year);  // 通过调用类函数传入私有数据
    if (time_1.Judge_date() == 1)  // 如果返回1则输出 否则返回0说明日期有误不输出
    {
        time_1.Print();
        time_1.Nextday();
    }
       
    //  a.month;     //错误!month是私有成员,不能通过对象访问
}//--------------------

#include<iostream>
using namespace std;
void func();
int n=1;
int main()
{
	static int a;
	int b=-10;
	cout<<"a:"<<a<<"b:"<<b<<"n:"<<n<<endl;
	b+=4;
	func();
	cout<<"a:"<<a<<"b:"<<b<<"n:"<<n<<endl;
	n+=10;
	func();
	return 0;
}

void func()
{
	static int a=2;
	int b=5;
}

#include<iostream>
using namespace std;
class Point
{
	int x,y;
	
	public:
		Point(int ux=0,int uy=0)
		{
			x=ux;
			y=uy;
		}
		int getx()
		{
			return x;
		}
		int gety()
		{
			return y;
		}
};

class Circle
{
	private:
		Point p;
		int r;
	public:
		float area()
		{
			return 3.1415*r*r;
		}
};

int main()
{
	Circle a(50,50,10),b;
	cout<<"A面积"<<a.area()<<endl;
	cout<<"B面积"<<b.area()<<endl;
	b=a;
	cout<<endl;
	cout<<"B面积"<<b.area()<<endl;
}
#include<iostream>
using namespace std;
class rect
{
	private:
		int length;
		int width;
		
	public:
		void Set(int len,int wid);
		int area();
};                                            //定义完类后面要记得加; 

void rect :: Set(int len,int wid)
{
	length=len;
	width=wid;
}

int area()
{
	return length*width;
}

int main()
{

	rect aa,bb;
	aa.Set(20,30);
	cout<<"面积:"<<aa.area()<<endl;
	bb.Set(40,50);
	cout<<"面积:"<<bb.area()<<endl;
}

//声明一个Circle类,所有成员函数都在类体内定义。
//1)有int类型的数据成员radius(半径)。
//2)有1个构造函数,且为有1个参数(参数默认值为1)的构造函数,将参数赋给半径数据成员。
//3)有成员函数double area(),用于计算并返回圆的面积。pi的值使用3.14。
#include <iostream>
#include <iomanip>
using namespace std;

#define pi 3.14

class Circle
{
private:
	int radius;
	
	
public:
    Circle(int r=1)
    {
        radius=r;
    }
    
    
    double area()
    {
        return pi*radius*radius;
    }
};

int main()
{
   Circle c1;
   cout << setiosflags(ios::fixed) << setprecision(2) << c1.area() << endl;
   //输出要求小数点后必须保留2位有效数字(四舍五入),不足补零

   int a;
   cout<<"输入半径"<<endl; 
   cin >> a;
   Circle c2(a);
   cout << setiosflags(ios::fixed) << setprecision(2) << c2.area() << endl;
   return 0;
}
//设计一个立方体类Box
//它能计算并输出立方体的体积和表面积。要求:
//(1)包含成员变量m_a(立方体边长)。
//(2)包含函数SetA(double a)(设置立方体边长)、GetVolume()(计算体积)、GetArea()(计算表面积)。
//(3)包含函数Display(),用来输出计算的结果。
//(4)设计测试用主函数main(),用来测试Box类。


#include<iostream>
using namespace std;

class Box 
{
public:
	//SetA(double a)(设置立方体边长)
	void SetA(double a) 
	{
		m_a = a;
	}

	//GetVolume()(计算体积)
	double GetVolume() 
	{
		return m_a * m_a * m_a;
	}

	//GetArea()(计算表面积)
	double GetArea()
	 {
		return m_a * m_a * 6;
	}

	//函数Display(),用来输出计算的结果。
	void Display() 
	{
		cout << "体积为: " << GetVolume() << endl;
		cout << "表面积为: " << GetArea() << endl;
	}


private: 
	double m_a;  //成员变量m_a 
};


int main() 
{
	Box box;
	
	box.SetA(2.5);  //将box的边长设置成2.5
	
	box.Display();  //展示体积和表面积的计算结果
	return 0;
}
//设计一个Rectangle类。要求:
//(1)包含两个成员变量m_length和m_width,其默认值为1。
//(2)包含成员函数Perimeter()计算长方形的周长,Area()计算长方形面积。
//(3)包含成员函数SetWidth()和GetWidth()用来设置和得到m_width的值
//SetLength()和GetLength()用来设置和得到m_length的值。
//Set_()函数应验证m_length和m_width均为0.0到20.0之间的浮点数。
//(4)编写主函数,测试Rectangle类。

#include<iostream>
using namespace std;

class Rectangle 
{
public:
	//Perimeter()计算长方形的周长
	double Perimeter() { return 2 * (m_length + m_width); }

	//Area()计算长方形面积
	double Area() { return m_length * m_width; }

	//SetWidth()和GetWidth()用来设置和得到m_width的值
	void SetWidth(double w) { m_width = w; }
	double GetWidth() { return m_width; }

	//SetLength()和GetLength()用来设置和得到m_length的值
	void SetLength(double l) { m_length = l; }
	double GetLength() { return m_length; }

	//Set_()函数应验证m_length和m_width均为0.0到20.0之间的浮点数。
	bool Set_() 
	{
		bool n = 1;
		if (0 > m_length || 20 < m_length)
		 {
			cout << "length is not in 0.0 - 20.0" << endl;
			n = 0;
		}
		if (0 > m_width || 20 < m_width)
		 {
			cout << "width is not in 0.0 - 20.0" << endl;
			n = 0;
		}
		if (n)
			cout << "length and width are both in 0.0 - 20.0" << endl;
		
	}
private:
	//两个成员变量m_length和m_width,其默认值为1
	double m_length = 1;
	double m_width = 1;
};

int main() 
{
	Rectangle rec;
	cout << "Length : " << rec.GetLength() << "    " << "Width : " << rec.GetWidth() << endl;
	rec.Set_();
	cout << endl;

	rec.SetLength(5.2);
	rec.SetWidth(4);
	cout << "Length : " << rec.GetLength() << "    " << "Width : " << rec.GetWidth() << endl;
	rec.Set_();
	cout << endl;

	rec.SetWidth(25);
	cout << "Length : " << rec.GetLength() << "    " << "Width : " << rec.GetWidth() << endl;
	rec.Set_();
	cout << endl ;

	return 0;
}

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


class Point
{
public:
	Point(int a=0,int b=0)
	{
		x=a;
		y=b;
	}

	void input()
	{
		cin>>x>>y;
	}
	
	double distance(Point &a)
	{
		double dx=x-a.x,dy=y-a.y;
		return sqrt(dx*dx+dy*dy);
	}
	
	void Rotate()
   	{
   		double xx,yy;                               //旋转平面中的点 
			xx= x*cos(45) - y*sin(45) ;
            yy= x*sin(45) + y*cos(45) ;
		cout<<"("<<xx<<"),("<<yy<<")"<<endl;
	}
			 
   
private:
	int x,y;
};                 //注意不要忘记这个;!!! 


class Triangle
{
public:
	Triangle(int x1,int y1,int x2,int y2,int x3,int y3)
	{
		P1=(x1,y1);
		P2=(x2,y2);
		P3=(x3,y3);
	}
	Triangle(Point &a,Point &b,Point &c)
	{
		P1=a;
		P2=b;
		P3=c;
	}
	
	double GetPerimeter()
	{
		return P1.distance(P2)+P2.distance(P3)+P3.distance(P1);
	}
	double GetArea()
	{
		double s=GetPerimeter()/2.0;
		return sqrt(s*(s-P1.distance(P2))*(s-P2.distance(P3))*(s-P3.distance(P1)));          //三角形面积计算方法 
	}
	void output()
	{	
		cout<<endl<<"三角形周长为:"<<GetPerimeter()<<",面积为:"<<GetArea();
	}
   
private:
	Point P1,P2,P3;
};


bool ifTriangle(Point &p1,Point &p2,Point &p3)
{	
	here:
	if((p1.distance(p2)+p2.distance(p3)>p3.distance(p1))&& (p1.distance(p2) + p3.distance(p1) > p2.distance(p3)) && (p2.distance(p3) + p3.distance(p1) > p1.distance(p2)))
	{	
		
		cout<<endl<<"旋转后的的三角形坐标为 "<<endl;
		p1.Rotate();
		p2.Rotate();
		p3.Rotate();
		
		
		Triangle T(p1,p2,p3);
		T.output();
		
	}
	else
	{
		cout << "不能构成三角形!重新输入坐标!" << endl;
		return 0;
	}
	
}


int main()
{
	Point A,B,C;
	here:
	A.input();
	B.input();
	C.input();
	if (ifTriangle(A, B, C) == 0)                //如果输入不能构成三角形,需要重新输入 
	{
		goto here;
	}
	
	return 0;
}

平面中点绕一个点旋转后得到的新点

假设对图片上任意点(x,y),绕一个坐标点(rx0,ry0)逆时针旋转a角度后的新的坐标设为(x0, y0),有公式:

    x0= (x - rx0)*cos(a) - (y - ry0)*sin(a) + rx0 ;

    y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ;

三角形面积公式

#include<iostream>
using namespace std;
class Point
{
	public:
		void initPoint(float x=0,float y=0)
		{
			this->x=x;
			this->y=y;
			
		}
		
		void move(float offx,float offy)
		{
			x+=offx;
			y+=offy;
		}
		float getx()const
		{
			return x;
		}
		float gety()const
		{
			return y;
		}
		
		private:
			float x
};,y;

class Rectangle:public Point
{
	public:
		void initRectangle(float x,float y,float w,float h)
		{
			initPoint(x,y);
			this->w=w;
			this->h=h;
			
		}
		
			float getx()const
		{
			return x;
		}
		float gety()const
		{
			return y;
		}
		
		
		float geth()const
		{
			return h;
		}
		float getw()const
		{
			return w;
		}
		
		private:
			float w,h;	
};

int main()
{
	Rectangle R1;
	float x1,y1,w1,h1;

	cout<<"左下角坐标为"<<endl;
	cin>>x1>>y1;
	cout<<"三角形的宽和长为"<<endl;
	cin>>w1>>h1;
	R1.initRectangle(x1,y1,w1,h1);
	return 0;
}
#include<iostream>
using namespace std;
class Line 
{
	public:
		void setLength(int l)
		{
			length =l;
		}
	protected:
	     int length;
};

class Square:public Line
{
	public:
		int getArea()
		{
			return(length*length);
		}
		
	
		int getPerimeter()
		{
			return(length*4);
		}
		
		
};

class Cube:public Square
{
	public:
		int getSArea(int area)
		{
			return(6*area);
		}
		int getVolume()
		{
			return(length*length*length);
		}
};

int main()
{  
   Square Squ;
   int area;
   Cube Cub;
   int volume;
 
   Squ.setLength(5);
   Cub.setLength(5);
   area=Squ.getArea();
   cout << "正方形面积为: " <<Squ.getArea() << endl;
   cout << "正方形周长为: " <<Squ.getPerimeter() << endl;
   cout << "正方体表面积为: " <<Cub.getSArea(area) << endl;
   cout << "正方体体积为: " << Cub.getVolume() << endl;
   
   
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值