- 全部实验报告
E01. 编写程序计算用户输入的3个整数的平均值、和、最大值和最小值,并输出到屏幕上。输出格式要求平均值占 8列,保留4位小数,左对齐;最大值和最小值占 6列,右对齐。//author CN.TTDragon #include<bits/stdc++.h> typedef long long ll; const ll mod=1e9+7; const ll maxn=1e5+7; const double pi=acos(-1); using namespace std; int main() { int a[3];//用数组来存,简单sort一下就可以避免老是去max,min了 cin>>a[0]>>a[1]>>a[2]; sort(a,a+3); cout<<setw(8)<<left<<fixed<<setprecision(4)<<(double(a[0]+a[1]+a[2])/3)<<endl; cout<<setw(8)<<left<<a[0]+a[1]+a[2]<<endl; cout<<setw(6)<<a[2]<<endl; cout<<setw(6)<<a[0]<<endl; return 0; }
E02. 从键盘输入一个字母,如果输入的是小写字母,请将其转换为大写字母输出,如果输入的是大写字母,请将其转换为小写字母输出,其余情况原样输出,请编写程序实现。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
char n;
cin>>n;
if(n<123&&n>96)//借助ASCII码值来判断字母的大小写
{
cout<<char(n-32)<<endl;
}
else if(n<91&&n>64)
{
cout<<char(n+32)<<endl;
}
else
{
cout<<char (n)<<endl;
}
return 0;
}
E03. 已知2017年1月1日是星期日,输入2017年的一个月和日,输出该天是星期几。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int a,b;
cin>>a>>b;
int tot=b;//这里就已经假定了1月1号是1了
for(int i=0;i<a;i++)
{
tot+=m[i];
}
cout<<"星期"<<(tot-1)%7<<endl;
return 0;
}
E04. 一只大象口渴了,要喝20升水才能解渴,但现在只有一个深h厘米,底面半径为r厘米的小圆桶(h和r都是整数)。问大象至少要喝多少桶水才会解渴。(注:小圆桶的深h和底面半径r请用户输入,单位默认是厘米。)
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int h,r;
cin>>h>>r;
double v;
v=pi*r*r*h;
cout<<int(20000/v)+1<<endl;//注意单位换算一下就好
return 0;
}E05. 在苏大校园里,没有自行车,上课办事会很不方便.但实际上,并非去办任何事情都是骑车快,因为骑车总要找车、开锁、停车、锁车等,这要耽误一些时间。假设找到自行车、开锁并骑上自行车的时间为27秒;停车锁车的时间为23秒;步行每秒行走1.2米,骑车每秒行走3.0米。请判断走不同的距离去办事,是骑车快还是走路快。程序的输入是一个整数表示距离,单位是米,如果计算结果是步行快,请输出“步行快”,如果计算结果是骑车快,请输出“骑车快”,否则输出“一样快”。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int s;
cin>>s;
double t1,t2;//比较时间就好,也可以反算距离
t1=double(s)/1.2;
t2=double(s)/3.0+50;
if(t1>t2)
{
cout<<"骑车快"<<endl;
}
else if(t1==t2)
{
cout<<"一样快"<<endl;
}
else
{
cout<<"步行快"<<endl;
}
return 0;
}
E06. 编写程序打印出ASCII码从32-126的ASCII字符(每行打印5个)。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int cnt=0;
for (int i=32;i<=126;i++)
{
cout<<char(i)<<" ";
cnt++;
if(cnt%5==0)
{
cout<<endl;
cnt=0;
}
}
return 0;
}
E07. 求Sn = a + aa + ... + aa...a的值。其中a是一个数字。a和n都是由键盘输入。例如:
求 S = 2 + 22 + 222 + 2222 + 22222 + 222222,那么a = 2且n = 6。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int a,n;
cin>>a>>n;
int tot=0;
for(int i=1;i<=n;i++)
{
tot+=i*pow(10,n-i)*a;//这个数作为个十百千……出现了几次
}
cout<<tot<<endl;
return 0;
}
E08. 程序员小明年薪N万,他希望在天赐庄买一套55平米的房子,现在价格是200万,假设房子价格以每年百分之k增长,并且该程序员未来年薪不变,且不吃不喝,不用交税,每年所得N万全都积攒起来,问第几年能够买下这套房子?(N和k请用户输入,N(10 <= N <= 50), K(1 <= K <= 20)),如果在第20年之前(含20年)就能买下这套房子,则输出一个整数M,表示最早需要在第M年能买下,否则输出Impossible。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int m,k;
cin>>m>>k;
double price=200;
for(int i=1;i<=20;i++)
{
if(i*m>price)
{
cout<<i<<endl;
return 0;
}
price*=double(100+k)/100.0;
}
cout<<"Impossible"<<endl;
return 0;
}
E09. 公元前5世纪,我国数学家张丘建在《算经》中提出百钱百鸡问题:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,百钱买百鸡,问鸡翁、母、雏各几何?
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
for(int i=0;i<=20;i++)//枚举
{
for(int j=0;j<=34;j++)
{
for(int k=0;k<=100;k+=3)
{
if(i*5+j*3+k/3==100&&i+j+k==100)
{
cout<<"鸡翁:"<<i<<" 鸡母:"<<j<<" 鸡雏:"<<k<<endl;
}
}
}
}
return 0;
}
E10. 校长将粉笔作为工资,发放给认真的教师。第一天,教师收到一支粉笔;之后两天(第二天和第三天)里,每天收到两支粉笔;之后三天(第四、五、六天)里,每天收到三支粉笔;之后四天(第七、八、九、十天)里,每天收到四支粉笔……这种工资发放模式会一直这样延续下去:当连续N天每天收到N支粉笔后,教师会在之后的连续N+1天里,每天收到N+1支粉笔(N为任意正整数)。
请编写一个程序,确定从第一天开始的给定n天数内(n<500),教师一共获得了多少粉笔。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
int tot=0;
for(int i=1;i<=10000;i++)
{
if(n-i>=0)
{
tot+=i*i;
n-=i;
}
else
{
tot+=i*n;
break;
}
}
cout<<tot<<endl;
return 0;
}E11. 求1000以内的所有质数,每行 8个输出到屏幕上(不要用筛法)。写一个判断一个整数是否是素数的函数。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
bool isprime(int n)
{
if(n<2)
{
return false;
}
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
int main()
{
ios::sync_with_stdio(false);
int cnt=0;
for(int i=1;i<=1000;i++)
{
if(isprime(i))
{
cout<<i<<" ";
cnt++;
if(cnt%8==0)
{
cout<<endl;
cnt=0;
}
}
}
return 0;
}
E12. 编写一个函数,将一个整数的各位数字对调,并编写测试程序,在测试函数中输入整数和输出新的整数。例如:输入 123,调用该函数之后,得到结果为 321。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
void cha(int &n)
{
int a[10]={0};
int cnt=0;
while(n)
{
a[cnt++]=n%10;
n/=10;
}
for(int i=0;i<cnt;i++)
{
n+=pow(10,cnt-i-1)*a[i];
}
}
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
cha(n);//我也忘了对于100是要返回001还是1了,这里返回的是1
cout<<n<<endl;
return 0;
}
E13. 设计一个函数calc,完成如下功能。
1) 接受两个整数作为参数。
2) 计算出最大公约数和最小公倍数。(不要在calc函数中使用输入输出流)
3) 可以在函数参数中使用引用。
4) 编写主函数,测试函数功能。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
void calc(int &a,int &b)
{
int temp=a*b;
a=__gcd(a,b);//偷懒~~
b=temp/a;
}
int main()
{
ios::sync_with_stdio(false);
int a,b;
cin>>a>>b;
calc(a,b);
cout<<"最大公约数:"<<a<<" 最小公倍数:"<<b<<endl;
return 0;
}
E14. 编写三个重载函数printOut分别实现输出整型数、浮点型数和字符,输出要求如下:
1) 整型数:占10列,右对齐
2) 浮点型:有4位小数
3) 字符:占3列,右对齐
4) 编写测试程序,测试程序中的整数、浮点数和字符串由用户输入。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
void printOut(int n)
{
cout<<setw(10)<<right<<n<<endl;
}
void printOut(double n)
{
cout<<fixed<<setprecision(4)<<n<<endl;
}
void printOut(char n)
{
cout<<setw(3)<<right<<n<<endl;
}
int main()
{
ios::sync_with_stdio(false);
printOut(123);
printOut('a');
printOut(123.5);
return 0;
}
E15. 从键盘输入10个整数初始化一个包含10个元素的数组,请输出该数组元素的最大值、最小值和平均值。输出形式为:最大值 最小值 平均值(保留3位小数),三个数据之间用空格分隔。
例如: 数组为:1,2,3,4,5,6,7,8,9,12
输出结果:12 1 5.700
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int a[10];
int sum=0;
for(int i=0;i<10;i++)
{
cin>>a[i];
sum+=a[i];
}
sort(a,a+10);
cout<<a[9]<<" "<<a[0]<<" "<<fixed<<setprecision(3)<<double(sum)/10<<endl;
return 0;
}
E16. 从键盘输入一行英文字母,先将所有字母转换成小写字母,然后统计26个英文字母的出现次数。最后按出现次数从大到小对字母排序,然后输出字母及其出现次数,没有出现的字母不输出。输出形式为如下,每个字母及其出现次数占一行:
例如: 英文字母: aabbaadabbdddb
输出结果:
a 5
b 5
d 4
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
pair<char, int> p[26];
int main()//写的稀烂
{
ios::sync_with_stdio(false);
for(int i=0;i<26;i++)
{
p[i].first=char (97+i);
}
string s;
cin>>s;
transform(s.begin(), s.end(), s.begin(), ::tolower);
for(int i=0;i<s.length();i++)
{
p[s[i]-'a'].second++;
}
for(int i=0;i<26;i++)
{
for(int j=0;j<25;j++)
{
if(p[j].second>p[j+1].second)
{
char t=p[j].first;
int te=p[j].second;
p[j].first=p[j+1].first;
p[j].second=p[j+1].second;
p[j+1].first=t;
p[j+1].second=te;
}
}
}
for(int i=0;i<26;i++)
{
for(int j=0;j<25;j++)
{
if(p[j].second==p[j+1].second)
{
if(p[j].first<p[j+1].first)
{
char t=p[j].first;
int te=p[j].second;
p[j].first=p[j+1].first;
p[j].second=p[j+1].second;
p[j+1].first=t;
p[j+1].second=te;
}
}
}
}
for(int i=25;i>=0;i--)
{
if(p[i].second!=0)
cout<<p[i].first<<" "<<p[i].second<<endl;
}
return 0;
}
E17. 用手机发短信,一条短信资费为0.1元,但限定一条短信的内容在70个字以内(包括70个字)。如果你一次所发送的短信超过了70个字,则会按照每70个字一条短信的限制把它分割成多条短信发送。假设如下数组存储了一个人一个月发送的每个短信的的分别字数
int arry [12]={35,146,57,13,224,35,99,68,113,79,88,46},
试统计一下他当月短信的总资费。将总费用输出到屏幕,保留2位小数,不需要任何提示信息。例如:23.45
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int arry [12]={35,146,57,13,224,35,99,68,113,79,88,46};
double ans=0;
for(int i=0;i<12;i++)
{
if(arry[i]%70==0)
{
ans+=arry[i]/70*0.1;
}
else
{
ans+=((arry[i]/70)+1)*0.1;
}
}
cout<<fixed<<setprecision(2)<<ans<<endl;
return 0;
}
E18. 已知数组 int arry [10]={13,24,35,46,57, 79, 93},请将用户输入的一个数字按照从小到大的顺序插在对应的位置后输出整个数组,每个元素占4列,右对齐。例如:输入的数字为20,则输出为:
13 20 24 35 46 57 79 93
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int arry [10]={0,13,24,35,46,57,79,93};
int n;
int f=0;
cin>>n;
for(int i=0;i<10;i++)
{
if(arry[i]<=n&&arry[i+1]>=n)
{
for(int j=9;j>=i+1;j--)
{
arry[j+1]=arry[j];
}
arry[i+1]=n;
f=1;
break;
}
}
if(!f)
{
arry[8]=n;
}
for(int i=01;i<=8;i++)
{
cout<<arry[i]<<" ";
}
cout<<endl;
return 0;
}
E19. 先从键盘输入一个小于100的正整数n,然后再从键盘输入n干个小于10000的整数,存储到一个向量中,对该向量的所有元素按逆序重新存放。输出逆序后的向量元素,元素之间用空格分隔。例如:
输入整数n: 5
输入5个整数为:8 6 5 4 1
则输出为:1 4 5 6 8
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
vector<int> a;
int main()
{
ios::sync_with_stdio(false);
int n,b;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>b;
a.push_back(b);
}
for(size_t i=n-1;i<n;i--)//这个判断条件有点独特!
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
E20. 编写程序对12个月的英文单词根据字典序进行从小到大的排序,请使用string向量实现,将排序后的英文单词输出到屏幕,每个单词占一行。说明:12个月份的单词直接在程序中按1~12月份的顺序给出,要求字母全部为小写,并且自己实现排序算法。
//author CN.TTDragon
#include<bits/stdc++.h>
using namespace std;
vector<string> vec_month(12);
vector<string>::iterator it;
int main()
{
vec_month.push_back("january");
vec_month.push_back("february");
vec_month.push_back("march");
vec_month.push_back("april");
vec_month.push_back("may");
vec_month.push_back("june");
vec_month.push_back("july");
vec_month.push_back("august");
vec_month.push_back("september");
vec_month.push_back("october");
vec_month.push_back("november");
vec_month.push_back("december");
for (it=vec_month.begin(); it!=vec_month.end(); ++it)
{
vector<string>::iterator it2=it+1;
for (; it2 != vec_month.end(); ++it2)
{
vector<string>::iterator it3=it2-1;
if (*(it3)>*(it2))
{
string s=*it3;
*it3 = *it2;
*it2=s;
}
}
}
vector<string>::iterator ou;
for(ou=vec_month.begin()+12;ou!=vec_month.end();ou++)
{
cout<<*ou;
cout<<" ";
}
cout<<endl;
return 0;
}
E22[第8章:类的基本操作]设计一个类Circle,表示圆形。
Circle类以圆心坐标(x,y)和半径r来确定圆。
Circle类具有如下方法:
i. 可设置圆心坐标。
ii. 可设置半径。
iii. 可计算圆的面积。
iv. 可计算圆的周长。
其中pi取3.14159.
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
class CCircle//最基本的类
{
private:
double x,y,r;
public:
void set_xy(double mx,double my)
{
x=mx;
y=my;
}
void set_r(double mr)
{
r=mr;
}
double get_circlum()
{
return 2*pi*r;
}
double get_area()
{
return pi*r*r;
}
};
int main()
{
CCircle c1;
double x,y,r1,r2;
cout<<"输入圆心坐标和半径"<<endl;
cin>>x>>y>>r1;
c1.set_xy(x,y);
c1.set_r(r1);
cout<<"圆周长和面积: "<<fixed<<setprecision(3)<<c1.get_circlum()<<" "<<c1.get_area()<<endl;
cout<<"输入新的半径:"<<endl;
cin>>r2;
c1.set_r(r2);
cout<<"圆周长和面积: "<<fixed<<setprecision(3)<<c1.get_circlum()<<" "<<c1.get_area()<<endl;
return 0;
}E23 [第8章:类的基本操作]设计一个类Column,表示圆柱体。设圆柱体底面在Z=0的平面内。
Column类以底面圆(参见E22题)和高来确定圆柱体。
Column类具有如下方法:
i. 可设置底面圆心。
ii. 可设置底面半径。
iii. 可设置高。
iv. 可计算底面积。
v. 可计算底面的周长。
vi. 可计算侧面积。
vii. 可计算体积。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
class CColumn//依旧基本
{
private:
double x,y,r,h;
public:
void SetXY(double mx,double my)
{
x=mx;
y=my;
}
void SetR(double mr)
{
r=mr;
}
void SetH(double mh)
{
h=mh;
}
double GetBottomCirclum()
{
return 2*pi*r;
}
double GetBottomArea()
{
return pi*r*r;
}
double GetSideArea()
{
return GetBottomCirclum()*h;
}
double GetVolume()
{
return GetBottomArea()*h;
}
};
using namespace std;
int main()
{
CColumn c1;
double x,y,r1,r2,h1,h2;
cout<<"输入圆心坐标、半径和高"<<endl;
cin>>x>>y>>r1>>h1;
c1.SetXY(x,y);
c1.SetR(r1);
c1.SetH(h1);
cout<<"圆柱的底面周长、底面积、侧面积和体积:"<<endl;
cout<<fixed<<setprecision(3)<<c1.GetBottomCirclum()<<" "
<<c1.GetBottomArea()<<" "
<<c1.GetSideArea()<<" "
<<c1.GetVolume()<<endl;
cout<<"输入底圆半径和高:"<<endl;
cin>>r2>>h2;
c1.SetR(r2);
c1.SetH(h2);
cout<<"圆柱的底面周长、底面积、侧面积和体积:"<<endl;
cout<<fixed<<setprecision(3)<<c1.GetBottomCirclum()<<" "
<<c1.GetBottomArea()<<" "
<<c1.GetSideArea()<<" "
<<c1.GetVolume()<<endl;
//system("pause");
return 0;
}
E24 [第8章:类的基本操作,运算符重载]在E22圆形类的基础上,完成如下功能:
定义加法运算,规则:两圆之和为一个新的圆,圆心是第一个操作数的圆心(如a+b,则a的圆心为a+b的圆心),半径为两圆半径之和。加法运算不改变操作数。
定义减法运算,规则:两圆之差为一个新的圆,圆心是第一个操作数的圆心,面积为两圆面积之差的绝对值。减法运算不改变操作数。
定义自增、自减运算(含前/后自增/自减),对半径进行自增、自减运算。
定义输出流运算,输出圆心坐标、半径、周长、面积,保留3位小数,右对齐。输出样例如下:(圆心坐标 半径 周长 面积)
(1.000,1.000) 3.000 18.850 28.274
定义>、<运算,比较两圆面积之间的大小关系。
定义==、!=运算,比较两圆是否是完全相同的圆,当两个圆的圆心坐标和半径完全一致时则两圆完全相同。
定义&运算,确定两圆是否同心。
定义|运算,确定两圆的位置关系(1表示相交、2表示相切、3表示相离、0表示包含)。
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
const double expp=1e-6;
using namespace std;
class CCircle
{
private:
double x,y,r;
public:
void set_xy(double mx,double my)
{
x=mx;
y=my;
}
void set_r(double mr)
{
r=mr;
}
double get_circlum()
{
return 2*pi*r;
}
double get_area()
{
return pi*r*r;
}
CCircle operator +(CCircle & obj)
{
CCircle temp;
temp.set_xy(x,y);
temp.set_r(r+obj.r);
return (temp);
}
friend CCircle operator -(CCircle & obj1,CCircle &obj2)
{
double temp=obj1.get_area()-obj2.get_area();
//cout<<"1:"<<obj1.get_area()<<endl;
//cout<<"2:"<<obj2.get_area()<<endl;
temp=abs(temp);
obj1.set_r(sqrt(temp/pi));
return (obj1);
}
CCircle & operator ++()
{
r++;
return (*this);
}
CCircle operator ++( int b)
{
CCircle temp(*this );
r++;
return (temp);
}
CCircle & operator --()
{
r--;
return (*this);
}
CCircle operator --( int b)
{
CCircle temp(*this );
r--;
return (temp);
}
friend ostream& operator<<(ostream& o, const CCircle& yuan)
{
o<<setfill('0')<<fixed<<setiosflags(ios::right)<<setprecision(3)<<"("<<yuan.x<<","<<yuan.y<<") ";
o<<yuan.r<<" "<<2*pi*yuan.r<<" "<<pi*yuan.r*yuan.r;
return o;
}
bool operator >(CCircle &obj)
{
if(get_area()>obj.get_area())
{
return true;
}
return false ;
}
bool operator <(CCircle &obj)
{
if(get_area()<obj.get_area())
{
return true;
}
return false ;
}
bool operator ==(CCircle &obj)
{
if(x==obj.x&&y==obj.y&&r==obj.r)
{
return true;
}
return false ;
}
bool operator !=(CCircle &obj)
{
if(x!=obj.x||y!=obj.y||r!=obj.r)
{
return true;
}
return false ;
}
bool operator &(CCircle &obj)
{
if(x==obj.x&&y==obj.y)
{
return true;
}
return false ;
}
int operator | (CCircle & obj)//这里比较复杂
{
//相交代表距离小于半径和
double d=sqrt((obj.x-x)*(obj.x-x)+(obj.y-y)*(obj.y-y));//圆心距
//1表示相交、2表示相切、3表示相离、0表示包含
//cout<<"圆心距:"<<d<<"半径和:"<<rr<<endl;
if(obj.r+d<r)
{
return 0;
}
if(d>r+obj.r)
{
return 3;
}
if(d==r+obj.r)
{
return 2;
}
if(d<r+obj.r&&d!=0)
{
return 1;
}
return -1;
}
};
int main()
{
CCircle c1,c2,c3,c4;
double x1,y1,x2,y2,r1,r2;
int istate;
cout<<"请输入圆心坐标和半径: "<<endl;
cin>>x1>>y1>>r1;
c1.set_xy(x1,y1);
c1.set_r(r1);
cout<<c1<<endl;
cout<<"请输入圆心坐标和半径: "<<endl;
cin>>x2>>y2>>r2;
c2.set_xy(x2,y2);
c2.set_r(r2);
cout<<c2<<endl;
c3=c1+c2;
cout<<" 两圆相加: "<<c3<<endl;
c4=c3-c2;
cout<<" 两圆相减: "<<c4<<endl;
++c4;
cout<<" C4前自增: "<<c4<<endl;
cout<<" C4后自增: "<<c4++<<" C4自增后: "<<c4<<endl;
--c4;
cout<<" C4前自减: "<<c4<<endl;
cout<<" C4后自减: "<<c4--<<" C4自减后: "<<c4<<endl;
cout<<"c1: "<<c1<<" c2: "<<c2<<endl;
if (c1>c2)
cout<<"c1>c2"<<endl;
else if (c1<c2)
cout<<"c1<c2"<<endl;
else
cout<<"c1<>c2"<<endl; // 表示无比较结果
cout<<"c1: "<<c1<<" c4: "<<c4<<endl;
if (c1==c4)
cout<<"c1==c4"<<endl;
else if (c1 != c4)
cout<<"c1!=c4"<<endl;
else
cout<<"c1<>c4"<<endl; // 表示无比较结果
if (c1&c4)
cout<<"c1与c4同心"<<endl;
istate = c1|c2;
switch(istate)
{
case 0:
cout<<"c1包含c2"<<endl;
break;
case 1:
cout<<"c1与c2相交"<<endl;
break;
case 2:
cout<<"c1与c2相切"<<endl;
break;
case 3:
cout<<"c1与c2相离"<<endl;
break;
default:
cout<<"c1与c2位置判断错误"<<endl;
}
istate = c3|c1;
switch(istate)
{
case 0:
cout<<"c3包含c1"<<endl;
break;
case 1:
cout<<"c3与c1相交"<<endl;
break;
case 2:
cout<<"c3与c1相切"<<endl;
break;
case 3:
cout<<"c3与c1相离"<<endl;
default:
cout<<"c3与c1位置判断错误"<<endl;
}
return 0;
}E25 [第8章:运算符重载,友元函数]设有描述复数的类CComplex定义如下:
重载<<运算符,使得可以用cout<<输出复数,每个复数输出的格式为:
“实部+虚部*i”;
重载+运算符,使得可以实现两个复数相加;
重载+运算符,使得可以实现复数和实数的相加;
重载前置++运算符,使得可以实现复数的实部和虚部分别加1;
重载后置++运算符,使得可以实现复数的实部和虚部分别加1;
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
class CComplex
{
double m_real;
double m_image;
public:
void setValue(double real,double image); // 设置复数的实部和虚部
friend ostream &operator << (ostream & o , const CComplex & obj)
{
o<<obj.m_real<<"+"<<obj.m_image<<"i";
}
CComplex operator +(CComplex & obj)
{
CComplex temp;
temp.setValue(m_real+obj.m_real,obj.m_image+m_image);
return temp;
}
CComplex operator +(double b)
{
m_real=m_real+b;
return (*this);
}
friend CComplex &operator ++(CComplex &obj)
{
obj.m_real ++;
obj.m_image++;
return obj ;
}
friend CComplex operator ++(CComplex &obj,int b)
{
CComplex temp(obj);
obj.m_real ++;
obj.m_image++;
return temp;
}
};
void CComplex ::setValue(double real , double image)
{
m_real=real;
m_image=image;
}
int main()
{
double x1,y1,x2,y2;
CComplex c1,c2,c3;
cout<<"输入四个实数:"<<endl;
cin>>x1>>y1>>x2>>y2;
c1.setValue(x1,y1);
c2.setValue(x2,y2);
cout<<"复数c1: "<<c1<<endl;
cout<<"复数c2: "<<c2<<endl;
c3=c1+c2;
cout<<"c1+c2: "<<c3<<endl;
cout<<"c1+x2: "<<c1+x2<<endl;
cout<<"++c2: "<<++c2<<endl;
cout<<"c2++: "<<c2++<<endl;
cout<<"c2: "<<c2<<endl;
//system("pause");
return 0;
}
E26 [第8章:静态成员]现需要处理银行活期存款业务,设账户类为,CAount,请根据如下需求实现该类,并在 main函数中测试。
每个账户需要有一个浮点型的成员m_Money用于存储账上余额;
每个账户需要描述存款的日期;
银行的年利息采用浮点型静态数据成员m_InterestRate描述,从而避免为每个账户存储利息;
为年利息成员提供静态成员SetInterestRate进行设置;
为年利息成员提供静态成员GetInterestRate进行获取;
提供存款成员函数SaveMoney;
提供取款成员函数LendMoney;
提供结算利息函数SaveInterest,该函数将计算出的利息结算到本金中。
提供输出运算符重载函数,输出内容:存款日期,账户余额,当前利率。
示例:2014-1-1 10000 0.1
为简化计算,请考虑以下定义或限制:
(1).本题目不考虑闰年,每个月都认为30天,一年认为360天。
(2).存款仅考虑发生一次!
(3).取款允许发生多次,但取款是否允许需要考虑“本金是否足够”的条件
银行相关业务和利息的计算方法举例如下:
(1). 假设年利率m_InterestRate=0.036(表示 3.6%)。m_InterestRate是静态成员变量,按照静态成员变量的概念,对所有账户 CAount类的对象而言m_InterestRate只有 1个,这样才能实现一改全改的效果!
(2). 我于 2014-1-1到银行存了 100000元,m_Money=100000。
(3). 2014-3-10银行给我“结算利息”一次。2014-1-1到 2014-3-10之间一共间隔了70天,本金 m_Money=100000+1000000.036/36070=100700。“结算利息”以后,存款日期变为了 2014-3-10!
(4). 2014-3-30我到银行想取款 200000,由于本金 m_Money只有 100700,所以不允许取款!
(5). 2014-4-4我到银行想取款50000,由于本金有100700,所以允许取款。2014-3-10到2014-4-4之间一共间隔了 25天,本次取得的金额是50000+500000.036/36025=50125,本金m_Money=100700-50000=50700(利息不从我的账户中扣除,这是由银行提供给我的回报),存款日期仍然维持为2014-3-10
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
class CSystemTime
{
private:
int m_year, m_month, m_day;
public:
void setDate(int y,int m, int d)
{
m_year=y;
m_month=m;
m_day=d;
}
int getYear()const{return m_year;}
int getMonth()const{return m_month;}
int getDay()const{return m_day;}
};
CSystemTime systemTime; // 全局变量表示当前系统时间
class CAount
{
private:
double m_Money;
CSystemTime t;
static double m_InterestRate;
public:
void SetInterestRate(double ir)
{
m_InterestRate=ir;
}
double GetInterestRate()
{
return m_InterestRate;
}
void SaveMoney(double sm)
{
m_Money+=sm;
t.setDate(systemTime.getYear(),systemTime.getMonth(),systemTime.getDay());
}
double LendMoney(double lm)
{
if(m_Money>=lm)
{
m_Money-=lm;
///
double pay;
int d=(systemTime.getDay ()-t.getDay () +1 )+(systemTime.getMonth ()- t.getMonth () )*30+
( systemTime.getYear()-t.getYear () )*360;
pay=lm+lm*d*m_InterestRate/360;
///
return pay;
}
return -1;
}
friend ostream& operator<<(ostream& o, const CAount& m1)
{
o<<m1.t.getYear ()<<"-"<<m1.t.getMonth ()<<"-"<<m1.t.getDay()<<" "<<m1.m_Money<<" "<<m1.m_InterestRate ;
return o;
}
void SaveInterest()
{
int days=(systemTime.getDay ()-t.getDay () +1 )+(systemTime.getMonth ()- t.getMonth () )*30+(systemTime.getYear()-t.getYear () )*360;
double pay=m_Money*days*m_InterestRate/360;
m_Money+=pay;
t.setDate (systemTime.getYear (),systemTime.getMonth (),systemTime.getDay ());
}
};
double CAount::m_InterestRate =0.1;
int main()//这题最难了
{
CAount aount;
int year,month, day;
double money,istate;
cout<<"输入当前日期(年月日):"<<endl;
cin>>year>>month>>day;
systemTime.setDate(year,month,day);
cout<<"输入存款金额:"<<endl;
cin>>money;
aount.SaveMoney(money);
cout<<"当前账户信息:"<<endl;
cout<<aount<<endl;
cout<<"输入新的日期(年月日):"<<endl;
cin>>year>>month>>day;
systemTime.setDate(year,month,day);
aount.SaveInterest();
cout<<"当前账户信息:"<<endl;
cout<<aount<<endl;
cout<<"输入新的日期(年月日):"<<endl;
cin>>year>>month>>day;
systemTime.setDate(year,month,day);
aount.SetInterestRate(0.2);
cout<<"输入取款金额:"<<endl;
cin>>money;
istate=aount.LendMoney(money);
if (istate<0)
cout<<"账户本金不足"<<endl;
else
cout<<"取款: "<<istate<<endl;
cout<<"当前账户信息:"<<endl;
cout<<aount<<endl;
//system("pause");
return 0;
}
写在最后:实验七和八由于还未全部提交,暂不写出,实验五,鸽了鸽了

京公网安备 11010502036488号