21xrx.com
2024-09-19 08:55:59 Thursday
登录
文章检索 我的文章 写文章
C++课程实践题答案选编
2023-07-05 07:36:33 深夜i     --     --
C++ 课程实践题 答案 选编 编程实践

C++课程是计算机专业必修的一门课程,学习这门课程主要是为了对C++语言的掌握和应用熟练度进行提高。在学习过程中,除了理论课程的学习外,实践题也是至关重要的一部分。在本文中,我们将选编一些C++课程实践题的答案供大家参考。

1. 设计一个程序,输入一个英文字母,判断其是大写字母,小写字母还是其他字符,并做相应的输出。

答案:

#include

using namespace std;

int main()

{

  char ch;

  cout<<"Please input a character:";

  cin>>ch;

  if(ch>='A' && ch<='Z') cout<<"It's a capital letter."<

  else if(ch>='a' && ch<='z') cout<<"It's a lowercase letter."<

  else cout<<"It's not a letter."<

  return 0;

}

2. 编写程序,输入三个数字,判断是否能组成三角形,如果可以,输出三角形的类型;如果不行,输出不能组成三角形。

答案:

#include

using namespace std;

int main()

{

  double a,b,c;

  cout<<"Please input three numbers:";

  cin>>a>>b>>c;

  if(a+b>c && a+c>b && b+c>a)

  {

    if(a==b && b==c) cout<<"It's an equilateral triangle."<

    else if(a==b || a==c || b==c) cout<<"It's an isosceles triangle."<

    else cout<<"It's a scalene triangle."<

  }

  else cout<<"It's not a triangle."<

  return 0;

}

3. 编写程序,输入一个正整数n,求出1~n之间所有奇数的和。

答案:

#include

using namespace std;

int main()

{

  int n,sum=0;

  cout<<"Please input a positive integer:";

  cin>>n;

  for(int i=1;i<=n;i+=2)

  {

    sum+=i;

  }

  cout<<"The sum of all odd integers from 1 to "< <<" is:"< <

  return 0;

}

4. 编写程序,输入一个字符串,将其中所有小写字母转换成大写字母输出。

答案:

#include

using namespace std;

int main()

{

  string str;

  cout<<"Please input a string:";

  getline(cin,str);

  for(int i=0;i

  {

    if(str[i]>='a' && str[i]<='z') str[i]-=32;

  }

  cout<<"The converted string is:"< <

  return 0;

}

5. 编写程序,输入一个数列,将其中的元素按降序排列后输出。

答案:

#include

using namespace std;

int main()

{

  int n,a[100],temp;

  cout<<"Please input the length of the array:";

  cin>>n;

  cout<<"Please input the array:";

  for(int i=0;i

  {

    cin>>a[i];

  }

  for(int i=0;i

  {

    for(int j=i+1;j

    {

      if(a[i]

      {

        temp=a[i];

        a[i]=a[j];

        a[j]=temp;

      }

    }

  }

  cout<<"The sorted array in descending order is:";

  for(int i=0;i

  {

    cout< <<" ";

  }

  cout<

  return 0;

}

以上是选编的部分C++课程实践题答案,希望对大家学习C++语言有所帮助。在实践过程中,需要反复练习,加深对语言的理解和应用能力。同样,也需要注重理论知识的学习,两者相辅相成,才能打下扎实的C++语言基础。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复
    相似文章