1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// project1
// this project alphabetizes a list of names
// written by ****** *****
#include <iostream>
#include <fstream>
using namespace std;
//void read_in_file(char array[]);
//void read_out_file (char array[]);
void print_list(char array[][10]);
void sort_list(char array[][10]);
void swap_lines (char s1[], char s2[]);
int strcmp (char s1[], char s2[], int x);
int main()
{
char file_name[100];
char list[10][10];
int i = 0, j;
/*ask user for file to open, scans in file the creates object linked to file*/
cout << "what file would you like to organize? " << endl;
cin >> file_name;
ifstream input_stream;
input_stream.open (file_name);
//read file into array
for (i; i < 10; i++)
{
input_stream >> list[i];
}
print_list(list);
sort_list(list);
print_list(list);
//create new output file with sorted list inside
ofstream output_stream;
output_stream.open ("sorted_list.txt");
output_stream << list;
output_stream.close();
return 0;
}
/*void read_in_file(char array[]);
{
char list[10][10];
int i = 0;
ifstream input_stream;
input_stream.open (array[]);
for (i; i < 10; i++)
{
input_stream >> list[i];
}
}*/
void print_list(char array[][10])
{
int i = 0;
for (i; i < 10; i++)
{
cout << list[i];
}
}
void sort_list(char array[][10])
{
/*uses a simple bubble sort algorithm to copare lines of the array and make
any swaps that are needed*/
int i,j;
int flag = 1;
char s1[10], s2[10];
for (i=1;(i<10)&&flag;i++)
{
flag = 0;
for(j=0;j<10;j++)
{
flag=0;
int k=strcmp(list[j], list[j+1]);
if(k==1)
{
swap(list[j], list[j+1]);
flag=1;
}
}
}
}
void swap_lines (char s1[], char s2[])
{
int i;
char temp[10];
for(i=0;i<10;i++)
{
temp[i]=s1[i];
s1[i]=s2[i];
s2[i]=temp[i];
}
}
int strcmp (char s1[], char s2[])
{
int x = 0, i;
for(i=0;i<10;i++)
{
if (s1[i]<s2[i])
{
x = -1;
}
else if (s1[i]>s2[i])
{
x = 1;
}
else if (s1[i]==s2[i])
{
continue;
}
else
{
x = 0;
}
}
return x;
}
/*void read_out_file (char array)
{
ofstream output_stream;
output_stream.open ("sorted_list.txt");
output_stream << list;
output_stream.close;
}*/
|