C++程序设计,课程代码4737,课后习题3答案

2018年3月1日18:22:14 发表评论 860 views

习题3

一、填空题

  1. 在C++语言中,函数“double abc(double, char);”表示的含义是 声明一个返回double类型的,且第一个参数为double和第二个参数为char的函数abc
  2. 定义内联函数的关键字为 inline
  3. C++函数参数传递的方式有 传值和传引用
  4. 函数声明char& func(char, int)的含义是 声明一个返回char&引用类型的,且第一个参数为char和第二个参为int的函数func
  5. 函数fun的返回值是指针,其中一个参数是字符,另一个是参数int类型的引用,声明fun的函数的原型是int * fun(char, int &);

二、单项选择题

  1. 不允许调用者改变函数参数的函数原型声明是( A )。
    A. string input(const int);  B. string input(int &);  C. string* input(int *);  D.string input(string &);
  2. 关于函数重载的正确叙述是( C )。
    A. 函数的参数个数不能相同  B. 函数参数的数据类型不能相同  C. 函数的返回值可以相同  D. 函数的返回值不能相同
  3. 下列对模板的声明中正确的一项是( C )。
    A. template<T>  B. template<class T1, T2>  C. template<class T1, class T2>  D.template<class T1; class T2>

三、改错题

  1. 下面程序错在何处?
    template <class T>
    T fun(T x) {
        T y;
        //y = x * x - T;
        y = x * x - y;
        return y;
    }

    答:code:4: error: 'T'不是有效的变量或常量。

  2. 找出下面程序中的错误并改正之。
    #include <iostream.h>
    template <class Type>
    //Type max(T x, y)
    Type max(Type x,Type y)
    { return (x > y) ? (x) : (y); }

    答:code:3: error:'T''y'不是有效的变量

  3. 找出下面程序中的错误并改正之。
    //void change(const string &s)
    void change(string &s)
    { s = s + "pig!"; }
    //}
    void main() {
        string str(" it is a");
        change(str);
    }

    code:1: error:常量s的值不能改变

四、编程题

  1. 编写一个求方程ax2+bx+c=0的根的程序,用3个函数分别求当b24ac大于零、等于零、小于零时的方程的根。要求从主函数输入a、b、c的值并输出结果。
    #include <iostream>
    #include <cmath>
    using namespace std;
    /*
    	实实在在的数学题,判断△=b^2+4ac的三种情况
    	△>0方程有两个不相等的实数根
    	△=0方程有两个相等的实数根
    	△<0方程无实数根,但有2个共轭复根。
    	公式:
    	x1=(-b+√(b^2-4ac))/2
    	x2=(-b-√(b^2-4ac))/2
     */
    //大于0的函数,有两个不相等的实数根
    void greter(int a,int b,int c,double result){
    	double x1,x2;
    	x1 = (-b+sqrt(result))/2;
    	x2 = (-b-sqrt(result))/2;
    	cout << "b^2-4ac>0时,方程有两个根:x1=" << x1 
    			<< ",x2=" << x2 << endl;
    }
    //等于0的函数,有两个相等的实数根
    void equalTo(int a,int b,int c,double result){
    	double x1,x2;
    	x1 = (-b+sqrt(result))/2;
    	x2 = (-b-sqrt(result))/2;
    	cout << "b^2-4ac=0时,方程有两个根:x1=" << x1 
    			<< ",x2=" << x2 << endl;
    }
    //小于0的函数,无实数根
    void lessThan(int a,int b,int c,double result){
    	cout << "无解" << endl;
    }
    
    int main(int argc, char const *argv[])
    {
    	cout << "请输入a,c,b三个变量的值,请用空格隔开" << endl;
    	int a,b,c;
    	cin >> a >> b >> c;
    	double result = b*b - 4*a*c;
    	if(result>0){
    		greter(a,b,c,result);
    	}else if(result==0){
    		equalTo(a,b,c,result);
    	}else{
    		lessThan(a,b,c,result);
    	}
    	return 0;
    }

     

  2. 定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的。
    #include <iostream>
    #include <cctype> 
    using namespace std;
    
    char up(char ch){
    	if(ch>='a' && ch<='z'){
    		return ch + ('A' - 'a');
    	}else{
    		return ch;
    	}
    }
    
    int main(int argc, char const *argv[])
    {
    	cout << "请输入一个字母" << endl;
    	char ch;
    	cin >> ch;
    	cout << "你输入的是:" << up(ch) << endl;
    	return 0;
    }
    /**
     * 第二种
     */
    #include <iostream>
    char up(char ch) {
        return toupper(ch);
    }
    int main()
    {
        char ch;
        std::cout << "Plase enter a character:";
        std::cin >> ch;
        std::cout << up(ch) << std::endl;
        return 0;
    }

     

  3. 编写主程序调用带实数r和整数n两个参数的函数并输出r的n次幂。
    #include <iostream>
    using namespace std;
    double exponentiation(double r, int n) {
        double result = 1.0;
        for(int i = 0; i < n; ++i) {
            result *= r;
        }
        return result;
    }
    int main()
    {
        double r = 0.0;
        int n = 0;
        cout << "Please enter r and n:" << std::endl;
        cin >> r >> n;
        cout << "r^n:" << exponentiation(r, n) << std::endl;
        return 0;
    }

     

  4. 编写有字符型参数C和整型参数N的函数,让它显示出由字符C组成的三角形。其方式为第1行有1个字符C,第2行有2个字符C等等。
    #include <iostream>
    using namespace std;
    void print_triangle(char c, int n) {
        for(int i = 0; i < n; i++) {
            for(int j = 0; j <= i; j++) {
                std::cout << c;
            }
            std::cout << std::endl;
        }
    }
    int main()
    {
        char c;
        int n;
        cout << "Plase enter c and n:" << std::endl;
        cin >> c >> n;
        print_triangle(c, n);
        return 0;
    }

     

  5. 编写一个求字符串长度的函数strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序。
    #include <iostream>
    #include <string>
    using namespace std;
    
    //求长度
    int strlen(string str){
        int len = 0;
        while(str[len] != '\0'){
            len++;
        }
        return len;
    }
    //倒序字符串
    string revers(string s){
       string str(s.rbegin(),s.rend());
       return str;
    }
    
    int main(int argc, char const *argv[])
    {
        cout << "输入一个字符串求其长度" << endl;
        string str = "";
        getline(cin,str);
        cout << "字符串长度:" << strlen(str) << endl;
        cout << "字符串是:" << str << endl;
        cout << "revers后的字符串:" << revers(str) << endl;
        return 0;
    }

     

  6. 用函数模板实现3个数值中按最小值到最大值排序的程序。
    #include <iostream>
    using namespace std;
    template <class T>
    void sort(T a[], int len) {
        T temp;
        for(int i = 0; i < len; i++) {
            temp = a[i];
            for(int j = i + 1; j < len; j++) {
                if(temp > a[j]) {
                    a[i] = a[j];
                    a[j] = temp;
                    temp = a[i];
                }
            }
        }
    }
    int main()
    {
        int a[3] = {0, 2, 1};
        sort(a, 3);
        for(int i = 0; i < 3; i++) {
            cout << a[i] << " " << endl;
        }
        return 0;
    }

     

  7. 利用函数模板设计一个求数组元素总和的函数,并检验之。
    #include <iostream>
    using namespace std;
    template <class T>
    T sum(T a[], int len) {
        T temp;
        for(int i = 0; i < len; i++) {
            temp += a[i];
        }
        return temp;
    }
    int main()
    {
        int a[5] = {1, 3, 5, 7, 9};
        cout << sum(a, 5) << endl;
        return 0;
    }

     

  8. 重载上题中的函数模板,使它能够进行两个数组的求和。
    #include <iostream>
    template <class T>
    T sum(T a[], int len) {
        T temp;
        for(int i = 0; i < len; i++) {
            temp += a[i];
        }
        return temp;
    }
    template <class T>
    T sum(T a[], int len, T b[], int len2) {
        return sum(a, len) + sum(b, len2);
    }
    int main()
    {
        int a[5] = {1, 3, 5, 7, 9};
        int b[5] = {2, 4, 6, 8, 10};
        std::cout << sum(a, 5, b, 5) << std::endl;
        return 0;
    }

     

 

历史上的今天:


欢迎来到菜鸟头头的个人博客
本文章百度已收录,若发现本站有任何侵犯您利益的内容,请及时邮件或留言联系,我会第一时间删除所有相关内容。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: