C语言学习(三)——多维数组&字符串数组

一、多维数组&字符串数组

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//多维数组(二维以上)

int main01()
{
	using namespace std;
	int num[2][3][4];//定义了一个三维数组,有两个二维数组,每个二维数组有3个一维数组,每个一维数组有4个元素
	//三维数组初始化
	/*int sum[2][3][4] = {
		{
			{1,2,3,1},{1,2,3,2},{1,2,3,4}
		},
		{
			{},{},{}
		}
	}*/
	//打印三维数组---用三个for循环嵌套
	
	system("pause");
	return (0);
}


//字符数组:
//定义字符数组:   char  数组名[]
//数组类型为  char []

int main02()
{
	using namespace std;
	//字符类型的数据在内存中是以ASCII码存储的!!!
	//打印字符数组
	//字符串就是字符数组中有\0字符的数组
	//因为有\0字符的字符数组,操作起来方便
	char a[5] = { 'a','b','c','d','e' };
	char b[5] = { 'a','b','c' };
	char c[5] = { 'a', 'b', 'c', 'd', '\0' };//单引号定义字符数组时,需要有\0,只有用双引号定义字符串数组时,系统会自动添加\0;
	char d[] = "world";//定义了一个字符数组,有6个元素,最后一个元素是\0
	char e[100] = "abcd";//定义了一个字符数组,有100个元素
	char f[100] = "\0";//将数组元素的第0个元素填\0,其他元素就是\0
	char g[100] = { 0 };//与上同理
	for (int i = 0; i < 5; i++)
	{
		//putchar(a[i]);
		cout << a[i] << "   ";
	}
	cout << endl;
	cout << sizeof(d) << endl;

	printf("%s\n", b);
	//################################################
	char h[128] = "abcd\0efg\0";//输出时只会输出abcd;
	cout << h << endl;
	char i[] = { 'a','b','0','c' };//输出为ab0c。。。。乱码
	char j[] = { 'a','b',0,'c' };//数字0与\0等价    ‘0’在内存中以ASCII码存储为48,
	char k[]= { 'a','b','\0','c'};//j  k 两个数组输出为ab
	//注意!!!\0后面最好不要连着数字,有可能几个数字连起来刚好是一个转义字符
	system("pause");
	return (0);
}



//scanf输入字符串
int main03()
{
	using namespace std;
	char ch = 0;
	//scanf("%c",&ch);  从键盘读取一个字符
	char num[128] = "";
	//scanf("%s",num);%s从键盘中获取一个字符串,遇到\n结束,遇到空格结束
	//缺点:遇到空格就会提前结束读取,如果存放读取字符空间不足,就会继续像狗存放,造成内存污染

	system("pause");
	return (0);
}

//gets读取字符串
int main04()
{
	using namespace std;
	char num[128] = "";
	//gets(num);//()里面的参数要的是存放读取字符串的地址
	//优点:gets()遇到\n结束,但是遇到空格不结束读取空格
	//缺点:gets也会造成内存污染
	system("pause");
	return (0);
}


//fgets()
//库函数:从键盘读取一个字符串
//格式:fgets(读取字符串地址,最大可输入的多少个数据,stdin(代表标准输入-键盘输入))
int main05()
{
	using namespace std;
	char num[128] = "";

	//fgets(num, sizeof(num), stdin);//如例子中num数组,有128个元素,
	//用fgets()函数读取字符串时,最多只能读取127个字符,因为fgets会自动在数组的最后添加一个\0;
	//fgets()函数会读取空格,会把回车键\n读取
	//fgets函数不会造成内存污染

	//因为fgets()会读取\n,所以需要消除读取的\n
	//思路是将\n替换成\0
	char buf[128] = "hello\n";//buf[5]=0;就看容易消除\n
    //需要找到最后一个字符的下标
	int i = 0;
	//求的是字符数组中有效字符的个数
	while (buf[i] != '\0')
	{
		i++;
	}
	cout << i << endl;
	buf[i - 1] = '\0';

	//strlen()函数:测字符数组的有效字符个数
	i = strlen(buf);
	buf[i - 1] = '\0';
	//与上面的while循环的作用相同
	cout << i << endl;
	system("pause");
	return (0);
}

//字符数组的输出:
//puts()函数和fputs()
int main06()
{
	using namespace std;
	char buf[1024] = "helloworld";
	puts(buf);//()里同样是数组首元素地址,有换行,
	fputs(buf, stdout);//输出到文件或者标准输出终端,stdout标准输出(屏幕),没有换行
	system("pause");
	return (0);
}

 二、字符串数组练习-猜数字游戏

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>


//1.字符追加
int main07()
{
	using namespace std;
	char str1[128] = "hello";
	char str2[128] = "12345";
	int i = 0;
	int j = 0;
	while (str1[i] != '\0')
	{
		i++;
	}
	for (j = 0; j < sizeof(str2)&& str2[j] != 0; j++, i++)
	{
		str1[i] = str2[j];
	}
	cout << str1 << endl;
	

	i = strlen(str1);//测出字符数组的字符个数
	for (j = 0; j < sizeof(str2) && str2[j]!=0; j++)
	{
		str1[i] = str2[j];//将str2中的字符,追加到str1最后一个有效字符的后面
	}
	cout << str1 << endl;
	cout << sizeof(str2) << endl;
	system("pause");
	return (0);
}



//系统产生随机数函数:
//步骤:1.设置随机数种子  2.获得随机数
//srand(10)  设置种子
//int a=rand();   获得随机数、
//设置的种子如果不变,那么生成的随机数的值也不变
//所以,可以将时间用于随机数种子,这样就可以获得一个shike在改变的随机数

int main08()
{
	using namespace std;
	int t = time(NULL);//time函数获得当前时间
	srand(t);//设置种子,只设置一次
	//或者不用t,直接设置时间为种子
	//srand(time(NULL));
	int a = rand();//rand()获得随机数,想要获得随机数,就用一次rand()赋值
	cout << a << endl;
	system("pause");
	return (0);

}


//猜数字游戏:
//让系统随即产生一个四位数字,让用户输入一个四位数字,
//用户确定输入后,提示用户输入的数字与答案数字中每一位的大小关系,知道用户输入正确的数字为止


//生成四位随机数函数(方法时:生成四个随机数,用其个位数组成一个四位随机数)
int getrand()
{
	srand(time(NULL));
	int a = rand();
	int b = rand();
	int c = rand();
	int d = rand();
	int str[4];
	int num_rand = 0;
	str[0] = a % 10;
	str[1] = b % 10;
	str[2] = c % 10;
	str[3] = d % 10;
	num_rand = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
	return num_rand;

}

//比较大小函数
int compare(int a,int b)
{
	int swap;
	if (a > b)
	{
		swap = 1;
	}
	else if (a < b)
	{
		swap = 2;
	}
	else
	{
		swap = 3;
	}
	return (swap);
}









int main()
{
	using namespace std;
	int getrand();
	int num_in;
	char jutice;
	int num_rand = getrand();
	int i, swap;
	int buf[4],str[4];
	cout << "是否获取一个随机数?   Y/N" << endl;
	cin >> jutice;
	

	cin >> num_in;//从键盘中输入一个四位数字
	buf[3] = num_rand % 10;
	buf[2] = num_rand / 10 % 10;
	buf[1] = num_rand / 100 % 10;
	buf[0] = num_rand /1000;

	str[3] = num_in % 10;
	str[2] = num_in / 10 % 10;
	str[1] = num_in / 100 % 10;
	str[0] = num_in / 1000;
	while (num_in == num_rand)
	{
		
		
		
		
		
		cout << "请重新输入" << endl;
		cin >> num_in;
	}
	cout << "您猜对了!!" << endl;

	system("pause");
	return (0);
}

 三、升级版猜数字游戏

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	using namespace std;
	int getrand();
	int num_in;					//用户输入的四位数
	char jutice;				//判断是否生成随机数Y/N
	int num_rand;	//四位随机数
	int i, swap=0;
	int flag_1 = 0;
	int flag_2 = 0;
	int buf[4], str[4];			//用于存放随机数和用户输入数的千位,百位,十位,个位
	while (swap == 0)
	{
		cout << "是否获取一个随机数?   Y/N" << endl;
		while (flag_1 == 0)
		{
			cin >> jutice;
			if (jutice == 'Y')
			{

				cout << "为您生成随机数" << endl;
				num_rand = getrand();
				cout << "请输入您猜的数:" << endl;
				cin >> num_in;
				//flag_1 = 1;
				break;
				
			}


			else if (jutice == 'N')
			{
				cout << "结束游戏" << endl;
				//flag_1 = 1;
				swap = 1;
				break;

			}
			else
			{
				cout << "您输入有误,请重新输入:" << endl;
			}
			
		}

		while (num_in != num_rand)
		{
			//求随机数的四个位
			buf[3] = num_rand % 10;
			buf[2] = num_rand / 10 % 10;
			buf[1] = num_rand / 100 % 10;
			buf[0] = num_rand / 1000;

			//求输入数的四个位
			str[3] = num_in % 10;
			str[2] = num_in / 10 % 10;
			str[1] = num_in / 100 % 10;
			str[0] = num_in / 1000;

			cout << endl;

			//判断千位数字是否正确
			if (buf[0] > str[0])
			{
				cout << "千位数字:比正确数字小" << endl;
			}
			else if (buf[0] < str[0])
			{
				cout << "千位数字:比正确数字大" << endl;
			}
			else
			{
				cout << "千位数字:正确" << endl;
			}

			//判断百位数字是否正确
			if (buf[1] > str[1])
			{
				cout << "百位数字:比正确数字小" << endl;
			}
			else if (buf[1] < str[1])
			{
				cout << "百位数字:比正确数字大" << endl;
			}
			else
			{
				cout << "百位数字:正确" << endl;
			}

			//判断十位数字是否正确
			if (buf[2] > str[2])
			{
				cout << "十位数字:比正确数字小" << endl;
			}
			else if (buf[2] < str[2])
			{
				cout << "十位数字:比正确数字大" << endl;
			}
			else
			{
				cout << "十位数字:正确" << endl;
			}

			//判断个位数字是否正确
			if (buf[3] > str[3])
			{
				cout << "个位数字:比正确数字小" << endl;
			}
			else if (buf[3] < str[3])
			{
				cout << "个位数字:比正确数字大" << endl;
			}
			else
			{
				cout << "个位数字:正确" << endl;
			}
			
			if (num_in != num_rand)
			{
				cin >> num_in;
			}
		}
		cout << "恭喜你!猜对了!" << endl;
		//判断用户是否还要继续游戏
		cout << "还要继续游戏吗?  Y/N" << endl;
		while (flag_2 == 0)
		{

			cin >> jutice;
			if (jutice == 'Y')
			{
				swap = 0;
				//flag_2 = 1;
				break;
			}
			else if (jutice == 'N')
			{
				swap = 1;
				//flag_2 = 1;
				break;
			}
			else
			{
				cout << "您输入有误,请重新输入:" << endl;

			}
		}

		

	}
	cout << "游戏结束,感谢您的支持!" << endl;
	

	system("pause");
	return (0);
}


//获得四位随机数
int getrand()
{
	//生成四个随机数,分别取四个随机数的个位数,组合成一个四位随机数,并返回
	srand(time(NULL));
	int a = rand();
	int b = rand();
	int c = rand();
	int d = rand();
	int str[4];
	int num_rand = 0;
	str[0] = a % 10;
	str[1] = b % 10;
	str[2] = c % 10;
	str[3] = d % 10;
	num_rand = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
	return num_rand;

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值