/ src / Major 2 / m2.cpp
m2.cpp
  1  /*
  2  * FILE : m2.cpp
  3  * PROJECT : SENG1000 - Major ASSIGNMENT 2
  4  * PROGRAMMER : Tian Yang
  5  * FIRST VERSION : 2024-02-09
  6  * DESCRIPTION :
  7  * This program calculates powers using a function that I create.
  8  */
  9  
 10  #include <stdio.h>
 11  #include <stdbool.h>
 12  
 13  int getNum(void);
 14  int powerCalculation(int, int);
 15  int numberRangeChecker(int, int, int);
 16  
 17  int main(int argc, char* argv[])
 18  {
 19  	int num = 0;
 20  	int base = 1;
 21  	int exponent = 1;
 22  	bool exit = false;
 23  	while (!exit)
 24  	{
 25  		/*
 26  		* Prompt use to enter number.
 27  		*/
 28  		printf("\n");
 29  		printf("\n");
 30  		printf("Menu: \n");
 31  		printf("\t1. Change base \n");
 32  		printf("\t2. Change exponent \n");
 33  		printf("\t3. Display base raised to exponent\n");
 34  		printf("\t4. Exit program\n");
 35  		printf("Option? ");
 36  		num = getNum();
 37  
 38  		if (num == 1)
 39  		{
 40  			printf("Enter new base: ");
 41  			int newBase = getNum();
 42  			if (numberRangeChecker(newBase, 1, 30))
 43  			{
 44  				base = newBase;
 45  			}
 46  			else
 47  			{
 48  				printf("Invalid base. Please enter a number between 1 and 30.\n");
 49  			}
 50  		}
 51  		else if (num == 2)
 52  		{
 53  			printf("Enter new exponent: ");
 54  			int newExponent = getNum();
 55  			if (numberRangeChecker(newExponent, 1, 6))
 56  			{
 57  				exponent = newExponent;
 58  			}
 59  			else
 60  			{
 61  				printf("Invalid base. Please enter a number between 1 and 6.\n");
 62  			}
 63  		}
 64  		else if (num == 3)
 65  		{
 66  
 67  			printf("%d raised to the power of %d is %d\n", base, exponent, powerCalculation(base, exponent));
 68  		}
 69  		else if (num == 4)
 70  		{
 71  			exit = true;
 72  		}
 73  		else
 74  		{
 75  			printf("Invalid entry\n");
 76  		}
 77  	}
 78  
 79  	return 0;
 80  }
 81  
 82  //
 83  // FUNCTION : powerCalculation
 84  // DESCRIPTION :
 85  // This function calculate power.
 86  // PARAMETERS :
 87  // int base : base.
 88  // int exponent: exponent.
 89  // RETURNS :
 90  // int : result of power.
 91  //
 92  int powerCalculation(int base, int exponent)
 93  {
 94  	int result = 1;
 95  	for (int i = 0; i < exponent; i++)
 96  	{
 97  		result *= base;
 98  	}
 99  	return result;
100  }
101  
102  //
103  // FUNCTION : numberRangeChecker
104  // DESCRIPTION :
105  // This function check if a number is in the range.
106  // PARAMETERS :
107  // int value : number be checked.
108  // int min : minimum.
109  // int max : maximum.
110  // RETURNS :
111  // int : 1 is in the range, 0 is out of range.
112  //
113  int numberRangeChecker(int value, int min, int max)
114  {
115  	return ((value <= max) && (value >= min)) ? 1 : 0;
116  }
117  
118  //
119  // FUNCTION : getNum
120  // DESCRIPTION :
121  // This function gets user-entered number.
122  // PARAMETERS :
123  // void : nothing input.
124  // RETURNS :
125  // int : return the number that user entered.
126  //
127  #pragma warning(disable: 4996)
128  int getNum(void)
129  {
130  	/* we will see in a later lecture how we can improve this code */
131  	char record[121] = { 0 }; /* record stores the string */
132  	int number = 0;
133  	/* NOTE to student: indent and brace this function consistent with
134  	your others */
135  	/* use fgets() to get a string from the keyboard */
136  	fgets(record, 121, stdin);
137  	/* extract the number from the string; sscanf() returns a number
138  	* corresponding with the number of items it found in the string */
139  	if (sscanf(record, "%d", &number) != 1)
140  	{
141  		/* if the user did not enter a number recognizable by
142  		* the system, set number to -1 */
143  		number = -1;
144  	}
145  	return number;
146  }