C++学习笔记——第二章习题(四)-用循环语句打印图案

    技术2022-05-11  65

    //2.7--------------------------用循环语句打印菱形图案---------------------------

    // for(int j=1; j<=19; j++){//  if(j<=10){//  cout<<setw(11-j) <<setfill(' ')<<" "//   <<setw(2*j-1) <<setfill('%')<<"%"<<endl;//  }//  else{//  cout<<setw(j-9)   <<setfill(' ')<<" "//   <<setw(2*(20-j)-1) <<setfill('%')<<"%"<<endl;//  }// }//2.8.(1)-------------------用循环语句打印图案:两个直角三角形------------------------------// for(int i=1; i<=10; ++i){//  cout<<setw(11-i)<<setfill(' ')<<" "//   <<setw(i) <<setfill('#')<<"#"//   <<setw(5) <<setfill(' ')<<" "//   <<setw(i) <<setfill('$')<<"$"<<endl;// }//--------------------------------------------------------// cout<<setw(6)<<setfill('$')<<27<<endl;// cout<<string('$',5)<<endl;//2.8.(2)---------------------用循环语句打印图案:倒三角,ST交叉字符----------------------------/*for(int i=1;i<=10;i++){ cout<<setw(i) <<setfill(' ')<<" "; for(int j=1;j<=21-2*i;j++){  if(j%2==0) cout<<"T";  else cout<<"S"; } cout<<endl;}*///--------------------------------------------------------

    //2.12.(1)------------------用循环语句打印数字矩阵------------------------------/*for(int i=1;i<=6;i++){ cout<<i<<" "; for(int j=-1;j<=5;j++){  cout<<"  "<<(i+j)%7; } cout<<endl;}//--------------------------------------------------------//2.12.(2)-------------------用循环语句打印矩阵坐标-----------------------------for(int i=1;i<=6;i++){ for(int j=1;j<=7;j++){  cout<<"("<<i<<", "<<j<<")   "; } cout<<endl;}//--------------------------------------------------------//2.13.(1)------------------------九九乘法表下三角矩阵------------------------ cout<<"   *    1    2    3    4    5    6    7    8    9"<<endl; cout<<"-------------------------------------------------"<<endl; for(int i=1;i<=9;i++){  cout<<"   "<<i;  for(int j=1;j<=i;j++){   cout<<setw(5)<<setfill(' ')<<i*j;  }  cout<<endl; }//--------------------------------------------------------//2.13.(2)-------------------------九九乘法表上三角矩阵----------------------- cout<<"   *    1    2    3    4    5    6    7    8    9"<<endl; cout<<"-------------------------------------------------"<<endl; for(int i=1;i<=9;i++){  cout<<"   "<<i;  for(int j=1;j<=9;j++){   if(j<i){    cout<<setw(5)<<setfill(' ')<<" ";   }   else{   cout<<setw(5)<<setfill(' ')<<i*j;   }  }  cout<<endl; }//------------------------------以上的几个程序都比较简单,其中8.(2)应该有更好一点的,不用那么多循环,网上有程序用“ST”和“S”字符串表示的,应该会好一点-------------------------- 

               还有书中提到用string(n,' $ ')的形式表示字符流,我在VC中没有调通说: : error C2665: 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' : none of the 7 overloads can convert parameter

           书上用的这个!!反正我没有调出来,只好换用另一种了:cout<<setw(5)<<setfill(' ')<<" ";

     


    最新回复(0)