C++基础练习程序

每天一点编程
#include
using namespace std;
int main()
{
int grade[5];
int num;
int i;2018.11.06-团队项目进展工作汇报-翟海祥.docx
for(i=1;i<5;i++)
{
cout << “请输入您的成绩,第:” < cin >> grade[i];
}
for(i=1;i<5;i++)
{
num = grade[i] / 10;
cout << “第 “< switch (num)
{
case 6:cout << “及格” << endl; break;
case 7:cout << “良” << endl; break;
case 8:cout << “中” << endl; break;
case 9:cout << “优秀” << endl; break;
default:cout << “不及格” << endl; break;
}
}
return 0;
}

找出100~200之间的质数,并输出在屏幕上
#include
#include
#include
using namespace std;
int main()
{
int m, k, i, n = 0;
bool prime;
for (m = 101; m <= 200; m++)
{
prime = true;
k = int(sqrt(m));
for(i=2;i<=k;i++)
if (m%i == 0)
{
prime = false;
break;
}
if (prime)
{
cout << setw(5) << m;
n = n + 1;

	}

	if (n % 10 == 0)cout << endl;
}
cout << endl;
cout << n << endl;
return 0;

}

求1!+2!+…+n!
#include
using namespace std;

double fun(int num);
int main()
{
double sum = 0;
int i, n;
cout << “请输入阶乘个数:”;
cin >> n;
if (n == 0)
cout << 1 << endl;
else
{
for (i = 1; i <= n; i++)
{
sum += fun(i);

	}
	cout << sum << endl;
}

return 0;

}

double fun(int num)
{
double sum = 1.0;
int j;
for (j = 1; j <= num; j++)
{
sum = sum*j;
}
return (sum);
}

#include
using namespace std;

double fun(int num);
int main()
{
double sum = 0;
int i, n;
cout << “请输入阶乘个数:”;
cin >> n;
if (n == 0)
cout << 1 << endl;
else
{
for (i = 1; i <= n; i++)
{
sum += fun(i);

	}
	cout << sum << endl;
}

return 0;

}

double fun(int num)
{
double sum = 1.0;
int j;
for (j = 1; j <= num; j++)
{
sum = sum*j;
}
return (sum);
}

2018.10.27
1调用子函数输出四个数值中最大的一个,可以通过子函数进行两两比较,然后将比较后的两个数值再次进行子函数的调用,最后输出最大一个,此时最大一个就是四个数值中最大一个,程序代码如下:
#include
using namespace std;

int max(int x, int y);
int main()
{
int a, b, c, d, n, m, p;
cout << “please input 4 nums:”;
cin >> a
>> b
>> c
>> d;
n = max(a, b);
m = max(c, d);
p = max(n, m);
cout << "max = " << p << endl;
return 0;

}

int max(int x, int y)
{
int z;
z = x > y ? x : y;
return z;
}
2.递归
递归解题时分为以下两步

  1. 回溯 2.递推
    如请一个递推时候的年纪问题
    #include
    using namespace std;

int main()
{
int age(int n);
cout << age(100) << endl;
return 0;
}

int age(int n)
{
int c;
if (n == 1)
c = 10;
else c = age(n - 1) + 2;
return c;
}

  1. 利用一般方法就n的阶乘,用if else以及while语句进行实现
  2. #include
  3. using namespace std;
  4. int main()
  5. {
  6.  float sum = 1;
    
  7.  int n;
    
  8.  cout << "please input two nums:";
    
  9. cin >> n;
    
  10. if (n < 0)
    
  11. {
    
  12. 	sum = -1;
    
  13. 	cout << "error" << endl;
    
  14. }
    
  15. // cout << sum << " error" << endl;
  16. else if (n == 0 || n == 1)
    
  17. 	sum = 1;
    
  18. else 
    
  19. 	while (n !=1)
    
  20. 	{
    
  21. 		sum = sum*n;
    
  22. 			n--;
    
  23. 	}
    
  24. cout << sum << endl;
    
  25. return 0;
    

28.}

3.通过Template定义模板,作为头文件的声明,从而实现对不同类型的子函数进行调用。
Template
T max(T a, T b, T c)
{
书写子函数想逛
}

例如比较三个数的最大值,程序如下:

include

using namespace std;
template

T max(T a, T b, T c)
{
if (b > a) a = b;
if (c > a) a = c;
return a;
}
int main()
{
int i1 = 185, i2 = -76, i3 = 452,i;
double d1 = 56.87, d2 = 90.23, d3 = -2543.02, d;
i = max(i1, i2, i3);
d = max(d1, d2, d3);
cout << i << endl;
cout << d << endl;
return 0;
}

4.斐波拉数据
1 1 2 3 5 8 13 21 …
通过数组的形式进行输出
程序如下所示
#include
#include
using namespace std;

int main()
{
int i;
int f[50] = { 1,1 };
for (i = 2; i < 50; i++)
f[i] = f[i - 2] + f[i - 1];
for (i = 0; i < 50; i++)
{
if (i % 5 == 0)cout << endl;
cout << setw(12) << f[i];
}
cout << endl;
return 0;
}

5.起泡法,就是相邻两个进行对比,将小的放在前面,这样来回进行,直到输出最大的值
程序如下:
#include
using namespace std;
int main()
{
int i, j, t;
int a[11];
cout << “input ten nums:”;
for (i = 1; i < 11; i++)
cin >> a[i];
cout << endl;
for (j = 1; j <= 9; j++)
for (i = 1; i <= 10 - j; i++)
if (a[i]>a[i + 1])
{
t = a[i]; a[i] = a[i + 1]; a[i + 1] = t;
}
for (i = 1; i < 11; i++)
cout << a[i] << endl;
return 0;
}
5.矩阵的转置
#include
#include
using namespace std;
int main()
{
int i, j;
int a[2][3] = { { 1,2,3 },{ 4,5,6 } };
int b[3][2];
for (i = 0; i <=1; i++)
{
for (j = 0; j <= 2; j++)
{
cout < b[j][i] = a[i][j];
}
cout << endl;
}

for (i = 0; i <= 2; i++)
{
	for (j = 0; j <= 1; j++)
		cout << setw(3) << b[i][j];
	cout << endl;
	

}
return 0;

}
2018.10.28
指针学习

  1. #include
    using namespace std;
    int main()
    {
    void exchange(int *, int *, int *);
    int a, b, c, *p1, *p2, *p3;
    cin >> a >> b >> c;
    p1 = &a; p2 = &b; p3 = &c;
    exchange(p1, p2, p3);
    cout << a << " " << c << endl;
    }

void exchange(int *q1, int *q2, int *q3)
{
void swap(int *, int *);
if (*q1 < *q2)swap(q1, q2);
if (*q1 < *q3)swap(q1, q3);
if (*q2 < *q3)swap(q2, q3);
}

void swap(int *pt1, int *pt2)
{
int temp;
temp = *pt1;
*pt1 = *pt2;
*pt2 = temp;
}

2.指针的指针,如下程序所示
#include
#include
#include
using namespace std;
int main()
{
char p;
char *name[] = { “BASIC”,“CHINA”,“C++” };
p = name+ 2;
cout << *p << endl;
cout << p << endl;
return 0;
}
可以看出,首先是p指向name[]的二个地址,意思类似int p= a[]
所以
p输出的是C++,而
p表示是将C++中的第一个C地址给
p,因此,**p输出的是C字符。

3.通过地址进行值交换,程序如下
#include
#include
#include
using namespace std;
int main()
{
void swap(int *, int *);
int i = 3, j = 5;
swap(&i, &j);//实参是两个数的地址
cout << i << " " << j << endl;
return 0;
}

void swap(int *p1, int *p2)
{
int temp;
temp = *p1;
*p1= *p2;
*p2 = temp;
}

4.结构体类型,用struct进行说明
首先用struct 声明类
Struct Date{
int month;
int day;
int year;
}
Struct Student{
int num;
char name[20];
char sex;
Date birthday;
Float score;
}student1,student2={10002,”zhangxin”,’f’,5/23/1992,89.5};

int main()
{
Student1=student2;//赋值
….
}

具体程序如下所示
#include
using namespace std;

struct Date
{
int month;
int day;
int year;
};

struct Student
{
int num;
char name[20];
char sex;
Date birthday;//结构体类型
float score;
}student1, student2 = { 10002,“Wang Li”,‘f’,5,23,1992,89.5 };

int main()
{
student1 = student2;
cout < cout << student1.name << endl;
cout << student1.sex << endl;
cout << student1.birthday.month << ‘/’ << student1.birthday.day << ‘/’
<< student1.birthday.year << endl;
cout << student1.score << endl;
return 0;
}

4.对三个候选人进行唱票,要求有10个人进行投票,结束后要输出每个候选人的

5.结构体中对指针的使用
Struct Student
{

};
Student stu;
Student *p=&stu;
指的是p将指向stu所在的结构体类型,程序如下:

#include
#include
using namespace std;

int main()
{
struct Student
{
int num;
string name;
char sex;
float score;
};

Student stu;
Student *p = &stu;
stu.num = 101800015;
stu.name = "Zhai Haixiang";
stu.sex = 'M';
stu.score = 99;
cout << stu.num <<" "<< stu.name << " " << stu.sex << " " << stu.score << endl;
cout << (*p).num << " "<<(*p).name << " " << (*p).sex << " " << (*p).score << endl;
return 0;

}
6.用结构体输出学生信息类型
结构体中其中采用的有数组元素,这样方便对不同科目成绩进行输出,程序如下所示:
#include
#include

using namespace std;

struct Student
{
int num;
string name;
float score[3];
};

int main()
{
void shuchu(Student stu);
Student stu;
stu.num = 101800015;
stu.name = “Zhai Haixiang”;
stu.score[0] = 95.0;
stu.score[1] = 96.4;
stu.score[2] = 90.8;
shuchu(stu);
return 0;

}

void shuchu(Student stu)
{
cout << stu.num << " " << stu.name << " " << stu.score[0] << " " << stu.score[1] << " " << stu.score[2] << " " << endl;
}

2018.10.30
1.用class对一个类进行输出,注意在使用过程中用到了public,private;
对于类中要注意区分。如下一个简单的类输出,输出当前时间
#include
using namespace std;

class Time
{
public:
int hour;
int minute;
int sec;
};

int main()
{
Time t1;
cin >> t1.hour;
cin >> t1.minute;
cin >> t1.sec;
cout << t1.hour << “:” << t1.minute << “:” << t1.sec << endl;
return 0;
}

2.利用类函数进行输出可以简化主程序的复杂冗余程度,程序如下
#include
using namespace std;

class Time
{
public:
int hour;
int minute;
int sec;
};

int main()
{
void set_time(Time&t);
void show_time(Time&t);
Time t1;
set_time(t1);
show_time(t1);

Time t2;
set_time(t2);
show_time(t2);

return 0;

}
void set_time(Time&t)
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void show_time(Time&t)
{
cout << t.hour << “:” << t.minute << “:” << t.sec << endl;
}

你可能感兴趣的:(编程)