C习题课二2008年上半年.ppt_第1页
C习题课二2008年上半年.ppt_第2页
C习题课二2008年上半年.ppt_第3页
C习题课二2008年上半年.ppt_第4页
C习题课二2008年上半年.ppt_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

计算机程序设计,顾 刚 计算机教学实验中心,第6章第1题,#include /例4-6 int main() const int COUNT=16; int listCOUNT=503, 87, 512, 61, 908, 170, 897, 275, 653, 426, 154, 509, 612, 677, 765, 703 ; for(int i=0; ii; j=j-1) if(listj-1listj) int tmp = listj-1; listj-1 = listj; listj = tmp; cout “The result is :“ endl; for(int k=0;k16;k+) cout listk “ “; coutendl; return 0; ,void bubble_up(int *ptr, int count) for(int i=0; ii; j=j-1) if(*(ptr+j-1)*(ptr+j) int tmp = *(ptr+j-1); *(ptr+j-1) = *(ptr+j); *(ptr+j) = tmp; ,void bubble_up(int ptr, int count) for(int i=0; ii; j=j-1) if( ptrj-1ptrj) int tmp = ptrj-1; ptrj-1 = ptrj; ptrj = tmp; ,第6章第2题,解题思路: 结果放字符数组里 循环分离输入的整数,直到为0 置逆字符数组 输出字符数组 别的方法?,#include #include int main() int num; char array50; char *ptr=array; coutnum; int k=0; while(num!=0) *ptr=num%10+0; num=num/10; ptr+; k+; if(k%3=0) *ptr=,; ptr+; k=0; ; *ptr=0; strrev(array); cout“该整数按照标准的三位分节格式输出为:“; coutarrayendl; return 0; ,第6章第3题,#include int main() int array=1, 2, 3, 4, 5, 6, 7, 8, 9, 10; int *ptr=array; int *qtr=array+10; while(ptrqtr) cout*ptr“ “; ptr+; coutendl; return 0; ,第6章第4题,定义声明字符数组,五个计数器 输入字符串 循环判别每个字符,相关计数器加1 大写字母条件: 小写字母条件: 数字字符条件: 其它字符条件: 输出结果,#include int main() char str100; char *ptr=str; int total, capital, small, numeral, others; total=capital=small=numeral=others=0; cout=A ,第6章第5题,输入整数字符串 判别首字符是否为符号位 从左至右循环处理转换每个字符 Num*10+(*ptr-0) 输出结果 问题小数如何转换?,#include int atoi(char *string) int num=0; int s=1; if(*string=-) s=-1; string+; if(*string=+) s=1; string+; while(*string!=0 ,第6章第6题,#include char *mystrspc(char * string, int n) char * ptr = string; while (n0) *string= ; string+; n- -; *string = 0; return ptr; int main() char str51; int n; cout n; cout “+“ mystrspc(str, n) “+“ endl; cout “+123456789A123456789B123456789C123456789D123456789E+“ endl; return 0; ,第7章第1题,递归形式: X0=1 Xk=X*Xk-1,#include double power(double x, int k) if(k=0) return 1; else return power(x, k-1)*x; ,第7章第2题,递归形式: ack(0, n)=n+1 ack(m, 0)=ack(m-1, 1) ack(m, n)=ack(m-1, ack(m, n-1),int ack(int m, int n) if(m=0)return n+1; else if(n=0)return ack(m-1, 1); return ack(m-1, ack(m, n-1); ,第7章第3题,递归形式: F(1)=1,F(2)=2 F(n)=F(n-1)+F(n-2),int fib(int n) if(n=0)return 0; else if(n=1)return 1; return fib(n-1)+fib(n-2); ,第7章第4题,int cube(int x) return x*x*x; double cube(double x) return x*x*x; ,第7章第5题,int max(int x, int y) return (xy)?x:y; double max(double x, double y) return (xy)?x:y; char max(char x, char y) return (xy)?x:y; ,第7章第6题,指针指向字符串最后一个字符 循环判别*ptr= 成立ptr 不成立*(+ptr)=0,#include char *mytrim(char *string) char *ptr=string; while(*ptr!=0) ptr+; do ptr-; while(*ptr= ); *(+ptr)=0; return string; int main() char str = “The art of computer programming “; cout “截取前的原始字符串是: “ strendl; cout “截取空格后的字符串是: “ mytrim(str)endl; return 0; ,第7章第7题,#include #include char *myltrim(char * string) char *ptr=string; while(*ptr= ) ptr+; strcpy(string, ptr); return string; int main() char str = “ The art of computer programming“; cout “截取前的原始字符串是: “ strendl; cout “截取空格后的字符串是: “ myltrim(str)endl; coutstr; return 0; /其它解法,利用上一题,第7章第9题,#include #include double equation(double (*func)(double), double a, double b, double eps) double x; do x=(a+b)/2; if(func(x)*func(a)0) a=x; else b=x; while(fabs(func(x)eps); return x; double f1(double x) return x*x+2*x-3; int main() double a, b; double eps=1.0E-7; coutab; cout“结果是: “equation(f1, a, b, eps)endl; return 0; ,第8章第1题,定义结构体数组 先定义结构体 struct person char name9; char sex; int year; 再定义结构体数组 struct person people5; 循环输入5个人的信息 循环5次,统计各项数据 输出结果,第8章第2题,#include struct Circle int topleft_x; int topleft_y; int bottomright_x; int bottomright_y; ; int main() const double pi=3.1415926; Circle c; coutc.topleft_xc.topleft_yc.bottomright_xc.bottomright_y; double area=pi*(c.topleft_x -c.bottomright_x )*(c.topleft_x -c.bottomright_x )/4; cout“该圆的面积是:“areaendl; return 0; ,第8章第3题,#include #include struct Person char name20; char tel20; ; int main() const int COUNT=5; Person pCOUNT; int i, j; pi.tel; Person tmp; for(i=0; ii; j=j-1) if(strcmp(, )0) tmp=pj; pj=pj-1; pj-1=tmp; cout“按姓名的字典顺序排列后的结果:“endl; for(i=0; iCOUNT; i=i+1) “t“pi.telendl; return 0; ,第8章第4题,穷举方式:三重循环 0-4分别代表五种颜色 用枚举类型处理还需分离各种不同类型颜色,#include int main() int count=0; for(int i=0;i=2;i+) for(int j=i+1;j=4;j+) for(int k=j+1;k=4;k+) count+; coutijkendl; return 0; ,第8章第5题,循环2-10000次 每个数的全部因子用循环

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论