1 / 24

C 程序设计

C 程序设计. -- 授课教师:苏君. 回顾. #incluude <iostream> #include <cstdio> Using namespace std; int main() { int a,b; cin>>a>>b; cout<<a<<“ ”<<b<<endl; system(“pause”); return 0; }. 学习目的. 1 、字符型变量的定义与使用 2 、输入任意一个字符,如果是小写字母,则输出其对应的大写,如果是大写,则输出其对应的小写字母. 需要用到的知识.

Download Presentation

C 程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C程序设计 --授课教师:苏君

  2. 回顾 • #incluude <iostream> • #include <cstdio> • Using namespace std; • int main() • { • int a,b; • cin>>a>>b; • cout<<a<<“ ”<<b<<endl; • system(“pause”); • return 0; • }

  3. 学习目的 • 1、字符型变量的定义与使用 • 2、输入任意一个字符,如果是小写字母,则输出其对应的大写,如果是大写,则输出其对应的小写字母

  4. 需要用到的知识 • 1、字符型变量的定义 • 2、字符在内存中的存储方式 • 3、ASCCII字母表 • 4、字符变量的数学运算 • 5、大小写的转的换

  5. 字符变量的定义 • char---character(字符英文单词) • 定义:char 【名称】 • 如:char a,china; 定义了两个字符型变量分别叫a、china。命名要遵循变量命名规则 • 其在内存中的存储形式是一个字节(8 bit)的二进制状态位。按排列组合,即有28 个字符。 • 字符A在内存中以 • 字符a在内存中以

  6. ASCII码表(美国信息交换标准代码)

  7. 例1 • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • char a; • cin>>a; • cout<<a<<endl;//输出字符 • cout<<a<<endl;//输出字符 • cout<<(int)a<<endl;输出十进制的ASCII码编码值 • system(“pause”); • return 0; • }

  8. 例2 • 请注意,字符型变量可在一定范围内(0~254)与整型变量可进行四则运算 • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • char a; • int b; • cin>>a; • b=a; • cout<<a<<endl; • cout<<b<<endl; • b=a+3; • cout<<b<<end; • system(“pause”); • return 0; • }

  9. 例3 • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • char a; • cin>>a; • if(a>=‘A’&&a<=‘Z’)//如果是大写,则转换成小写 • a=a+32; • cout<<a<<endl; • system(“pause”); • return 0; • }

  10. 字符加密 • 简单加密:将26个英文字母排成圆盘,以其后面的第三个字母将其替换。如a加密后则为d,z则以c替换。 • #include<iostream> • using namespace std; • int main() • { • char n; • cin>>n; • if(n>=‘A’&&n<=‘Z’) • n=(n-’A’+3)%26 + ‘A’; • if(n>=‘a’&&n<=‘z’) • n=(n-’a’+3)%26 + ‘a’; • cout<<n<<endl; • return 0; • }

  11. 回顾 • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • char a; • while(1) • { • cin>>a; • if(a>='a'&&a<='z')//类似转换成26进制 • a=(a-'a'+3)%26+'a'; • else if(a>='A'&&a<='Z') • a=(a-'A'+3)%26+'A'; • cout<<a; • } • system("pause"); • return 0; • }

  12. 今日目的 • 1、循环语句的使用 • 2、掌握并灵活使用while语句 • 3、理解while语句的执行过程

  13. 输出10个* • 1、当输出一个*时用: cout<<“*”; • 2、当要输出10个*时,我们想办法让1重复执行10次。因此我们想到了计数。

  14. i=1 • 1、令计数器i初值为1(i=1) • 2、当(while)计数器i有没有超过10,如果没有,则执行3;否则执行4; • 3、执行cout<<“*”,同时让计数器i增加1(i=i+1),执行2 • 4、循环结束 i<=10 N Y cout<<“*” i=i+1

  15. while语句 • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • int i; • i=1; • while(i<=10) • { • cout<<“*”; • i=i+1; • } • system(“pause”); • return 0; • }

  16. While语句格式 • while(循环条件) • { • 语句组 • } • 注意: • 1、循环变量一定要赋初值 • 2、在循环过程中,一定要改变循环变量的值,否则就是死循环!

  17. while例子 • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • int i; • i=10; //初值大于终值 • while(i>=1) • { • cout<<“*”; • i=i-1; • } • system(“pause”); • return 0; • } • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • int i; • i=1;//初值小于终值 • while(i<=10) • { • cout<<“*”; • i=i+1; • } • system(“pause”); • return 0; • }

  18. 用while输出所有的ASCCII • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • int i; • i=0; • while(i<=255) • { • cout<<(char)i<<“ ”; • i=i+1; • } • system(“pause”); • return 0; • }

  19. 用while输出10行10列的* #include <iostream> #include <cstdio> using namespace std; int main() { int i,j; i=1; while(i<=10) { i=i+1; } system("pause"); return 0; } j=1; while(j<=10) { cout<<"*"; j=j+1; } cout<<endl;

  20. 求1+2+3+4+5+….+n • #include <iostream> • #include <cstdio> • using namespace std; • int main() • { • int i ; • int n; • int sum;//累和 • i=1; • sum=0; • cin>>n;//输入n • while(i<=n) • { • sum=sum+i; • i=i+1; • } • cout<<"sum="<<sum<<endl; • system("pause"); • return 0; • }

  21. 输出九九表 • #include<iostream> • using namespace std; • int main() • { • int i,j; • i=1; • while(i<=9) • { • j=1; • while(j<=i) //每行循环次不超过行号i • { • cout<<i<<“*”<<j<<“=”<<i*j<<“ ”; • } • cout<<endl;//换行 • } • }

  22. 课后任务 • 输入任意一个自然数n,倒序输出其各个位上的数值。如输入的是7895,则输出的是5 9 8 7;如输入的是523,则输出3 2 5(提示:任意一个整数n模10,结果值是整数n个位上的数值,任意整数n整除10,则将个位数“砍掉”)

  23. 综合练习 • 请输入做任意整数n,输出对应的数字图形。 • 如输入4 如输入6 • 1 • 222 • 33333 • 4444444 1 222 33333 4444444 555555555 66666666666 实例

  24. 挑战 • 请输入做任意整数n,输出对应的数字图形。 • 如输入4 如输入6 • 1 • 222 • 33333 4444444 1 222 33333 4444444 555555555 66666666666 实例

More Related