学号姓名院(部) 专业考试时间: 2008 年月日------------------------------------------------ 密-------------------- 封---------------------- 线----------------------------------------------------------------- 《c++ 》编程题试卷第三章 1. 编写一个求方程 ax2 + bx+c=0的根的程序,用 3 个函数分别求当 b2-4ac 大于零、等于零、和小于零时的方程的根。要求从主函数输入 a,b,c 的值并输出结果。#include < > #include < > 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* ); x2= (-b - sqrt(temp) )/ (2*a* ); 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* ); 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 *); 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; 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 < > #include < > double power(double a, int b) { int i; double result = ; for(i=0;i< b;i++) result = result * a; return result; } void main() { double r; int n; cout<<"r = "; cin>>r; cout<<"n = "; cin>>n; cout<< r<<" 的"<
c++编程题汇总450份 来自淘豆网www.taodocs.com转载请标明出处.