没有合适的资源?快使用搜索试试~ 我知道了~
c++简单编程题汇总450份(适合初学c++者)
4星 · 超过85%的资源 需积分: 14 84 下载量 112 浏览量
2013-06-28
12:34:54
上传
评论 12
收藏 269KB DOC 举报
温馨提示
试读
64页
里面包括了c++的450份编程题目。例如字符串排序、求面积、数值相加等等。对于初学c++的同学来说最适合不过了。学习了之后可以更加了解c++,编程考试更加容易了。
资源推荐
资源详情
资源评论
《c++》编程题试卷
第三章
1.编写一个求方程 ax2 + bx + c = 0 的根 的程序,用 3 个函数分别求当 b2-4ac
大于零、等于零、和小于零时的方程的根。要求从主函数输入 a,b,c 的值并输出结果。
#include < iostream.h >
#include < math.h >
void equation_1 (int a, int b, int c)
{
double x1, x2, temp;
temp = b*b - 4 * a * c;
x1 = (-b + sqrt(temp) ) / (2 * a * 1.0);
x2 = (-b - sqrt(temp) ) / (2 * a * 1.0);
cout<<"两个不相等的实根"<< endl;
cout<<"x1 = "<< x1<<", x2 = "<< x2<< endl;
}
void equation_2 (int a, int b, int c)
{
double x1, x2, temp;
temp = b*b - 4 * a * c;
x1 = (-b + sqrt(temp) ) / (2 * a * 1.0);
x2 = x1;
cout<<"两个相等的实根"<< endl;
cout<<"x1 = "<< x1<<", x2 = "<< x2<< endl;
}
void equation_3 (int a, int b, int c)
{
double temp, real1, real2, image1, image2;
temp = - (b*b - 4 * a * c);
real1 = -b / (2 * a *1.0);
real2 = real1;
image1 = sqrt(temp);
image2 = - image1;
cout<<"两个虚根"<< endl;
cout<<"x1 = "<< real1<<" + "<< image1<<"j"<< endl;
cout<<"x2 = "<< real2<<" + "<< image2<<"j"<< endl;
}
void main()
{
int a, b, c;
第 1 页 共 104 页
double temp;
cout<<"输入 a,b,c 的值"<< endl;
cin>>a>>b>>c;
cout<<"方程为:"<< a<<"x*x+"<< b<<"x+"<< c<<" = 0"<<
endl;
temp = b*b - 4 * a * c;
if(temp > 0)
equation_1 (a, b, c);
if(temp == 0)
equation_2 (a, b, c);
if(temp < 0)
equation_3 (a, b, c);
}
2.定义函数 up(ch),如字符变量 ch 是小写字母就转换成大写字母并通过 up 返回,
否则字符 ch 不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的。
#include < iostream >
using namespace std;
char up (char c)
{
if(c >= 97 && c <= 122)
return (c - 32) ;
else
return c;
}
void main()
{
int i;
char c[15] = {'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'};
for(i = 0 ; i < 15 ; i++)
cout<< up(c[i])<<", ";
cout<< endl;
}
3.编写主程序条用带实数 r 和整数 n 两个参数的函数并输出 r 的 n 次幂。
#include < iostream.h >
#include < math.h >
double power(double a, int b)
{
int i;
double result = 1.0;
for(i=0;i< b;i++)
第 2 页 共 104 页
result = result * a;
return result;
}
void main()
{
double r;
int n;
cout<<"r = ";
cin>>r;
cout<<"n = ";
cin>>n;
cout<< r<<"的"<< n<<"次幂是:"<< power(r,n)<< endl;}
4.编写有字符型参数 C 和整形参数 N 的函数,让他们显示出由字符 C 组成的三角形。
其方式为第 1 行有 1 个字符 C,第 2 行有 2 个字符 C ,等等。
#include < iostream >
using namespace std;
void print_triangle(char c, int n)
{
int i, j;
for(i=0; i< n; i++)
{
for(j=0; j<=i; j++)
{
cout<< c;
}
cout<< endl;
}
}
void main()
{
print_triangle('a',10);
}
5.编写一个 ieqiu 字符串长度的函数,strlen(),再用 strlen()函数编写一个
函数 revers(s)的倒序递归程序,使字符串 s 逆序。
#include < iostream >
#include < string >
using namespace std;
int strlen(char *str)
{
int len = 0;
while(str[len] != '\0')
{
第 3 页 共 104 页
len++;
}
return len;
}
void revers(char *b)
{
char c;
int j, len;
len=strlen(b);
j=len/2-1;
while(j>=0)
{
c=*(b+j);
*(b+j)=*(b+len-j-1);
*(b+len-j-1)=c;
j--;
}
b[len]='\0';
}
void main()
{
char str[]={"1234567890"};
cout<< str<<"----的长度:"<< strlen(str)<< endl;
cout<< str<< endl;//倒序前
revers(str);//
cout<< str<< endl;//倒序后
}
6.用函数模板实现 3 个数值中按最小值到最大值排序的程序。
#include < iostream >
using namespace std;
template
void sort(T a, T b, T c)
{
T array[3],temp;
int i,j;
array[0] = a;
array[1] = b;
array[2] = c;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
if(array[j]>array[j+1])
{
第 4 页 共 104 页
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
cout<< array[0]<< array[1]<< array[2]<< endl;
}
void main()
{
sort(5,1,9);
}
7.利用函数模板设计一个求数组元素中和的函数,并检验之。
#include < iostream >
using namespace std;
template
T sum (T a[],int n)
{
int i;
T s=0;
for(i=0;i< n;i++)
s = s + a[i];
return s;
}
void main ()
{
int a[5]={1,2,3,4,5};
int s = sum(a,5);
cout<< s<< endl;
}
8.重载上题中的函数模板,使他能够进行两个数组的求和。
#include < iostream >
using namespace std;
template
T sum (T a[], int n)
{
int i;
T s=0;
for(i=0;i< n;i++)
s = s + a[i];
return s;
}
第 5 页 共 104 页
剩余63页未读,继续阅读
资源评论
- 秋江带雨2014-12-01可以,值得参考
dang221
- 粉丝: 6
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功