c语言函数.docVIP

  • 13
  • 0
  • 约5.62千字
  • 约 20页
  • 2017-02-08 发布于重庆
  • 举报
c语言函数

用递归法将一个正整数n的各个数字分别输出 int f(int num) { int x; if(num==0) return 0; else{ x=num%10; f(num/10); } int main() { int n=1,m,sum,i; while(1) { m=n; sum=0; for(i=1;i8;i++) { m=m*2; sum=m+sum; } sum=sum+n; if(sum==765) { coutfistn\n; coutlastm; break; } n++; } return 0; } 宏定义 可以定义函数 例如交换函数swap #Define swap(a,b) {int c;c=a;a=b;b=c;} 简单结构体成员 号码 姓名 #include StdAfx.h #includestdio.h #includestring.h #includeiostream using namespace std; struct student { int num; char name[50]; struct student *next; }; struct student *creat(int n) { int i; struct student *head,*p1,*p2; int num1; char name1[50]; head=NULL; for(i=n;i0;--i) { p1=(struct student* )malloc(sizeof(struct student)); cout输入编号; cinnum1; cout输入姓名; cinname1; p1-num=num1; strcpy(p1-name,name1); if(head==NULL) { head=p1; p2=p1; } else { p2-next=p1; p2=p1; } }p2-next=NULL; return head; } void main() { int n; struct student *q; cinn; q=creat(n); while(q) { cout编号q-num\n; cout姓名q-name\n; q=q-next; } getchar(); } 利用栈实现进制转换 #include StdAfx.h #includestdio.h typedef struct tagSTACK { int stack[255]; int top; }STACK; STACK s; void push(int i) { s.top++; s.stack[s.top] = i; } int pop() { int ret; if (s.top -1) { ret = s.stack[s.top]; s.top--; return ret; } return -1; } void Bin(int i, int k, char str[]) { int j, ret; while (i) { push(i % k); i /= k; } for (j = 0; ret = pop(), ret != -1; ++j) { if (ret 9) { str[j] = ret + A - 10; } else { str[j] = ret + 0; } } str[j] = 0; } int main(void) { int i; int j; char result[255]; s.top = -1; printf(Please inuput the Number:); scanf(%d, i); getchar(); printf(Please inuput the mode (2,8,16):); scanf(%d, j); getchar(); switch (j) { case 2: Bin(i, 2, result); printf(二进制:\t\t

文档评论(0)

1亿VIP精品文档

相关文档