| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 263 人关注过本帖
标题:求助c++高手,数字的插入删除排序的程序!
只看楼主 加入收藏
leer1112
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-6-10
收藏
 问题点数:0 回复次数:1 
求助c++高手,数字的插入删除排序的程序!
[local]5[/local][local]2[/local]要求从txt中读取一串数字,并显示。
菜单包括:
1或insert键,插入一个整数为被选数字。2或delete键,删除被选数字。3或f2键,将数字由小到大排序。4或向下键,选择下一个数字。5或右键,向右移动被选数字。6或左键,向左移动被选数字。7或f1键,退出程序,并将数字写入txt文件。
txt包括:
7(数字总个数)
1(被选数字位置,array[1])
1 2 3 4 5 6 7(显示数字)

下面是我目前写的:

#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <fstream>
using namespace std;

const int MAX_SIZE = 10;

void initialize(int a[], int& number_used, int& active_item);
void display_list(int a[], int size, int& number_used, int& active_item);
void select_next(int a[], int& number_used, int& active_item);
void insert_item(int a[], int size, int& number_used, int active_item);
void delete_item(int a[], int size, int& number_used, int active_item);
void display_menu();
int get_menu_selection();
void move_right(int a[], int number_used, int& active_item);
void move_left(int a[], int number_used, int& active_item);
void swap_values(int& a1, int& a2);
int get_index_of_lowest(const int a[], int start_index, int number_used);
void sort(int a[], int number_used, int& active_item);
bool check_selection(int number_used, int active_item);
bool array_full(ifstream& fin);
bool array_empty(ifstream& fin);
bool last_item_selected(int number_used, int active_item);
bool first_item_selected(int active_item);
bool move_right_valid(int number_used, int active_item);
bool move_left_valid(int number_used, int active_item);
int get_item();
int get_in();

void read_file(ifstream& fin, int a[], int& number_used, int& active_item);
void write_file(ofstream& fout, int a[], int& number_used, int& active_item);

int main()
{
    int array_list[MAX_SIZE], number_used, active_item, item;
    int c;
    bool is_valid = false;
    ifstream fin;
    ofstream fout;
   
    read_file(fin, array_list, number_used, active_item);
    //initialize(array_list, number_used, active_item);
    bool valid = true;
   
    display_menu();
    display_list(array_list, MAX_SIZE, number_used, active_item);
    cout << endl;
   
    c = get_menu_selection();
   
    switch(c)
    {
        case '1':
        case 82:
             item = get_item();
             insert_item(array_list, size, number_used, active_item);
             break;
        case '2':
        case 83:
             delete_item(array_list, size, number_used, active_item);
             break;
        case '3':
        case 60:
             sort(array_list, number_used, active_item);
             break;
        case '4':
        case 80:
             select_next(array_list, number_used, active_item);
             break;
        case '5':
        case 77:
             move_right(number_used, active_item);
             break;
        case '6':
        case 75:
             move_left(number_used, active_item);
             break;
        case '7':
        case 59:
             write_file(fout, array_list, number_used, active_item);
             cout << "\nEnd of program.\n";
             system("PAUSE");
             exit(1);
             break;
        default:
             break;
    }
   
    system("PAUSE");
    return 0;
}

void display_menu()
{
     cout << "1.Insert     \"Insert\" key\n"
          << "2.Delete     \"Delete\" key\n"
          << "3.Sort       \"F2\" key\n"
          << "4.Select     \"Down Arrow\" key\n"
          << "5.Move Right \"Right Arrow\" key\n"
          << "6.Move Left  \"Left Arrow\" key\n"
          << "7.Exit       \"F1\" key\n\n\n";
}

void display_list(int a[], int size, int& number_used, int& active_item)
{
     for(int i = 0; i < number_used; i++)
     {
             if(active_item == -1)
                  cout << "[]";
             if(i == active_item)
                  cout << "[" << a[i] << "] ";
             else
                  cout << a[i] << " ";
     }
}

void select_next(int a[], int& number_used, int& active_item)
{
     if(active_item >= 0)
     {
            if(active_item == number_used - 1)
                   active_item = 0;
            else
                   active_item++;
     }
}

void insert_item(int a[], int size, int& number_used, int active_item)
{
     int integer;
     
     cout << "Please enter an integer: ";
     cin >> integer;
     get_in();
}

void swap_values(int& a1, int& a2)
{
     int temp;
     temp = a1;
     a1 = a2;
     a2 = temp;
}

int get_index_of_lowest(const int a[], int start_index, int number_used)
{
      int min = a[start_index];
      int min_index = start_index;
      
      for(int i = start_index + 1; i < number_used; i++)
      {
          if(a[i] < min)
          {
              min = a[i];
              min_index = i;
          }
      }
      return min_index;
}

void sort(int a[], int number_used, int& active_item)
{
     int next_smallest;
     
     for(int i = 0; i < number_used - 1; i++)
     {
          next_smallest = get_index_of_lowest(a, i, number_used);
          swap_values(a[i], a[next_smallest]);
     }
}

int get_int()
{
    char c;
    string s;
    bool flag = true;
    bool a_flag = true;
    int number, counter = 0;
   
    do
    {
         if(!flag)
         cout << "Wrong Input. Try again: ";
         flag = true;
         a_flag = (cin >> number);
         while(!a_flag)
         {
             flag = false;
             cin.clear();
            
             a_flag = true;
         }
         char symbol;
         do
         {
              cin.get(symbol);
              if(symbol != '\n')
                  flag = false;
         }while(symbol != '\n');
    }while(!flag);
    cout << endl;
    return number;
}

void read_file(ifstream& fin, int a[], int& number_used, int& active_item)
{
     int i = 0;

     fin.open("test.txt");
     if(fin.fail())
     {
         cout << "Input file opening failed.\n";
         system("PAUSE");
         exit(1);
     }
     
     if(array_empty)
     {
         number_used = 0;
         active_item = -1;
     }
     else
     {
         fin >> number_used;
         fin >> active_item;
         while(fin >> a[i])
             i++;
     }
}

void write_file(ofstream& fout, int a[], int& number_used, int& active_item)
{
     fout.open("test.txt");
     if(fout.fail())
     {
         cout << "Output file opening failed.\n";
         system("PAUSE");
         exit(1);
     }
     
     fout << number_used << endl;
     fout << active_item << endl;
     for(int i = 0; i < number_used; i++)
         fout << a[i] << " ";
}
2011-06-10 13:58
leer1112
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-6-10
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

final_project11.zip (153.23 KB)
2011-06-10 14:02
快速回复:求助c++高手,数字的插入删除排序的程序!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022790 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved