高级语言C++程序设计 (第二版) 刘景、周玉龙編
高级语言C++程序设计 (第二版) 刘景、周玉龙编
第四章基本控制结构与导出数据类型 顺序结构:执行顺序=书写顺序 ■4.1控制语句、复合语句和空语句 411简单的计算器程序(用顺序、分支和循环来实现) 1、只有顺序结构(P82~84) //program4-1 cpp #indlude <iostream.h> void maino int X,yi cout<< first integer: i CIn>>XI cout<< second integer: i cIn>>yi Cou<<X<<+”<<y<<=<<X+y<<end;}
第四章 基本控制结构与导出数据类型 顺序结构:执行顺序=书写顺序 ◼ 4.1 控制语句、复合语句和空语句 4.1.1 简单的计算器程序(用顺序、分支和循环来实现) 1、只有顺序结构(P.82~84.): //program4-1.cpp #include<iostream.h> void main() { int x,y; cout<<“first integer:”; cin>>x; cout<<“second integer:”; cin>>y; cout<<x<<“+”<<y<<“=”<<x+y<<endl; }
第四章基本控制结构与导出数据类型 2、带有分支结构的计算器(P84~85) //program4-2 cpp #indlude <iostream.h> void maino int X,y char op: cout <<first integer: "1 cin>>XI cout <<second integer:i cIn>>yi Cout<<“ operato(+;,*/%):”; cIn>>op;
第四章 基本控制结构与导出数据类型 2、带有分支结构的计算器(P.84~85.): //program4-2.cpp #include<iostream.h> void main() { int x,y; char op; cout<<“first integer:”; cin>>x; cout<<“second integer:”; cin>>y; cout<<“operator(+,-,*,/,%):”; cin>>op;
第四章基本控制结构与导出数据类型 switch(op) case Cou<<×<<+”<<y<“=”<<X+y; break; case Cout<x<<”<<y<<“=<<Xy; break; case <*2. cou<<×<<*<<y<<“=”<<X*y; break; case if(y! =cout<<x<< <<y<<=""<<X/y; break;y case%’ if(y!=0){cu<<<%<<y<<=<<x%y; break;} default. cout<<Wrong! 131
第四章 基本控制结构与导出数据类型 switch(op) { case ‘+’: cout<<x<<“+”<<y<<“=”<<x+y;break; case ‘-’: cout<<x<<“-”<<y<<“=”<<x-y;break; case ‘*’: cout<<x<<“*”<<y<<“=”<<x*y;break; case ‘/’: if(y!=0){cout<<x<<“/”<<y<<“=”<<x/y;break;} case ‘%’: if(y!=0){cout<<x<<“%”<<y<<“=”<<x%y;break;} default: cout<<“Wrong!”;}}
第四章基本控制结构与导出数据类型 3、带有分支结构和循环结构的计算器(P85~86) //program4-3 cpp #indlude <iostream.h> void maino int X,y char op, cont bool quit=true; while(quit) cout <endl<< Do you want to continue? y or n)' cin>>cont if(cont==quit=false; }}
第四章 基本控制结构与导出数据类型 3、带有分支结构和循环结构的计算器(P.85~86.): //program4-3.cpp #include<iostream.h> void main() { int x,y; char op,cont; bool quit=true; while(quit) {… cout<<endl<<“Do you want to continue?(y or n)”; cin>>cont; if(cont==‘n’)quit=false; } }