C - Spreadsheet Calculator UVA - 215

#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
int m, n;
int flag[25][15];//判定是否死锁 0表示已解锁,1表示待解锁,2表示死锁
char str[25][15][80];
int num[25][15];
bool f;
void dfs(int x, int y) {
	flag[x][y] = 2;
	int s = 0, op = 1, x0 = -1, y0 = -1;
	for (int i = 0; str[x][y][i] != '\0'; ++i) {
		char ch = str[x][y][i];
		if (ch >= 'A' && ch <= 'Z') {
			x0 = ch - 'A';
		}
		else if (ch >= '0' && ch <= '9') {
			if (x0 == -1) {
				if (y0 == -1) {
					y0 = 0;
				}
				y0 = y0 * 10 + ch - '0';
				if (!(str[x][y][i + 1] >= '0' && str[x][y][i + 1] <= '9') || str[x][y][i + 1] == '\0') {
					s = s + y0 * op;
					y0 = -1;
				}
				continue;
			}
			y0 = ch - '0';
			if (flag[x0][y0] == 2) {
				f = true;
				return;
			}
			else if (flag[x0][y0] == 1) {
				dfs(x0, y0);
			}
			if (flag[x0][y0] == 0) {
				s = s + op*num[x0][y0];
			}
			else {
				f = true;
				return;
			}
			x0 = -1, y0 = -1;
		}
		else {
			if (ch == '+') {
				op = 1;
			}
			else {
				op = -1;
			}
		}
	}
	num[x][y] = s;
	flag[x][y] = 0;
}
int main() {
	while (~scanf("%d%d", &m, &n)) {
		if (m == 0 && n == 0) {
			break;
		}
		for (int i = 0; i < m; ++i) {
			for (int j = 0; j < n; ++j) {
				scanf("%s", str[i][j]);
				if ((str[i][j][0] >= '0' && str[i][j][0] <= '9') || str[i][j][0] == '+' || str[i][j][0] == '-') {
					int k = 0, op = 1;
					if (str[i][j][0] == '-') {
						op = -1;
						k = 1;
					}
					else if (str[i][j][0] == '+') {
						k = 1;
					}
					flag[i][j] = 0;
					int s = 0;
					for (; str[i][j][k] != '\0'; ++k) {
						s = s * 10 + str[i][j][k] - '0';
					}
					s *= op;
					num[i][j] = s;
				}
				else {
					flag[i][j] = 1;
				}
			}
		}
		f = false;
		for (int i = 0; i < m; ++i) {
			for (int j = 0; j < n; ++j) {
				if (flag[i][j] == 1) {
					dfs(i, j);
				}
			}
		}
		if (f) {
			for (int i = 0; i < m; ++i) {
				for (int j = 0; j < n; ++j) {
					if (flag[i][j] == 2) {
						printf("%c%c: %s\n", i + 'A', j + '0', str[i][j]);
					}
				}
			}
		}
		else {
			putchar(' ');
			for (int i = 0; i < n; ++i) {
				printf("%6d", i);
			}
			for (int i = 0; i < m; ++i) {
				printf("\n%c", i + 'A');
				for (int j = 0; j < n; ++j) {
					printf("%6d", num[i][j]);
				}
			}
			putchar('\n');
		}
		putchar('\n');
	}
	return 0;
}
细节太多了,表达式中可能含数字,输入的可能有负数,打印时右对齐6位,其他没什么太多难的,另外是标记当前表达式是否已解锁为数字,标记其是否已死锁状态或是待解锁状态。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值