C++习题(from the teacher)Apr.
来源:互联网 发布:mac截图怎么截 编辑:程序博客网 时间:2024/06/05 19:55
Apr.
04_22_1
//4.22-1 字符正方形
#include <iostream>
using namespace std;
int main(void)
{
int n,flag=0;
char ch;
cout<<"Please enter a number(1<=n<=50) and char(A~Z)"<<endl;
do
{
cin>>n;
cin>>ch;
if (n<1||n>50)
{
cout<<"Please enter a number(1<=<=50)"<<endl;
continue;
}
if (ch<'A'||ch>'Z')
{
cout<<"Please enter a char(A~Z)"<<endl;
continue;
}
flag=1;
}while (!flag);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<ch;
}
cout<<"/n";
}
return 0;
}
说明:此程序在对错误输入处理部分仍有问题,比如输入”cc”,”5cc”都会造成无限循环。疑似输入流缓冲区的问题,其中也包含了int和char间的强制转换。
04_22_2
//04.22-2 字符三角形
#include <iostream>
using namespace std;
int main(void)
{
int a=7,b=13; //等腰三角形隐含着存在b=2a-1,对4_23_1有所启发
int flag=0;
char ch;
cout<<"Please enter a char (A~Z)"<<endl;
do
{
cin>>ch;
if (ch>='A'&&ch<='Z')
{
flag=1;
}
else
{
cout<<"!!a char (A~Z)"<<endl;
continue;
}
}while (!flag);
for (int i=0;i<a;i++)
{
for (int j=i;j<a-1;j++)
{
cout<<' ';
}
for (int k=0;k<2*i+1;k++)
{
cout<<ch;
}
cout<<endl;
}
return 0;
}
04_23_1
//04.23-1 对称字符等腰三角形
#include <iostream>
using namespace std;
int main(void)
{
int n; //底长为2n-1
cin>>n;
char ch='A';
for (int i=0;i<n;i++)
{
for (int j=i;j<n-1;j++)
{
cout<<' ';
}
for (int k=i+1;k>0;k--)
{
cout<<char(ch+k-1);
}
//运行到此k等于0了
for (;k<i;k++)
{
cout<<char(ch+k+1);
}
cout<<endl;
}
return 0;
}
04_23_2
//04.23-2 文本文件读出并显示
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h> //提供对abort()函数的支持
void fun(int ,int );
void main(void)
{
fstream in;
in.open("04_23_2.txt",ios::in);
if (!in)
{
cerr<<"04_23_2.txt can't open./n";
abort(); //异常终止一个进程
}
char s[80];
while (!in.eof())
{
in.getline(s,sizeof(s));
cout<<s<<endl; //加回车是因为,getline()会去掉每行的换行符
}
in.close();
}
04_24_1
//04.24-1 读取CPP源文件并加行号显示
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
void fun(int ,int );
void main(void)
{
ifstream in;
in.open("04241.cpp");
if (!in)
{
cerr<<"04_23_2.cpp can't open./n";
abort();
}
int line_number=0;
char s[80];
while (!in.eof())
{
cout<<++line_number<<'/t';
in.getline(s,sizeof(s));
cout<<s<<endl;
}
in.close();
}
04_24_2
//04.24-2 测定基本数据类型所占用的位数
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ofstream outfile;
outfile.open("0424-output.txt");
outfile<<"The Width of /"int/" is "<<sizeof(int)<<" bits"<<endl;
outfile<<"The Width of /"short/" is "<<sizeof(short)<<" bits"<<endl;
outfile<<"The Width of /"long/" is "<<sizeof(long)<<" bits"<<endl<<endl;
outfile<<"The Width of /"char/" is "<<sizeof(char)<<" bits"<<endl<<endl;
outfile<<"The Width of /"float/" is "<<sizeof(float)<<" bits"<<endl;
outfile<<"The Width of /"double/" is "<<sizeof(double)<<" bits"<<endl;
outfile<<"The Width of /"long double/" is "<<sizeof(long double)<<" bits"<<endl;
outfile.close();
/*"The width of "int"","The width of "int"","The width of "int"",
"The width of "int"","The width of "int"","The width of "int"",*/
return 0;
}
说明:感觉这么写,很笨,但并未想出太好的方法。尝试用过字符数组+循环,但用C++处理不好,如果换成C还凑合。
04_25_1
//04.25-1格式阵列输出
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void draw(int n);
void main(void)
{
int n;
ofstream outFile ("datas.dat");
if(!outFile)
{
cerr<<"File could not be opened"<<endl;
exit(1);
}
//输入数据到数据文件,每行以问好提示,回车隔开,Ctrl+Z结束输入
cout<<"Enter the datas."<<endl
<<"Enter end-of-file to end input./n?";
int data;
while (cin>>data)
{
outFile<<data<<endl;
cout<<"?";
}
//读取文件
ifstream inFile("datas.dat");
if(!inFile)
{
cerr<<"File could not be opened"<<endl;
exit(1);
}
while(inFile>>n)
{
draw(n);
cout<<endl;
}
}
//格式阵列输出
void draw(int n)
{
int k=-1;
int a[100];
for(int i=1;i<=n;i++)
{
cout.setf(ios::right,ios::adjustfield);
cout<<setw(2)<<i<<" ";
a[i]=k;
k=(a[i]+1)%n;
for(int j=1;j<=n;j++)
{
cout<<setw(3)<<k;
k=(k+1)%n;
}
cout<<endl;
}
}
04_25_2
//04.25-2 累加算法求和
#include <iostream>
#include <fstream>
using namespace std;
int add(int n);
int main(void)
{
int n;
ofstream outFile ("datas.dat");
if(!outFile)
{
cerr<<"File could not be opened"<<endl;
exit(1);
}
cout<<"Enter the datas."<<endl
<<"Enter end-of-file to end input./n?";
//以问号提示,回车隔开
int data;
while(cin>>data)
{
outFile<<data<<endl;
cout<<'?';
}
ifstream inFile("datas.dat");
if(!inFile)
{
cerr<<"File could not be opened"<<endl;
exit(1);
}
while(inFile>>n)
{
cout<<add(n)<<endl;
}
return 0;
}
//求和函数
int add(int n)
{
int s=0;
while(n>0)
{
s+=n;
n--;
}
return s;
}
04_26_1
//04.26-1 求1!到n!的和
#include <iostream>
#include <fstream>
int add(int );
long fun(int );
using namespace std;
int main(void)
{
int n;
//写数据文件
ofstream outFile ("datas.dat");
if (!outFile)
{
cerr<<"File could not be opened"<<endl;
exit(1);
}
cout<<"Enter the datas."<<endl
<<"Enter end-of-file to end input./n";
//开始输入
int data;
while (cin>>data)
{
outFile<<data
<<endl;
// cout<<"?";
}
//打开数据文件
ifstream inFile ("datas.dat");
if(!inFile)
{
cerr<<"File could not be opened"<<endl;
exit(1);
}
while(inFile>>n)
{
cout<<add(n)<<endl;
}
return 0;
}
//调用fun(),实现题目公式
int add(int m)
{
long k=0;
while (m>0)
{
k+=fun(m);
m--;
}
return k;
}
//递归实现求阶乘
long fun(int m)
{
long ans;
if (m>0)
{
ans=fun(m-1)*m;
}
else
ans=1;
return ans;
}
04_26_2
#include <iostream>
long add(int ,int );
long fun(int ,int );
using namespace std;
void main(void)
{
int q=2;
int n=3;
cout<<fun(q,n)<<endl;
cout<<add(q,n)<<endl;
}
long add(int q,int m)
{
int k=0;
while (m>=0)
{
k+=fun(q,m);
m--;
}
return k;
}
//求N次幂
long fun(int q,int m)
{
int k=1;
for (;m>0;m--)
{
k=q*k;
}
return k;
}
说明:也没有达到从数据文件中输入数据!!
04_27_1
04_28_1
//04.28-1 5位以内的对称素数
#include <iostream>
#include <cmath>
using namespace std;
const int TRUE(1);
const int FALSE(0);
int digit(int );
int PrimeN(int );
int SymmetryN(int );
void main(void)
{
int a[50];
int n;
cout<<"输入你将处理的数据个数n:";
cin>>n; //
for (int i=0;i<n;i++)
{
cin>>a[i];
}
for (int j=0;j<n;j++)
{
cout<<a[j]<<'/t';
if (digit(a[j])<=5 && SymmetryN(a[j]) && PrimeN(a[j]))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
//位数判断
int digit(int m)
{
int count=0;
while(m>0)
{
m/=10;
count++;
}
return count;
}
//判断一个正整数是否为一个对称数,是返回TRUE
int SymmetryN(int m)
{
int i=0;
int a[100];
int n=m;
while(n>0)
{
a[i]=n%10;
n/=10;
i++;
}
for (int j=0,k=i,s=0;j<i;j++,k--)
{
s+=a[j]*pow(10,(k-1));
}
if (s==m)
return TRUE;
else
return FALSE;
}
//判断一个正整数是否为素数,是返回TRUE,否则返回FALSE
int PrimeN(int n)
{
for(int i=2;i<n;i++)
{
if (n%i != 0)
return TRUE;
else
return FALSE;
}
}
04_28_2
//04.28-2 “顺”序列
#include <iostream>
using namespace std;
void evaluate(int n,int b[]);
void sort(int n,int b[]);
int fun(int n,int b[]);
void main(void)
{
int n;//真正处理多少数据
int a[10]; //n个卡片,不能写作a[n],即使n已经赋过值
cout<<"请输入卡片个数n(1<=n<=10):";
//确保n值符合范围
cin>>n;
if(n<=10 && n>=1)
;
else
{
cout<<"!! 1<=n<=10:";
cin>>n;
}
evaluate(n,a);
sort(n,a);
cout<<"排序后的数列:";
for(int i=0;i<n;i++)
cout<<a[i]<<' ';
cout<<endl<<endl<<"顺?/t";
if (fun(n,a))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
//数组赋值,即卡片赋值
void evaluate(int n,int b[])
{
cout<<"请输入每张卡片值m(1<=m<=100):"<<endl;
for(int i=0;i<n;i++)
{
cin>>b[i];
/*
if(b[i]>=1 && b[i]<=100)
;
else
{
cout<<"请重新输入该值:"<<endl;
}
*/
}
}
//排序
void sort(int n,int b[])
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(b[j]>b[j+1])
{
int t;
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
}
}
//判断一个顺序数列是否为等差数列
int fun(int n,int b[])
{
for(int i=0;i<n-1;i++)
{
if( (b[i+1]-b[i]) == (b[i+2]-b[i+1]) )
return 1;
else
return 0;
}
}
Note:红色代码所代表的错误处理方式,出现在这种循环赋值语句,将带来一个trouble。只有你每输入一个数据,按回车它才能处理。如果你输入一行数据,再回车,它只能处理最后一个数据。出现这类现象的原因是由于输入缓冲区的问题。使用时应给予重视!!
04_29_1
//04_29_1十进制转换成二进制,并输出
void dec_bina(int n)
{
for (int i=0,count=0;i<n;i++)
{
a[i]=n%2;
n/=2;
count++;
}
for (int j=count;j>0;j--)
{
cout<<a[j];
}
}
04_29_2
//04.29-2 Fibonacci数
#include <iostream>
using namespace std;
long Fibonacci(int n);
void main()
{
int n;
cin>>n; //求第n项
cout<<Fibonacci(n);
}
long Fibonacci(int n)
{
int k;
if(n>1)
k=Fibonacci(n-1)+Fibonacci(n-2);
else if(n==1)
k=1;
else
k=0;
return k;
}
Note: 仔细看下老师那里所谓的第n项,是把“0”仍了。
04_30_1
//04_30_1
//百钱买百鸡
#include <iostream>
using namespace std;
void main(void)
{
int cook,hen,chicken; //翁、母、雏
int i=1;
for (cook=0;cook<=20;cook++)
{
for(hen=0;hen<=33;hen++)
{
chicken=100-cook-hen;
if (chicken%3==0 && 5*cook+3*hen+chicken/3 ==100)
{
cout<<i<<':'<<"cook="<<cook<<" hen="<<hen<<" chicken="<<chicken<<endl;
i++;
}
}
}
}
04_30_2
//04.30-2 歌手大赛评分
- C++习题(from the teacher)Apr.
- C++习题(from the teacher)May.
- teacher.c
- The Teacher Story
- the student-teacher paradigm
- Come from my OS teacher
- 06-Student Becomes the Teacher
- the c programming language 习题 第二章
- The c progamming language 习题3.3
- from c to c++: using the boolean
- 数据结构---习题(C++)
- 波老师(teacher)
- 英语练习 61 The smartest teacher
- Python 入门教程 10 ---- Student Becomes the Teacher
- Python 入门教程 10 ---- Student Becomes the Teacher
- Codeacademy Python-06-Student Becomes the Teacher
- Codeacademy Python-07-Student Becomes the Teacher
- Using the WebBrowser Control from C/C++ (From MSDN)
- 擦除式图片轮番显示效果
- 关于四舍五入的问题,toFixed()
- 用Javascript和DHTML构建的矢量图形库
- soap学习笔记(一)
- 用Javascript写的一个映射表类
- C++习题(from the teacher)Apr.
- TreeView(扩充XMLSelTree)(二)
- 排序Select中Option项的一个示例
- javax.servlet.ServletException: org/aspectj/lang/Signature,说找不到类
- 解决国外空间access数据库乱码问题
- 在网页中控制wmplayer播放器
- 让网页自动穿上外套
- 网站流量统计代码
- 获取mssql的xml返回结构的方法