谭浩强版C++程序设计_习题课1-第3章部分习题.pptVIP

  • 7
  • 0
  • 约4.39千字
  • 约 12页
  • 2018-01-26 发布于河南
  • 举报

谭浩强版C++程序设计_习题课1-第3章部分习题.ppt

谭浩强版C程序设计_习题课1-第3章部分习题

4.字符输入输出 #include iostream using namespace std; int main ( ) {char c1,c2; //int c1=97,c2=98; cout请输入两个字符c1,c2:; c1=getchar(); //将输入的第一个字符赋给c1 c2=getchar(); //将输入的第二个字符赋给c2 cout用putchar函数输出结果为:; putchar(c1); putchar(c2); coutendl; cout用cout语句输出结果为:; coutc1c2endl; return 0; } 用getchar函数时,应当连续输入两个字符,再按回车键 12.逆序输出一个数的各位数 #include iostream using namespace std; int main () {long int num; int indiv,ten,hundred,thousand,ten_thousand,place; /*分别代表个位,十位,百位,千位,万位和位数*/ coutenter an integer(0~99999):; cinnum; if (num9999) place=5; else if (num999) place=4; else if (num99) place=3; else if (num9) place=2; else place=1; coutplace=placeendl; //计算各位数字 ten_thousand=num/10000; thousand=(int)(num-ten_thousand*10000)/1000; hundred=(int)(num-ten_thousand*10000-thousand*1000)/100; ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10; indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10); coutoriginal order:; switch(place) {case 5:coutten_thousand,thousand,hundred,ten,indivendl; coutreverse order:; coutindivtenhundredthousandten_thousandendl; break; case 4:coutthousand,hundred,ten,indivendl; coutreverse order:; coutindivtenhundredthousandendl; break; case 3:couthundred,ten,indivendl; coutreverse order:; coutindivtenhundredendl; break; case 2:coutten,indivendl; coutreverse order:; coutindivtenendl; break; case 1:coutindivendl; coutreverse order:; coutindivendl; break; } return 0; } 14.由小到大顺序输出4个整数 int main () {int t,a,b,c,d; coutenter four numbers:; cinabcd; couta=a, b=b, c=c,d=dendl; if (ab) {t=a;a=b;b=t;} if (ac) {t=a; a=c; c=t;} if (ad) {t=a; a=d; d=t;} if (bc) {t=b; b=c; c=t;} if (bd) {t=b; b=d; d=t;} if (cd) {t=c; c=d; d=t;} coutthe sorted sequence:endl; couta, b, c, dendl; return 0; } 15.求最大公约和最小公倍数 int ma

文档评论(0)

1亿VIP精品文档

相关文档