include stdafx h include iostream h include stdio

  • Slides: 41
Download presentation

例 #include "stdafx. h" #include "iostream. h" #include "stdio. h“ int main(int argc, char*

例 #include "stdafx. h" #include "iostream. h" #include "stdio. h“ int main(int argc, char* argv[]) { char Ent; int b; Ent=getchar(); b=10; b=Ent; cout<<Ent<<endl; 回车,ASCII码13,"r" cout<<b<<endl; 换行,ASCII码10,"n" return 0; } 7

P 48 例3. 2 #include "stdafx. h" #include <iostream> #include <iomanip> using namespace std;

P 48 例3. 2 #include "stdafx. h" #include <iostream> #include <iomanip> using namespace std; int main(int argc, char* argv[]) { int a=8123, b=32, c=123; double pi=3. 141593; cout<<"123456789012345"<<endl; cout<<setw(3)<<a<<setw(4)<<b<<setw(5)<<c<<endl; cout<<setfill('#'); cout<<setw(3)<<a<<setw(4)<<b<<setw(5)<<c<<endl; cout<<setw(10)<<setprecision(8)<<pi<<endl; return 0; } 10

例: #include "stdafx. h" #include <iostream> #include <iomanip> using namespace std; void main() {

例: #include "stdafx. h" #include <iostream> #include <iomanip> using namespace std; void main() { char ch 1, ch 2; 输入字符 cin>>ch 1; 单分支选择 if(ch 1>='A'&&ch 1<='Z') ch 2=ch 1 -'A'+'a'; cout<<setw(4)<<ch 1<<setw(4)<<ch 2<<endl; } 输出间隔 输出字符 将输入的 大写字母 转换成小 写字母。 18

例:判断闰年 输入一个年份,判断它 int main(int argc, char* argv[]) 是否是闰年,给出相应 { 的提示 int year; 闰年是可以被 4整除而不能被

例:判断闰年 输入一个年份,判断它 int main(int argc, char* argv[]) 是否是闰年,给出相应 { 的提示 int year; 闰年是可以被 4整除而不能被 100 cout<<"Input the year: "; 整除,或者能被 400整除的年份。 cin>>year; if(year%4==0&&year%100!=0||year%400==0) cout<<year<<" is a leap year"<<endl; 闰年的判断 else cout<<year<<" is not a leap year!"<<endl; return 0; } 20

例: #include "stdafx. h" #include"stdio. h" #include<iostream> using namespace std; int main(int argc, char*

例: #include "stdafx. h" #include"stdio. h" #include<iostream> using namespace std; int main(int argc, char* argv[]) { int x, y; cout<<"Input two integrals: "; 输入两个整数 cin>>x>>y; cout<<"x="<<x<<"y="<<y<<endl; if(x!=y) 当x和y不相等时 { 当x>y时 if(x>y) cout<<"x>y"<<endl; 当x<y时 else cout<<"x<y"<<endl; } 当x和y相等时 else cout<<"x=y"<<endl; return 0; } 22

例: int main(int argc, char* argv[]) { int score; cout<<"Input scores: "; cin>>score; if(score>=90)

例: int main(int argc, char* argv[]) { int score; cout<<"Input scores: "; cin>>score; if(score>=90) 要求:对学生的成绩进行评定。 90以上 输出 A 89 — 80 输出 B 79 — 70 输出 C 69 — 60 输出 D 59以下 输出 E cout<<"A"<<endl; else if (score>=80) cout<<"B"<<endl; 多分支选择 else if (score>=70) cout<<"C"<<endl; else if (score>=60) cout<<"D"<<endl; else cout<<“E"<<endl; return 0; 24

例: void main(void ) { int i=10; switch(i) { case 9: i++; case 10:

例: void main(void ) { int i=10; switch(i) { case 9: i++; case 10: i++; case 11: i++; default: i++; } cout<<“i=”<<i<<endl; } i=11 i=12 i=13 27

思考题: 如何利用 C++的分支 结构模拟物 流自动分拣 系统? 网络课程平台http: //course. cn/G 2 S/Template/View. aspx? action=view&course. Type=0&course.

思考题: 如何利用 C++的分支 结构模拟物 流自动分拣 系统? 网络课程平台http: //course. cn/G 2 S/Template/View. aspx? action=view&course. Type=0&course. Id=2272 28

例: l 编程计算sum=1+1/2+1/3+…+1/100的值 int main(int argc, char* argv[]) { { int i=1; double sum=0.

例: l 编程计算sum=1+1/2+1/3+…+1/100的值 int main(int argc, char* argv[]) { { int i=1; double sum=0. 0; 循环条件 do while (i<=100) 先执行 { { 先判断 再判断 sum+=1. 0/i; 再执行 i++; 循环条件 } } while(i<=100); 分号不可少 cout<<"sum="<<sum<<endl; return 0; } } 37