View Code of Problem 11

#include <iostream>
#include <string>
using namespace std;

int t;
int cnum = 1;
string ss;

int main() {
	while(cin >> t && t != 0) {
		printf("Case #%d:\n", cnum++);
		cin.ignore(); // 清除以回车结束的输入缓冲区的内容
		while(t--) {
//			cin.ignore();
			getline(cin, ss);
			int a = 0, b = 0, c = 0;
			int n1 = 0, n2 = 0, n3 = 0;
			int fx = 0, fy = 0, fz = 0;
			for(int i = 0; i < ss.length(); i++) {
				if(isalpha(ss[i])) {
					if(ss[i] == 'X') {
						fx = 1;
						a += ss[i - 2] - '0';
						n1 = ss[i + 2] - '0';
					}
					if(ss[i] == 'Y') {
						fy = 1;
						b += ss[i - 2] - '0';
						n2 = ss[i + 2] - '0';
					}
					if(ss[i] == 'Z') {
						fz = 1;
						c += ss[i - 2] - '0';
						n3 = ss[i + 2] - '0';
					}
				}
			}

			if(fx) {
				if(n1 == 0) printf("%d", a);
				else printf("%d * X ^ %d", a * n1, n1 - 1);
			}
			if(fy && fx) {
				if(n2 == 0) printf(" + %d", b);
				else printf(" + %d * Y ^ %d", b * n2, n2 - 1);	
			}
			else if(fy && !fx){
				if(n2 == 0) printf("%d", b);
				else printf("%d * Y ^ %d", b * n2, n2 - 1);
			}	
			if(fz && (fy || fx)) {
				if(n3 == 0) printf(" + %d", c);
				else printf(" + %d * Z ^ %d", c * n3, n3 - 1);
			}
			else if (fz && !fy && !fx){
				if(n3 == 0) printf("%d", c);
				else printf("%d * Z ^ %d", c * n3, n3 - 1);
			}	
			cout << endl;
			
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 11