arr_rand.cpp

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #include <iostream.h>
#include <strstrea.h>

void main(void)
 {
   char name[]="Jamsa's C/C++ Programmer's Bible";
   char iostr[80];
   strstream ios(iostr, sizeof(iostr), ios::in | ios::out);
   char ch;

   ios << name;
   ios.seekg(7, ios::beg);
   ios >> ch;
   cout << "Name: " << name << endl;
   cout << "Character at position 8: " << ch;
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于菜单栏的排序系统的C++代码,其中包含了您所需要的五种排序算法的实现: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void insertionSort(int arr[], int n) { int i, key, j; for (i = 1; i < n; i++) { key = arr[i]; j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = key; } } void selectionSort(int arr[], int n) { int i, j, min_idx; for (i = 0; i < n - 1; i++) { min_idx = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; swap(arr[min_idx], arr[i]); } } void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n - 1; i++) for (j = 0; j < n - i - 1; j++) if (arr[j] > arr[j + 1]) swap(arr[j], arr[j + 1]); } void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return (i + 1); } void quickSort(int arr[], int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } int main() { int arr[10]; srand((unsigned)time(NULL)); for (int i = 0; i < 10; i++) { arr[i] = rand() % 999 + 1; } int choice; do { cout << "1. Simple Insertion Sort" << endl; cout << "2. Selection Sort" << endl; cout << "3. Bubble Sort" << endl; cout << "4. Merge Sort" << endl; cout << "5. Quick Sort" << endl; cout << "6. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: insertionSort(arr, 10); break; case 2: selectionSort(arr, 10); break; case 3: bubbleSort(arr, 10); break; case 4: mergeSort(arr, 0, 9); break; case 5: quickSort(arr, 0, 9); break; case 6: exit(0); break; default: cout << "Invalid choice!" << endl; } cout << "Sorted array: "; for (int i = 0; i < 10; i++) { cout << arr[i] << " "; } cout << endl; } while (choice != 6); return 0; } ``` 上面的代码中,我们使用了C++的标准库,包括iostream、cstdlib和ctime。其中,cstdlib和ctime库用于产生随机数。 为了方便起见,我们将测试数据产生的代码放在了main函数外面,并在main函数中调用各个排序函数。在菜单栏中,我们使用switch语句来选择排序函数,并在选择完成后输出排序结果。 注意,这个代码中只是简单地演示了如何实现基于菜单栏的排序系统,实际应用中还需要对代码进行优化,例如加入输入数据、输出结果等的图形界面等。 希望这个代码能为您提供一些帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值