欣乐

The eagles are coming!

导航

第六章 语句

 

 

code:

 

 

 

// 第六章 语句

/*
1. 简单语句
2. 声明语句
3. 复合语句(块)
4. 语句作用域
5. if 语句
6. switch 语句
7. while 语句
8. for 循环语句
9. do while 语句
10. break 语句
11. continue 语句
12. goto 语句
13. try 块和异常处理
14. 使用预处理器进行调试
小结
*/
 


// 1. 简单语句 ------------------------------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;

int main()
{
  // read until we hit end-of-file or find an input equal to sought
  string s,sought("over");
  while (cin >> s && s != sought)
  ; // null statement
  
  return 0;
}


// 2. 声明语句 -------------------------



// 3. 复合语句(块) ------------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;

int main()
{
  // read until we hit end-of-file or find an input equal to sought
  string s,sought("over");
  while (cin >> s && s != sought)
    {} // empty block
  
  return 0;
}



// 4. 语句作用域 -------------------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;

int main()
{
  int a[10]={3};
  for(int i=0; i<10; ++i)
    cout << a[i] << ' ';
  cout << endl;
  //cout << i << endl; // error:i is not visible here
  
  return 0;
}



// 5. if 语句 ----------------------------------------------------------------------------


// my test
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int i,j,k;
  i=j=k=0;
  while(cin>>i) {
    if(i>=3) {}
      else if(1==i) ++j;
        else if(2==i) ++k;
          else {}
  }
  cout << j << ' ' << k << endl;
  
  return 0;
}



// 6. switch 语句 -------------------------------------------------------------------------------


#include <iostream>
#include <string>
using namespace std;
int main()
{
  char ch;
  // initialize counters for each vowel
  int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
  while(cin >> ch)
  {
    // if ch is a vowel, increment the appropriate counter
    switch(ch)
    {
      case 'a':
        ++aCnt;
        break;
      case 'e':
        ++eCnt;
        break;
      case 'i':
        ++iCnt;
        break;
      case 'o':
        ++oCnt;
        break;
      case 'u':
        ++uCnt;
        break;
    }
  }
  // print results
  cout << "Number of vowel a: \t" << aCnt << '\n' 
       << "Number of vowel e: \t" << eCnt << '\n' 
       << "Number of vowel i: \t" << iCnt << '\n' 
       << "Number of vowel o: \t" << oCnt << '\n'
       << "Number of vowel u: \t" << uCnt << endl;
  
  return 0;
}


// 如果木有 break
#include <iostream>
#include <string>
using namespace std;
int main()
{
  char ch;
  // initialize counters for each vowel
  int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
  while(cin >> ch)
  {
    // if ch is a vowel, increment the appropriate counter
    switch(ch)
    {
      case 'a': ++aCnt; //如果木有break,输入a,后面的eCnt等,都会加一!
      case 'e': ++eCnt;
      case 'i': ++iCnt;
      case 'o': ++oCnt;
      case 'u': ++uCnt;
    }
  }
  // print results
  cout << "Number of vowel a: \t" << aCnt << '\n' 
       << "Number of vowel e: \t" << eCnt << '\n' 
       << "Number of vowel i: \t" << iCnt << '\n' 
       << "Number of vowel o: \t" << oCnt << '\n'
       << "Number of vowel u: \t" << uCnt << endl;
  
  return 0;
}


/*

在pascal中,没有break,不会出现这种情况:
var 
  a,b,c,d,e:longint;
  ch:char;
begin 
  a:=0; b:=0; c:=0; d:=0; e:=0;
  while( not eof ) do begin
    read(ch);
    case ch of
      'a':inc(a);
      'e':inc(b);
      'i':inc(c);
      'o':inc(d);
      'u':inc(e);
    end;
  end;
  writeln(a,' ',b,' ',c,' ',d,' ',e);
end. 

*/


#include <iostream>
#include <string>
using namespace std;
int main()
{
  char ch;
  // initialize counters for each vowel
  // int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
  int vowelCnt=0;
  while(cin >> ch)
  {
    // if ch is a vowel, increment the appropriate counter
    switch(ch)
    {
      case 'a': case 'e': case 'i': case 'o': case 'u':
      ++vowelCnt;
      break;
    }
  }
  // print results
  cout << "Number of vowel : \t" << vowelCnt << endl;
  
  return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main()
{
  char ch;
  // initialize counters for each vowel
  // int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
  int vowelCnt=0, otherCnt=0;
  while(cin >> ch)
  {
    // if ch is a vowel, increment the appropriate counter
    switch(ch)
    {
      case 'a': case 'e': case 'i': case 'o': case 'u':
        ++vowelCnt;
        break;
      default:
        ++otherCnt;
        break;
    }
  }
  // print results
  cout << "Number of vowel : \t" << vowelCnt << endl;
  cout << "Number of other : \t" << otherCnt << endl;
  
  return 0;
}



// 7. while 语句 --------------------------------------------------------------------


#include <iostream>
#include <string>
using namespace std;
int main()
{
  int n(10);
  while(n--) //10次。如果用--n,只会有9次
    cout << n << endl;
  
  return 0;
}


#include <iostream>
#include <string>
using namespace std;
int main()
{
  int arr1[]={1,2,3};
  // arr1 is an array of ints
  int *source = arr1;
  size_t sz = sizeof(arr1) / sizeof(*arr1); // number of elements
  int *dest = new int[sz]; // uninitialized elements
  int *ptemp = dest;
  while(source != arr1 + sz)
    *dest++ =  *source++;
  //  copy element and increment pointers
  delete [] ptemp;
}


// 8. for 循环语句 ----------------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int sum(0);
  for(int i=0,j=10; ;++i,--j)
    if(i>=j) break;
      else ++sum;
  cout << sum << endl;

  return 0;
}


// 9. do while 语句 -------------------------------------------------------------------


#include <iostream>
#include <string>
using namespace std;
int main()
{
  int n;
  do {
    cin>>n;
    cout<<n<<endl;
  } while(n!=0);

  return 0;
}


// error code
#include <iostream>
#include <string>
using namespace std;
int main()
{
  //int n;
  do {
    int n; // error
    cin>>n;
    cout<<n<<endl;
  } while(n!=0); // can ot find n

  return 0;
}



// 10. break 语句 -------------------------------------------------------------

  // 当 break 出现在嵌套的 switch 或者循环语句中时,将会终止里层的 switch 或循环语句,而外层的 switch 或者循环不受影响


// 11. continue 语句 ----------------------------------------------------------

#include <iostream>
#include <string>
using namespace std;
int main()
{
  int n;
  do {
    cin>>n;
    if(n>10) continue; // 大于10的数,不输出了
    cout<<n<<endl;
  } while(n!=0);

  return 0;
}


// 12. goto 语句 -----------------------------------------------------------------

// 从上世纪 60 年代后期开始,不主张使用 goto 语句。
#include <iostream>
#include <string>
using namespace std;
int main()
{
  int n;
  do {
    cin>>n;
    if(n>10) goto end;
    cout<<n<<endl;
  } while(n!=0);
  end:
  cout << n << endl;
  return 0;
}



// 13. try 块和异常处理 -------------------------------------------------------------

#include <iostream>
#include <string>
#include <stdexcept>
#include "Sales_item.h"
using namespace std;
int main()
{
  Sales_item item1, item2;
  std::cin >> item1 >> item2;
  // first check that data is for the same item
  if(!item1.same_isbn(item2))
    throw runtime_error("Data must refer to same ISBN");
  // ok, if we're still here the ISBNs are the same
  std::cout << item1 + item2 << std::endl;
  return 0;
}
/*

0-201-78345-X 3 20.00
0-201-78348-X 2 25.00

*/



#include <iostream>
#include <string>
#include <stdexcept>
#include "Sales_item.h"
using namespace std;
int main()
{
  Sales_item item1, item2;
  while(cin >> item1 >> item2)
  {
    try
    {
      // first check that data is for the same item
      if(!item1.same_isbn(item2))
        throw runtime_error("Data must refer to same ISBN");
      // execute code that will add the two Sales_items
      // if the addition fails, the code throws a runtime_error exception
    }
    catch(runtime_error err)
    {
      // remind the user that ISBN must match and prompt for another pair
      cout << err.what() << "\nTry Again? Enter y or n" << endl;
      char c;
      cin >> c;
      if(cin && c == 'n')
        break;
      // break out of the while loop
    }
  }
  // ok, if we're still here the ISBNs are the same
  cout << item1 + item2 << std::endl;
  
  return 0;
}

/*
0-201-78345-X 3 20.00
0-201-78348-X 2 25.00
*/



// 14. 使用预处理器进行调试 ----------------------------------------------------------------

#include <iostream>
using namespace std;

int main () 
{
  cout << "This is the line number " << __LINE__;
  cout << " of file " << __FILE__ << ".\n";
  cout << "Its compilation began " << __DATE__;
  cout << " at " << __TIME__ << ".\n";
  cout << "The compiler gives a __cplusplus value of ";
  cout << __cplusplus << endl;

  return 0;
}

 

 

 

 

TOP

 

posted on 2014-11-10 20:50  欣乐  阅读(148)  评论(0编辑  收藏  举报