|
本帖最后由 bqgup 于 2018-9-8 17:10 编辑
- /*************************************************************************************
- *项目需求:求和形如2 + 22 + 222 + 2222 + 22222
- *完成日期:01.13.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <math.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- u16 Same_Num(u8 value, u8 num)
- {
- u8 i;
- u16 sum = 0;
- for(i = 0; i < num; i++)
- {
- sum += value * (u16)(pow(10,i));
- }
- return sum;
- }
- u16 Same_Num_Sum(u8 value, u8 wei)
- {
- u8 n;
- u16 sum = 0;
- for(n = 0; n < wei; n++)
- {
- sum += Same_Num(value,n+1);
- }
- return sum;
- }
- int main(void)
- {
- u8 a,b;
- printf("请输入一些书求和,例如:\n");
- printf("2 + 22 + 222 + 2222 + 22222 (此时n = 5)\n");
- scanf("%d,%d",&a,&b);
- printf("我想以 %d 为基准,我想加 %d 次:",a,b);
- printf("%d\n", Same_Num_Sum(a, b));
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:一行字符结构分类
- *完成日期:01.13.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <math.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- typedef struct
- {
- char Engliash;
- char Null;
- char Num;
- char Else;
- }Add;
- Add add = {0,0,0,0};
- int main(void)
- {
- char c;
- while((c = getchar()) != '#')
- {
- if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
- {
- add.Engliash++;
- }
- else if(c == ' ')
- {
- add.Null++;
- }
- else if(c >= '0' && c <= '9')
- {
- add.Num++;
- }
- else
- {
- add.Else++;
- }
- printf("%c",c);
-
- }
- printf("%4d%8d%12d%16d\n",add.Engliash,add.Null,add.Num,add.Else);
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:水仙花数
- *完成日期:01.13.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <math.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- typedef enum
- {
- ge,
- shi,
- bai,
- }WEI;
- u8 Bits(WEI bbb,u16 num)
- {
- u8 value;
- switch(bbb)
- {
- case(ge): value = num % 10;break;
- case(shi):value = num % 100 /10;break;
- case(bai):value = num / 100;break;
- }
- return value;
- }
- int main(void)
- {
- u16 i;
- for(i = 100; i < 1000;i++)
- {
- if(i == (u16)(pow(Bits(bai,i),3) + pow(Bits(shi,i),3) + pow(Bits(ge,i),3)))
- {
- printf("%d\n",i);
- }
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:建立静态链表
- *完成日期:01.14.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- typedef struct
- {
- int num;
- float score;
- struct Data * next;
- } Data;
- int main(void)
- {
- Data a,b,c,*head,*p;
- a.num = 10101;
- a.score = 89.5;
- b.num = 10103;
- b.score = 90;
- c.num = 10107;
- c.score = 85;
- head = &a;
- a.next = &b;
- b.next = &c;
- c.next = NULL;
- p = head;
- do
- {
- printf("%ld %5.1f\n",p->num,p->score);
- p = p->next;
-
- }while(p != NULL);
-
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:建立动态内存
- *完成日期:01.14.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- void Check(int * p)
- {
- int i;
- printf("They are fail:");
- for(i = 0; i < 5; i++)
- {
- if(p < 60)
- {
- printf("%d ",p);//输出不合格的成绩
- }
- }
- printf("\n");
- }
- int main(void)
- {
- int * p1,i;
- p1 = (int *)malloc(20);//开辟20个字节动态内存区,将地址转换成int *型,然后放在p1中
- for(i = 0; i < 5; i++)
- {
- scanf("%d",&p1);
- }
- Check(p1);
- return 0;
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:动态内存的应用
- *完成日期:01.14.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- int *p1,i;
- p1 = (int *)malloc(6 * sizeof(int));//类型可以自动转换
- for(i = 0; i < 6; i++)
- {
- scanf("%d",p1 + i);
- }
- for(i = 0; i < 6; i++)
- {
- if(*(p1 + i) > 80)
- {
- printf("%d ",*(p1 + i));
- }
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:求完数
- *完成日期:02.04.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- void Printf(void)
- {
- printf(" its factors are ");
- }
- int main(void)
- {
- u16 i,j;
- u16 sum = 0;
- for(i = 2; i <= 1000; i++)
- {
- for(j = 1; j <= 1000; j++)
- {
- if((i % j) == 0)
- {
- if(j != i) //防止输出被除数
- {
- sum += j;
- }
- }
- }
- if(sum == i)
- {
- printf("%d ",i);
-
- Printf();
- for(j = 1; j <= 1000; j++)
- {
- if((sum % j) == 0)
- {
-
- if(sum != j) //防止输出被除数
- {
- printf("%d ",j);
- }
- }
- }
- putchar('\n');
-
- }
-
- sum = 0; //sum清零,以便下次运算
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:分数系列前20项求和
- *完成日期:02.04.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- /*************************************************************************************
- *函数名称:斐波那契列数(递归方法)
- *完成日期:02.04.2018
- *函数作者:bqgup
- *************************************************************************************/
- u16 FBNQL(u8 n)
- {
- u16 sum;
- if(n == 1)
- {
- sum = 1;
- }
- else if(n == 2)
- {
- sum = 1;
- }
- else if(n > 2)
- {
- sum = FBNQL(n - 1) + FBNQL(n - 2);
- }
- return sum;
- }
- void main(void)
- {
- u8 i;//求和的个数
- u16 a;//分母
- u16 b;//分子
- float sum = 0;
- for(i = 1; i <= 20; i++)
- {
- b = FBNQL(2 + i);//分子
- a = FBNQL(1 + i);//分母
- sum += (float)(b) / (float)(a);
- }
- printf("%.2f\n",sum);
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:分数系列前20项求和
- *完成日期:02.04.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- u8 i = 1;
- float sum = 100;
- float high = 100;
- do
- {
- i++;
- sum = sum / 2;
-
- high += (sum * 2);
- }while(i <= 10);
- printf("%f\n",sum);
- printf("%f\n",high);
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:甲乙队分组
- *完成日期:02.04.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- char i,j;
- for(i = 'A'; i <= 'C'; i++)
- {
- for(j = 'X';j <= 'Z'; j++)
- {
- if(i == 'A')
- {
- if(j == 'X')
- {
- continue;
- }
- }
- if(i == 'C')
- {
- if(j == 'X' || j == 'Z')
- {
- continue;
- }
- }
- printf("%c->%c\n",i,j);
- }
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:冒泡法排序
- *完成日期:02.04.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- u8 i,j;
- u8 array[10];
- u8 t;
- printf("Please Input ten numbers:");
-
- for(i = 0; i < 10; i++)
- {
- scanf("%d",&array);
- }
- for(i = 0; i < 9; i++)
- {
- for(j = 0; j < 9 - i; j++)
- {
- if(array[j + 1] < array[j])
- {
- t = array[j + 1];
- array[j + 1] = array[j];
- array[j] = t;
- }
- }
- }
- for(i = 0; i < 10; i++)
- {
- printf("%d ",array);
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:二维数组行列互换
- *完成日期:02.05.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- u8 a[2][3] = {{1,2,3},{4,5,6}};
- u8 b[3][2];
- u8 i,j;
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 2; j++)
- {
- b[j] = a[j];
- printf("%d ",b[j]);
- if(j == 1)
- {
- putchar('\n');
- }
- }
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:求矩阵中最大元素
- *完成日期:02.05.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- u8 a[][4] = {13,2,3,4,15,6,7,8,9,10,11,5};
- u8 i,j,t = 0;
- u8 q,e;
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 4; j++)
- {
- printf("%d ",a[j]);
- if(j == 3)
- {
- putchar('\n');
- }
- if(t < a[j])
- {
- t = a[j];
- q = i;
- e = j;
- }
- }
- }
- printf("第%d行第%d列的数值最大为%d\n",q + 1,e + 1,t);
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:字符串连接
- *完成日期:02.05.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- u8 str1[50] = "People's Republic of ";
- u8 str2[50] = "China";
- puts(strcat(str1,str2));
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:求素数
- *完成日期:02.06.2018
- *函数作者:bqgup
- *************************************************************************************/
- /*************************************************************************************
- 伪代码分析:
- 1、让此数a被i除(i的值从2变到a - 1)
- 2、如果a能被2~(a - 1)之中任何一个整数整除,则表示a肯定不是素数,跳出循环,此时i < a
- **************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
- u8 a;
- u8 i;
- printf("Please Input a num:");
- scanf("%d",&a);
- for(i = 2; i <= a - 1; i++)
- {
- if(a % i == 0)
- {
- break;
- }
- }
- if(i < a)
- {
- printf("no prime");
- }
- else
- {
- printf("yes prime");
- }
- putchar('\n');
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:用筛选法求100之内的素数
- *完成日期:02.06.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
-
- u8 i,j;
- for(i = 2; i <= 100; i++)
- {
- for(j = 2; j <= i - 1; j++)
- {
- if(i % j == 0)
- {
- break;
- }
- }
- if(j < i)
- {
- printf("%d no prime\n",i);
- }
- else
- {
- printf("%d yes prime\n",i);
- }
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:用选择法对10个整数排序
- *完成日期:02.06.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
-
- u8 i,j;
- u8 a[10];
- u8 t;
- for(i = 0; i < 10; i++)
- {
- scanf("%d",&a);
- }
- for(i = 0; i < 9; i++)
- {
- for(j = 0; j < 9 - i; j++)
- {
- if(a[i + 1] < a)
- {
- t = a[i + 1];
- a[i + 1] = a;
- a = t;
- }
- }
- }
- for(i = 0; i < 10; i++)
- {
- printf("%d ",a);
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:求一个3 X 3 的整型矩阵对角线元素之和
- *完成日期:02.06.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
-
- u8 i,j;
- u8 a[3][3];
- u8 sum = 0;
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 3; j++)
- {
- scanf("%d",&a[j]);
- }
-
- }
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 3; j++)
- {
- printf("%d ",a[j]);
- if(j == 2)
- {
- putchar('\n');
- }
- }
-
- }
- for(i = 0; i < 3; i++)
- {
- for(j = 0; j < 3; j++)
- {
-
- if(j == i)
- {
- sum += a[j];
- }
- }
-
- }
- printf("%d\n",sum);
-
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:按照原排序规律将一个数插入数组中
- *完成日期:02.07.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
-
- u8 i,j;
- u8 a[5] = {1,4,8,9,16};
- u8 b;
- u8 sum = 0;
- scanf("%d",&sum);
- for(i = 0; i < 5; i++)
- {
- if(sum >= a && sum <= a[i + 1])
- {
- b = i;
- break;
- }
-
- }
- a = sum;
- for(i = 0; i < 5; i++)
- {
- printf("%d ",a);
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:数组逆序重新存放
- *完成日期:02.07.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
-
- u8 i,j;
- u8 a[5];
- for(i = 0; i < 5; i++)
- {
- scanf("%d",&a);
- }
- for(i = 0; i < 5; i++)
- {
- printf("%d ",a);
- }
- putchar('\n');
- for(i = 0; i < 5; i++)
- {
- printf("%d ",a[4 - i]);
- }
- putchar('\n');
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:10 X 10杨辉三角
- *完成日期:02.14.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- int main(void)
- {
-
- u8 i,j;
- u8 a[10][10];
- for(i = 0; i < 10; i++)
- {
- for(j = 0; j < 10; j++)
- {
- if(j == i)
- {
- a[j] = 1;
- }
- if(j == 0)
- {
- a[j] = 1;
- }
- if(j != i && j != 0)
- {
- a[j] = a[i - 1][j] + a[i - 1][j - 1];
- }
-
- }
- }
- for(i = 0; i < 10; i++)
- {
- for(j = 0; j <= i; j++)
- {
- printf("%d ",a[j]);
- if(j == i)
- {
- putchar('\n');
-
- }
- }
- }
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:删除字符串中任意一个字符
- *完成日期:02.16.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- #define CHARACTER ' '
-
- typedef unsigned char u8;
- typedef unsigned int u16;
- u8 str[80];
- u8 i,j;
- int main(void)
- {
- gets(str);
- for(i = j = 0; str != '\0'; i++)
- {
- if(str != CHARACTER)
- {
- str[j++] = str;
- }
-
- }
- str[j] = '\0';
- printf("%s\n",str);
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:不用strcat连接两个字符串(普通版)
- *完成日期:02.16.2018
- *函数作者:bqgup
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- u8 str[80];
- u8 str1[] = "I love you ";
- u8 str2[] = "B Q G !!!!!";
- u8 i,j;
- int main(void)
- {
- for(i = 0; i < sizeof(str1); i++)
- {
- str = str1;
- }
- for(j = 0; j < sizeof(str2); j++)
- {
- str[sizeof(str1) + j - 1] = str2[j];
- }
- printf("%s\n",str);
-
- }
- /*********************end of file***************************/
- /*************************************************************************************
- *项目需求:不用strcat连接两个字符串(封装版)
- *完成日期:02.16.2018
- *函数作者:(来源于网上)
- *************************************************************************************/
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- typedef unsigned int u16;
- u8 str[80];
- u8 str1[] = "I love you ";
- u8 str2[] = "B Q G !!!!!";
- u8 i,j;
- int main(void)
- {
- while(str1 != '\0')
- {
- i++;
- }
- while(str2[j] != '\0')
- {
- str1[i++] = str2[j++];
- }
- str1 = '\0';
- printf("%s\n",str1);
- }
- /*********************end of file***************************/
复制代码
|
|