- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图弄清楚如何首先修改我的快速排序程序以使用指针算术,然后将我的冒泡排序程序更改为使用指针的递归程序,最后将这两个程序实现为计算器(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/
我想过滤掉特定列上具有特定颜色的行。我尝试几乎到处搜索但找不到答案。希望大家给点好的建议,谢谢。 最佳答案 据我所知,目前没有内置函数允许根据单元格的格式选择单元格。但在 this OOo forum
我有一个百分比表: A | B | C ------------------- 1| 12% | 22% | 42% ------------------- 2| 52% | 2%
我有一个格式为 HH:MM 的时间值求和列。总和的格式为 [HH]:MM,可防止 HH > 23 时翻转。我希望将此值转换为十进制数表示形式。例如: A B 1 12
LibreOffice Calc 中的什么公式将计算不同 Week_Number 出现的次数?答案应该是2。 最佳答案 这个问题之前已经被问过很多次了。一种方法是在 B6 中输入以下公式然后按 Ctr
我在 Calc 电子表格中有几个数据列,每个列有 400 到 500 个条目。对于这些列中的每一列,我只想简单地找到均值和标准差,但不知道如何进行。 有人可以用简单而非技术性的语言逐步指导我如何做到这
假设我在 LibreOffice calc 中有以下电子表格: | A | B | C | D | ------------------- |1| 1 | 2 | | 2 | |2| 2 |
LibreOffice Calc 中是否有接受 的函数整数 x 和 整数 y 并以该位置吐出单元格的内容? 有一个功能ADDRESS吐出给定 [x;y] 的地址。问题是我不知道如何取消引用它。 最佳答
我希望能够在 LibreOffice Calc 中选择两个单元格,然后按某种魔术键,结果交换两个单元格的内容。 我怎么做? 我为 Excel 找到了这个解决方案,但它在 LibreOffice 中不起
我有两列有数字。当另一个有重复时,另一个只有一次数字。这些列中的数字不匹配。我需要在 B 列中找到在 A 列中匹配的所有数字。 这可能更好地解释了它: 甲乙 1 2 2 2 4 5 6 5 7 6 8
我想为 Calc 编写一个返回 #VALUE 的 Basic 函数!可使用 ISERR() 进行测试。 Function foo() foo = #VALUE! End Function 但是
我正在寻找一种使用 LibreOffice 的 Calc 从网站获取一些数据的方法。 我之前使用带有 IMPORTXML 函数的 Google 表格,但因为它非常不可靠,所以我想改用 Calc。 我的
我有这些单元格的 A 列: A1: Apple A2: Banana A3: Cherry 我想要一个公式,将它们串在一个单元格中,如下所示: "Apple, Banana, Cherry" 最佳答案
我想从 LibreOffice Calc 中多次出现的字符中获取最后一次出现。 例如我有一个字符串 abc1ba2ac2adeaa43add .现在,如果我正在搜索 a它应该返回 18。 最佳答案 使
假设我有这个这个单元格: 然后出于某种原因,我想自动反转列的顺序,变成这样: 任何帮助将不胜感激! 最佳答案 快速步骤: 在 1 上方插入新行 用单调递增的整数索引向右填充行。 然后选择您的数据并降序
我想在 LibreOffice Calc(v5.4.4.2 但可以升级)中实现一些条件格式,以突出显示其值通过公式计算而不是直接输入的单元格。 例如,一个单元格包含 =A3 将突出显示,而 Hello
我有一个 LibreOffice 3 电子表格(在 Ubuntu 11.04 上),其中有一列包含数百个超链接,这些超链接仅显示为“链接”。 我需要将这些转换为纯文本,或者更糟糕的是显示超链接而不是文
我在屏幕上有四个绝对定位的 div,它们需要在所有分辨率下保持它们的位置。 div1 - 高度 150px,顶部 0px div2 - 高度 30%,顶部 150px div3 - 高度 50 像素,
所以我很清楚,一般来说,应该在 JS 和浏览器检测中使用特征检测。 jQuery 1.9 放弃了 $.browser 就是一个很好的例子。 此外,在我阅读的每篇文章中,都说永远不要使用浏览器检测。 但
到目前为止,我一直在使用 em-calc 在我的 Zurb Foundation 元素中定义 CSS 大小。但是,最近我注意到开发人员越来越多地使用 rem-calc。 我知道每个函数的作用,但我不太
如何在另一个 CSS calc 函数中使用 CSS calc 函数?根据这个post这是可能的,但没有这样的例子。 最佳答案 It is possible to use calc() inside a
我是一名优秀的程序员,十分优秀!