程序设计思维与方法B
第一次课后作业:

上机目标:
1.熟悉VS2010等集成开发环境,能够独立地编辑,编译 、链接,生成正确的可执行程序
2.完成简单的输入-输出功能及数据运算功能
3.通过查找资料,自学掌握C++输入输出操纵符的基本使用方法

基础编程题:

  1. 学生个人信息显示
    编写一个学生个人信息的屏幕显示程序。其中同学姓名学号为的真实信息。输出信息格式如下图所示。鼓励在此基础上进行界面改进。
1
2
3
4
5
6
7
8
9
10
11
12
 #include <iostream>
using namespace std;
int main() {
cout<<"==========================================="<<endl;
cout<<"| 学生信息 |"<<endl;
cout<<"==========================================="<<endl;
cout<<"| 姓名:刘渤源 |"<<endl;
cout<<"| 学号: |"<<endl;
cout<<"| 电话: |"<<endl;
cout<<"==========================================="<<endl;
return 0;
}
  1. 温度转换
    通过键盘输入一个华氏温度,程序将其转化为相应的摄氏温度,转化公式为:C=5/9*(F-32). 输出的摄氏温度保留2位小数点。输出见下图。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
double f,c;
cout << "==========================================="<<endl;
cout << "请输入要转换的华氏温度:";
cin>>f;
c=5.0 / 9.0 * (f - 32);
cout << "==========================================="<<endl;
printf("%.2f华氏度等于%.2f摄氏度\n",f,c);
//cout<< f << c << setprecision(2);
cout << "==========================================="<<endl;
return 0;
}

3. 大写字母转换成小写字母
从键盘输入一个大写英文字母,输出相应的小写字母。

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main() {
char letter;
cout << "Please enter a character:";
cin>>letter;
cout << "The lower case of " << letter << " is:";
cout << (char) ( (int) (letter + 32) ) << endl;
return 0;
}

4. 求平方根
输入1 个实数x,计算并输出其平方根(保留1 位小数)。 提示:利用setprecision()设定输出精度。

1
2
3
4
5
6
7
8
9
10
11
12
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double a;
cout << "The input is:";
cin >> a;
cout << fixed << setprecision(1);
cout << "The square root of " << a << " is:" << sqrt(a) << endl;
return 0;
}
  1. 数制转换
    输入1个10进制整数,输出其8进制和16进制的表示形式。提示:利用hex、oct、dec设定输出进制,showbase设置输出的进制标志。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int orig,cach;
cout << "Please enter an integer:";
cin >> orig;
cout << "The hex form of " << orig << " is: " << showbase << hex << orig << endl;
/*
showbase设置输出进制模式
并且影响后续的全部输出
直到遇到下一个showbase
hex 十六进制0x
oct 八进制(以0开头)
dec 十进制
*/
cout << "The oct form of " << orig << " is: " << showbase << oct << orig << endl;
return 0;
}

设计类编程题:
6. 计算旅途时间
输入2 个整数time1 和time2分别表示火车的出发时间和到达时间,计算并输出旅途时间。有效的时间范围是0000 到2359,不需要考虑出发时间晚于到达时间的情况。
例:输入: 711 1410(出发时间是7:11,到达时间是14:10)
输出: The train journey time is 6 hrs 59 mins.

  1. 数字加密
    输入1 个四位数,将其加密后输出。方法是将该数每一位上的数字加9,然后除以10 取余,做为该位上的新数字,最后将第1 位和第3 位上的数字互换,第2 位和第4 位上的数字互换,组成加密后的新数。
    例:输入: The original number is : 1257
    输出: The encrypted number is 4601