1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <iostream>
using namespace std;
struct Fraction
{
int num;
int den;
};
void add(Fraction* [], char, int&);
void edit(Fraction* [], char, int&);
void deletefrac (Fraction* [], int&);
int find(Fraction* [], char, int&);
void displayall (Fraction* [], char, int);
void deleteall (Fraction* [], int&);
int main()
{
Fraction* fracptrs[20] = {0};
int size = 0;
int choice;
char slash = '/';
do{
cout<<"Choose an option: \n"
<<"\t1. Add new fraction\n"
<<"\t2. Edit fraction\n"
<<"\t3. Delete fraction\n"
<<"\t4. Find fraction\n"
<<"\t5. Display all fractions\n"
<<"\t6. Show sum of all fractions\n"
<<"\t7. Delete all fractions\n"
<<"\t8. Quit\n"
<<"***You have to add a fraction before selecting options 2 - 7***"
<<"\nYour option is... ";
cin>> choice;
if(choice == 1)
{
add(fracptrs, slash, size);
}
else if(choice == 2)
{
displayall(fracptrs, slash, size);
edit(fracptrs, slash, size);
}
else if(choice == 3)
{
displayall(fracptrs, slash, size);
deletefrac(fracptrs, size);
}
else if(choice == 4)
{
int coord = find(fracptrs, slash, size);
if(coord != -1)
{
cout<<"\nThe fraction "<< fracptrs[coord] -> num
<< slash << fracptrs[coord] -> den << " is found at spot "
<< coord+1 << ".\n";
}
else
cout<<"\nThe fraction can not be found in the array.\n";
}
else if(choice == 5)
{
displayall(fracptrs, slash, size);
}
//else if(choice == 6)
else
{
deleteall(fracptrs, size);
}
cin.ignore();
}while(choice != 8);
delete []* fracptrs;
cout << '\n';
system ("pause");
return 0;
}
void add(Fraction* fracptrs[], char slash, int& size)//I wasn't asked to use a loop to populate the array
{
fracptrs[size] = new Fraction[1];
cout<<"Enter a fraction: ";
cin>> fracptrs[size] -> num >> slash >> fracptrs[size] -> den;
cout<<"\nYou added the fraction: " << fracptrs[size] -> num << slash << fracptrs[size] -> den << '\n';
size++;
}
void edit(Fraction* fracptrs[], char slash, int& size)
{
int spot;
cout<<"Enter the position of the fraction you'd like to edit: ";
cin>>spot;
cout<<"Enter your new fraction: ";
cin>> fracptrs[spot-1] -> num >> slash >> fracptrs[spot-1] -> den;
}
void deletefrac (Fraction* fracptrs[], int& size)
{
int spot;
cout<<"Enter the position of the fraction you'd like to delete: ";
cin>> spot;
fracptrs[spot-1] = 0;
}
int find(Fraction* fracptrs[], char slash, int& size)
{
Fraction test;
cout<<"Enter the fraction you'd like to search: ";
cin>> test.num >> slash >> test.den;
for(int i = 0; i < size; i++)
{
if(test.num == fracptrs[i]->num && test.den == fracptrs[i]->den)
{
return i;
}
}
return -1;
}
void displayall (Fraction* fracptrs[], char slash, int size)//not yet tested
{
for(int i = 0; i < size; i++)
{
if(fracptrs[i] != 0)
cout<<fracptrs[i] -> num << slash << fracptrs[i] -> den
<<" ";
}
}
void deleteall (Fraction* fracptrs[], int& size)
{
for(; size-1 >= 0; size--)
{
delete fracptrs[size];
}
}
|