C语言程序设计:综合设计实验一(设计一个文字游戏)

C语言程序设计:综合设计实验一(设计一个文字游戏)

1.实验要求

(1) 设计一个文字游戏,通过玩家输入命令或者选择某种操作,推进剧情的发展,要求程序有一定的可玩性。

(2) 在没有学习文件之前,用字符串数组保存剧情文字信息;

(3) 在游戏中设计随机元素,用户同样的选择,可以得到不同的结果。

(4) 根据自己能力完成上面的练习后,填写实验报告,将源文件和实验报告,命名为“学号-姓名-实验X”,比如:1740611111-张三-实验1,上交到智慧树。(注:源码以截图的方式上交。)

2.设计思路

我想设计一个炒股软件,简单的买卖~~~~

3.源码

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

#define BUF_size 5

typedef struct _shares
{
	int num;//股票代码code
	int first_price;//买入价格
	int buy;//买入数量
	int last_price;//涨跌后价格
}shares;

shares items[BUF_size];

void Initialization();
void Make();
void Event();

int round = 1;
int gameon = 1;
int gameoff = 0;
int money = 1500;

int main()
{
	while (gameon)
	{
		Initialization();
		Make();
		Event();
		//gameon = 0;
		round++;
	}
	return 0;
}

void Initialization()
{
	if (money <= 0) {
		printf("\n===================[/Lose]=========================\n");
		printf("!!!很遗憾,你已经破产了\n", money);
		printf("是否重新开始游戏【输入1重开,输入0则为退出】\n", money);
		printf("===================[Lose/]=========================\n\n");
		scanf_s("%d", &gameoff);
		if (gameoff) {
			money = 1500;
			gameoff = 0;
		}
		else {
			exit(0);
		}
	}
	printf("=================[Round.%.2d]=======================\n", round);
	printf("你现在有[%d]股\n", money);
}

void Make()
{
	int a = 1;
	srand((unsigned)time(NULL));

	int code = rand() % 40000 + 10000;
	int first_price = 1 + rand() % 10;
	int increasing = rand() % 51;
	int falling = rand() % 51;

	int put = 0;
	int buymoney;

	while(a == 1)
	{
		printf("====================[Shares]======================\n");
		printf("现在有一支代码为[%d]的股票\n", code);
		printf("当前价格为:[%d]元\n", first_price);
		printf("上涨概率为[%d%%]\n", increasing);
		printf("下跌概率为[%d%%]\n", falling);
		printf("是否进行买入?\n");
		printf("[输入1为买入,输入其他则为放弃]\n");
		printf("==================================================\n");
		
		scanf_s("%d", &put);
		if (put == 1)
		{
			printf("===================[Buy]==========================\n");
			printf("需要购入多少股?\n");
			printf("==================================================\n");
			scanf_s("%d", &buymoney);
			if (buymoney > money)
			{
				printf("===============[Failded to buy]===================\n");
				printf("买入失败,你当前只拥有[%d]股,请不要超过该数量\n\n", money);
			}
			else {
				money -=buymoney;
				items->buy = buymoney;
				items->num = code;
				items->first_price = first_price;
				printf("==============[Buy Successfully]==================\n");
				printf("买入[%d]股成功,当前剩余[%d]股\n\n", buymoney, money);
			}
		}

		//a = rand() % 2;
		a = 0;
	}
}

void Event()
{
	srand((unsigned)time(NULL));
	int a = rand() % 2;//0跌1升
	int income = 0;
	int difference = 0;
	if (items->num != 0)
	{
		if (a == 1)
		{
			items->last_price = items->first_price + rand() % 11;
			difference = items->last_price - items->first_price;
			income = difference * items->buy;
			money += income;
			printf("===================[Rise]=========================\n");
			printf("你购买的代码为[%d]的股票,上涨啦\n", items->num);
			printf("买入时价格为[%d]元,当前价格为[%d]元\n", items->first_price, items->last_price);
			printf("相较于买入时上涨了[%d元],一共买入[%d]股\n", difference, items->buy);
			printf("你获得了[%d]股,当前共有[%d]股\n", income, money);
			printf("==================================================\n\n");
		}
		else {
			items->last_price = 1 + rand() % items->first_price;
			difference = items->first_price - items->last_price;
			income = difference * items->buy;
			money -= income;
			printf("===================[Fall]=========================\n");
			printf("你购买的代码为[%d]的股票,下跌啦\n", items->num);
			printf("买入时价格为[%d]元,当前价格为[%d]元\n", items->first_price, items->last_price);
			printf("相较于买入时下跌了[%d元],一共买入[%d]股\n", abs(difference), items->buy);
			printf("你损失了[%d]股,当前还剩[%d]股\n", abs(income), money);
			printf("==================================================\n\n");
		}
	}
}

4.后话

本来想用动态数组实现对买入不同的股票进行管理的,代码中埋下了好多的坑都没补上,我太懒了~~~~所以,这是一个极度精简的文字炒股游戏,enjoy it~

posted @ 2021-12-11 14:41  虐心i  阅读(530)  评论(0编辑  收藏  举报