编程札记

C

C 练习实例76

C 练习实例76 题目: 编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)。 程序分析: 无。 程序源代码: // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include<stdio.h> #include<stdlib.h> double evenumber(int n); double oddnumber(int n); int main() { int n; double r; double (*pfunc)(int); printf("请输入一个数字:"); scanf("%d",&n); if(n%2==0) pfunc=evenumber; else pfunc=oddnumber; r=(*pfunc)(n); printf("%lf\n",r); system("pause"); return 0; } double evenumber(int n) { double s=0,a=0; int i; for(i=2;i<=n;i+=2) { a=(double)1/i; s+=a; } return s; } double oddnumber(int n) { double s=0,a=0; int i; for(i=1;i<=n;i+=2) { a=(double)1/i; s+=a; } return s; } 以上实例运行输出结果为: 请输入一个数字:2 0.500000

阅读全文 ↗
C

C 练习实例75

C 练习实例75 题目: 输入一个整数,并将其反转后输出。 程序分析: 无。 实例 - 使用 strlen() // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include <stdio.h> int main() { int n, reversedNumber = 0, remainder; printf("输入一个整数: "); scanf("%d", &n); while(n != 0) { remainder = n%10; reversedNumber = reversedNumber*10 + remainder; n /= 10; } printf("反转后的整数: %d", reversedNumber); return 0; } 以上实例运行输出结果为: 输入一个整数: 12345 反转后的整数: 54321

阅读全文 ↗
C

C 练习实例74

C 练习实例74 题目: 连接两个链表。 程序分析: 无。 程序源代码: // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; link delete_node(link pointer,link tmp) { if (tmp==NULL) /*delete first node*/ return pointer->next; else { if(tmp->next->next==NULL)/*delete last node*/ tmp->next=NULL; else /*delete the other node*/ tmp->next=tmp->next->next; return pointer; } } void selection_sort(link pointer,int num) { link tmp,btmp; int i,min; for(i=0;i<num;i++) { tmp=pointer; min=tmp->data; btmp=NULL; while(tmp->next) { if(min>tmp->next->data) { min=tmp->next->data; btmp=tmp; } tmp=tmp->next; } printf("\40: %d\n",min); pointer=delete_node(pointer,btmp); } } link create_list(int array[],int num) { link tmp1,tmp2,pointer; int i; pointer=(link)malloc(sizeof(node)); pointer->data=array[0]; tmp1=pointer; for(i=1;i<num;i++) { tmp2=(link)malloc(sizeof(node));

阅读全文 ↗
C

C 练习实例73

C 练习实例73 题目: 反向输出一个链表。 程序分析: 无。 实例 // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; LinkList CreateList(int n); void print(LinkList h); int main() { LinkList Head=NULL; int n; scanf("%d",&n); Head=CreateList(n); printf("刚刚建立的各个链表元素的值为:\n"); print(Head); printf("\n\n"); system("pause"); return 0; } LinkList CreateList(int n) { LinkList L,p,q; int i; L=(LNode*)malloc(sizeof(LNode)); if(!L)return 0; L->next=NULL; q=L; for(i=1;i<=n;i++) { p=(LinkList)malloc(sizeof(LNode)); printf("请输入第%d个元素的值:",i); scanf("%d",&(p->data)); p->next=NULL; q->next=p; q=p; } return L; } void print(LinkList h) { LinkList p=h->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } }

阅读全文 ↗
C

C 练习实例72

C 练习实例72 题目: 创建一个链表。 程序分析: 无。 实例 // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct LNode{ int data; struct LNode *next; }LNode,*LinkList; LinkList CreateList(int n); void print(LinkList h); int main() { LinkList Head=NULL; int n; scanf("%d",&n); Head=CreateList(n); printf("刚刚建立的各个链表元素的值为:\n"); print(Head); printf("\n\n"); system("pause"); return 0; } LinkList CreateList(int n) { LinkList L,p,q; int i; L=(LNode*)malloc(sizeof(LNode)); if(!L)return 0; L->next=NULL; q=L; for(i=1;i<=n;i++) { p=(LinkList)malloc(sizeof(LNode)); printf("请输入第%d个元素的值:",i); scanf("%d",&(p->data)); p->next=NULL; q->next=p; q=p; } return L; } void print(LinkList h) { LinkList p=h->next; while(p!=NULL){ printf("%d ",p->data); p=p->next; } }

阅读全文 ↗
C

C 练习实例71

C 练习实例71 题目: 编写input()和output()函数输入,输出5个学生的数据记录。 程序分析: 无。 程序源代码: // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include<stdio.h> #include<stdlib.h> typedef struct{ char name[20]; char sex[5]; int age; }Stu; void input(Stu*stu); void output(Stu*stu); int main() { Stu stu[5]; printf("请输入5个学生的信息:姓名 性别 年龄:\n"); input(stu); printf("5个学生的信息如下:\n姓名 性别 年龄\n"); output(stu); system("pause"); return 0; } void input(Stu*stu) { int i; for(i=0;i<5;i++) scanf("%s%s%d",stu[i].name,stu[i].sex,&(stu[i].age)); } void output(Stu*stu) { int i; for(i=0;i<5;i++) printf("%s %s %d\n",stu[i].name,stu[i].sex,stu[i].age); } 以上程序执行输出结果为: 请输入5个学生的信息:姓名 性别 年龄: aaa m 15 bbb m 16 ccc m 15 ddd m 17 eee m 16 5个学生的信息如下: 姓名 性别 年龄 aaa m 15 bbb m 16 ccc m 15 ddd m 17 eee m 16

阅读全文 ↗
C

C 练习实例70

C 练习实例70 题目: 写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。 程序分析: 无。 实例 // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include <stdio.h> #include <stdlib.h> int main() { int len; char str[20]; printf("请输入字符串:\n"); scanf("%s",str); len=length(str); printf("字符串有 %d 个字符。",len); } //求字符串长度 int length(char *s) { int i=0; while(*s!='\0') { i++; s++; } return i; } 以上程序执行输出结果为: 请输入字符串: www.codingdict.com 字符串有 18 个字符。

阅读全文 ↗
C

C 练习实例69

C 练习实例69 题目: 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。 程序分析: 无。 程序源代码: // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include <stdio.h> void main() { int num[50],n,*p,j,loop,i,m,k; printf("请输入这一圈人的数量:\n"); scanf("%d",&n); p=num; //开始给这些人编号 for (j=0;j<n;j++) { *(p+j)=j+1; } i=0;//i用于计数,即让指针后移 m=0;//m记录退出圈子的人数 k=0;//k报数1,2,3 while(m<n-1)//当退出的人数不大于总人数时,即留下的人数至少是一个人 //这句不能写成m<n,因为假设有8人,当退出了6人时,此时还是进行人数退出,即m++, //这时是7<8,剩下的一个人自己喊1,2,3那么他也就退出了,将不会有输出 { if (*(p+i)!=0)//如果这个人的头上编号不是0就开始报数加1,这里采用的方法是报数为3的人头上编号重置为0 { k++; } if (k==3) { k=0; //报数清零,即下一个人从1开始报数 *(p+i)=0;//将报数为3的人编号重置为0 m++; //退出人数加1 } i++; //指针后移 if (i==n)//这句很关键,如果到了队尾,就要使指针重新指向对头 //并且它只能放在i++后面,因为只有i++了才有可能i==n { i=0; } } printf("现在剩下的人是:"); for (loop=0;loop<n;loop++) { if (num[loop]!=0) { printf("%2d号\n",num[loop]); } } } 以上程序执行输出结果为: 请输入这一圈人的数量: 8 现在剩下的人是: 7号

阅读全文 ↗
C

C 练习实例68

C 练习实例68 题目: 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数。 程序分析: 无。 实例 // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include <stdio.h> #include <stdlib.h> int main() { int arr[20]; int i,n,offset; //输入数组大小和数组内容 printf("Total numbers?\n"); scanf("%d",&n); printf("Input %d numbers.\n",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); //输入滚动偏移量 printf("Set your offset.\n"); scanf("%d",&offset); printf("Offset is %d.\n",offset); //打印滚动前数组 print_arr(arr,n); //滚动数组并打印 move(arr,n,offset); print_arr(arr,n); } //打印数组 void print_arr(int array[],int n) { int i; for(i=0;i<n;++i) printf("%4d",array[i]); printf("\n"); } //滚动数组 void move(int array[],int n,int offset) { int *p,*arr_end; arr_end=array+n; //数组最后一个元素的下一个位置 int last; //滚动直到偏移量为0 while(offset) { last=*(arr_end-1); for(p=arr_end-1;p!=array;--p) //向右滚动一位 *p=*(p-1); *array=last; --offset; } }

阅读全文 ↗
C

C 练习实例67

C 练习实例67 题目: 输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。。 程序分析: 谭浩强的书中答案有问题。 实例 //利用指针实现数组的滚动 本例实现数组像右滚动几位 #include <stdio.h> #include <stdlib.h> int main() { int arr[20]; int i,n,offset; //输入数组大小和数组内容 printf("Total numbers?\n"); scanf("%d",&n); printf("Input %d numbers.\n",n); for(i=0;i<n;i++) scanf("%d",&arr[i]); //输入滚动偏移量 printf("Set your offset.\n"); scanf("%d",&offset); printf("Offset is %d.\n",offset); //打印滚动前数组 print_arr(arr,n); //滚动数组并打印 move(arr,n,offset); print_arr(arr,n); } //打印数组 void print_arr(int array[],int n) { int i; for(i=0;i<n;++i) printf("%4d",array[i]); printf("\n"); } //滚动数组 void move(int array[],int n,int offset) { int *p,*arr_end; arr_end=array+n; //数组最后一个元素的下一个位置 int last; //滚动直到偏移量为0 while(offset) { last=*(arr_end-1); for(p=arr_end-1;p!=array;--p) //向右滚动一位 *p=*(p-1); *array=last; --offset; } }

阅读全文 ↗
C

C 练习实例66

C 练习实例66 题目: 输入3个数a,b,c,按大小顺序输出。 程序分析: 利用指针方法。 实例 // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // # include<stdio.h> void swap(int *, int *); int main(void) { int a, b, c; int *p1, *p2, *p3; printf("输入 a, b ,c:\n"); scanf("%d %d %d", &a, &b, &c); p1 = &a; p2 = &b; p3 = &c; if(a>b) swap(p1, p2); if(a>c) swap(p1, p3); if(b>c) swap(p2, p3); printf("%d %d %d\n", a, b, c); } void swap(int *s1, int *s2) { int t; t = *s1; *s1 = *s2; *s2 = t; } 以上程序执行输出结果为: 输入 a, b ,c: 1 3 2 1 2 3

阅读全文 ↗
C

C 练习实例65

C 练习实例65 题目: 一个最优美的图案(在TC中实现)。 程序分析: 无。 程序源代码: // Created by www.codingdict.com on 15/11/9. // Copyright © 2013年 编程字典. All rights reserved. // #include "graphics.h" #include "math.h" #include "dos.h" #include "conio.h" #include "stdlib.h" #include "stdio.h" #include "stdarg.h" #define MAXPTS 15 #define PI 3.1415926 struct PTS { int x,y; }; double AspectRatio=0.85; void LineToDemo(void) { struct viewporttype vp; struct PTS points[MAXPTS]; int i, j, h, w, xcenter, ycenter; int radius, angle, step; double rads; printf(" MoveTo / LineTo Demonstration" ); getviewsettings( &vp ); h = vp.bottom - vp.top; w = vp.right - vp.left; xcenter = w / 2; /* Determine the center of circle */ ycenter = h / 2; radius = (h - 30) / (AspectRatio * 2); step = 360 / MAXPTS; /* Determine # of increments */ angle = 0; /* Begin at zero degrees */ for( i=0 ; i<MAXPTS ; ++i ){ /* Determine circle intercepts */ rads = (double)angle * PI / 180.0; /* Convert angle to radians */ points[i].x = xcenter + (int)( cos(rads)

阅读全文 ↗