Problem 1: Bridge Hand
Problem2: Recursive Sum
Problem3: Triangle ADT (triangle.h, triangle.cpp, test.cpp)
Problem 1: Bridge Hands
/*This program sorts and evaluates bridge hands*/ #include <iostream.h> #include <strstrea.h> #include <iomanip.h> #include <string.h>
#define HAND_NUM 13
int IsValid(char*); int Insertion_Sort(char[HAND_NUM][3], int, char[]); void Evaluate(char[HAND_NUM][3]);
int main(){ char cards[HAND_NUM][3]; char line[1024]; char pair[1024]; int i;
while(!cin.eof()){ cin.getline(line, 1024); //get a line from the input; cout << line << endl; istrstream sstrm(line); //generate an istrstream object; i = 0; //keep track of the number of while (sstrm>>pair){ //pairs for each line; if (!IsValid(pair)){ //check if each pair is valid; cerr<<"Input error!"<<endl; return 1; } if (Insertion_Sort(cards, i, pair)){ //Insert the new pair in sorted order. cerr<<pair<<" appears twice in the hand!"<<endl; return 1; } i++; } if (i != HAND_NUM){ //check number of cards for each hand; cerr<<"The number of cards in this hand is incorrect!"<<endl; return 1; } Evaluate(cards); //Evaluate the points and display the hand; } cout<<"Goodbye!"<<endl; return 0; }
/*This function checks the validity of an input pair. */ /*the rank must be a character in [23456789TJQKA] */ /*and the suit must be in [CDHS]. The function returns*/ /*1 if it is valid, returns 0 to indicate an error. */ int IsValid(char* s){ char suits[] = "CDHS"; char ranks[] = "TJQKA"; int i, len;
if (strlen(s) != 2) //Each pair must be of length 2; return 0; len = strlen(suits); for (i=0; i<len; i++){ if (s[1] == suits[i]) //check the suit; break; } if (i == len) //not in [CDHS]; return 0; if (s[0]>='2' && s[0]<='9') //check if the rank is in [2-9]; return 1; len = strlen(ranks); for (i=0; i<len; i++){ //check if the rank is in [TJQKA]; if (s[0] == ranks[i]){ break; } } if (i == len) return 0;
return 1; //return 1 on valid; }
/*This function finds the index of c in string ranks. */ int Index(char c){ char ranks[] = "TJQKA"; int i, len;
if (c>='2' && c<='9') return c-'2'; len = strlen(ranks); for (i=0; i<len; i++){ if (c == ranks[i]){ return i + 8; } } return -1; }
/*This function compares two pairs, it returns -1,0,1,*/ /*to indicate a is lower than, equal to, or higher */ /*than b, respectively. In this context, a is higher */ /*than b simply means a should be in front of b in the*/ /*sorted list. */ /*To avoid finding the index every time, we can store */ /*the indices in another array, or simply transform */ /*the ranks to charaters which can be compared with */ /*each other directly, e.g. 'TJQKA' to 'ABCDE', and */ /*store them in the array cards. */ int Compare(char a[3], char b[3]){ if (a[1] == b[1]) if (a[0] == b[0]) return 0; //a and b are equal; else return (Index(a[0])>Index(b[0]))?1:-1; else return (a[1] < b[1])?1:-1; }
/*This function inserts the new pair p into the sorted*/ /*list of cards h, if p has once appeared in the list,*/ /*the function returns 1 to indicate the error. */ int Insertion_Sort(char h[HAND_NUM][3], int n, char p[3]){ int i; int ret;
i = n-1; //find the right place to insert p while (i>=0){ //starting from h[n-1]; ret = Compare(p, h[i]); if (ret == 0) //the pair already exists; return 1; //return 1 to indicate the error; else if (ret == 1){ //the new pair is higher than h[i]; strcpy(h[i+1], h[i]); //move h[i] backwards; --i; } else break; } strcpy(h[i+1], p); //put p in the list; return 0; }
/*This function returns bridge values. 2-10's count 0 */ /*Aces count 4, Kings 3, Queens 2, Jacks 1. It also */ /*transforms T to 10, to make the readable output. */ int Translate(char c, char* s){ s[0] = c; s[1] = '\0'; switch (c){ case 'T': strcpy(s, "10"); return 0; case 'J': return 1; case 'Q': return 2; case 'K': return 3; case 'A': return 4; default: return 0; } }
/*This function returns bridge values for voids (3), */ /*sigletons (2), doubletons (1), and long suits. */ int Bonus(int n){ if (n<3 && n>=0) return 3-n; else if (n>5) return n-5; return 0; }
/*This function evaluates the hand using the standard */ /*bridge values, and displays the hand in readable */ /*form arranged both by suits and by rank within suit */ void Evaluate(char h[HAND_NUM][3]){ unsigned int i, j; int points; int number; int value; char kind[][9] = {"CLUBS", "DIAMONDS", "HEARTS", "SPADES"}; char suits[] = "CDHS"; char buf[6];
i = 0; points = 0; cout << setiosflags(ios::left); //left aligned; for (j=0; j<strlen(suits); j++){ cout << setw(8) << kind[j] << " "; //dispaly each suit; number = 0; while (h[i][1] == suits[j]){ value = Translate(h[i][0], buf); points += value; cout << setw(6) << buf; //display ranks in each suit; i++; number++; //keep track of the number of } //cards in each suit; points += Bonus(number); cout << endl; } cout<<"Points = "<<points<<endl; //output the points; }
#include <iostream.h>
int RecursiveSum(const int A[], int N)
{
if (N<=0)
return 0;
return (A[N-1] + RecursiveSum(A, N-1));
}
void main()
{
int a[30];
for (int i=0; i<30; i++)
{
a[i] = i;
}
cout<<"The sum of the first 27 items is: "<<RecursiveSum(a, 27)<<endl;
}
Note: the sum from 0 to 26 is 26*(26/2) + 13.
Problem 3: Triangle ADT
triangle.h
class Triangle { private: double A,B,C; //three sides of the triangle double Angle_AB, Angle_BC, Angle_AC; //three angles, expressed in radians. void ComputeAngles(); public: Triangle(); //default constructor Triangle(double init_a, double init_b, double init_c); //constructor, initializes its three sides. double GetSideA() const; //get the value of side a. double GetSideB() const; //get the value of side b. double GetSideC() const; //get the value of side c. double GetAngleAB() const; //get the value of angle ab. double GetAngleBC() const; //get the value of angle bc. double GetAngleAC() const; //get the value of angle ac. void SetSideA(double new_a); //set side a of the triangle. void SetSideB(double new_b); //set side b of the triangle. void SetSideC(double new_c); //set side c of the triangle. double Area() const; //determines the area of the triangle; bool IsRight() const; //determines if it is a right triangle; bool IsEquilateral() const; //determines if it is an equilateral triangle; bool IsIsosceles() const; //determines if it is an isosceles triangle; };
#include "triangle.h" #include <math.h>
void Triangle::ComputeAngles() { double area= Area();
Angle_AB = asin(2*area/(A*B)); Angle_BC = asin(2*area/(B*C)); Angle_AC = asin(2*area/(A*C)); }
Triangle::Triangle():A(1.0), B(1.0), C(1.0) { ComputeAngles(); }
Triangle::Triangle(double init_a, double init_b, double init_c): A(init_a>0?init_a:1.0), B(init_b>0?init_b:1.0), C(init_c>0?init_c:1.0) { ComputeAngles(); }
double Triangle::GetSideA() const { return A; }
double Triangle::GetSideB() const { return B; }
double Triangle::GetSideC() const { return C; }
double Triangle::GetAngleAB() const { return Angle_AB; }
double Triangle::GetAngleBC() const { return Angle_BC; }
double Triangle::GetAngleAC() const { return Angle_AC; }
void Triangle::SetSideA(double new_a) { A = (new_a>0)?new_a:A; ComputeAngles(); }
void Triangle::SetSideB(double new_b) { B = (new_b>0)?new_b:B; ComputeAngles(); }
void Triangle::SetSideC(double new_c) { C = (new_c>0)?new_c:C; ComputeAngles(); }
double Triangle::Area() const { double S = (A+B+C)/2.0; //semiperimeter return sqrt(S*(S-A)*(S-B)*(S-C)); }
bool Triangle::IsRight() const { double SA, SB, SC; SA = A * A; SB = B * B; SC = C * C; return (A == sqrt(SB+SC) || B == sqrt(SA+SC) || C == sqrt(SA+SB)); }
bool Triangle::IsEquilateral() const { return (A==B && B==C); }
bool Triangle::IsIsosceles() const { return (A==B || B==C || A==C); }
#include "triangle.h" #include <iostream.h>
void main() { Triangle A; Triangle B(3.0, 4.0, 5.0); Triangle C(8.0, 8.0, 10.0);
cout<<"A has sides: "<<A.GetSideA()<<", "<<A.GetSideB()<<", "<< \ A.GetSideC()<<endl; cout<<"A has angles: "<<A.GetAngleBC()<<", "<<A.GetAngleAC()<<", "<< \ A.GetAngleAB()<<endl; cout<<"The area of A is: "<<A.Area()<<endl; if (A.IsEquilateral()) cout<<"A is an equilateral triangle."<<endl<<endl;
cout<<"B has sides: "<<B.GetSideA()<<", "<<B.GetSideB()<<", "<< \ B.GetSideC()<<endl; cout<<"B has angles: "<<B.GetAngleBC()<<", "<<B.GetAngleAC()<<", "<< \ B.GetAngleAB()<<endl; cout<<"The area of B is: "<<B.Area()<<endl; if (B.IsRight()) cout<<"B is a right triangle."<<endl<<endl;
cout<<"C has sides: "<<C.GetSideA()<<", "<<C.GetSideB()<<", "<< \ C.GetSideC()<<endl; cout<<"C has angles: "<<C.GetAngleBC()<<", "<<C.GetAngleAC()<<", "<< \ C.GetAngleAB()<<endl; cout<<"The area of C is: "<<C.Area()<<endl; if (C.IsIsosceles()) cout<<"C is an isosceles triangle."<<endl<<endl;
cout<<"Now set side a of C to 6."<<endl; C.SetSideA(6.0); cout<<"C has sides: "<<C.GetSideA()<<", "<<C.GetSideB()<<", "<< \ C.GetSideC()<<endl; cout<<"C has angles: "<<C.GetAngleBC()<<", "<<C.GetAngleAC()<<", "<< \ C.GetAngleAB()<<endl; cout<<"The area of C is: "<<C.Area()<<endl; if (C.IsRight()) cout<<"C is a right triangle."<<endl; }