THis is my data structures code but i need the write up mentioned below: Need wr
ID: 3804181 • Letter: T
Question
THis is my data structures code but i need the write up mentioned below:
Need written descriptions of the program and screenshots of the code executing. Please identify the data structures and algorithms in your program and how they are fundamental to the successful execution of the program. must also include internal documentation (comments explaining the functioning of the source code).
Here is my scientfic calculator source code:
#include
#include
#include
using namespace std;
int main()
{
char letter;
char letter1;
char letter2;
char letter3;
char letter4;
int a, b;
double a1, b1;
int result;
double result1;
cout << " ***************** SCIENTIFIC CALCULATOR ****************** ";
do
{
cout << " 1 : Arithmetic Operations ";
cout << " 2 : Trigonometric Functions ";
cout << " 3 : Logarithmic Functions ";
cout << " 4 : Power Functions ";
cout << " 5 : Exit... ";
cin >> letter;
switch (letter)
{
case '1':
{
cout << " ";
cout << " 1 : Addition ";
cout << " 2 : Subtraction ";
cout << " 3 : Multipilication ";
cout << " 4 : Division ";
cin >> letter1;
switch (letter1)
{
case '1':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
result = a + b;
cout << " Result = " << result << endl;
break;
}
case '2':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
result = a - b;
cout << " Result = " << result << endl;
break;
}
case '3':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
result = a * b;
cout << " Result = " << result << endl;
break;
}
case '4':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
if (a != 0)
{
result = a / b;
cout << " Result = " << result << endl;
}
break;
}
} // end of inner switch
break;
} // end of case 1 arithmatic operation
case '2':
{
cout << " ";
cout << " 1 : Sin function ";
cout << " 2 : Cos function ";
cout << " 3 : Tan function ";
cin >> letter2;
switch (letter2)
{
case '1':
{
cout << " Enter a number...";
cin >> a1;
result1 = (sin(a1));
cout << " Result = " << result1 << endl;
break;
}
case '2':
{
cout << " Enter a number...";
cin >> a1;
result1 = (cos(a1));
cout << " Result = " << result1 << endl;
break;
}
case '3':
{
cout << " Enter a number...";
cin >> a1;
result1 = (tan(a1));
cout << " Result = " << result1 << endl;
break;
}
} // inner switch
break;
} // inner case 2 trignomatic
case '3':
{
cout << " ";
cout << " 1 : Natural log ";
cout << " 2 : log with base 10 ";
cin >> letter3;
switch (letter3)
{
case '1':
{
cout << " Enter a number...";
cin >> a1;
result1 = log(a1);
cout << " Result = " << result1 << endl;
break;
}
case '2':
{
cout << " Enter a number...";
cin >> a1;
result1 = log10(a1);
cout << " Result = " << result1 << endl;
break;
}
} // end of switch
break;
} // end of case 3 logrithmic
case '4':
{
cout << "1) Press 1 for Power ";
cout << "2) Press 2 for Square root ";
cout << "Enter your choice....";
cin >> letter4;
switch (letter4)
{
case '1':
{
cout << " Enter a number...";
cin >> a1;
cout << "Enter power...";
cin >> b1;
result1 = pow(a1, b1);
cout << " Result = " << result1 << endl;
break;
}
case '2':
{
cout << " Enter a number...";
cin >> a;
result1 = sqrt(a);
cout << " Result = " << result1 << endl;
break;
}
} // end of switch
break;
} // end of case power function
} // outer switch
} while (letter != '5');
return 0;
}
Code for Weather Temperature Recording using C++:
#include
#include
#include
#include
#include
#include
fstream file;
char year[5]="";
//Non-Member function to find month, to avoid user data-entrychar *returnMonth(int m){
switch(m){
case 1 : return("January");
case 2 : return("February");
case 3 : return("March");
case 4 : return("April");
case 5 : return("May");
case 6 : return("June");
case 7 : return("July");
case 8 : return("August");
case 9 : return("September");
case 10 : return("October");
case 11 : return("November");
case 12 : return("December");
}
return("Invalid");
}
//Non-Member functions, checks for whether file is loaded or not//it returns 1 if not loaded and 0 otherwise.int checkFile(){
if(file==NULL){
cout<<" There is No Data to Display ";
getch();
return(1);
}
return(0);
}
class weather
{
public:
double avgtemp[12];
char month[12][20];
weather();
void getdata(int m){ //simple getdata...
strcpy(month[m],returnMonth(m)); //avoiding user to input
cout<<" Enter temperature for "<
cin>>avgtemp[m];
}
void displaydata(int m){ //simple displaydata
cout<
<
<
}
void displaytemp(int i){ //Display only Temperature data
cout<
<
}
double returntemp(int i){
return(avgtemp[i]);
}
void loadfile();
void displaytempscale(); //for displaying temperature scalevoid updatedata(int m,double t)
{
strcpy(month[m],returnMonth(m));
avgtemp[m]=t;
}
int validate(double t){ //Validate entered temperature (change to suit ur requirement).if(t>55 || t < (-20))
return(1); //states that invalid entryelsereturn(0); //valid entry
}
};
weather :: weather(){ //Constructor to intialize objectfor(int i=0;i<12;i++)
{
avgtemp[i]=0;
month[i][0]=NULL;
}
}
void weather :: loadfile(){ //for Selecting Year or creation of new year file.
clrscr();
file.close(); //It is required when trying to open different year files,//while there is already loaded any year file.char filename[20]="";
cout<<" *****Select a Year to Work With***** ";
cout<<"Enter Year (eg: 2001) : ?";
cin>>year;
strcat(filename,"c:\"); //Here i am assuming that path must be c:
strcat(filename,"data"); //if u wan't to skip that criteria just remove this lines.
strcat(filename,year);
strcat(filename,".dat");
file.open(filename,ios::ate|ios::nocreate|ios::in|ios::out|ios::binary);
if(file==NULL){
char ans;
cout<<" File Dose Not Exist "
<<"Do you wan't to create it! (y or n) N : ?"; //By default No
ans=getche();
if(ans=='y'|| ans=='Y'){
file.open(filename,ios::ate|ios::in|ios::out|ios::binary);
cout<<" File Created Successfully";
}
else{
file;
cout<<" Fail to load data file";
}
}
else if(file!=NULL){
cout<<" Data File is successfully loaded";
}
getch();
}
void weather :: displaytempscale(){
int i,c=0;
cout<<" ";
for(i=1;i<=70;i++)
{
if(i<=10)
cout<<"0";
else if(i<=20)
cout<<"1";
else if(i<=30)
cout<<"2";
else if(i<=40)
cout<<"3";
else if(i<=50)
cout<<"4";
else if(i<=60)
cout<<"5";
else if(i<=70)
cout<<"7";
}
cout<<" ";
for(i=1;i<=7;i++)
{
for(c=0;c<10;c++)
cout<
}
cout<<" ";
}
void intro()
{
clrscr();
cout<<" ";
cout<<" Email : admin@syntax-example.com "
<<" Website : http://www.syntax-example.com";
cout<<" ";
cout<<"Thanks for using this software.";
getch();
}
void main(){
char choice='1';
int cnt,i,j,iNum,totstars=0,location,m;
double decNum,dNum,high=0,low=99999,avg=0,t;
char c;
weather obj;
file.close();
do{
clrscr();
cout<<" *****Main Menu***** ";
cout<<"1> Select a year to work with "
<<"2> Display data as Table "
<<"3> Display data as horizontal histogram "
<<"4> Display Yearly Statistics to date "
<<"5> Record Data "
<<"6> Change Data "
<<"7> Save Data "
<<"0> Exit "
<<"Please enter a number (0...7) => ";
choice=getche();
//again for sake of simplicity i have directly return code
//in case brace rather than going for member-function.
switch(choice){
case '0' : intro();
goto out;
case '1' : obj.loadfile();
break;
case '2' : clrscr();
cout<<" *****Display Data as a Table***** ";
if(checkFile()){
goto endtable;
}
cout<<" Table of Temperature data for "<
cout<<" ";
file.seekg(0,ios::end);
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
file.seekg(0,ios::beg);
for(i=1;i<=cnt;i++)
{
if(i==1)
cout<<" Quater 1 : ";
else if(i==4)
cout<<" Quater 2 : ";
else if(i==7)
cout<<" Quater 3 : ";
else if(i==10)
cout<<" Quater 4 : ";
file.read((char *)&obj, sizeof(obj));
if(!file.eof()){
obj.displaytemp(i);
}
file.clear();
}
getch();
endtable:
break;
case '3' : clrscr();
cout<<" *****Display Data as a Horizontal Histogram***** ";
if(checkFile()){
goto endhistogram;
}
cout<<"Histogram of Temperature data for "<
obj.displaytempscale();
file.seekg(0,ios::end);
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
file.seekg(0,ios::beg);
for(i=1;i<=cnt;i++)
{
cout<<" "<
file.read((char *)&obj, sizeof(obj));
if(!file.eof()){
decNum=obj.returntemp(i);
iNum=floor(decNum);
dNum=decNum-iNum; //for finding decimal part.
totstars=iNum;
if(dNum >= 0.5)
totstars++;
for(j=1;j<=totstars;j++)
cout<<"*";
cout<<" "<
}
file.clear();
}
obj.displaytempscale();
getch();
endhistogram:
break;
case '4' : clrscr();
cout<<" *****Display Yearly Statistics to Date***** ";
if(checkFile()){
goto endstatus;
}
cout<<"Temperature Statistics data for "<
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
file.seekg(0,ios::beg);
high=avg=0;
low=99999;
for(i=1;i<=cnt;i++)
{
file.read((char *)&obj, sizeof(obj));
double tmp=obj.returntemp(i);
if(!file.eof()){
if(high < tmp)
high=tmp;
if(low > tmp)
low=tmp;
avg=avg+tmp;
}
file.clear();
}
avg=avg/double(cnt);
cout<<" Highest Monthly Average : "<
cout<<" Lowest Monthly Average : "<
cout<<" Average Yearly Temperature : "<
getch();
endstatus:
break;
case '5' : clrscr();
cout<<" *****Record Data***** ";
if(checkFile()){
goto endRecord;
}
else{
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
if(cnt==12)
cout<<" Data-Entry of "<
for(i=cnt+1;i<=12;i++)
{
enteragain:
cout<<" Do u wan't to enter data for"<
c=getche();
if(c=='n' || c=='N')
goto endRecord;
obj.getdata(i);
if(obj.validate(obj.returntemp(i)))
{
cout<<" Invalid Data Entry ";
goto enteragain;
}
cin.get(c);
file.write((char *) &obj, sizeof(obj));
}
}
getch();
endRecord:
break;
case '6' : clrscr();
cout<<" *****Change Data***** ";
if(checkFile()){
goto endchange;
}
else{
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
tryagain:
cout<<" Enter Month (in digit) whose temperature is to be changed : ?";
cin>>m;
if(m <= 0 || m > cnt){
cout<<" Invalid Month ";
getch();
goto tryagain;
}
tempagain:
cout<<" Enter Temperature : ?";
cin>>t;
if(obj.validate(t))
{
cout<<" Invalid Data Entry ";
goto tempagain;
}
file.seekg(0,ios::beg);
location= (m-1) * sizeof(obj);
file.seekp(location);
obj.updatedata(m,t);
cin.get(c);
file.write((char *) &obj, sizeof(obj))<
}
endchange:
break;
case '7' : clrscr();
cout<<" *****Store the Current Data***** ";
if(checkFile()){
goto endsave;
}
flush(file);
cout<<" Data in Memory is Saved successfully ";
getch();
endsave:
break;
default : cout<<" Invalid Input ";
getch();
}
}while(choice!='0');
flush(file);
file.close();
clrscr();
out:
}
Explanation / Answer
#include
#include
#include
using namespace std;
int main()
{
char letter;
char letter1;
char letter2;
char letter3;
char letter4;
int a, b;
double a1, b1;
int result;
double result1;
cout << " ***************** SCIENTIFIC CALCULATOR ****************** ";
do
{
cout << " 1 : Arithmetic Operations ";
cout << " 2 : Trigonometric Functions ";
cout << " 3 : Logarithmic Functions ";
cout << " 4 : Power Functions ";
cout << " 5 : Exit... ";
cin >> letter;
switch (letter)
{
case '1':
{
cout << " ";
cout << " 1 : Addition ";
cout << " 2 : Subtraction ";
cout << " 3 : Multipilication ";
cout << " 4 : Division ";
cin >> letter1;
switch (letter1)
{
case '1':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
result = a + b;
cout << " Result = " << result << endl;
break;
}
case '2':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
result = a - b;
cout << " Result = " << result << endl;
break;
}
case '3':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
result = a * b;
cout << " Result = " << result << endl;
break;
}
case '4':
{
cout << " Enter first number...";
cin >> a;
cout << "Enter an other number...";
cin >> b;
if (a != 0)
{
result = a / b;
cout << " Result = " << result << endl;
}
break;
}
} // end of inner switch
break;
} // end of case 1 arithmatic operation
case '2':
{
cout << " ";
cout << " 1 : Sin function ";
cout << " 2 : Cos function ";
cout << " 3 : Tan function ";
cin >> letter2;
switch (letter2)
{
case '1':
{
cout << " Enter a number...";
cin >> a1;
result1 = (sin(a1));
cout << " Result = " << result1 << endl;
break;
}
case '2':
{
cout << " Enter a number...";
cin >> a1;
result1 = (cos(a1));
cout << " Result = " << result1 << endl;
break;
}
case '3':
{
cout << " Enter a number...";
cin >> a1;
result1 = (tan(a1));
cout << " Result = " << result1 << endl;
break;
}
} // inner switch
break;
} // inner case 2 trignomatic
case '3':
{
cout << " ";
cout << " 1 : Natural log ";
cout << " 2 : log with base 10 ";
cin >> letter3;
switch (letter3)
{
case '1':
{
cout << " Enter a number...";
cin >> a1;
result1 = log(a1);
cout << " Result = " << result1 << endl;
break;
}
case '2':
{
cout << " Enter a number...";
cin >> a1;
result1 = log10(a1);
cout << " Result = " << result1 << endl;
break;
}
} // end of switch
break;
} // end of case 3 logrithmic
case '4':
{
cout << "1) Press 1 for Power ";
cout << "2) Press 2 for Square root ";
cout << "Enter your choice....";
cin >> letter4;
switch (letter4)
{
case '1':
{
cout << " Enter a number...";
cin >> a1;
cout << "Enter power...";
cin >> b1;
result1 = pow(a1, b1);
cout << " Result = " << result1 << endl;
break;
}
case '2':
{
cout << " Enter a number...";
cin >> a;
result1 = sqrt(a);
cout << " Result = " << result1 << endl;
break;
}
} // end of switch
break;
} // end of case power function
} // outer switch
} while (letter != '5');
return 0;
}
Code for Weather Temperature Recording using C++:
#include
#include
#include
#include
#include
#include
fstream file;
char year[5]="";
//Non-Member function to find month, to avoid user data-entrychar *returnMonth(int m){
switch(m){
case 1 : return("January");
case 2 : return("February");
case 3 : return("March");
case 4 : return("April");
case 5 : return("May");
case 6 : return("June");
case 7 : return("July");
case 8 : return("August");
case 9 : return("September");
case 10 : return("October");
case 11 : return("November");
case 12 : return("December");
}
return("Invalid");
}
//Non-Member functions, checks for whether file is loaded or not//it returns 1 if not loaded and 0 otherwise.int checkFile(){
if(file==NULL){
cout<<" There is No Data to Display ";
getch();
return(1);
}
return(0);
}
class weather
{
public:
double avgtemp[12];
char month[12][20];
weather();
void getdata(int m){ //simple getdata...
strcpy(month[m],returnMonth(m)); //avoiding user to input
cout<<" Enter temperature for "<
cin>>avgtemp[m];
}
void displaydata(int m){ //simple displaydata
cout<
<
<
}
void displaytemp(int i){ //Display only Temperature data
cout<
<
}
double returntemp(int i){
return(avgtemp[i]);
}
void loadfile();
void displaytempscale(); //for displaying temperature scalevoid updatedata(int m,double t)
{
strcpy(month[m],returnMonth(m));
avgtemp[m]=t;
}
int validate(double t){ //Validate entered temperature (change to suit ur requirement).if(t>55 || t < (-20))
return(1); //states that invalid entryelsereturn(0); //valid entry
}
};
weather :: weather(){ //Constructor to intialize objectfor(int i=0;i<12;i++)
{
avgtemp[i]=0;
month[i][0]=NULL;
}
}
void weather :: loadfile(){ //for Selecting Year or creation of new year file.
clrscr();
file.close(); //It is required when trying to open different year files,//while there is already loaded any year file.char filename[20]="";
cout<<" *****Select a Year to Work With***** ";
cout<<"Enter Year (eg: 2001) : ?";
cin>>year;
strcat(filename,"c:\"); //Here i am assuming that path must be c:
strcat(filename,"data"); //if u wan't to skip that criteria just remove this lines.
strcat(filename,year);
strcat(filename,".dat");
file.open(filename,ios::ate|ios::nocreate|ios::in|ios::out|ios::binary);
if(file==NULL){
char ans;
cout<<" File Dose Not Exist "
<<"Do you wan't to create it! (y or n) N : ?"; //By default No
ans=getche();
if(ans=='y'|| ans=='Y'){
file.open(filename,ios::ate|ios::in|ios::out|ios::binary);
cout<<" File Created Successfully";
}
else{
file;
cout<<" Fail to load data file";
}
}
else if(file!=NULL){
cout<<" Data File is successfully loaded";
}
getch();
}
void weather :: displaytempscale(){
int i,c=0;
cout<<" ";
for(i=1;i<=70;i++)
{
if(i<=10)
cout<<"0";
else if(i<=20)
cout<<"1";
else if(i<=30)
cout<<"2";
else if(i<=40)
cout<<"3";
else if(i<=50)
cout<<"4";
else if(i<=60)
cout<<"5";
else if(i<=70)
cout<<"7";
}
cout<<" ";
for(i=1;i<=7;i++)
{
for(c=0;c<10;c++)
cout<
}
cout<<" ";
}
void intro()
{
clrscr();
cout<<" ";
cout<<" Email : admin@syntax-example.com "
<<" Website : http://www.syntax-example.com";
cout<<" ";
cout<<"Thanks for using this software.";
getch();
}
void main(){
char choice='1';
int cnt,i,j,iNum,totstars=0,location,m;
double decNum,dNum,high=0,low=99999,avg=0,t;
char c;
weather obj;
file.close();
do{
clrscr();
cout<<" *****Main Menu***** ";
cout<<"1> Select a year to work with "
<<"2> Display data as Table "
<<"3> Display data as horizontal histogram "
<<"4> Display Yearly Statistics to date "
<<"5> Record Data "
<<"6> Change Data "
<<"7> Save Data "
<<"0> Exit "
<<"Please enter a number (0...7) => ";
choice=getche();
//again for sake of simplicity i have directly return code
//in case brace rather than going for member-function.
switch(choice){
case '0' : intro();
goto out;
case '1' : obj.loadfile();
break;
case '2' : clrscr();
cout<<" *****Display Data as a Table***** ";
if(checkFile()){
goto endtable;
}
cout<<" Table of Temperature data for "<
cout<<" ";
file.seekg(0,ios::end);
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
file.seekg(0,ios::beg);
for(i=1;i<=cnt;i++)
{
if(i==1)
cout<<" Quater 1 : ";
else if(i==4)
cout<<" Quater 2 : ";
else if(i==7)
cout<<" Quater 3 : ";
else if(i==10)
cout<<" Quater 4 : ";
file.read((char *)&obj, sizeof(obj));
if(!file.eof()){
obj.displaytemp(i);
}
file.clear();
}
getch();
endtable:
break;
case '3' : clrscr();
cout<<" *****Display Data as a Horizontal Histogram***** ";
if(checkFile()){
goto endhistogram;
}
cout<<"Histogram of Temperature data for "<
obj.displaytempscale();
file.seekg(0,ios::end);
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
file.seekg(0,ios::beg);
for(i=1;i<=cnt;i++)
{
cout<<" "<
file.read((char *)&obj, sizeof(obj));
if(!file.eof()){
decNum=obj.returntemp(i);
iNum=floor(decNum);
dNum=decNum-iNum; //for finding decimal part.
totstars=iNum;
if(dNum >= 0.5)
totstars++;
for(j=1;j<=totstars;j++)
cout<<"*";
cout<<" "<
}
file.clear();
}
obj.displaytempscale();
getch();
endhistogram:
break;
case '4' : clrscr();
cout<<" *****Display Yearly Statistics to Date***** ";
if(checkFile()){
goto endstatus;
}
cout<<"Temperature Statistics data for "<
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
file.seekg(0,ios::beg);
high=avg=0;
low=99999;
for(i=1;i<=cnt;i++)
{
file.read((char *)&obj, sizeof(obj));
double tmp=obj.returntemp(i);
if(!file.eof()){
if(high < tmp)
high=tmp;
if(low > tmp)
low=tmp;
avg=avg+tmp;
}
file.clear();
}
avg=avg/double(cnt);
cout<<" Highest Monthly Average : "<
cout<<" Lowest Monthly Average : "<
cout<<" Average Yearly Temperature : "<
getch();
endstatus:
break;
case '5' : clrscr();
cout<<" *****Record Data***** ";
if(checkFile()){
goto endRecord;
}
else{
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
if(cnt==12)
cout<<" Data-Entry of "<
for(i=cnt+1;i<=12;i++)
{
enteragain:
cout<<" Do u wan't to enter data for"<
c=getche();
if(c=='n' || c=='N')
goto endRecord;
obj.getdata(i);
if(obj.validate(obj.returntemp(i)))
{
cout<<" Invalid Data Entry ";
goto enteragain;
}
cin.get(c);
file.write((char *) &obj, sizeof(obj));
}
}
getch();
endRecord:
break;
case '6' : clrscr();
cout<<" *****Change Data***** ";
if(checkFile()){
goto endchange;
}
else{
cnt=file.tellg();
cnt=cnt/sizeof(obj); //cnt contains how many records previously recorded
tryagain:
cout<<" Enter Month (in digit) whose temperature is to be changed : ?";
cin>>m;
if(m <= 0 || m > cnt){
cout<<" Invalid Month ";
getch();
goto tryagain;
}
tempagain:
cout<<" Enter Temperature : ?";
cin>>t;
if(obj.validate(t))
{
cout<<" Invalid Data Entry ";
goto tempagain;
}
file.seekg(0,ios::beg);
location= (m-1) * sizeof(obj);
file.seekp(location);
obj.updatedata(m,t);
cin.get(c);
file.write((char *) &obj, sizeof(obj))<
}
endchange:
break;
case '7' : clrscr();
cout<<" *****Store the Current Data***** ";
if(checkFile()){
goto endsave;
}
flush(file);
cout<<" Data in Memory is Saved successfully ";
getch();
endsave:
break;
default : cout<<" Invalid Input ";
getch();
}
}while(choice!='0');
flush(file);
file.close();
clrscr();
out:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.