Professional Documents
Culture Documents
}
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;
}
return 0;
}