首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

【一些零碎的有关问题~(注释详细,只是贴上来没有缩进了,麻烦各位了~)】

2012-03-03 
【一些零碎的问题~~(注释详细,只是贴上来没有缩进了,麻烦各位了~~)】//程序作用输入公里数和每箱油使用的加

【一些零碎的问题~~(注释详细,只是贴上来没有缩进了,麻烦各位了~~)】
//程序作用     输入公里数和每箱油使用的加仑数,计算每箱油每加仑跑出的公里数
//                     并且设置可以再次计算的选择性分支
//编写者         一号   魔法师
//编写日期     开学前

#include   <iostream.h>
void   main()
{
float   gallons,miles,speed;
char   respond=1;
while(respond)
{
cout < < "Enter   the   gallons   used   =>   ";//输入加仑
cin> > gallons;
cout < < "Enter   the   miles   driven   =>   ";//输入公里
cin> > miles;
speed=miles/gallons;                               //计算速度
cout < < "The   miles/gallons   for   this   tank   was   " < <speed < <endl;

char   TypeUncorrect=1;
while(TypeUncorrect)//判断格式是否正确,开始默认为1使语句块有效
{
cout < < "Continue   to   compute?(Y/N)   =>   ";
cin> > respond;                                   //给出是否继续运算的回应
switch(respond)
{
case   'Y ':case 'y ':                          
TypeUncorrect=0;break;         //格式正确,不再次询问是否再次计算
case 'N ':case 'n ':                            
respond=0;TypeUncorrect=0;break;
case   '\n ':
cout < < "Uncorrect.... " < <endl;
default:                                               //输入格式错误,给出出错信息
cout < < "Uncorrect   type,input   again   please " < <endl;
}
}
}
}
//程序中的问题:
//如何控制输出的精度?比如要小数点后8位哪怕是0也输出
//case   '\n '是无效的,如何使回车符也被认作不正确输入格式?
//输入数字时如果有字母混入立刻死循环,这个是硬伤,不知道如何处理


//程序目的     读入一个长方形的长和宽,然后用*绘制出一个空心正方形
//                     边长为输入值,长度1~20.例如,若输入5,则输出如下图形
//                     *****
//                     *       *
//                     *       *
//                     *       *
//                     *****
//                     将此题扩展至长方形,长宽不超过20
//编写者         一号   魔法师
//编写日期     开学前
#include   <iostream.h>
#define   MAXLEN_WID   20                                               //指定最大长宽为20
void   main()
{
int   length,width;                                               //分别记录长方形的长宽
int   TypeUncorrect=0;                                         //判断回应是否重新执行程序的格式是否错误,默认为正确


char   redraw;
do
{
cout < < "Rectangle         Len   or   Wid(   2- " < <MAXLEN_WID < < "   ) " < <endl;
cout < < "     Length   =>   ";                               //输入长度  
cin> > length;
if   (length <2   ||   length   > MAXLEN_WID)   //判断输入值是否正确
{
cout < < "Uncorrect   length " < <endl < <endl;
continue;
}
cout < < "     Width   =>   ";                                 //输入宽度
cin> > width;
if   (width <2   ||   width   > MAXLEN_WID)       //判断输入值是否正确
{
cout < < "Uncorrect   width " < <endl < <endl;
continue;
}
for   (int   len=0;len <length;len++)         //绘制第一行*
cout < < "* ";
cout < <endl;
for   (int   wid=0;wid <width;wid++)           //绘制中间的两边为*中间为空的部分
{
cout < < "* ";
for   (int   len=0;len <length-2;len++)
cout < < "   ";
cout < < "* " < <endl;
}
for   (len=0;len <length;len++)                 //绘制最后一行*
cout < < "* ";
do
{                                                                       //询问是否再次绘制长方形
cout < <endl < < "Redraw   a   rectangle?(Y/N)   =>   ";
cin> > redraw;                                         //给出答复
switch(redraw)                                     //判断答复的格式
{
case   'Y ':case 'y ':
redraw=1,TypeUncorrect=0;break;
case   'N ':case 'n ':
redraw=0,TypeUncorrect=0;break;
default:
cout < < "Uncorrect   type,input   again   please ";
TypeUncorrect=1;
}
}
while(TypeUncorrect);                               //当答复格式不正确时重复输入
}
while(redraw);                                                     //若再次绘制则程序重新执行
}
//本来最后一个for里面是int   len=0;提示redifination于是去掉int,可以了,
//但是前面第三个for里面不也定义了int   len=0吗?为什么和第一个for里面的不冲突?


//程序目的   输入一个五位数,判断其是否为回文(如12321就是回文)
//                   提示   用除法和求模运算符将数字分为各个位
//                   将此题扩展至任意位数(但不考虑上溢下溢)
//编写者       一号   魔法师
//编写日期   开学前
#include   <iostream.h>
#define   Figures   10
int   IsPalindrome(long   num);                             //此函数用于判断输入值是否是回文


void   main()
{
long   num;                                                         //以此变量记录输入值
cout < < "Input   the   number   please   =>   ";
cin> > num;                                                         //输入数字
if   (num <0   ||   num> 2147483647)                   //判断数字是否在可计算范围内
{
cout < < "The   number   must   between   0   and   2147483647 " < <endl;
return;
}
if   (IsPalindrome(num))                               //通过判断函数返回值确定是否是回文
cout < < "The   number   is   a   palindrome " < <endl;
else
cout < < "The   number   is   not   a   palinedrome " < <endl;
}
int   IsPalindrome(long   num)
{
int   digits[Figures],                                   //数组记录各位数字
int   *fwd=digits,*bwd;                                 //设立两个指针,分别用来指向数组的首尾
for   (int   end=0;num> 0;fwd++,num/=10,end++)
*fwd=num%10;                                           //分离出数字的各个位数
for   (fwd=digits,bwd=digits+end-1;fwd <bwd;fwd++,bwd--)
if   (*fwd!=*bwd)                                     //两个指针分别从首尾向中间移动
return   0;                                         //一旦发现所指数字不一样,则判定不是回文
return   1;                                                 //如果检查到中间两边都对称,则是回文
}
//return无用,输入一长串3后,num值为2147483647,还是被传入了函数,
//如何才能使得程序立刻终止?以前的exit(0);好像可以做到这点但是
//c++里用不了,或者如何按照书上所说,抛出一个异常?

[解决办法]
比如要得到456的4,12的1,3466的3,56788的5。。。。
---
知道一个用C语言写的方法。。
#include <stdio.h>

int main(){
long a;
char tmp[20];
scanf( "%ld ",&a);
sprintf(tmp, "%ld ",a);
printf( "%c ",tmp);
return 0;
}
[解决办法]
回文数可以这样判断。。
#include <iostream>
using namespace std;

int IsPalindrome(char num[]); //此函数用于判断输入值是否是回文
void main()
{
char num[200]={0}; //以此变量记录输入值
cout < < "Input the number please => ";
cin> > num; //输入数字

if (IsPalindrome(num)) //通过判断函数返回值确定是否是回文
cout < < "The number is a palindrome " < <endl;
else
cout < < "The number is not a palinedrome " < <endl;


}
int IsPalindrome(char num[])
{
int i=0,j=199;
while(num[j--]==0);
j++;
while(i <j){
if (num[i++]!=num[j--])
return 0;
}
return 1;
}
[解决办法]
这个是画长方形的函数。
int draw(int width, int heigth){
int i,j;
for(i=1;i <=width;i++){
for(j=1;j <=heigth;j++){
if((i==1)||(i==width)||(j==1)||(j==heigth))
cout < < "* ";
else
cout < < " ";
}
cout < <endl;
}

return 0;
}

热点排行