转义字符跟变量的一些事

来源:互联网 发布:淘宝sha骗局 编辑:程序博客网 时间:2024/05/04 13:35

转义字符的应用

使用转义字符,编写代码,输出2M,然后换行.修改程序,输出2,跟着一个制表符,然后是M,最后是换行符.

使用转义字符的时候一定要注意,' '不能少, 比如换行符'/n',如果不加' ',n是未定义的....

#include<iostream.h>
int main()
{
 cout<<"2M"<<'/n';
 return 0;

操,Visual C++编译时定住了,就因为我没加 return 0;还是因为用了字符串而不是char的缘故?

#include<iostream.h>
int main()
{
 cout<<'2'<<'/t'<<'M'<<'/n';
 return 0;
}

可下面这样也没事哇

#include<iostream.h>
int main()
{
 cout<<"2"<<'/t'<<"M"<<'/n';
 return 0;
}

 

编写程序,要求用户输入两个数,底数(base)和指数(exponent),输出底数的指数次方的结果.

我用循环来做吧 - -

#include<iostream.h>
int main()
{
 int base,exp;
 long result=1;
 cout<<"enter base:"<<endl;
 cin>>base;
 cout<<"enter exponent:"<<endl;
 cin>>exp;
 if(exp<0)
 {
  cout<<"exponent can't be smaller than 0"<<endl;
  return -1;
 }
 if(exp>0)
 {
 for(int cnt=1;cnt<=exp;++cnt)
  result*=base;
  }
 cout<<base
  <<" raised to the power of "
  <<exp<<":"
  <<result<<endl;
 return 0;
}

里面要判断以下指数,指数为负数的话,那结果就是小数了...

先写到这里吧,我华丽的去吃东西了,饿 o(∩_∩)o

原创粉丝点击