第一章 C++的初步认识
1、分析下面程序运行结果
ThisisaC++program.
2、分析下面程序运行结果
a+b=33
3、分析下面程序运行结果。
//输入三个数比较大小输出最小值
4、改正程序错误并分析运行结果。
//原程序提取运算符错误
//没有定义c
//没有返回值
#include <iostream>
using namespace std;
int main() {
int a,b,c;
c=a+b;
cout<<"a+b="<<a+b<<endl;
return 0;
}
5、改正程序错误并分析运行结果。
//定义一个add函数
//输入两数相加并输出结果
#include <iostream>
int add(int x, int y);
using namespace std;
int main() {
int a,b,c;
cin>>a>>b;
c=add(a,b);
cout<<"a+b="<<c<<endl;
return 0;
}
int add(int x, int y) {
int z=x+y;
return (z);
}
6、输入程序编译运行并分析结果
//定义一个排序函数
//从键盘键入三个数并按照从小到大输出
7、求2个或者3个正整数中的最大数,用带有默认参数的函数实现
#include <iostream>
using namespace std;
void x(int a,int b){
if(a>b)
cout<<a;
else
cout<<b;
}
int main(){
x(2,3);
return 0;
}
8、输入两个整数,将他们由大到小的顺序输出,要求使用变量的引用。
#include <iostream>
using namespace std;
void swap(int &a,int &b){
if (a>b) cout<<a<<b;
else cout <<b<<a;
}
int main () {
int a,b;
cin>>a>>b;
swap(a,b);
return 0;
}
9、对三个变量按由小到大顺序排序,要求使用变量的引用
#include <iostream>
using namespace std;
void res(int &x,int &y,int &z)
{ int temp;
if(x>y)
{ temp=y;y=x;x=temp;};
if(x>z)
{temp=z;z=x;x=temp;};
if(y>z)
{temp=y;y=z;z=temp;};
}
int main()
{ int a,b,c;
cout<<"输入三个数字"<<endl;
cin>>a>>b>>c;
res(a,b,c);
cout<<a<<" "<<b<<" "<<c;
}
10、编写一个程序,将两个字符串连接起来,结果取代第一个字符串。要求用string方法。
#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1="week",s2="end";
cout<<"s1="<<s1<<endl;
cout<<"s2="<<s2<<endl;
s1=s1+s2;
cout<<"The new string is:"<<s1<<endl;
return 0;
}
11、输入一个字符串,把其中的字符按逆序输出。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int i,n;
char temp;
cout<<"请输入一个字符串:"<<endl;
cin>>str; // 输入字符串//
n=str.size(); //读出字符串个数
for(i=0;i<n/2;i++) //判断语句,把最后一个给第一个,第一个再给回最后一个以此类推。//
{
temp=str[i];
str[i]=str[n-i-1];
str[n-i-1]=temp;
}
cout<<str<<endl;
return 0;
}
12、有5个字符串,要求对他们按由小到大顺序排列,用string方法。
#include <string>
#include <iostream>
using namespace std;
void Sort(string s[]);
int main() {
string s[5];//string型数组,类似c的二级字符数组,但好用一些
int i;
cout << "Please input strings:" << endl;
for (i = 0; i < 5; i++)//输入
{
cin >> s[i];
}
Sort(s);//排序
for (i = 0; i < 5; i++)//输出
{
cout << s[i] << endl;
}
return 0;
}
//冒泡排序
void Sort(string s[]) {
string temp;
int i, j;
for (i = 1; i < 5; i++) {
for (j = 0; j < 5 - i; j++) {
if (s[j] > s[j + 1]) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
return;
}
13、编写一个程序,用同一个函数名对n个数据进行从小到大的排序,数据类型可以是整型,单精度实型,双精度实型,用重载函数实现
#include <iostream>
#include<cstring>
using namespace std;
int main() {
int pl(int *q, int n);
double pl(double *p, int n);
float pl(float *p, int n);
int a, n;
cout << "您需要比较什么类型的数:\n(1)整形\n(2)单精度\n(3)双精度" << endl << "输入类型前的编号" << endl;
cin >> a;
cout << "请输入您需要比较数据的个数:" << endl;
cin >> n;
cout << "请输入" << n << "个数,用空格分开\n";
if (a == 1) {
int *p = new int[n];
for (int i = 0; i < n; i++)
cin >> p[i];
pl(p, n);
}
if (a == 2) {
double *p = new double[n];
for (int i = 0; i < n; i++)
cin >> p[i];
pl(p, n);
}
if (a == 3) {
float *p = new float[n];
for (int i = 0; i < n; i++)
cin >> p[i];
pl(p, n);
}
return 0;
}
int pl(int *q, int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (q[i] > q[j]) {
int x;
x = q[j];
q[j] = q[i];
q[i] = x;
}
}
}
for (int x = 0; x < n; x++)
cout << q[x] << " ";
return 0;
}
float pl(float *q, int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (q[i] > q[j]) {
float x;
x = q[j];
q[j] = q[i];
q[i] = x;
}
}
}
for (int x = 0; x < n; x++)
cout << q[x] << " ";
return 0;
}
double pl(double *q, int n) {
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (q[i] > q[j]) {
double x;
x = q[j];
q[j] = q[i];
q[i] = x;
}
}
}
for (int x = 0; x < n; x++)
cout << q[x] << " ";
return 0;
}