Anybody can help me, I got runtime error for the this problem, 1B - spread sheet, it works fine in my computer.
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<stack>
using namespace std;
char convert(int cno){
return cno - 1 + 'A';
}
int convert2n (char ch){
return ch - 'A' + 1;
}
int main()
{
int n, index, cno, total=0, multiplicand=1;
string input, row, col;
stack<char>ch_stack;
cin >> n;
while(n--){
cin >> input;
if(input[0]=='R' && (input[1]>=48 && input[1]<=57)){
for(int i=2; i<input.size(); i++){
if(input[i]=='C') {index=i; break;}
}
row = input.substr(1,index-1);
col = input.substr(index+1);
cno = atoi(col.c_str());
if(cno<=26){
cout << convert(cno) << row << endl;
}
else {
while(cno>26){
ch_stack.push(convert(cno%26));
cno /= 26;
}
ch_stack.push(convert(cno));
while (!ch_stack.empty()){
cout << ch_stack.top();
ch_stack.pop();
}
cout << row << endl;
}
}
else {
for(int i=1; i<input.size(); i++){
if(input[i]>=48 && input[i]<=57) {index=i; break;}
}
row = input.substr(index);
col = input.substr(0, index);
for(int i=col.size()-1; i>=0; i--){
char ch = col[i];
total += convert2n(ch)*multiplicand;
multiplicand *= 26;
}
cout << "R" << row << "C" << total << endl;
multiplicand = 1;
total = 0;
}
}
//system("pause");
return 0;
}
I did it in a stupid way, but i wanted to know why i got that runtime error, Please help me.
Thank you.
#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<stack>
using namespace std;
char convert(int cno){
return cno - 1 + 'A';
}
int convert2n (char ch){
return ch - 'A' + 1;
}
int main()
{
int n, index, cno, total=0, multiplicand=1;
string input, row, col;
stack<char>ch_stack;
cin >> n;
while(n--){
cin >> input;
if(input[0]=='R' && (input[1]>=48 && input[1]<=57)){
for(int i=2; i<input.size(); i++){
if(input[i]=='C') {index=i; break;}
}
row = input.substr(1,index-1);
col = input.substr(index+1);
cno = atoi(col.c_str());
if(cno<=26){
cout << convert(cno) << row << endl;
}
else {
while(cno>26){
ch_stack.push(convert(cno%26));
cno /= 26;
}
ch_stack.push(convert(cno));
while (!ch_stack.empty()){
cout << ch_stack.top();
ch_stack.pop();
}
cout << row << endl;
}
}
else {
for(int i=1; i<input.size(); i++){
if(input[i]>=48 && input[i]<=57) {index=i; break;}
}
row = input.substr(index);
col = input.substr(0, index);
for(int i=col.size()-1; i>=0; i--){
char ch = col[i];
total += convert2n(ch)*multiplicand;
multiplicand *= 26;
}
cout << "R" << row << "C" << total << endl;
multiplicand = 1;
total = 0;
}
}
//system("pause");
return 0;
}
I did it in a stupid way, but i wanted to know why i got that runtime error, Please help me.
Thank you.