gpt4 book ai didi

c - 使用指针的快速排序和冒泡排序,将这两个实现到 calc.c 中

转载 作者:行者123 更新时间:2023-11-30 21:10:18 24 4
gpt4 key购买 nike

我试图弄清楚如何首先修改我的快速排序程序以使用指针算术,然后将我的冒泡排序程序更改为使用指针的递归程序,最后将这两个程序实现为计算器(calc.c)程序。我将按照提到的顺序发布我拥有的代码。

快速排序:

#include <stdio.h>

#define N 10
void quicksort(int a[], int low, int high);
int split(int a[], int low, int high);
int main(void)
{
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);
// bubblesort(a, 0, N-1);
printf("In sorted order: ");
for (i = 0; i < N; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}

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;
}
}
}
}

计算.c:

/**  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>
#define KEY "Enter the calculator operation you want to do: "
#define enter_option() printf("%s", KEY)

#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 );

// 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;
// Can you integrate the quick sort program into our calculator?

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()
{
clean_input();
printf("\nComing Attraction!!!\n");
printf("We'll integrate the quicksort program into our calculator!!!\n\n");
}

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);
}

最佳答案

基于指针的快速排序的 header 应如下所示:

void quicksort(int * low, int * high)
{
int * middle = low + ((high-low)/2);
/* ... */

关于c - 使用指针的快速排序和冒泡排序,将这两个实现到 calc.c 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30113842/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com