You are on page 1of 15

#include "stdafx.

h" //tools of the trade


#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <exception>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <sstream>
#include <time.h>
using namespace std;

char co,ro,co1,ro1; //stores data on columns & rows as character


char ov; //stores input on whether to overwrite an existing value
in a cell
int c,c1; //stores data on columns as integer
int r,r1; //stores data on rows as integer
int opascii; //stores the operator in ascii
char as='Y'; //stores the input for the auto-save function
string ext=".txt"; //provides extension to the file name given by the user
string line; //provides a temporary storage when a line is read by the inputstre
am during file opening
string formula; //stores the formula
string label; //stores the label
string fname; // stores the file name
char inp[2]; //stores the user command after the instructions are displayed
char ftype; //stores an element of the formula which is then used to check the t
ype of formula
char celladd[2]; //stores the cell address
char celladd1[2]; //stores the cell address
double sheet[7][7]; //creates the spreadsheet which stores numerical values
string sheet1[7][7]; //creates the spreadsheet which stores formulae and labels
i.e. string types
double csheet[7][7]; //creates custom sheet for display
string csheet1[7][7]; // creates custom sheet for display
string temp,temp1; //used to temporarily store strings for comparison with eleme
nts in sheet1
double val; //stores the data entered by the user to be saved in the spreadsheet
string split[20];
int nsplit = 0;// stores counter for split
class finalproject
{
public:
void quit(char as1) //exit
{
if(as1=='Y')
{
fileSave();
cout<<"File saved. Exiting ...\n\n";
exit(1);
}
else
cout<<"Exiting ...\n\n";
exit(1);
} //checks for auto save and then exits the TSP
void autoSave()
{
cout<<"Enter Y to keep auto save ON. Enter N to turn auto save O
FF\n";
cin>>as;
} //checks for autosave input
void displaySheet()
{
for(int a=0;a<7;a++)
{
if(a==0) //header
{
cout<<"_________________________________________
______________________________________\n";
for(int b=0;b<=7;b++)
{
if(b!=0)
{
cout<<setw(10)<<char(64+b)<<"|";
}
else cout<<setw(2)<<"|";
}
cout<<endl;
}
else
{
cout<<"_________________________________________
______________________________________\n";
cout<<a<<"|"; // vertical numeric designation
for(int b=0;b<7;b++)
{
temp=sheet1[a][b];
if(temp.empty())
{
cout<<setw(10)<<sheet[a][b]<<"|"; //disp
lays the numeric contents of the sheet
}
else
cout<<setw(10)<<sheet1[a][b]<<"|"; //dis
plays the formulae and labels
}
cout<<endl;
}
}
cout<<"_________________________________________________________
______________________\n";
} //displays a 6*6 spreadsheet
void cdisplaySheet()
{
for(int a=0;a<7;a++)
{
if(a==0) //header
{
cout<<"_________________________________________
______________________________________\n";
for(int b=0;b<=7;b++)
{
if(b!=0)
{
cout<<setw(10)<<char(64+b)<<"|";
}
else cout<<setw(2)<<"|";
}
cout<<endl;
}
else
{
cout<<"_________________________________________
______________________________________\n";
cout<<a<<"|"; // vertical numeric designation
for(int b=0;b<7;b++)
{
temp=csheet1[a][b];
if(temp.empty())
{
cout<<setw(10)<<csheet[a][b]<<"|"; //dis
plays the numeric contents of the sheet
}
else
cout<<setw(10)<<csheet1[a][b]<<"|"; //di
splays the formulae and labels
}
cout<<endl;
}
}
cout<<"_________________________________________________________
______________________\n";
}//custom display
void redisplaySheet()
{
for(int a=0;a<7;a++)
{
if(a==0) //header
{
cout<<"_________________________________________
______________________________________\n";
for(int b=0;b<=7;b++)
{
if(b!=0)
{
cout<<setw(10)<<char(64+b)<<"|";
}
else cout<<setw(2)<<"|";
}
cout<<endl;
}
else
{
cout<<"_________________________________________
______________________________________\n";
cout<<a<<"|"; // vertical numeric designation
for(int b=0;b<7;b++)
{
cout<<setw(10)<<sheet[a][b]<<"|"; //disp
lays the numeric contents of the sheet

}
cout<<endl;
}
}
cout<<"_________________________________________________________
______________________\n";
}// displays only numbers
void instructions() // display the instructions
{
cout<<"\n\tThese are the codes for the various commands \n \tAll
commands and cell addresses must be uppercase\n";
cout<<"\tColumns are (A-G) & Rows (1-6)\n";
cout<<"\t*************************************************\n";
cout<<" EF : Enter a formula \n";
cout<<" ED : Enter data \n";
cout<<" EL : Enter a label \n";
cout<<" CS : Clears the terminal screen\n";
cout<<" AS : Toggle Autosave On or Off \n";
cout<<" DI : Displays the spreadhseet with all the values \n";
cout<<" CD : Custom Display \n";
cout<<" D : Compute the formulae and display the spreadsheet \n
";
cout<<" P : Print screen\n";
cout<<" S : Save the spreadsheet to the HDD\n";
cout<<" R : Open the spreadsheet from the HDD\n";
cout<<" C : Clears all data in the spreadsheet\n";
cout<<" Q : Quit the program \n \t* Please save the spreadsheet
if you intend to use it later\n\n\n";
dateTime();
} //displays the commands and the codes to execute them
void setData()
{
cout<<"Enter the cell address.\nColumns are (A-G) & Rows (1-6)";
cin>>celladd;
cout<<"Column is "<<celladd[0]<<" and Row is "<<celladd[1]<<endl
;
co=celladd[0];
ro=celladd[1];
c=(int)co;
c=c-65;
r=(int)ro;
r=r-48;
cout<<"\nInput data \n";
cin>>val;
sheet[r][c]=val;
displaySheet();
} //sets numeric data in the spreadsheet
void storeFormula()
{
cout<<"Enter the cell address where the formula will be stored.\
nColumns are (A-G) & Rows (1-6)";
cin>>celladd1;
cout<<"Column is "<<celladd1[0]<<" and Row is "<<celladd1[1]<<en
dl;
co1=celladd1[0];
ro1=celladd1[1];
c1=(int)co1;
c1=c1-65;
r1=(int)ro1;
r1=r1-48;
cout<<"Enter the formula\n";
cout<<"You can use any of the following three formats \n";
cout<<"<operator>,<cell1>,<cell2> : to perform operation on two
cells\n";
cout<<"<operator>,<cell1>-<cell2> : to perform operation on a ra
nge of cells\n";
cout<<"<operator>,<constant>,<cell1> : to perform operation on a
cell and a constant\n";
cout<<"Note : please input the constant as a four digit number.
Eg : 23.00 or 23.23 or 232.3 \n";
cin>>formula;
if(sheet1[r1][c1].empty())
{
sheet1[r1][c1]=formula;
displaySheet();
}
else
{ cout<<"The cell "<<co1<<r1<<" Contains "<<sheet1[r1][c1]
<<" . Overwrite ? Y/N\n";
cin>>ov;
if(ov=='Y')
{ sheet1[r1][c1]=formula;
displaySheet();
}
else
cout<<"Keeping the old value \n\n";
displaySheet();
}
} //stores the formula in the spreadsheet
void storeLabel()
{
cout<<"Enter the cell address where the Label will be stored.\nC
olumns are (A-G) & Rows (1-6)";
cin>>celladd1;
cout<<"Column is "<<celladd1[0]<<" and Row is "<<celladd1[1]<<en
dl;
co1=celladd1[0];
ro1=celladd1[1];
c1=(int)co1;
c1=c1-65;
r1=(int)ro1;
r1=r1-48;
cout<<"Enter the Label.\nPlease refrain from using more than 8 c
haracters as it will exceed the cell size \n";
cin>>label;
if(sheet1[r1][c1].empty())
{
sheet1[r1][c1]=label;
displaySheet();
}
else
{ cout<<"The cell "<<co1<<r1<<" Contains "<<sheet1[r1][c1]
<<" . Overwrite ? Y/N\n";
cin>>ov;
if(ov=='Y')
{
cout<<"Overwriting...\n\n";
sheet1[r1][c1]=label;
displaySheet();
}
else
cout<<"Keeping the old value \n\n";
displaySheet();
}
} //stores the label in the spreadsheet
void printer()
{
cout<<" Prints to the default printer.\n*Developer's note : The
function has not been tested in real time.\n\n";
ofstream prn;
cout<<"Printing to the default printer.... please wait\n\n";
for(int a=0;a<7;a++)
{
if(a==0) //header
{
prn<<"__________________________________________
_____________________________________\n";
for(int b=0;b<=7;b++)
{
if(b!=0)
{
prn<<setw(10)<<char(64+b)<<"|";
}
else prn<<setw(2)<<"|";
}
prn<<endl;
}
else
{
prn<<"__________________________________________
_____________________________________\n";
prn<<a<<"|"; // vertical numeric designation
for(int b=0;b<7;b++)
{
temp=sheet1[a][b];
if(temp.empty())
{
prn<<setw(10)<<sheet[a][b]<<"|"; //print
s the numeric contents of the sheet
}
else
prn<<setw(10)<<sheet1[a][b]<<"|"; //prin
ts the formulae and labels
}
prn<<endl;
}
}
prn<<"__________________________________________________________
_____________________\n";
// the following code snippet prints the local time and date to
the printer. Beta version
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
prn<<"Local time and date at the time of printing : "<<asctime (
timeinfo);
prn<<endl;
prn.close();
} //prints the spreadsheet to the default printer
void dateTime ()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "Current local time and date: %s", asctime (timeinfo) )
;
cout<<endl;
}// displays the date & time
void fileSave()
{
cout<<"Enter a name for the new file.\nFollow the guidelines of
your operating system while choosing a name.\n";
cout<<"File will be saved as a text document with a <.txt> exten
sion \n\n";
cin>>fname;
fname+=ext;
ofstream myfile(fname);
for(int a=0;a<7;a++)
{
if(a==0) //header
{
myfile<<"_______________________________________
________________________________________\n";
for(int b=0;b<=7;b++)
{
if(b!=0)
{
myfile<<setw(10)<<char(64+b)<<"|";
}
else myfile<<setw(2)<<"|";
}
myfile<<endl;
}
else
{
myfile<<"_______________________________________
________________________________________\n";
myfile<<a<<"|"; // vertical numeric designation
for(int b=0;b<7;b++)
{
temp=sheet1[a][b];
if(temp.empty())
{
myfile<<setw(10)<<sheet[a][b]<<"|"; //di
splays the numeric contents of the sheet
}
else
myfile<<setw(10)<<sheet1[a][b]<<"|"; //d
isplays the formulae and labels
}
myfile<<endl;
}
}
myfile<<"_______________________________________________________
________________________\n";
// the following code snippet saves the local time and date to t
he file
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
myfile<<"Local time and date when the file was last saved : "<<a
sctime (timeinfo);
myfile<<endl;
cout<<"Save successful !\n\n";
myfile.close();
} //saves the spreadsheet to a file on the HDD
void fileRetrieve()
{
cout<<"Enter file name to be opened.\nOnly text files are suppor
ted in this version of TSP\n";
cin>>fname;
fname+=ext;
ifstream myfile (fname);
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
cout<<line<<endl;
}
myfile.close();
}
} //reads a file from the HDD and displays its contents in the ter
minal
void clear()
{
for (int x=0;x<7;x++)
{
for(int y=0;y<7;y++)
{
sheet[x][y]=0.0;
sheet1[x][y]="";
}
}displaySheet();
} //clears the values of all cells in the spreadsheet & initialize
s them with 0.0
void cScreen()
{
system("CLS");
} //clears the terminal screen
void compute()
{
for(int h=0;h<7;h++)
{
for(int p=0;p<7;p++)
{
temp1=sheet1[h][p];
if(!temp1.empty()) // check if not empty
{
opascii=temp1[0]; //stores ascii v
alue of the operator
if(opascii==42||opascii==43||opascii==45
||opascii==47) //if it's a valid operator (+,-,*,/)
{
//also distinguishes from a label
ftype=temp1[4];
if(ftype==',')
{
double z;
z = calc1(temp1)
; // operates upon two cell values
sheet[h][p] = z;
}
else if(ftype=='-')
{
double z;
z = calc2(te
mp1); // operates upon a range of cells
sheet[h][p] = z;
}
else
{
double z;
z= calc3(temp1);
// operates upon a constant and a cell
sheet[h][p] = z;
}
}
}
}
}
redisplaySheet();
}//computes all the formulae and displays the sheet
double calc1(string s1)
{
char o;
char c1,c2;
char r1,r2;
int cc1,cc2;
double ans;
o=s1[0];
c1=s1[2];
r1=s1[3];
c2=s1[5];
r2=s1[6];
r1=r1-48;
r2=r2-48;
cc1=(int)c1;
cc2=(int)c2;
cc1=cc1-65;
cc2=cc2-65;

if(o=='+')
ans=sheet[r1][cc1] + sheet[r2][cc2];
else if(o=='-')
ans=sheet[r1][cc1] - sheet[r2][cc2];
else if(o=='/')
{
if(sheet[r2][cc2]!=0)
ans=sheet[r1][cc1] / sheet[r2][cc2];
else
cout<<"Can't divide by zero !\n";
ans=0.0;
}
else if(o=='*')
ans=sheet[r1][cc1]*sheet[r1][cc1];
return ans;
}//calculates answer for formula type 1
double calc2(string s1)
{
char o;
char c1,c2;
char r1,r2;
int cc1,cc2;
double ans1=0.0;
double ans2=1.0;
o=s1[0];
c1=s1[2];
r1=s1[3];
c2=s1[5];
r2=s1[6];
r1=r1-48;
r2=r2-48;
cc1=(int)c1;
cc2=(int)c2;
cc1=cc1-65;
cc2=cc2-65;
if(o=='+') //addition
{
if(cc1==cc2)
{
for(int x=r1;x<r2+1;x++)
{
ans1+=sheet[x][cc1];
}
}
else
{
for(int x=cc1;x<cc2+1;x++)
{
ans1+=sheet[r1][x];
}
}
return ans1;
}
else // product
{
if(cc1==cc2)
{
for(int x=r1;x<r2+1;x++)
{
ans2*=sheet[x][cc1];
}
}
else
{
for(int x=cc1;x<cc2+1;x++)
{
ans2*=sheet[r1][x];
}
}
return ans2;
}

}//calculates answer for formula type 2


void splitline(string s1)
{
nsplit = 0;
bool val = false;
string temp = "";
for (int i = 0;i < s1.length();i++)
{
if (s1[i] == ' ' || s1[i] == ',') //condition for token
{
split[i] = ',';
if (!val)
{
split[nsplit++] = temp;
temp = "";
val = true;
}
}
else
{
val = false;
temp += s1[i];
if (i == s1.length()-1)
{
split[nsplit++] = temp;
}
}
}
}//method to split the line in tokens
bool isnumber(string s1)
{
if (s1.compare("0") == 0) return true;
else
{
double d = atof(s1.c_str()); //type cast to double
if (d == 0) return false;
return true;
}
}//checks for numerical content in s1
double getvalue(string s1)
{
int a, b;
char e = s1[0];
char f = s1[1];
a = (int) e;
a -=65;
b = (int) f;
b -=48;
double d = sheet[b][a];
return d;
}// return the numeric value of a cell
double calc3(string s1)
{
splitline(s1); // splits the string to read individual tokens
double n, m, ans;
if (isnumber(split[1])) n = atof(split[1].c_str());
else {
n = getvalue(split[1]);
}
if (isnumber(split[2])) m = atof(split[2].c_str());
else {
m = getvalue(split[2]);
}
if (split[0].compare("+") == 0) ans = n + m;
if (split[0].compare("-") == 0) ans = n - m;
if (split[0].compare("*") == 0) ans = n * m;
if (split[0].compare("/") == 0)
{
if (m == 0) cout << "\nError: division by 0!";
else ans = n / m;
}
return ans;

}//calculates answer for formula type 3


void cDisplay()
{
string cell;
cout<<"Enter cell address \n";
cout<<"Enter single cell address : <column><row>\n";
cout<<"Enter a range of a row or a coulmn <column1><row1>-<colum
n2><row2>\n\n";
cin>>cell;
int a, b, c, d;
char e = cell[0];
char f = cell[1];
char g = cell[3];
char h = cell[4];
a = (int) e; //col1
a -=65;
b = (int) f; //ro1
b -=48;
c = (int) g; //col2
c -=65;
d = (int) h; //row2
d -=48;
if(cell[2]=='-') //indicates a range of cells
{
if(a==c) //same column
{
for(int t=b;t<d+1;t++) //copy contents of the ro
w
{
csheet[t][a]=sheet[t][a];
csheet1[t][a]=sheet1[t][a];
}
}
else //same row
{
for(int t=a;t<c+1;t++)
{
csheet[b][t]=sheet[b][t];
csheet1[b][t]=sheet1[b][t];
}
}
cdisplaySheet();
}
else // indicates a single cell
{
csheet[b][a]=sheet[b][a];
csheet1[b][a]=sheet1[b][a];
cdisplaySheet();
}
} // print the table with only the indicated cell
};

int _tmain(int argc, _TCHAR* argv[])


{

finalproject ob; //creating an object for the class


cout<<"\t \t Welcome to the Terminal Spreadsheet Program TSPv1.0 \n";
cout<<"\t \t \tAuthor : p4 \t \n";
cout<<"\t \t * AutoSave is enabled by default \n";
//brain of the program
while(true){
ob.instructions();
cin>>inp;
if(strcmp(inp,"Q")==0)
ob.quit(as);
else if(strcmp(inp,"EF")==0)
ob.storeFormula();
else if(strcmp(inp,"ED")==0)
ob.setData();
else if(strcmp(inp,"CD")==0)
ob.cDisplay();
else if(strcmp(inp,"EL")==0)
ob.storeLabel();
else if(strcmp(inp,"DI")==0)
ob.displaySheet();
else if(strcmp(inp,"P")==0)
ob.printer();
else if(strcmp(inp,"S")==0)
ob.fileSave();
else if(strcmp(inp,"R")==0)
ob.fileRetrieve();
else if(strcmp(inp,"C")==0)
ob.clear();
else if(strcmp(inp,"CS")==0)
ob.cScreen();
else if(strcmp(inp,"AS")==0)
ob.autoSave();
else if(strcmp(inp,"D")==0)
ob.compute();
else
cout<<"Incorrect command \n\n";
}

return 0;
}

You might also like