实验一
来源:互联网 发布:战舰世界数据更新出错 编辑:程序博客网 时间:2024/04/29 01:32
实验一 C++的初步知识
班级: 学号: 姓名: 成绩:
一. 实验目的
1.掌握C++程序的输入输出;
2.掌握函数重载和带默认参数函数的使用;
3.掌握引用的定义与使用;
4.掌握字符串类string的使用。
二. 使用的设备和仪器
计算机+Windows XP +Visual C++6.0
三. 实验内容及要求
1、 编写程序,用同一函数名Girth和Area来分别计算圆、长方形、三角形的周长和面积。在主函数中调用它们完成相应的功能。
2、 求2个或3个正整数中的最大数,用带默认参数的函数实现。
3、 求3个变量按由小到大顺序排序,要求使用变量的引用。
4、 编写程序,要求用户首先输入英文名和英文姓;然后用空格将其组合起来,并输出。之后再对其进行逆序的输出。
运行示例:
5、 输入5个国家的英文名称,将它们按照由小到大的顺序输出,用string方法。
6、 使用函数模板分别实现对n个数据所组成的数组的输入、输出和排序。并在主函数中对整型、双精度型数据进行操作。
四. 实验步骤
1、 程序代码:#include<iostream>
2、 #include<math.h>
3、 using namespace std;
4、 const double PI=3.14;
5、 double Girth(int a)
6、 {
7、
8、 float p;
9、 p=2*a*PI;
10、 return p;
11、
12、
13、 }
14、 double area(int a)
15、 {
16、 double s;
17、
18、 s=PI*a*a;
19、 return s;
20、 }
21、 int Girth(int a,int b)
22、 {
23、 int p;
24、 p=2*(a+b);
25、 return p;
26、 }
27、 int area(int a,int b)
28、 {
29、 int s;
30、 s=a*b;
31、 return s;
32、
33、 }
34、 int Girth(int a,int b,int c)
35、 {
36、 int p;
37、 p=a+b+c;
38、 return p;
39、 }
40、 double area(int a,int b,int c)
41、 {
42、 double s,p;
43、 p=(a+b+c)/2;
44、 s=sqrt(p*(p-a)*(p-b)*(p-c));
45、
46、 return s;
47、 }
48、 int main()
49、 {
50、
51、 int a,b,c,d;
52、 cout<<"请输入圆的半径";
53、 cin>>a;
54、
55、 cout<<"周长:"<<Girth(a)<<" "<<"面积:"<<area(a)<<endl;
56、 cout<<"请输入长方形长宽";
57、 cin>>a>>b;
58、 cout<<"周长:"<<Girth(a,b)<<" "<<"面积:"<<area(a,b)<<endl;
59、
60、 cout<<"请输入三角形三边";
61、 cin>>a>>b>>c;
62、 if((a+b<c)||(a-c>b)||(a+c)<b||a>(b+c))
63、 cout<<"不合法"<<endl;
64、 else
65、 cout<<"周长:"<<Girth(a,b,c)<<" "<<"面积:"<<area(a,b,c)<<endl;
66、
67、
68、 return 0;
69、 }
运行结果:
程序代码:#include<iostream>
using namespace std;
int MAX(int a,int b,int c=0)
{
if(a<b) a=b;
if(a<c) a=c;
return a;
}
int main()
{
int a,b,c,n;
cout<<"请输入你要比较的整数个数";
cin>>n;
if(n==2)
{
cin>>a>>b;
cout<<"两个数最大为"<<MAX(a,b)<<endl;
}
else if(n==3)
{
cin>>a>>b>>c;
cout<<"三个数最大为"<<MAX(a,b,c)<<endl;
}
else
cout<<"不合法"<<endl;
return 0;
}
运行结果:
#include<iostream>
using namespace std;
void Swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(a>b) Swap(a,b);
if(a>c) Swap(a,c);
if(b>c) Swap(b,c);
cout<<"顺序为"<<a<<" "<<b<<" "<<c<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string n1,n2;
char t;
int a1,a2,i,j;
cout<<"Your first name is:";
cin>>n1;
cout<<"Your last name is:";
cin>>n2;
cout<<"Your name is:"<<n1<<" "<<n2<<endl;
a1=sizeof(n1);
a2=sizeof(n2);
for(i=0,j=a1-1;i<j;i++,j--)
{
t=n1[i];
n1[i]=n1[j];
n1[j]=t;
}
for(i=0,j=a2-1;i<j;i++,j--)
{
t=n2[i];
n2[i]=n2[j];
n2[j]=t;
}
cout<<"convert your name is:"<<n2<<" "<<n1<<endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s[5],t;
int i,j;
cout<<"Plase Input five contry name :";
for(i=0;i<5;i++)
cin>>s[i];
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
if(s[i]>s[j])
t=s[i],s[i]=s[j],s[j]=t;
}
cout<<"顺序为"<<endl;
for(i=0;i<5;i++)
cout<<s[i]<<endl;
return 0;
}
#include<iostream>
using namespace std;
const int N=1000;
template<class T>
void Input(T a[],int n)
{
int i;
for(i=0;i<n;i++)
cin>>a[i];
}
template<class T>
void Sort(T a[],int n)
{
double p;
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
p=a[i];
a[i]=a[j];
a[j]=p;
}
}
}
}
template<class T>
void Output(T a[],int n)
{
int i;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
int main()
{
double a[N];
int n;
cout<<"请输入元素个数:";
cin>>n;
cout<<"数组为:";
Input(a,n);
Sort(a,n);
cout<<"排序为:"<<endl;
Output(a,n);
return 0;
}
五. 实验总结
学习C++,感觉比C轻松了一点点,不过重要的还是需要练习,必须自己多上机才能发现问题,比如template<class T>需要每个函数前面都注明,虽然自己练习时找这个问题找了好久,不过最终自己找出了这个问题,感觉有点小得意。总的来说就是需要多练习,多上机。
- 实验一&实验二
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- 实验一
- android开发之常用OAuth登录与分享详解
- Qt开发,链接了数据库后,调用QSqlQuery::setQuery执行SQL语句获取我们想要的数据
- Spring源码解析
- ThreadLocal源码解析
- The first mainland city firm H-share listing Chongqing bank do poineering work the two times to crea
- 实验一
- iOS_Application
- 第4周项目1-三角形类的构造函数(2)-默认构造函数
- 滕王阁序
- XCode6 生成prefix.pch文件
- Java基础(网络编程)
- cocos2d-x中添加TV按键响应
- asp.net 验证码
- C语言中产生随机数