C++实验三
点击下载
(1)/////////////////////////////////////////////
# include <iostream.h>
void main(){
char ch;
int nnum=0,nspace=0,nchar=0;
cout<<"请输入一段文本:"<<endl;
do{
ch=cin.get();
if(ch>=48 && ch<=57){
nnum++;}
else if(ch==32) {
nspace++;}
else
{nchar++;}
}
while (ch!=EOF);
cout<<"数字个数"<<nnum<<endl;
cout<<"空格个数"<<nspace<<endl;
cout<<"其他字符个数"<<nchar-2<<endl;
}
(2)///////////////////////////////////////////
# include <iostream.h>
void main()
{
int day,month,year,sum,leap;
cout<<"please input year,month,day"<<endl;
cin>>year>>month>>day;
switch(month)/*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:cout<<"data error"<<endl;break;
}
sum=sum+day;
if(year%400==0||(year%4==0&&year%100!=0))/*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2)/*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
cout<<"It is the "<<sum<<"th day."<<endl;
}
———————————————————————————-
C++实验四
点击下载
# include <iostream.h>
int a,b,min,max;
char inn;
void input();
void process();
void output();
void main()
{
input();
}
void input()
{
cout<<"请输入第一个数据:";
cin>>a;
cout<<"请输入第二个数据:";
cin>>b;
process();
output();
}
void process()
{
min=a<b?a:b;
max=a>b?a:b;
while(max%min!=0)
{
int temp=min;
min=max%min;
max=temp;
}
}
void output()
{
cout<<"最大公约数:"<<min<<endl;
cout<<"继续<Y(yes)> OR <N(no)>?"<<endl;
cin>>inn;
while ( inn==89 || inn==121 || inn==78 || inn==110)
{
if (inn==89 || inn==121)
input();
if (inn==78 || inn==110)
break;
}
}
////////////////////////////////////////////////////////////////
实验五(1)
#include<iostream.h>
double fun(double);
void main()
{
while(1){
int m=0,n=0;
cout<<"请输入m的值:"<<endl;
cin>>m;
cout<<"请输入n的值:"<<endl;
cin>>n;
if(n>=m)
cout<<"结果为:"<<fun(n)/(fun(m)*fun(n-m))<<endl;
else
cout<<"数据大小不合适!"<<endl;
cout<<"继续?<y/n>"<<endl;
char yn;
cin>>yn;
if (yn=='n') break;
}
}
double fun(double x)
{
double y;
if (x==0||x==1) y=1;
else
y=x*fun(x-1);
return y;
}
//////////////////////////////////////////////////////////////////
实验六
//定义一个圆类(Circle),
//属性为半径和周长、面积,
//操作为输入半径并计算周长、面积,
//输出半径、周长和面积。
//要求定义构造函数和拷贝构造函数。
#include<iostream.h>
class Circle{
private:
float R;//半径
const float PI;//pi
public:
void Circle::registCircle(float);
float outr(void);
float outc(void);
float outs(void);
};
void Circle::registCircle(float r)
{
R=r;
PI=3.1415926;
}
float Circle::outr(void)
{
return(R);
}
float Circle::outc(void)
{
float c;
c=2*PI*R;
return(c);
}
float Circle::outs(void)
{
float s;
s=PI*R*R;
return(s);
}
void main()
{
Circle yuan;
float rr;
cout<<"请输入圆半径:"<<endl;
yuan.registCircle(rr);
cout<<"圆的半径为:"<<yuan.outr();
cout<<"圆的周长为:"<<yuan.outc();
cout<<"圆的面积为:"<<yuan.outs();
}