21xrx.com
2025-03-27 01:04:46 Thursday
文章检索 我的文章 写文章
C++程序设计第三版题解
2023-07-03 02:03:30 深夜i     11     0
C++ 程序设计 第三版 题解 教材

《C++程序设计第三版》是一本经典的计算机科学教材,广为程序设计初学者所使用。但是,对于初学者来说,这本书里的题目并不是那么容易接受和理解。因此,本文将对《C++程序设计第三版》中的题目做出详尽解答,让读者更好地掌握相关知识。

第一章

1.1 题目:写一个程序,输出“Hello,World!”。

解答:程序如下

#include <iostream>
using namespace std;
int main()
cout << "Hello

1.2 题目:写一个程序,输出你的名字。

解答:程序如下

#include <iostream>
using namespace std;
int main()
cout << "My name is xxx!" << endl;
return 0;

1.3 题目:写一个程序,输出两个整数之和。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
cout << "The sum of " << a << " and " << b << " is " << a+b << endl;
return 0;
}

1.4 题目:将华氏温度转换为摄氏温度。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
double f,c;
cout << "Please input F temperature: ";
cin >> f;
c = 5.0/9.0 * (f-32.0);
cout << "C temperature is " << c << endl;
return 0;
}

第二章

2.1 题目:求1到n的和。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
int sum = 0, n;
cout << "Please input a integer number: ";
cin >> n;
for(int i=1;i<=n;i++)
 sum += i;
cout << "The sum of 1 to " << n << " is " << sum << endl;
return 0;
}

2.2 题目:求平均数。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
int n;
double sum = 0;
cout << "Please input the number of scores: ";
cin >> n;
for(int i=1;i<=n;i++)
{
 double score;
 cout << "Please input No." << i << " score: ";
 cin >> score;
 sum += score;
}
cout << "The average score is " << sum/n << endl;
return 0;
}

2.3 题目:猜数字(1到100之间)。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
int num = rand()%100+1;
cout << "Guess a number(1-100), 0 to exit" << endl;
for(;;)
{
 int x;
 cin >> x;
 if(x==0) break;
 if(x<num) cout << "Too small" << endl;
 else if(x>num) cout << "Too large" << endl;
 else
 
  cout << "Congratulations! You guessed it." << endl;
  break;
 
}
return 0;
}

第三章

3.1 题目:求体积。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
double r,h,V;
const double pi = 3.1415926;
cout << "Please input the radius and height of the cylinder: ";
cin >> r >> h;
V = pi*r*r*h;
cout << "The volume of the cylinder is " << V << endl;
return 0;
}

3.2 题目:输入一个三位数,求其各个位数上数字之和。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
cout << "Please input a three-digit number: ";
cin >> n;
sum += n/100;
sum += n%100/10;
sum += n%10;
cout << "The sum of the digits of " << n << " is " << sum << endl;
return 0;
}

3.3 题目:求平方和。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
int n;
double sum = 0;
cout << "Please input n: ";
cin >> n;
for(int i=1;i<=n;i++)
 sum += i*i;
cout << "The sum of square from 1 to " << n << " is " << sum << endl;
return 0;
}

第四章

4.1 题目:输入一个三位数,将其百位,十位,个位颠倒后输出。

解答:程序如下

#include <iostream>
using namespace std;
int main()
int n;
cout << "Please input a three-digit number: ";
cin >> n;
cout << n%10 << n%100/10 << n/100 << endl;
return 0;

4.2 题目:输入一个字符串(不超过80个字符),输出其中的单词个数。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
char str[81];
cout << "Please input a string: ";
cin.getline(str,81);
int cnt = 0;
for(int i=0;str[i]!='\0';i++)
{
 if(str[i]==' '&&str[i+1]!=' ')
  cnt++;
}
cout << "The number of words is " << cnt+1 << endl;
return 0;
}

4.3 题目:输出乘法口诀表。

解答:程序如下

#include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=9;i++)
{
 for(int j=1;j<=i;j++)
  cout << j << "*" << i << "=" << i*j << '\t';
 cout << endl;
}
return 0;
}

综上,以上是《C++程序设计第三版》中第一至第四章的经典题目及其答案,希望能够对读者有所启发,并在学习编程的过程中取得更佳的成果。

  
  

评论区