没有合适的资源?快使用搜索试试~ 我知道了~
C++面向程序设计(谭浩强)1~8章答案
需积分: 0 1 下载量 112 浏览量
2013-12-21
20:48:54
上传
评论
收藏 225KB DOC 举报
温馨提示
试读
24页
这里有详细的答案,帮助大家完成C++。大家都可以参考下
资源详情
资源评论
资源推荐
7
第一章
5:
#include <iostream>
using namespace std;
int main()
{
cout<<"This"<<"is";
cout<<"a"<<"C++";
cout<<"program."<<endl;
return 0;
}
6:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=23;
c=a+b;
cout<<"a+b=";
cout<<c;
cout<<endl;
return 0;
}
7:
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
int f(int x,int y,int z);
cin>>a>>b>>c;
c=f(a,b,c);
cout<<c<<endl;
return 0;
}
int f(int x,int y,int z)
{
int m;
if (x<y) m=x;
else m=y;
if (z<m) m=z;
return(m);
}
8: #include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b;
c=a+b;
cout<<"a+b="<<a+b<<endl;
return 0;
}
9:
#include <iostream>
using namespace std;
int main()
{int add(int x,int y);
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 c;
c=x+y;
return(c);
}
10:
#include <iostream>
using namespace std;
int main()
{void sort(int x,int y,int z);
int x,y,z;
cin>>x>>y>>z;
sort(x,y,z);
return 0;
}
void sort(int x, int y, int z)
{
int temp;
if (x>y)
{temp=x;x=y;y=temp;}
//{ }内 3 个语句的作用是将
x 和 y 的值互换)
if (z<x)
cout<<z<<','<<x<<','<<y<<en
dl;
else if (z<y)
cout<<x<<','<<z<<','<<y<<en
dl;
else
cout<<x<<','<<y<<','<<z<<en
dl;
}
11:
#include <iostream>
using namespace std;
int main()
{int max(int a,int b,int c=0);
int a,b,c;
cin>>a>>b>>c;
cout<<"max(a,b,c)="<<max(
a,b,c)<<endl;
cout<<"max(a,b)="<<max(a,
b)<<endl;
return 0;
}
int max(int a,int b,int c)
{if(b>a) a=b;
if(c>a) a=c;
return a;
}
12:
#include <iostream>
using namespace std;
int main()
7
{
void change(int ,int );
int a,b;
cin>>a>>b;
if(a<b) change(a,b);
cout<<"max="<<a<<"
min="<<b<<endl;
return 0;
}
void change(int ,int )
{
int r1,r2,temp;
temp=r1;
r1=r2;
r2=temp;
}
13:
#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
int a,b,c,a1,b1,c1;
cout<<"Please enter 3
integers:";
cin>>a>>b>>c;
a1=a;b1=b;c1=c;
sort(a1,b1,c1);
cout<<a<<" "<<b<<"
"<<c<<" in sorted order is ";
cout<<a1<<" "<<b1<<"
"<<c1<<endl;
return 0;
}
void sort(int &i,int &j,int &k)
{ void change(int &,int &);
if (i>j) change(i,j);
if (i>k) change(i,k);
if (j>k) change(j,k);
}
void change(int &x,int &y)
{ int temp;
temp=x;
x=y;
y=temp;
}
14:
#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;
}
15:
#include <iostream>
#include <string>
using namespace std;
int main()
{ string str;
int i,n;
char temp;
cout<<"please input a
string:";
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;
}
16:
#include <iostream>
#include <string>
using namespace std;
int main()
{ int i;
string
str[5]={"BASIC","C","FORTRA
N","C++","PASCAL"};
void sort(string []);
sort(str);
cout<<"the sorted
strings :"<<endl;
for(i=0;i<5;i++)
cout<<str[i]<<" ";
cout<<endl;
return 0;
}
void sort(string s[])
{int i,j;
string t;
for (j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (s[i]>s[i+1])
{t=s[i];s[i]=s[i+1];s[i+1]=t;}
}17: #include <iostream>
#include <string>
using namespace std;
int main()
{
long c[5]={10100,-123567,
1198783,-165654, 3456};
int a[5]={1,9,0,23,-45};
Eoat b[5]={2.4, 7.6, 5.5, 6.6,
-2.3 };
void sort(int []);
void sort(Eoat []);
void sort(long []);
sort(a);
sort(b);
sort(c);
return 0;
}
void sort(int a[])
{int i,j,t;
7
for (j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted
numbers :"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
void sort(long a[])
{int i,j;
long t;
for (j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted
numbers :"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
void sort(Eoat a[])
{int i,j;
Eoat t;
for (j=0;j<5;j++)
for(i=0;i<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted
numbers :"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
18: #include <iostream>
#include <string>
using namespace std;
template <typename T>
void sort(T a[])
{int i,j,min;
T t;
for(i=0;i<5;i++)
{min=i;
for (j=i+1;j<5;j++)
if(a[min]>a[j]) min=j;
t=a[i]; a[i]=a[min];
a[min]=t;
}
cout<<"the sorted
numbers :"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}
int main()
{ int a[5]={1,9,0,23,-45};
Eoat b[5]={2.4, 7.6, 5.5, 6.6,
-2.3 };
long c[5]={10100,-123567,
1198783,-165654, 3456};
sort(a);
sort(b);
sort(c);
return 0;
}
第二章
1
#include <iostream>
using namespace std;
class Time
{
public:
void set_Gme();
void show_Gme();
private: //成员改
为公用的
int hour;
int minute;
int sec;
};
void Time::set_Gme() //
在 main 函数之前定义
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void
Time::show_Gme() //在
main 函数之前定义
{
cout<<hour<<":"<<minute<
<":"<<sec<<endl;
}
int main()
{Time t1;
t1.set_Gme();
t1.show_Gme();
return 0;
}
2:
#include <iostream>
using namespace std;
class Time
{public:
void set_Gme(void)
{cin>>hour;
cin>>minute;
cin>>sec;
}
void show_Gme(void)
{cout<<hour<<":"<<minut
e<<":"<<sec<<endl;}
7
private: int hour;
int minute;
int sec;
};
Time t;
int main()
{
t.set_Gme();
t.show_Gme();
return 0;
}
3:
#include <iostream>
using namespace std;
class Time
{public:
void set_Gme(void);
void show_Gme(void);
private:
int hour;
int minute;
int sec;
};
void Time::set_Gme(void)
{cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_Gme(void)
{cout<<hour<<":"<<minute<
<":"<<sec<<endl;}
Time t;
int main()
{ t.set_Gme();
t.show_Gme();
return 0;
}
4:
//xt2-4-1.cpp(main.cpp)
#include <iostream>
using namespace std;
#include "xt2-4.h"
int main()
{Student stud;
stud.set_value();
stud.display();
return 0;
}
//xt2-4-2.cpp( 即
student.cpp)
#include "xt2-4.h"
//在此文件中进行函数的定
义
#include <iostream>
using namespace std;
//不要漏写此行
void Student::display( )
{ cout<<"num:"<<num<<end
l;
cout<<"name:"<<name<<e
ndl;
cout<<"sex:"<<sex<<endl;
}
void Student::set_value()
{ cin>>num;
cin>>name;
cin>>sex;
}
5:
//xt2-5-1.cpp(Hle1.cpp)
#include <iostream>
#include "xt2-5.h"
int main()
{Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}
//xt2-5-2.cpp(arraymax.cpp)
#include <iostream>
using namespace std;
#include "xt2-5.h"
void Array_max::set_value()
{ int i;
for (i=0;i<10;i++)
cin>>array[i];
}
void Array_max::max_value()
{int i;
max=array[0];
for (i=1;i<10;i++)
if(array[i]>max)
max=array[i];
}
void
Array_max::show_value()
{cout<<"max="<<max<<endl
;
}
6:解法一
#include <iostream>
using namespace std;
class Box
{public:
void get_value();
Eoat volume();
void display();
public:
Eoat lengh;
Eoat width;
Eoat height;
};
void Box::get_value()
{ cout<<"please input lengh,
width,height:";
cin>>lengh;
cin>>width;
cin>>height;
}
剩余23页未读,继续阅读
123liling
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- TG-2024-04-26-183849310.mp4
- 汇编语言的概要介绍与分析
- 个人博客系统设计与开发.zip
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- htmlzwbjq_downyi.com.zip
- 无头单向非循环链表的实现(Test.c)
- 无头单向非循环链表的实现(SList.c)
- 浏览器重定向插件更新文件
- SSA-BP麻雀算法优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0