2012年5月30日星期三

从c++开始练习1


我的代码:
1.编写代码用来显示姓名和地址;
// exercises2.cpp : 定义控制台应用程序的入口点。
//
#include "StdAfx.h"
#include <iostream>
int main()
{
using namespace std;
cout<<"My name is ChaAng and My addresses is GD";
int n;
cin>>n;
}
2.编写代码输入一个浪的距离,然后将它转化为码,1浪等于22码
#include "StdAfx.h"
#include <iostream>
double yd(double);
int main()
{
using namespace std;
double waves;
cout<<"How many waves do you know?"<<endl;
cin>>waves;
cout<<waves<<"Waves = ";
double yards = yd(waves);
cout<<yards<<"Yards";
int n;
cin>>n;
}
double yd(double wav )
{
return 22*wav;
}


3.编写代码,自定义两个函数。每一个都要被调用两次显示出一样的两段文字。
#include "StdAfx.h"
#include <iostream>
void mice(int);
void run(int);
int main()
{
using namespace std;
mice(1);
mice(2);
run(3);
run(4);
int m;
cin>> m;
}
void mice(int m)
{
using namespace std;
cout<<"Three blind mice."<<endl;
}
void run(int r)
{
using namespace std;
cout<<"See how they run."<<endl;
}

4.编写代码,由main()调用自定义函数将摄氏度转化为华氏度公式:华氏=摄氏×1.8+32.0

#include "StdAfx.h"
#include <iostream>
double Fh(double);
int main()
{
using namespace std;
double Cle;
cout<<"Please enter a Celsius value:";
cin>> Cle;
double fh = Fh(Cle);
cout<<endl;
cout<<Cle<<"degrees Clesius is "<<fh<<"degrees Fahrenheit.";
int n;
cin>>n;
}
double Fh(double cle)
{
return 1.8*cle+32;
}

5.编写代码,要求输入小时分钟数并传递给自定义void函数,被main()调用后以时间格式显示。
#include "StdAfx.h"
#include <iostream>
void hm(int,int);
int main()
{
using namespace std;
int hr;
cout<<"Enter the number of hours:";
cin>>hr;
int min;
cout<<"Enter the number of minutes:";
cin>>min;
int hrs = hr;
int mins = min;
hm(hrs,mins);
int n;
cin>>n;
}
void hm(int h,int m)
{
using namespace std;
cout<<"Time:"<<h<<":"<<m<<endl;
}

没有评论:

发表评论