近期主要做毕设,学习进度放慢,简单回顾一下之前的代码,主要是一些简单案例,敲一遍回顾一下
1、猜数字—while循环
//while循环—猜数字
#include<iostream>
#include<ctime>
using namespace std;
int main()
{
//随机数
srand((unsigned int )time(NULL));
int vaul=rand() % 100 + 1;//1-100之间的数
cout << "请输入猜的数字:" << endl;
int num = 0;
int time = 0;
while (time<5)
{
cin >> num;
if (num > vaul)
{
cout << "猜大了" << endl;
time++;
}
else if (num < vaul)
{
cout << "猜小了" << endl;
time++;
}
else
{
cout << "猜对了,游戏成功" << endl;
time = 0;
break;
}
}
if (time != 0)
{
cout << "游戏失败" << endl;
}
system("pause");
return 0;
}
2、水仙花数—数字分位表示以及do while循环
//水仙花数,一个三位数的各个位数的三次方的和等于这个数
#include<iostream>
using namespace std;
int main()
{
cout << "水仙花数" << endl;
int i = 100;
do
{
int a = i / 100;//百位
int b = i / 10 % 10;//十位
int c = i % 10;//个位
if (a*a*a+b*b*b+c*c*c == i)
{
cout << i << endl;
}
i++;
} while (i < 1000);
system("pause");
return 0;
}
3、for循环—敲桌子
//for循环,敲桌子案例
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 100; i++)
{
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
{
cout << "敲桌子" << endl;
}
else
{
cout << i << endl;
}
}
system("pause");
return 0;
}
4、打印星图—for循环
//for嵌套循环打印星图
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << "* ";
}
cout << endl;
}
system("pause");
return 0;
}
5、打印乘法口诀—for循环
//for循环打印乘法口诀表1
#include<iostream>
using namespace std;
int main()
{
for (int i = 1; i < 10; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j << "*" << i << "=" << j * i << "\t";
}
cout << endl;
}
system("pause");
return 0;
}
6、一维数组
//一维数组
#include<iostream>
using namespace std;
int main()
{
char str1[] = { 'a','b','c','d' };
int str2[] = { 1,4,2,6,8,76,998,4 };
//数组名表示该数组的首地址
cout << "数组的首地址:" << (int)str1 << endl;
cout << "数组的首地址:" << (int)&str1[0] << endl;
cout << "数组的地址:" << (int)&str1[1] << endl;
//数组所占内存
cout << "数组的内存大小:" << sizeof(str1) << endl;
cout << "数组的内存大小:" << sizeof(str2) << endl;
//求数组的元素个数
cout << "数组的元素个数:" << sizeof(str1) / sizeof(str1[0]) << endl;
system("pause");
return 0;
}
7、一维数组—称体重
//一维数组五只小猪称体重
#include<iostream>
using namespace std;
int main()
{
int str[] = { 250,260,270,290,300 };
int max = 0;
int i = 0;
while (i < 5)
{
if (str[i] > max)
{
max = str[i];
}
i++;
}
cout << "最重的小猪: " << max << endl;
system("pause");
return 0;
}
8、一维数组—元素逆置
//一维数组元素逆置
#include<iostream>
using namespace std;
int main()
{
int str[] = { 1,32,45,6,8,9,0 };
int len = sizeof(str) / sizeof(str[0]);
cout << "逆置前的数组:" << endl;
for (int i = 0; i <len;i++)
{
cout << str[i] << endl;
}
int start = 0;
int temp = 0;
int end = len - 1;
while (start<end)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
cout << "逆置后的数组:" << endl;
for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++)
{
cout << str[i] << endl;
}
system("pause");
return 0;
}
9、一维数组—冒泡排序
//冒泡排序升序
#include<iostream>
using namespace std;
int main()
{
int str[] = { 1,4,7,4,56,7,87,9790,823454,565,7 };
int len = sizeof(str) / sizeof(str[0]);
cout << "排序前的数组:" << endl;
for (int i = 0; i < len; i++)
{
cout << str[i] << " ";
}
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (str[j] > str[j + 1])
{
int temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
cout << "排序后的数组:" << endl;
for (int i = 0; i < len; i++)
{
cout << str[i] << " ";
}
system("pause");
return 0;
}
10、二维数组—成绩求和
//二维数组求成绩和
#include<iostream>
using namespace std;
int main()
{
int str[3][3] =
{
{99, 98, 97},
{93, 100, 99},
{95, 98, 99}
};
int m = sizeof(str) / sizeof(str[0]);
int n = sizeof(str[0]) / sizeof(str[0][0]);
for (int i = 0; i < m; i++)
{
int sum = 0;
for (int j = 0; j < n; j++)
{
sum = sum + str[i][j];
}
cout << "第" << i + 1 << "个人的总成绩:" << sum << endl;
}
system("pause");
return 0;
}
11、函数相关
//函数的调用与四种函数样式
#include<iostream>
using namespace std;
//交换函数
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "子函数交换后的值:" << "a=" << a << endl;
cout << "子函数交换后的值:" << "b=" << b << endl;
}
void swap2(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
cout << "子函数交换后的值:" << "a=" << *a << endl;
cout << "子函数交换后的值:" << "b=" << *b << endl;
}
int main()
{
int a = 10;
int b = 20;
swap(a,b);
cout << "主函数交换后的值:" << "a=" << a << endl;
cout << "主函数交换后的值:" << "b=" << b << endl;
swap2(&a, &b);
cout << "主函数交换后的值:" << "a=" << a << endl;
cout << "主函数交换后的值:" << "b=" << b << endl;
system("pause");
return 0;
12、指针与数组
//用指针遍历数组
#include<iostream>
using namespace std;
int main()
{
int str[] = { 1,3,5,7,8,9 };
int* p = str;
int len = sizeof(str) / sizeof(str[0]);
for (int i = 0; i < len; i++)
{
cout << *p << endl;
p++;
}
system("pause");
return 0;
}
13、指针数组与函数
//指针进行冒泡排序
#include<iostream>
using namespace std;
void bubblesort(int* str,int len)
{
for (int i = 0; i < len-1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (str[j] > str[j + 1])
{
int temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
int main()
{
int str[] = { 1,5,7,23,9,33,46,78,80 };
int len = sizeof(str) / sizeof(str[0]);
int* p = str;
cout << "排序前的数组:" << endl;
for (int i = 0; i < len; i++)
{
cout << *p << " ";
p++;
}
cout << endl;
bubblesort(str,len);
p = str;
cout << "排序后的数组:" << endl;
for (int i = 0; i < len; i++)
{
cout << *p << " ";
p++;
}
cout << endl;
system("pause");
return 0;
}