- 实验作业
1.调试分析课本每一个例题,有可能的话更改成2-3个方法的新程序;
2.编程实现课本每一个编程习题。
3. 编程实现输入两个4X5矩阵和5X3矩阵,定义函数并在主函数中调用计算它们的积。
4.编程计算S[n]=1!+21+3!+...n!。要求定义两个函数,一个计算n!,一个计算s[n],在后一个函数中调用前一个函数。然后在主程序中输入数n的值,然后调用定义函数输出结果。
5.编写一个函数,输入一个十六进制数,输出相应的十进制数。
课本习题~
#include <iostream>
#include <math.h>
using namespace std;
double circlearea(double);
int main ()
{
double area = circlearea(5.0);
cout<<"area = "<<area<<endl;
return 0;
}
double circlearea(double r)
{
double pi = 3.14;
double area = pi*r*r;
return area;
}
例5.4
#include <iostream>
#include <math.h>
using namespace std;
int sum(int x,int y)
{
int temp;
temp = x+y;
return temp;
}
int main ()
{
int a,b,c;
a = 10;b = 5;
c = sum(a,b);
cout<<a<<"+"<<b<<"="<<c<<endl;
return 0;
}
例5.5
#include <iostream>
using namespace std;
int ncomp(int i,int j)
{
if(i>j) return 1;
if(i==j) return 0;
return -1;
}
int main()
{
int k=2;
int n=ncomp(k,k++);
cout<<n;
return 0;
}
例5.8
#include <iostream>
using namespace std;
void swap(int u,float v);
int main()
{
int a=3,b=4;
cout<<"a = " <<a<<"\tb = "<<b<<endl;
swap(a,b);
cout<<"a = "<<a<<"\tb = "<<b<<endl;
return;
}
void swap(int u,int v)
{
int temp;
temp = u;
u = v;
v =temp;
}
例5.10
#include <iostream>
using namespace std;
void display(int x,float y)
{
cout<<x<<" "<<y;
return;
}
int main()
{
float a;
int b;
cin>>b>>a;
display(b,a);
return 0;
}
例5.13
#include <iostream>
using namespace std;
long f2(int);
long f1(int p)
{
int k;
long r;
k = p*p;
r = f2(k);
return r;
}
long f2(int q)
{
long fact = 1;
for(int i=1;i<=q;i++)
fact *= i;
return fact;
}
int main()
{
int i;
long sum = 0;
for(i=2;i<=3;i++)
sum += f1(i);
cout<<"sum = "<<sum<<endl;
return 0;
}
例5.14
#include <iostream>
#include<cmath>
using namespace std;
float f(float x);
float root(float x1,float x2);
float point(float x1,float x2);
int main()
{
float x1,x2,y1,y2,x;
do
{
cout<<"请输入根所在范围:";
cin>>x1>>x2;
y1 = f(x1);
y2 = f(x2);
cout<<"两端点的值为["<<y1<<","<<y2<<"]"<<endl;
}
while(y1*y2>=0);
x = root(x1,x2);
cout<<"在"<<x1<<"与"<<x2<<"之间,方程的解为"<<x<<endl;
return 0;
}
float f(float x)
{
return(x*x*x-4*x*x+6*x-10);
}
float root(float x1,float x2)
{
float y1,x,y;
y1 = f(x1);
do
{
x = point(x1,x2);
y = f(x);
if (y*y1>0)
{
y1 = y; x1 = x;
}
else x2 = x;
}
while (fabs(y)>=0.0001);
return x;
}
float point(float x1,float x2)
{
float y;
y = (x1*f(x2)-x2*f(x1))/f(x2)-f(x1);
return y;
}
不知道怎么会变成这样子的咧`