楼主: exuan1991
6790 65

Learn to Program with C-Learn to Program using the Popular C Programming Languag   [推广有奖]

61
ReneeBK(真实交易用户) 发表于 2016-1-11 08:16:16
  1. //   Program P8.x - insertionsort2
  2.         #include <stdio.h>
  3.         int main() {
  4.            void insertionSort2(int [], int, int);
  5.            int v, num[10];
  6.            printf("Type up to 10 numbers followed by 0\n");
  7.            int n = 0;
  8.            scanf("%d", &v);
  9.            while (v != 0) {
  10.               num[n++] = v;
  11.               scanf("%d", &v);
  12.            }
  13.            //n numbers are stored from num[0] to num[n-1]
  14.            insertionSort2(num, 0, n-1);
  15.            printf("\nThe sorted numbers are\n");
  16.            for (int h = 0; h < n; h++) printf("%d ", num[h]);
  17.            printf("\n");
  18.         } //end main

  19.         void insertionSort2(int list[], int lo, int hi) {
  20.         //sort list[lo] to list[hi] in ascending order
  21.            void insertInPlace(int, int [], int, int);
  22.            for (int h = lo + 1; h <= hi; h++)
  23.               insertInPlace(list[h], list, lo, h - 1);
  24.         } //end insertionSort2

  25.         void insertInPlace(int newItem, int list[], int m, int n) {
  26.         //list[m] to list[n] are sorted
  27.         //insert newItem so that list[m] to list[n+1] are sorted
  28.            int k = n;
  29.            while (k >= m && newItem < list[k]) {
  30.               list[k + 1] = list[k];
  31.               --k;
  32.            }
  33.            list[k + 1] = newItem;
  34.         } //end insertInPlace
复制代码

62
ReneeBK(真实交易用户) 发表于 2016-1-11 08:18:21
  1. //   Program P8.x Parallel sort
  2.      #include <stdio.h>
  3.      #include <string.h>
  4.      #define MaxNameSize 14
  5.      #define MaxNameBuffer MaxNameSize+1
  6.      #define MaxNames 8
  7.      int main() {
  8.         void parallelSort(int, int, int max, char [][max], int[]);
  9.         char name[MaxNames][MaxNameBuffer] = {"Samlal, Rawle", "Williams, Mark",
  10.                          "Delwin, Mac", "Taylor, Victor", "Mohamed, Abu",
  11.                          "Singh, Krishna", "Tawari, Tau", "Abdool, Alana" };
  12.         int id[MaxNames] = {8742,5418,4833,4230,8583,2458,5768,3313};

  13.         parallelSort(0, MaxNames-1, MaxNameBuffer, name, id);
  14.         printf("\nThe sorted names and IDs are\n\n");
  15.         for (int h = 0; h < MaxNames; h++) printf("%-18s %d\n", name[h], id[h]);
  16.      } //end main

  17.      void parallelSort(int lo, int hi, int max, char list[][max], int id[]) {
  18.      //Sort the names in list[lo] to list[hi] in alphabetical order, ensuring
  19.      //that each name remains with its original id number.
  20.      //The maximum string size is max - 1 (one char taken up by \0).
  21.         char key[max];
  22.         for (int h = lo + 1; h <= hi; h++) {
  23.            strcpy(key, list[h]);
  24.            int m = id[h];  // extract the id number
  25.            int k = h - 1; //start comparing with previous item


  26.            while (k >= lo && strcmp(key, list[k]) < 0) {
  27.               strcpy(list[k + 1], list[k]);
  28.               id[k+ 1] = id[k];  // move up id number when we move a name
  29.               --k;
  30.            }
  31.            strcpy(list[k + 1], key);
  32.            id[k + 1] = m; // store the id number in the same position as the name
  33.         } //end for
  34.      } //end parallelSort
复制代码

63
ReneeBK(真实交易用户) 发表于 2016-1-11 08:47:18
  1. //Program P3.4
  2. //request 3 integers; print their average
  3. #include <stdio.h>
  4. int main() {
  5.    int a, b, c;
  6.    double average;
  7.    printf("Enter 3 integers: ");
  8.    scanf("%d %d %d", &a, &b, &c);
  9.    average = (a + b + c) / 3.0;
  10.    printf("\nTheir average is %3.1f\n", average);
  11. }
复制代码

64
ReneeBK(真实交易用户) 发表于 2016-1-11 08:49:42
  1. //Program P3.5
  2. //request a whole number; print its square
  3. #include <stdio.h>
  4. int main() {
  5.    int num, numSq;
  6.    printf("Enter a whole number: ");
  7.    scanf("%d", &num);
  8.    numSq = num * num;
  9.    printf("\nSquare of %d is %d\n", num, numSq);
  10. }
复制代码

65
ReneeBK(真实交易用户) 发表于 2016-1-11 08:50:55
  1. //Program P3.6
  2. //calculate interest and service charge for bank customer
  3. #include <stdio.h>
  4. int main() {
  5.    char customer[30], acctNum[30];
  6.    double avgBalance, interest, service;
  7.    int numTrans;

  8.    printf("Name? ");
  9.    gets(customer);
  10.    printf("Account number? ");
  11.    gets(acctNum);
  12.    printf("Average balance? ");
  13.    scanf("%lf", &avgBalance);
  14.    printf("Number of transactions? ");
  15.    scanf("%d", &numTrans);

  16.    interest = avgBalance * 0.06;
  17.    service = numTrans * 0.50;

  18.    printf("\nName: %s\n", customer);
  19.    printf("Average balance: $%3.2f\n", avgBalance);
  20.    printf("Interest: $%3.2f\n", interest);
  21.    printf("Service charge: $%3.2f\n", service);
  22. }
复制代码

66
e0g411k014z(未真实交易用户) 学生认证  发表于 2016-7-5 05:33:17 来自手机
Xiexie louzhu

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-30 00:30