【发布时间】:2015-05-18 04:31:38
【问题描述】:
对于我的代码,我需要在数组中搜索最大的数字,将其中的数字相加并求平均值,然后在其中搜索最大的数字。我的代码有我想为每个人做什么的基本想法。
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include<fstream>
using namespace std;
void menu();
string createFile();
void displayNumTotalAverage(string);
void displaySortedNums();
void BubbleSort();
void SearchNum();
void displayLargestNum();
void appendRandomNum(string);
void exit();
void CreateFile();
void printFunc(int[]);
void fillFunc(int[]);
int main()
{
menu();
string FileName;
createFile();
CreateFile();
system("pause");
return 0;
}
void menu()
{
int choice;
string FileName;
do
{
//program output
cout << "** MENU **" << endl << endl;
cout << "Curret Data File: " << endl << endl;
cout << "(1) Select / create data file (.txt file extention will be added automaticly)" << endl;
cout << "(2) Display all numbers, total and average" << endl;
cout << "(3) Display all numbers sorted" << endl;
cout << "(4) search for a number and display how many times it occurs" << endl;
cout << "(5) display the largest number" << endl;
cout << "(6) Append a random number(s)" << endl;
cout << "(7) Exit the program" << endl << endl;
//user input
cout << "Menu Choice: ";
cin >> choice;
while (choice > 7 || choice < 1)
{
cout << "Menu Choice: ";
cin >> choice;
}
switch (choice)
{
case 1:
cout << "\nChoice 1" << endl << endl;
createFile();
break;
case 2:
cout << "\nChoice 2" << endl << endl;
displayNumTotalAverage(FileName.c_str());
break;
case 3:
cout << "\nChoice 3" << endl << endl;
break;
case 4:
cout << "\nChoice 4" << endl << endl;
break;
case 5:
cout << "\nChoice 5" << endl << endl;
break;
case 6:
cout << "\nChoice 6" << endl << endl;
appendRandomNum(FileName.c_str());
break;
case 7:
exit();
break;
}
} while (choice != 7);
}
string createFile()
{
string FileName;
ifstream inFile;
cout << "Name of data file: ";
cin >> FileName;
FileName = "C:\\Users\Wizard\Libraries\Documents\Final Project.txt" + FileName;
inFile.open(FileName + ".txt");
if (inFile)
{
cout << FileName;
}
else
cout << "File not found, creating file."<<endl;
system("PAUSE");
return FileName;
}
void displayNumTotalAverage(string FileName)
{
ifstream inFile;
cout << "Display Number Total Average - Option 2" << endl << endl << endl;
inFile.open("C:\\Users\Wizard\Libraries\Documents\Final Project" + FileName + ".txt");
int num;
int total=0;
cout << "Display Number Total Average function" << FileName << endl;
double average;
bool containsNum = false;
inFile.open(FileName + ".txt");
if (inFile)
{
while (inFile >> num)
{
cout << num << endl;
}
inFile.close();
}
else
{
cout << "Error opening file" << FileName << "." << endl;
}
system("PAUSE");
return;
}
void displaySortedNums(int arr[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (arr[count]>arr[count + 1])
{
temp = arr[count];
arr[count] = arr[count + 1];
swap = true;
}
}
} while (swap);
system("PAUSE");
return;
}
void searchNum()
{
cout << " I am the searchNum function - option 4" << endl;
system("PAUSE");
return;
}
void displayLargestNum(int arr[], int numElems, int value)
{
{
int index = 0;
int position = -1;
bool found = false;
while (index < numElems && !found)
{
if (arr[index] == value)
found = true;
position = index;
}
index++;
return;
}
system("PAUSE");
return;
}
void appendRandomNum(string FileName)
{
int num = 0;
int count = 0;
ofstream outFile;
outFile.open(FileName + ".txt", ios::app);
cout << "How many random numbers: ";
cin >> count;
for (int i = 0; i < count; i++)
outFile << rand() % 10 << endl;
outFile.close();
cout << endl << "Number(s) Added" << endl << endl;
system("PAUSE");
return;
}
void exit()
{
std::exit(0);
system("PAUSE");
return;
}
void CreateFile()
{
int random[50]; //Random Numbers
srand((unsigned)time(NULL));
fillFunc(random);
printFunc(random);
return;
}
void fillFunc(int arr[])
{
for (int i = 0; i < 50; i++)
{
arr[i] = 1 + rand() % 10;
}
}
void printFunc(int arr[])
{
ofstream fout("C:\\Users\Wizard\Libraries\Documents\Final Project");
if (fout.is_open()){
for (int i = 0; i < 50; i++)
{
fout << arr[i] << std::endl;
}
}
}
【问题讨论】:
-
你的问题是什么?
-
首先我做得对吗。
-
您可能(或可能不)有兴趣知道标准库有
accumulate,它可以将数组中的值相加,max_element,它可以找到数组中的最大值数组,sort可以对数组进行排序,find,lower_bound和upper_bound可以在数组中进行搜索。这些也可以与更明智的集合一起使用,例如std::vector,而不是实际的数组。 -
这个问题似乎更适合代码审查网站:codereview.stackexchange.com
标签: c++ arrays sorting search average