当前位置:   article > 正文

C++案例

c++案例

目录

一、输出

1. 用for循环写一个爱心代码

2. 在爱心的基础上,做一些高级的表白爱心 

3. 蛇形矩阵

4. 杨辉三角

5. 9×9乘法表

6. 水仙花数

7. 递归--斐波那契数列

8. 递归--数的阶乘

9. 进制转化

10. 输出菱形

二、循环+数组

1. while循环猜数组

2. for循环敲桌子游戏 

3. 一维数组--元素逆置 

4. 冒泡排序

三、字符--字符串

1. 字符的大小写转化(c语言版)

四、函数

1. 封装一个函数--利用冒泡排序,实现对整型数组的升序排序

五、switch--case

1. 模拟计算器(switch case)

六、结构体

1. 结构体案例:查看明天的日期

2. 结构体嵌套结构体

3. 结构体排序 


一、输出

1. 用for循环写一个爱心代码

说明,在控制台输出一个由  “*” 组成的爱心。

  1. #include<stdio.h>
  2. int main ()
  3. {
  4. float x,y,a;
  5. for( y=1.5f; y> -1.5f ;y -=0.1f){
  6. for ( x=-1.5f;x<1.5f;x+=0.05f){
  7. float a=x*x+y*y-1;
  8. putchar(a*a*a-x*x*y*y*y<=0.0f?'*':' ');
  9. }
  10. putchar ('\n');
  11. }
  12. return 0;
  13. }

运行结果:

 

2. 在爱心的基础上,做一些高级的表白爱心 

说明:在控制台输出

遇到你
我才发现
曾经所有的条件
似乎都成了我等你的借口

我对你的感情已经决堤
所以
请允许我,从今往后映入你
明媚的眼

想和你
耳鬓厮磨,相濡以沫!答应我吧!
输入yes,你可以看到我的真心

控制所有字体改变颜色。

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #define I 20
  5. #define R 340
  6. #include <string.h>
  7. int main()
  8. {
  9. char answer[4];
  10. printf("遇到你\n我才发现\n曾经所有的条件\n似乎都成了我等你的借口\n\n");
  11. printf("我对你的感情已经决堤\n所以\n请允许我,从今往后映入你\n明媚的眼\n");
  12. printf("我\n想和你\n耳鬓厮磨,相濡以沫!");
  13. printf("答应我吧!\n输入yes,你可以看到我的真心\n");
  14. scanf("%s",&answer);
  15. float y, x, z, f;
  16. for (y = 1.5f; y > -1.5f; y-=0.1f){
  17. for (x = -1.5f; x < 1.5f; x += 0.05f){
  18. z = x*x + y*y -1;
  19. f = z*z*z - x*x*y*y*y;
  20. putchar(f <= 0.0f ? "*********"[(int)(f*-8.0f)]:' ');
  21. }
  22. putchar('\n');
  23. }
  24. long time;
  25. for(; ;)
  26. {
  27. system("color a");
  28. for(time=0;time<99999999;time++);
  29. system("color b");
  30. for(time=0;time<99999999;time++);
  31. system("color c");
  32. for(time=0;time<99999999;time++);
  33. system("color d");
  34. for(time=0;time<99999999;time++);
  35. system("color e");
  36. for(time=0;time<99999999;time++);
  37. system("color f");
  38. for(time=0;time<99999999;time++);
  39. system("color 0");
  40. for(time=0;time<99999999;time++);
  41. system("color 1");
  42. for(time=0;time<99999999;time++);
  43. system("color 2");
  44. for(time=0;time<99999999;time++);
  45. system("color 3");
  46. for(time=0;time<99999999;time++);
  47. system("color 4");
  48. for(time=0;time<99999999;time++);
  49. system("color 5");
  50. for(time=0;time<99999999;time++);
  51. system("color 6");
  52. for(time=0;time<99999999;time++);
  53. system("color 7");
  54. for(time=0;time<99999999;time++);
  55. system("color 8");
  56. for(time=0;time<99999999;time++);
  57. system("color 9");
  58. }
  59. getchar();
  60. return 0;
  61. }

运行结果:

D

 3. 蛇形矩阵

说明:

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int N;
  5. scanf("%d", &N);
  6. int i, j, down = 1, right;
  7. for(i = 0; i < N; i++){
  8. down += i; // 记录第一列的数字
  9. right = down; // 记录某一行应该从哪个数值开始加
  10. printf("%d ", down); // 打印每一列的第一个数字
  11. for(j = i+1; j < N; j++){
  12. right = right + j+1; // 记录每一行相加后的值
  13. printf("%d",right);
  14. if(j!=N-1) printf(" "); // 最后一个数字后不用空格
  15. }
  16. printf("\n");
  17. }
  18. return 0;
  19. }

运行结果:

4. 杨辉三角

  1. #include<stdio.h>
  2. int main ()
  3. {
  4. int i,j,n;
  5. int a[100][100];
  6. while(scanf("%d",&n)!=EOF)
  7. for (i=1;i<=n;i++)
  8. {
  9. for(j=1;j<=i;j++)
  10. {
  11. a[0][0]=1;
  12. if(i==j||j==0)
  13. a[i][j]=1;
  14. else
  15. a[i][j]=a[i-1][j-1]+a[i-1][j];
  16. printf("%d ",a[i][j]);
  17. }
  18. printf("\n");
  19. }
  20. return 0;
  21. }

运行结果:

5. 9×9乘法表

说明:就是我们从小背的9×9乘法表

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include <bits/stdc++.h> // 万能
  4. #include<string.h>
  5. #include<ctype.h> // 字符串字母大小写函数
  6. #include<iomanip> // 保留小数位数
  7. #include<math.h> // 数学
  8. #include <time.h> // 时间函数库
  9. //clock_t clock(void)
  10. using namespace std;
  11. int main ()
  12. {
  13. for(int i=1;i<10;i++)
  14. {
  15. for( int j=1;j<i+1;j++)
  16. {
  17. cout<<j<<"*"<<i<<"="<<j*i<<"\t";
  18. }
  19. cout<<endl;
  20. }
  21. return 0;
  22. }

运行结果:

  1. #include<stdio.h>
  2. int main()
  3. {
  4. int n;
  5. int i = 1,j;
  6. scanf("%d",&n);
  7. while( i <= n ){
  8. j = 1;
  9. while( j <= i ){
  10. printf("%d * %d = %d",j,i,i*j);
  11. if( i*j < 10 ){
  12. printf(" ");
  13. }else{
  14. printf(" ");
  15. }
  16. j++;
  17. }
  18. printf("\n");
  19. i++;
  20. }
  21. return 0;
  22. }

 运行结果:

 6. 水仙花数

 说明:

        水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number),水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身。例如:1^3 + 5^3+ 3^3 = 153。

         输出100~1000内的水仙花数。

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a,b,c;
  6. for(int i=100;i<1000;i++){
  7. a=i/100;
  8. b=i/10%10;
  9. c=i%10;
  10. if(a*a*a+b*b*b+c*c*c==i){
  11. cout<<i<<endl;
  12. }
  13. }
  14. return 0;
  15. }

运行结果:

 do-while循环

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int a,b,c;
  6. int num=100;
  7. do{
  8. a=num/100;
  9. b=num/10%10;
  10. c=num%10;
  11. if(a*a*a+b*b*b+c*c*c==num){
  12. cout<<num<<endl;
  13. }
  14. num++;
  15. }while(num<1000);
  16. return 0;
  17. }

运行结果

7. 递归--斐波那契数列

  1. #include<iostream>
  2. using namespace std;
  3. int fib(int n)
  4. {
  5. if(n==0||n==1)
  6. return 1;
  7. else
  8. return fib(n-1)+fib(n-2);
  9. }
  10. int main ()
  11. {
  12. int n;
  13. cin>>n;
  14. cout<<fib(n)<<endl;
  15. return 0;
  16. }
  17. //1 1 2 3 5 8 13

运行结果:

8. 递归--数的阶乘

  1. #include<iostream>
  2. using namespace std;
  3. long fac(int n)
  4. {
  5. if(n==0)
  6. return 1;
  7. else
  8. return(n*fac(n-1));
  9. }
  10. int main ()
  11. {
  12. int n;
  13. cin>>n;
  14. for(int i=0;i<n;i++){
  15. cout<<fac(i)<<endl;
  16. }
  17. return 0;
  18. }

运行结果:

9. 进制转化

常用的八进制,十进制和十六进制的转化

  1. // %d —— 以十进制形式打印一个整型值
  2. // %o —— 八进制
  3. // %x —— 十六进制
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int main()
  8. {
  9. int num;
  10. scanf("%d", &num);
  11. printf("%o\n", num);
  12. printf("%x\n",num);
  13. return 0;
  14. }

运行结果:

 10. 输出菱形

  1. #include<iostream>
  2. #include<stdio.h>
  3. using namespace std;
  4. int main()
  5. {
  6. char str;
  7. int n;
  8. cin>>str>>n;
  9. //输出上半部分
  10. for(int i=0;i<n;i++){
  11. //输出空格
  12. for(int j=0;j<n-1-i;j++){
  13. cout<<" ";
  14. }
  15. //输出字符
  16. for(int k=0;k<i+1;k++){
  17. cout<<str<<" ";
  18. }
  19. cout<<endl;
  20. }
  21. //输出下半部分
  22. for(int i=0;i<=n-1;i++){
  23. for(int j=0;j<i+1;j++){
  24. cout<<" ";
  25. }
  26. for(int k=0;k<n-1-i;k++){
  27. cout<<str<<" ";
  28. }
  29. cout<<endl;
  30. }
  31. return 0;
  32. }

运行结果:

二、循环+数组

1. while循环猜数组

说明:随机一个100以内的数字,共10次机会,每次猜测都反馈偏大还是偏小,猜对后显示所用次数,10次机会用完后结束。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include <bits/stdc++.h> // 万能
  4. #include<string.h>
  5. #include<ctype.h> // 字符串字母大小写函数
  6. #include<iomanip> // 保留小数位数
  7. #include<math.h> // 数学
  8. #include <time.h> // 时间函数库
  9. //clock_t clock(void)
  10. using namespace std;
  11. int main ()
  12. {
  13. srand((unsigned int)time(NULL));
  14. int num = rand()%100+1;
  15. cout<<"哈哈,提前告诉你是:"<<num<<endl;
  16. int value;
  17. int count=0;
  18. cout<<"请输入你猜的数字:"<<endl;
  19. while(true)
  20. {
  21. cin>>value;
  22. if(value>num&&count<9)
  23. {
  24. count++;
  25. cout<<"猜的大了,你还有"<<10-count<<"次机会"<<endl;
  26. }
  27. else if(value<num&&count<9)
  28. {
  29. count++;
  30. cout<<"猜的小了,你还有"<<10-count<<"次机会"<<endl;
  31. }
  32. else if(value==num&&count<9)
  33. {
  34. count++;
  35. cout<<"猜对了,用了"<<count<<"次机会"<<endl;
  36. break;
  37. }
  38. else if(count==9||9-count==0)
  39. {
  40. cout<<"10次机会都用完了,你都没猜出来!!!!!!!"<<endl;
  41. break;
  42. }
  43. }
  44. return 0;
  45. }

运行结果:

2. for循环敲桌子游戏 

说明:0~100内的数字,逢到7的倍数(7,14,21...)或者含有7的数字(17,27,37...)必须用敲桌子代替。

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include <bits/stdc++.h> // 万能
  4. #include<string.h>
  5. #include<ctype.h> // 字符串字母大小写函数
  6. #include<iomanip> // 保留小数位数
  7. #include<math.h> // 数学
  8. #include <time.h> // 时间函数库
  9. //clock_t clock(void)
  10. using namespace std;
  11. int main ()
  12. {
  13. for(int i=0;i<100;i++)
  14. {
  15. if(i>10)
  16. {
  17. if(i%7==0 || i/10==7 || i%10==7)
  18. {
  19. cout<<"敲桌子"<<endl;
  20. }
  21. else
  22. {
  23. cout<<i<<endl;
  24. }
  25. }
  26. else
  27. {
  28. if(i%7==0)
  29. {
  30. cout<<"敲桌子"<<endl;
  31. }
  32. else
  33. {
  34. cout<<i<<endl;
  35. }
  36. }
  37. }
  38. return 0;
  39. }

运行结果:

3. 一维数组--元素逆置 

说明:将一维数组中的元素排序反转输出

  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. int main ()
  5. {
  6. // 元素逆置
  7. int arr[10]={1,19,23,45,56,87,5,4,8,9};
  8. int start=0;
  9. int end=sizeof(arr)/sizeof(arr[0]);
  10. for(int i=0;i<=end/2;i++){
  11. for(int j=0;j<end;j++){
  12. cout<<arr[j]<<" ";
  13. }
  14. cout<<endl;
  15. int temp=arr[start+i];
  16. arr[start+i]=arr[end-1-i];
  17. arr[end-1-i]=temp;
  18. }
  19. cout<<endl;
  20. for(int i=0;i<end;i++){
  21. cout<<arr[i]<<" ";
  22. }
  23. cout<<endl;
  24. return 0;
  25. }

运行结果:

 方法二:

  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. int main ()
  5. {
  6. // 元素逆置
  7. int arr[10]={1,19,23,45,56,87,5,4,8,9};
  8. int start=0;
  9. int end=sizeof(arr)/sizeof(arr[0]);// 结束下标
  10. while(start<end-1){
  11. int temp=arr[start];
  12. arr[start]=arr[end-1];
  13. arr[end-1]=temp;
  14. start++;
  15. end--;
  16. }
  17. for(int i=0;i<10;i++){
  18. cout<<arr[i]<<" ";
  19. }
  20. cout<<endl;
  21. return 0;
  22. }

运行结果:

4. 冒泡排序

作用:最常用的排序算法,对数组内元素进行排序

过程:

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 对每一对相邻元素做同样的工作,执行完毕后,找到第一个最大值。
  3. 重复以上的步骤,每次比较次数-1,直到不需要比较

图示:

示例:

  1. #include<stdio.h>
  2. #include<iostream>
  3. using namespace std;
  4. int main ()
  5. {
  6. int arr[10]={2,4,0,5,8,7,1,3,9,6};
  7. for(int i=0;i<10;i++){
  8. for(int j=0;j<10-i-1;j++){
  9. if(arr[j]>arr[j+1]){
  10. int temp=arr[j];
  11. arr[j]=arr[j+1];
  12. arr[j+1]=temp;
  13. }
  14. }
  15. for(int k=0;k<10;k++){
  16. cout<<arr[k]<<" ";
  17. }
  18. cout<<endl;
  19. }
  20. cout<<endl;
  21. for(int i=0;i<10;i++){
  22. cout<<arr[i]<<" ";
  23. }
  24. cout<<endl;
  25. return 0;
  26. }

 运行结果:

  

三、字符--字符串

1. 字符的大小写转化(c语言版)

利用ASCLL码对字符进行转化

  1. #include<stdio.h>
  2. int main ()
  3. {
  4. char a[30];
  5. int i,j;
  6. for(i=0;i<30;i++)
  7. {
  8. while(scanf("%c",&a[i])!=EOF)
  9. if(a[i]>='a'&&a[i]<='z')
  10. {
  11. a[i]-=32;
  12. printf("%c",a[i]);
  13. }
  14. else if(a[i]>='A'&&a[i]<='Z')
  15. printf("%c",a[i]+=32);
  16. else if(a[i]=='0'){ // 按0退出
  17. i=31;
  18. break;
  19. }
  20. else
  21. printf("%c",a[i]);
  22. }
  23. return 0;
  24. }

运行结果:

四、函数

1. 封装一个函数--利用冒泡排序,实现对整型数组的升序排序

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. void bubbleSort(int *arr,int len)
  5. {
  6. for(int i=0;i<len-1;i++){
  7. for(int j=0;j<len-1-i;j++){
  8. if(arr[j]>arr[j+1]){
  9. int temp=arr[j];
  10. arr[j]=arr[j+1];
  11. arr[j+1]=temp;
  12. }
  13. }
  14. }
  15. }
  16. void printArr(int *arr,int len)
  17. {
  18. for(int i=0;i<len;i++){
  19. cout<<arr[i]<<" ";
  20. }
  21. cout<<endl;
  22. }
  23. int main()
  24. {
  25. int arr[10]={4,3,6,9,1,2,10,8,7,5};
  26. // 数组长度
  27. int len=sizeof(arr)/sizeof(arr[0]);
  28. printArr(arr,len);
  29. bubbleSort(arr,len);
  30. printArr(arr,len);
  31. return 0;
  32. }

运行结果:

五、switch--case

1. 模拟计算器(switch case)

利用switch-case,模拟计算器的功能。

  1. #include<stdio.h>
  2. int main(){
  3. int a,b;
  4. char c;
  5. while(scanf("%d %c %d",&a,&c,&b)!=EOF)
  6. switch(c)// 用于判读符号
  7. {
  8. case '+':
  9. printf("%d%c%d=%d ",a,c,b,a+b);
  10. break;
  11. case '-':
  12. printf("%d%c%d=%d ",a,c,b,a-b);
  13. break;
  14. case '*':
  15. printf("%d%c%d=%d ",a,c,b,a*b);
  16. break;
  17. case '/':
  18. printf("%d%c%d=%d ",a,c,b,a/b);
  19. break;
  20. case '%':
  21. printf("%d%c%d=%d ",a,c,b,a%b);
  22. break;
  23. }
  24. }

运行结果:

六、结构体

1. 结构体案例:查看明天的日期

说明:结构体中存放年月日三个变量,根据传入的日期,判断明天的日期

  1. #include<stdio.h>
  2. #include<stdbool.h>
  3. struct date {
  4. int month;
  5. int day;
  6. int year;
  7. };
  8. bool isleap(struct date d);
  9. int numberofdays(struct date d);
  10. int main (int argc,char const *argv[])
  11. {
  12. struct date today;
  13. struct date tomorrow;
  14. // 定义两个结构体
  15. printf("Enter today's date(mm dd yyyy):");
  16. scanf("%d %d %d",&today.month,&today.day,&today.year);
  17. if( today.day != numberofdays(today) ){
  18. tomorrow.day = today.day + 1;
  19. tomorrow.month = today.month;
  20. tomorrow.year = today.year;
  21. // 普通年份普通月份,日期加一;
  22. }else if( today.month == 12 ){
  23. tomorrow.day = 1;
  24. tomorrow.month = 1;
  25. tomorrow.year = today.year + 1;
  26. // 一年中最后一天,直接到下一年第一天;
  27. }else{
  28. tomorrow.day = 1;
  29. tomorrow.month = today.month +1;
  30. tomorrow.year = today.year;
  31. // 某一年的某月的最后一天;所以第二天是下一个月的第一天;
  32. }
  33. printf("Tomorrow's date is : %i-%i-%i\n",
  34. tomorrow.month,tomorrow.day,tomorrow.year);
  35. return 0;
  36. }
  37. int numberofdays(struct date d)
  38. {
  39. int days;
  40. const int dayspermonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  41. if( d.month == 2 && isleap(d) )
  42. // 判断2月是否是闰年;
  43. days = 29;
  44. else
  45. days = dayspermonth[d.month-1]; // 数组从0开始,所以要减一
  46. return days;
  47. }
  48. bool isleap(struct date d)
  49. {
  50. bool leap = false;
  51. if( ( d.year %4 == 0 && d.year %100 ==0 ) || ( d.year %400 ==0 ))
  52. // if语句用于判断是否是闰年;
  53. leap = true;
  54. return leap;
  55. }

运行结果:

2. 结构体嵌套结构体

说明:三个老师的结构体数组,下面带五个学生结构体数组,给五个学生打随机分数

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<string>
  5. #include<ctype.h>
  6. #include<math.h>
  7. #include<time.h>
  8. using namespace std;
  9. struct students {
  10. string name;
  11. int score;
  12. };
  13. struct teachers {
  14. string name;
  15. struct students student[5];
  16. };
  17. // 给老师和学生赋值函数
  18. void space(struct teachers teacher[], int len)
  19. {
  20. string nameseed = "ABCDE";
  21. for (int i = 0; i < len; i++) {
  22. teacher[i].name = "Teacher_";
  23. teacher[i].name += nameseed[i];
  24. for (int j = 0; j < 5; j++) {
  25. teacher[i].student[j].name = "Student_";
  26. teacher[i].student[j].name += nameseed[j];
  27. int random = rand() % 61 + 40;
  28. teacher[i].student[j].score = random;
  29. }
  30. }
  31. }
  32. void printinfo(struct teachers teacher[], int len)
  33. {
  34. for (int i = 0; i < len; i++) {
  35. cout << "老师的姓名: " << teacher[i].name << endl;
  36. for (int j = 0; j < 5; j++) {
  37. cout << "\t学生的姓名: " << teacher[i].student[j].name
  38. << " 学生的考试分数:" << teacher[i].student[j].score << endl;
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. // 随机种子
  45. srand((unsigned int)time(NULL));
  46. teachers teacher[3];
  47. int len = sizeof(teacher) / sizeof(teacher[0]);
  48. space(teacher, len);
  49. printinfo(teacher, len);
  50. return 0;
  51. }

运行结果:

3. 结构体排序 

说明:

设计一个英雄的结构体,包括成员姓名,年龄,性别;创建结构体数组,数组中存放5名英雄。

通过冒泡排序的算法,将数组中的英雄按照年龄进行升序排列,最终打印排序后的结果。

  1. #include<iostream>
  2. using namespace std;
  3. // 英雄结构体
  4. struct Hero
  5. {
  6. string name; // 姓名
  7. int age; // 年龄
  8. string sex; // 性别
  9. };
  10. // 通过冒泡排序进行排序,按照年龄进行升序排列
  11. void bubbleSort(struct Hero heroArr[],int len)
  12. {
  13. for(int i=0;i<len-1;i++){
  14. for(int j=0;j<len-i-1;j++){
  15. if(heroArr[j].age>heroArr[j+1].age){
  16. struct Hero temp =heroArr[j];
  17. heroArr[j]=heroArr[j+1];
  18. heroArr[j+1]=temp;
  19. }
  20. }
  21. }
  22. }
  23. // 输出函数
  24. void printArr(struct Hero heroArr[],int len)
  25. {
  26. for(int i=0;i<len;i++){
  27. cout<<"姓名:"<<heroArr[i].name<<"\t年龄:"<<heroArr[i].age<<"\t性别:"<<heroArr[i].sex<<endl;
  28. }
  29. }
  30. int main()
  31. {
  32. struct Hero heroArr[5]={
  33. {"刘备",23,"男"},
  34. {"关羽",22,"男"},
  35. {"张飞",20,"男"},
  36. {"赵云",21,"男"},
  37. {"貂蝉",19,"女"},
  38. };
  39. int len=sizeof(heroArr)/sizeof(heroArr[0]);
  40. printArr(heroArr,len);
  41. // 排序
  42. bubbleSort(heroArr,len);
  43. cout<<endl<<"排序后的结果"<<endl;
  44. printArr(heroArr,len);
  45. return 0;
  46. }

运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/917538
推荐阅读
相关标签
  

闽ICP备14008679号