1 编写函数实现十进制正整数到十四进制数的转换,在屏幕输出转换结果。
说 明:用0, 1, 2, 3,....., 8, 9, A, B, C, D表示十四进制的基本的14个数。
例:键盘输入14,屏幕输出10。
2 结构RECT可以表示一个平面上的矩形区域 :
struct RECT
{
int left, top, right, bottom;
};
如果给定两个矩形区域A和B,请构造一个函数,用若干矩形区域表示出区域A被B切掉之后剩余的区域,并在屏幕输出结果。例如,区域 (0, 0)-(2, 2) 被 (1, 1)-(3, 3) 切掉之后,剩余的两个区域可以表示为 (0, 0)-(2, 1) 和 (0, 1)-(1, 2),或者是 (0, 0)-(1, 2) 和 (1, 0)-(2, 1)(两种表示方式均可)。
3 文件名:3.cpp
功 能:编程实现猜词游戏
说 明:对于单词“hello”,程序提示输出:?????,等待用户输入。
用户输入时,若单词包含该字母,如“l”,则程序显示输出“??ll?”;
若单词不含该字母,如“a”,则程序提示用户猜错。
继续等待用户输入,直到用户猜出全部字母,或输入错误次数超过最大允许出错次数,游戏结束。
条 件:1) 单词由程序内定,由全小写字母组成
2) 提示输出问号数量等于单词长度
3) 最大允许出错次数等于单词长度
哪位高手写一下代码....小弟做了..感觉不是很好...想看一下高手是怎么写的??
========================================================================
1:
void fun( int input)
{
if (input >= 14)
fun(input/14);
printf("%c","0123456789ABCD"[input%14]);
}
2.0
#include
using namespace std ;
class Rect
{
public:
Rect(int x, int y , int m ,int n)
{
left = x ;
top = y ;
right = m ;
bottom = n ;
}
int left, top, right, bottom;
};
void printArea(int x, int y , int m ,int n)
{
cout < <"(" < }
void intersection(const Rect &r1, const Rect &r2)
{
//有15种相切情况,穷举每一种情况。
if(r2.bottom < r1.bottom && r2.bottom > r1.top)
{
if(r2.left <= r1.left && r2.right >= r1.right && r2.top <= r1.top)
{
printArea(r1.left , r2.bottom, r1.right , r1.bottom) ;//1
return ;
}
else if(r2.left <= r1.left && r2.right >= r1.right && r2.top > r1.top)
{
printArea(r1.left , r1.top , r1.right, r2.top) ;//2
printArea(r1.left , r2.bottom, r1.right , r1.bottom) ;
return ;
}
else if(r2.right < r1.right && r2.right > r1.left && r2.left <= r1.left && r2.top <= r1.top)
{
printArea(r1.left , r2.bottom , r2.right , r1.bottom) ;//3
printArea(r2.right ,r1.top , r1.right ,r1.bottom ) ;
return ;
}
else if(r2.left > r1.left && r2.left < r1.right && r2.right >= r1.right && r2.top <= r1.top)
{
printArea(r1.left ,r1.top , r2.left ,r1.bottom) ;//4
printArea(r2.left ,r2.bottom ,r1.right ,r1.bottom) ;
return ;
}
else if(r2.left > r1.left && r2.right < r1.right && r2.top <= r1.top)
{
printArea(r1.left,r1.top , r2.left ,r2.bottom) ;//5
printArea(r2.right , r1.top , r1.right ,r2.bottom) ;
printArea(r1.left , r2.bottom , r1.right ,r1.bottom) ;
return ;
}
}
if(r2.top > r1.top && r2.top < r1.bottom && r2.bottom >= r1.bottom )
{
if(r2.left <= r1.left && r2.right >= r1.right )
{
printArea(r1.left ,r1.top ,r1.right ,r2.top) ;//6
return ;
}
else if(r2.right < r1.right && r2.right > r1.left && r2.left <= r1.left)
{
printArea(r1.left , r1.top ,r2.left , r1.bottom) ;//7
printArea(r2.left ,r2.bottom ,r1.right ,r1.bottom) ;
return;
}
else if(r2.left > r1.left && r2.left < r1.right && r2.right >= r1.right)
{
printArea(r1.left ,r1.top , r2.left , r1.bottom) ;//8
printArea(r2.left , r1.top ,r1.right ,r2.top) ;
return ;
}
else if(r2.left > r1.left && r2.right < r1.right )
{
printArea(r1.left ,r1.top ,r1.right ,r2.top) ;//9
printArea(r1.left ,r2.top ,r2.left , r1.bottom) ;
printArea(r2.right ,r2.top ,r1.right ,r1.bottom) ;
return ;
}
}
if(r2.right > r1.left && r2.right < r1.right)
{
if(r2.top <= r1.top && r2.bottom >= r1.bottom && r2.left <= r1.left)
{
printArea(r2.right ,r1.top ,r1.right,r1.bottom) ;//10
return ;
}
else if(r2.top <= r1.top && r2.bottom >= r1.bottom && r2.left > r1.left)
{
printArea(r1.left ,r1.top ,r2.left ,r1.bottom) ;//11
printArea(r2.right ,r1.top ,r1.right ,r1.bottom) ;
return ;
}
else if(r2.top > r1.top && r2.bottom < r1.bottom && r2.left <= r1.left)
{
printArea(r1.left ,r1.top ,r2.right ,r2.top) ;//12
printArea(r1.left ,r2.bottom ,r2.right , r1.bottom) ;
printArea(r2.right , r1.top ,r1.right ,r1.bottom) ;
return ;
}
}
if(r2.left < r1.right && r2.left >r1.left && r2.right >= r1.right)
{
if(r2.top <= r1.top && r2.bottom >= r1.bottom )
{
printArea(r1.left ,r1.top , r2.left , r2.bottom) ;//13
return ;
}
else if(r2.top > r1.top && r2.bottom < r1.bottom)
{
printArea(r1.left , r1.top ,r2.left , r1.bottom) ;//14
printArea(r2.left ,r1.top ,r1.right ,r2.top) ;
printArea(r2.left ,r2.bottom , r1.right ,r1.bottom) ;
return ;
}
}
if(r2.left > r1.left && r2.right < r1.right && r2.top > r1.top && r2.bottom < r1.bottom)
{
printArea(r1.left ,r1.top ,r2.left ,r2.top) ;//15
printArea(r2.left,r1.top ,r2.right ,r2.top) ;
printArea(r2.right,r1.top ,r1.right ,r2.top) ;
printArea(r1.left , r2.top ,r2.left ,r2.bottom) ;
printArea(r2.right ,r2.top ,r1.right ,r2.bottom) ;
printArea(r1.left , r2.bottom , r2.left , r1.bottom) ;
printArea(r2.left ,r2.bottom , r2.right , r1.bottom) ;
printArea(r2.right , r2.bottom , r1.right,r1.bottom) ;
return ;
}
cout < <"不相切!" <
}
int main()
{
Rect r1(0,0,2,2) ;
Rect r2(1,1,3,3) ;
//Rect r1(0,0,3,3) ;
//Rect r2(1,1,2,2) ;
//Rect r1(0,0,3,3) ;
//Rect r2(0,1,3,2) ;
intersection(r1,r2) ;
return 0 ;
}
3.0
#include <iostream>
using namespace std;
#include <string>
void guessword(char * srcword){
int len = strlen(srcword);
if(len==0)
return;
char * buf = new char [len+1];
memset(buf, 0x00, len+1);
int i;
for(i=0; i<len; i++){
buf[i] = '?';
}
cout<<buf<<endl;
int errortime = 0;
do{
char chuser;
cin>>chuser;
bool errorflag = true;
for(i=0; i<len; i++){
if(srcword[i] == chuser){
errorflag = false;
buf[i] = chuser;
}
}
cout<<buf<<endl;
if(errorflag) // 说明这次猜错了
errortime ++;
if(errortime == len){
cout<<"Error! You have tried so many times!"<<endl;
break;
}
for(i=0; i<len; i++){
if(buf[i]=='?')
break;
}
if(i==len){
cout<<"Congratulations! You are right!"<<endl;
break;
}
}while(1);
delete [] buf;
}
void main(){
char * srcword = "appreciate";
guessword(srcword);
}