报名优惠方案:急!请C++高手帮忙编程。100分送上。

来源:百度文库 编辑:中科新闻网 时间:2024/05/06 00:29:27
1、函数应用程序设计:
(1)定义一个函数 int count(int a[],int n) 在n个元素的数组a中,统计出大于零的元素个数,此个数作为函数返回值。 在main()函数中,对数组b做如下初始化
int b[]={15,16,-23,7,-5,19,-2,0,28,11};
然后调用你定义的函数,在主函数中输出数组b中小于零元素的个数。
(2)写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果。两个整数由键盘输入。
(3)求方程ax2+bx+c=0的根,用三个函数分别求当b2-4ac大于0、等于0和小于0时的根,并输出结果。从主函数出入a、b、c的值,编程求解该方程的根。
(4)写一个判素数的函数,在主函数输入一个整数,输出是否素数的信息
2、要求尽量使用引用作函数参数进行编程:
(1) 编写一个函数,实现两个整数的交换。如:主调函数中
int a=10;
int b=20;
使用引用作为函数的参数,交换后为:
a=20;
b=10;
(2) 编程实现两个字符串的交换。如:
char *p1=”hello”
char *p2=”good”
使用引用作为函数的参数,交换后为:
p1: ” good”
p2:” hello”
题稍微有些多,请高手救命啊!!!!!!明天就用了,最好没有错可以运行。

全部VC6.0下运行通过的
一(1)
// zd_42.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <string.h>

int count(int a[],int n);

int main(int argc, char* argv[])
{
int n;
int b[]={15,16,-23,7,-5,19,-2,0,28,11};
n=count(b,10);
printf("大于0的数有 %d 个\n",n);
return 0;
}

int count(int a[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
if(a[i]>0)
sum++;
return sum;
}

运行结果:
大于0的数有 6 个
Press any key to continue
(2)
// zd_43.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

int hcf(int u,int v)
{
int t,r;
if(v>u)
{t=u;u=v;v=t;}
while((r=u%v)!=0)
{
u=v;
v=r;
}
return v;
}

int lcd(int u,int v,int h)
{
return u*v/h;
}

int main(int argc, char* argv[])
{
int u,v,h,l;
printf("输入两个整数:u,v\n");
scanf("%d,%d",&u,&v);
h=hcf(u,v);
printf("H.C.F=%d\n",h);
l=lcd(u,v,h);
printf("L.C.D=%d\n",l);
return 0;
}

运行结果:
输入两个整数:u,v
4,6
H.C.F=2
L.C.D=12
Press any key to continue
(3)
// zd_44.cpp : Defines the entry point for the console application.
//

#include <math.h>
#include <stdio.h>

float x1,x2,disc,p,q;

greater_than_zero(float a,float b)
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
}

equal_to_zero(float a,float b)
{
x1=x2=(-b)/(2*a);
}

smaller_than_zero(float a,float b)
{
p=-b/(2*a);
q=sqrt(abs(disc))/(2*a);
}

int main(int argc, char* argv[])
{
float a,b,c;
printf("Input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
printf("\nequation:%5.2f*x*x+%5.2f*x+%5.2f=0\n",a,b,c);
disc=b*b-4*a*c;
printf("root:\n");
if(disc>0)
{
greater_than_zero(a,b);
printf("x1=%5.2f\tx2=%5.2f\n\n",x1,x2);
}
else if(disc==0)
{
equal_to_zero(a,b);
printf("x1=%5.2f\tx2=%5.2f\n\n",x1,x2);
}
else
{
smaller_than_zero(a,b);
printf("x1=%5.2f+%5.2fi\tx2=%5.2f-%f5.2i\n",p,q,p,q);
}
printf("Hello World!\n");
return 0;
}

运行结果:
Input a,b,c:7,4,3

equation: 7.00*x*x+ 4.00*x+ 3.00=0
root:
x1=-0.29+ 0.59i x2=-0.29-0.5890155.2i
Hello World!
Press any key to continue
(4)
// zd_45.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

int main(int argc, char* argv[])
{
int prime(int);
int n;
printf("\ninput an integer:");
scanf("%d",&n);
if(prime(n))
printf("\n%d is a prime.",n);
else
printf("\n %d is not a prime.",n);
return 0;
}

int prime(int n)
{
int flag=1,i;
for(i =2;i<n/2 && flag==1;i++)
if(n%i==0)
flag=0;
return flag;
}

运行结果:

input an integer:96

96 is not a prime.Press any key to continue

(1)

// zd_46.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}

int main(int argc, char* argv[])
{
int a,b;
printf("input a,b:\n");
scanf("%d,%d",&a,&b);
printf("before swap\n");
printf("a=%d\tb=%d\n",a,b);
swap(a,b);
printf("after swap\n");
printf("a=%d\tb=%d\n",a,b);
return 0;
}

运行结果:
input a,b:
7,4
before swap
a=7 b=4
after swap
a=4 b=7
Press any key to continue

(2)
// zd_47.cpp : Defines the entry point for the console application.
//

#include <stdio.h>

void swap(char **a,char **b)
{
char *temp;
temp=*a;
*a=*b;
*b=temp;
}

int main(int argc, char* argv[])
{
char *p1="hello";
char *p2="good";
printf("before swap\n");
printf("p1=%s\tp2=%s\n",p1,p2);
swap(&p1,&p2);
printf("after swap\n");
printf("p1=%s\tp2=%s\n",p1,p2);
return 0;
}

运行结果:
before swap
p1=hello p2=good
after swap
p1=good p2=hello
Press any key to continue

你给上1000分,我可能会帮你解决掉问题

1
(1)题:
#include<iostream>
int Count(int a[],int n){
int count=0;
for(int i=0;i<n;i++)if(a[i]>0)count++;
return count;
}
int main(){
int b[10]={15,16,-23,7,-5,19,-2,0,28,11};
std::cout<<"输出数组中大于0的个数:"<<Count(b,10)<<std::endl;
system("pause");
return 0;
}

这是用Dev-C++编的.

(2)
#include<iostream>
int Gongyue(int a,int b){
while(a!=b){
if(a>b)a-=b;
else b-=a;
return b;
}

int Gongbei(int a,int b){
return a>b?(b/Gongyue(a,b)*a):(a/Gongyue(a,b)*b);
}

int main(){
int a,b;
std::cout<<"请输入两个数:"<<std::endl;
std::cin>>a;
std::cin>>b;
std::cout<<"最大公约数为:"<<Gongyue(a,b)<<std::endl;
std::cout<<"最小公倍数为:"<<gongbei(a,b)<<std::endl;
system("pause");
return 0;
}

/*(1)定义一个函数 int count(int a[],int n) 在n个元素的数组a中,统计出大于零的元素个数,此个数作为函数返回值。 在main()函数中,对数组b做如下初始化
int b[]={15,16,-23,7,-5,19,-2,0,28,11};
然后调用你定义的函数,在主函数中输出数组b中小于零元素的个数。
*/

#include "iostream.h"

int z=0;//零的个数

int count(int a[],int n)
{
int dl=0; //大于0的个数
int i;
for(i=0;i<n;i++)
{
if(a[i]>0)
dl++;
else if(a[i]==0)
z++;
}
return dl;
}

void main()
{

int n,x;
int b[]={15,16,-23,7,-5,19,-2,0,28,11};
n=10;
x=n-count(b,n);
x=x-z;
cout<<"less than zero number="<<x<<endl;
}

/*(2)写两个函数,分别求两个整数的最大公约数和最小公倍数,
用主函数调用这两个函数并输出结果。两个整数由键盘输入。

利用最大公约数求最小公倍数

由于两个数的乘积等于这两个数的最大公约数与最小公倍数的积。这就是说,求两个数的最小公倍数,可以先求出两个数的最大公约数,再用这两个数的最大公约数去除这两个数的积,所得的商就是两个数的最小公倍数。

例 求105和42的最小公倍数。

因为105和42的最大公约数是21,

105和42的积是4410,4410÷21=210,

所以,105和42的最小公倍数是210。

*/

#include "iostream.h"

int gys,gbs;

int gy(int a,int b)
{
int r;
r=a%b;
if(r!=0)
{
a=b;
b=r;
r=a%b;
}
return b;
}

int gb(int a,int b)
{
int r;
r=a*b/gys;
return r;
}

int main()
{
int a,b,t;
cout<<"Input a"<<endl;
cin>>a;
cout<<"Input b"<<endl;
cin>>b;

if(a==0 || b==0)
{
cout<<"Input Error"<<endl;
return 0;
}

if(a<b)
{
t=a;a=b;b=t;
}
gys=gy(a,b);
gbs=gb(a,b);

cout<<"gys="<<gys;
cout<<"gbs="<<gbs;

}

/*(3)求方程ax2+bx+c=0的根,用三个函数分别求当b2-4ac大于0、
等于0和小于0时的根,并输出结果。从主函数出入a、b、c的值,
编程求解该方程的根。
*/

#include "iostream.h"
#include <math.h>

void dy(double a,double b,double c,double derta)
{
double d;
double x1,x2;
if(a==0)
{
x1=-c/b;
x2=-c/b;
}
else
{
d=pow(derta,0.5);
x1=(-b+d)/2/a;
x2=(-b-d)/2/a;
}
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}

void deng(double a,double b,double c, double derta)
{
dy(a,b,c,derta);
}

void xiao(void)
{
cout<<"no answer!"<<endl;
}

int main()
{
double a,b,c;
double derta;
cout<<"Input a"<<endl;
cin>>a;
cout<<"Input b"<<endl;
cin>>b;
cout<<"Input c"<<endl;
cin>>c;

derta=b*b-4*a*c;
if( derta>0)
{
dy(a,b,c,derta);
}
else if(derta==0)
{
deng(a,b,c,derta);
}
else // derta<0
{
xiao();
}

}

/*(4)写一个判素数的函数,在主函数输入一个整数,输出是否素数的信息
*/

#include "iostream.h"
#include <math.h>

int compare(int n)
{
int b=1;
int i;
if(n==2 || n==1)
{
}
else
{
for(i=2;i<=n;i++)
{
if(n%i==0)
{
b=0;
break;
}
}
}

return b;
}

int main()
{
int n;
cout<<"Input n"<<endl;
cin>>n;
if(compare(n)==1)
{
cout<<"this number is prime number!"<<endl;
}
else
{
cout<<"this number is not prime number!"<<endl;
}
}

/*(1) 编写一个函数,实现两个整数的交换。如:主调函数中
int a=10;
int b=20;
使用引用作为函数的参数,交换后为:
a=20;
b=10;

*/

#include "iostream.h"
void change(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}

void main()
{
int a=10;
int b=20;
change(a,b);
cout<<"a="<<a<<"\nb="<<b<<endl;
}

/*(2) 编程实现两个字符串的交换。如:
char *p1=”hello”
char *p2=”good”
使用引用作为函数的参数,交换后为:
p1: ” good”
p2:” hello”

*/

#include "iostream.h"
#include "stdio.h"
#include "string.h"

void change(char *&p1,char *&p2)
{
char *t1;
t1=p1;
p1=p2;
p2=t1;
}

void main()
{
char *p1="hello";
char *p2="good";
change(p1,p2);
cout<<"p1="<<p1<<"\np2="<<p2<<endl;
}

#include<iostream>
#include<cmath>
using namespace std;

void f1(float a,float b,float c)//d=0
{
float x0;
x0=(-b)/2*a;
cout<<"这个方程只有一个根为:"<<x0<<endl;
}

void f2(float a,float b,float c,float d)
{
//d>0;
int x1,x2;
x1=((-b)+sqrt(d))/2*a;
x2=((-b)-sqrt(d))/2*a;
cout<<"这个函数的两个根分别为:"<<"x1="<<x1
<<endl<<"x2="<<x2<<endl;
}

void f3(float a,float b,float c,float d)
{
//d<0;
int m,n;
m=(-b)/2*a;
n=sqrt(-d)/2*a;
cout<<"这个方程的两虚根分别为:"<<endl
<<"x1="<<m<<"+"<<n<<"i"<<endl
<<"x2="<<m<<"-"<<n<<"i"<<endl;
}

void main()
{
int a,b,c,d;
cout<<"请输入a,b,c"<<endl;
cin>>a;cin>>b;cin>>c;
d=b*b-4*a*c;
if(d==0)
f1(a,b,c);
//else
if(d>0)
f2(a,b,c,d);
else
f3(a,b,c,d);
}

第一题:
#include <iostream>
using namespace std;

const int ERROR = -32768;
int count(int a[], int n)
{
if(a == NULL)
return ERROR;

int result = 0;
for(int i = 0; i < n; i++)
if(a[i] < 0)
result++;

return result;
}

int main()
{
int b[] = {15, 16, -23, 7, -5, 19, -2, 0, 28, 11};

cout << "数组b的成员为:";
for(int i = 0; i < 10; i++)
cout << ' ' << b[i];
cout << endl;

cout << "小于0的个数为:\t" << count(b, 10) << endl;
}