I have written a sorting modification to a calc.c program. The quicksort code and variables are here respectively. The issue I am having regarding the code is that when I write to the function case 'D' (Demo) for the program, I cannot get the proper numbers out of quicksort. I've checked its program and the split, and both seem to be correct when I verified by using function 'Q' to do a quicksort. I don't know what the issue is, as both quick and bubble sorts work otherwise. In addition, I need to take these functions and create pointers on those functions. I don't know much about pointers and references to texts or notes haven't helped. I've compiled the program and it does not give me any errors (Cygwin is my operator and gcc is the compiler)
/* This is the last version of the calculator example that **
* you can use as the starting point for the final exam. **/
/** To compile this code, use **
* gcc -o calc calc.c -std=c99 -lm **/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#define KEY "Enter the calculator operation you want to do: "
#define enter_option() printf("%s", KEY)
#define N 10
#define SCREEN_HEIGHT 15
#define clear_screen() \
for ( int i = 0; i < SCREEN_HEIGHT; i++ ) printf("\n");
#define clean_input() while ((getchar())!='\n') continue;
#define calculator_operations() printf("\n"); \
printf("******** Welcome to C calculator ***********\n"); \
printf("**-----------------------------------------------**\n"); \
printf("** Press 'Q' or 'q' to quit the program **\n"); \
printf("** Press 'H' or 'h' to display below options **\n"); \
printf("** Press 'C' or 'c' to clear the screen **\n"); \
printf("**-----------------------------------------------**\n"); \
printf("** Enter 'A' or 'a' for arithmetic mode **\n"); \
printf("** Enter 'F' or 'f' for function mode **\n"); \
printf("** Enter 'S' or 's' for sorting mode **\n"); \
printf("***************************************************\n"); \
printf("\n");
#define DISP(f,x) printf(#f"(%g) = %g\n",x,f(x));
#define DISP_I(f,x) printf(#f"(%d) = %d\n",(int) x,(int) f((int) x));
// Function prototype declaration
void arith_mode();
void func_mode();
void sort_mode();
void read_line(char *);
int factorial(int );
void quicksort(int a[ ], int low, int high);
int split(int a[], int low, int high);
void bubblesort(int a[ ], int low, int high);
void Q_Sort();
void B_Sort();
void D_Sort();
// Start of Main Program
int main()
{
int X=1;
char Calc_Mode;
do
{
calculator_operations();
enter_option();
Calc_Mode=getchar();
switch(Calc_Mode)
{
case 'A': case 'a': arith_mode(); break;
case 'F': case 'f': func_mode(); break;
case 'S': case 's': sort_mode(); break;
case 'H': case 'h': case '\n': break;
case 'Q': case 'q': printf("Leaving Calculator!\n");
exit(0);
case 'C': case 'c': clear_screen();
clean_input(); break;
}
} while (X);
}
//Function Definitions
void arith_mode()
{
double value, operand;
char operator;
printf("\nPlease enter arithmetic expression "
"as in \"x +-*/%%^ y +-*/%%^ z ...\" \n");
printf("Your expression is: ");
/* Read first operand */
scanf("%lf", &value);
/* Read successive operators and operands */
while ((operator = getchar()) != '\n') {
if (operator == ' ') continue;
scanf("%lf", &operand);
switch (operator) {
case '+': value += operand; break;
case '-': value -= operand; break;
case '*': value *= operand; break;
case '/': value /= operand; break;
case '%': value = ((int) value)%((int) operand); break;
case '^': value = pow(value,operand); break;
default: printf("\n Invalid Operator! \n"); exit(0);
}
}
/* Print result */
printf("Value of expression: %g\n\n", value);
}
void func_mode()
{
char *cp, func_initial, line[40];
double variable;
printf("\nPlease enter functions as in "
"\"name(x)\". \n");
printf("Your function is: ");
clean_input();
read_line(line);
func_initial = line[0];
for (cp = line; *cp; cp++) {
if (*cp == '(') {
sscanf(++cp,"%lf", &variable);
switch (func_initial) {
case 'F': case 'f': DISP_I(factorial,variable); break;
case 'E': case 'e': DISP(exp,variable); break;
case 'S': case 's': DISP(sqrt,variable); break;
default: printf("\n Invalid Function! \n"); exit(0);
}
break;
}
}
}
void sort_mode()
{
char choice;
printf("\nYour choices are: Q for quick, B for bubble, D for Demo, P for pointer Demo\n");
printf("Please choose sorting method: ");
scanf("%s", &choice);
switch(choice){
case 'Q': Q_Sort(); break;
case 'B': B_Sort(); break;
case 'D': D_Sort(); break;
case 'P': break;
default: printf("\n Invalid Sorting Method! \n"); exit(0);
}
clean_input();
}
void read_line(char *line)
{
int index; char ch;
index = 0;
while ((ch= getchar()) !='\n') {
if (ch == ' ') continue;
line[index++] = ch;
}
line[index] = '\0';
}
int factorial(int n)
{
if(n<=1) return 1;
else return n*factorial(n-1);
}
void quicksort(int a[ ], int low, int high)
{
int middle;
if (low >= high) return;
middle = split(a, low, high);
quicksort(a, low, middle - 1);
quicksort(a, middle + 1, high);
}
int split(int a[ ], int low, int high)
{
int part_element = a[low];
for (;;) {
while (low < high && part_element <= a[high])
high--;
if (low >= high) break;
a[low++] = a[high];
while (low < high && a[low] <= part_element)
low++;
if (low >= high) break;
a[high--] = a[low];
}
a[high] = part_element;
return high;
}
void bubblesort(int a[ ], int low, int high)
{
int i, j, temp;
for (j = high; j > low; j--) {
for (i = low+1; i <= j;i++) {
if (a[i] < a[i-1]) {
temp=a[i];a[i]=a[i-1];a[i-1]=temp;
}
}
}
}
void Q_Sort()
{
int a[N], i;
printf("Enter %d numbers to be sorted: ", N);
for (i = 0; i < N; i++){
scanf("%d", &a[i]);
}
quicksort(a, 0, N-1);
printf("\nQuick Sorting!\n");
printf("In sorted order\n: ");
for (i = 0; i < N; i++){
printf("%d ", a[i]);
}
printf("\n");
}
void B_Sort()
{
int a[N], i;
printf("Enter %d numbers to be sorted: ", N);
for (i = 0; i < N; i++){
scanf("%d", &a[i]);
}
printf("\nBubble Sorting!\n");
printf("In sorted order\n: ");
for (i = 0; i < N; i++){
printf("%d ", a[i]);
}
printf("\n");
}
void D_Sort()
{
srand(time(NULL));
int i,len, max_len = 20000, original[max_len], active[max_len];
double t1, t2, t3, t4, t_tot, t_tot2;
printf("***************Report of Performance Comparison***************\n");
printf(" quicksort bubblesort (in microseconds)\n");
for(len = 2000; len <= max_len; )
{
for(i=len; i<= max_len ; i++)
{
active[i]=original[i]= rand( ) % (2*len + 1)+(-len);
}
printf("N=%d\t", len );
t1 = clock();
quicksort(original, 0, len);
t2 = clock();
t_tot = (t2 - t1)*1000/CLOCKS_PER_SEC;
printf("\t%7.1f\t", t_tot);
t3 = clock();
bubblesort(active, 0, len);
t4 = clock();
t_tot2 = (t4 - t3)* 1000/CLOCKS_PER_SEC;
printf("\t%7.1f\t", t_tot2);
printf("\tarray in range [%d,%d]\n", -len,len);
printf("\n");
switch (len)
{
case 2000 :
len = 4000;
break;
case 4000 :
len = 6000;
break;
case 6000 :
len = 8000;
break;
case 8000 :
len = 10000;
break;
case 10000 :
len = 12000;
break;
case 12000 :
len = 14000;
break;
case 14000 :
len = 16000;
break;
case 16000 :
len = 18000;
break;
case 18000 :
len = 20000;
break;
case 20000 :
len =22000;
break;
}
}
printf("***************End of Performance Report**********************\n");
printf("\n");
}
Aucun commentaire:
Enregistrer un commentaire