2013-02-26 42 views
5

在这个专注于使用结构数组的项目上,我一直很迷茫。我们刚刚了解到它们。我觉得我得到它的基本功能在看一些我们的实验室中,看着相关的问题后:使用结构数组计算?

StackOverflow的
Trouble passing an array of structs
array of pointers to structures
How to initialize array of structures with?
Arrays in stuctures
Creating an array of structs in C++
How to initialize an array of struct in C++?

Cplusplus
Declaring an array of structures
Array of Structs C++ Program Help
Problem with a Dynamic Array of Structs

我真的很感激任何建议或帮助任何人都可以给。我们被允许使用这个老实验室(这是正确的分配后,我们完成了这个实验,现在我们3个实验室过去吧)为起点:

动态数组

// Assignment : Lab 
// File : Lab.cpp 

// Description : This program will take a text file called words.txt and then swap 
// its individual (word)strings. Finally, it will calculate the vowels, consonants, 
// digits, and special characters in each string. 

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 
bool isvowel (char aletter); 
// const int MAXWORDS = 100; 

struct worddata 
{ 
    worddata(); 
    string word; 
    int vowels; 
    int consonants; 
    int digits; 
    int specialchars; 
}; 


int ReadFile (ifstream & input, worddata * & Words); 
void WriteReport(ostream & output, worddata Words [], int count); 
void Swap (worddata & a, worddata & b); 
void WordSort (worddata W [], int N); 

int main (int argc, char * argv []) 
{ 
    // Check to see if the user entered a file name 
    // Exit if no file name 
    if (argc < 2) 
    { 
     cout << "Usage: " << argv[0] << " <filename>\n"; 
     exit (1); 
    } 
    // Open the input file 
    ifstream input (argv[1]); 
    if (input.fail()) 
    { 
     cout << "File: " << argv[1] << " not found\n"; 
     exit (2); 
    } 
    // Declare a pointer to an array of worddata objects 
    // to hold the words and their vowel, consonant, digit, and 
    // special character counts. 
    // worddata WordArray [MAXWORDS]; 
    worddata * WordArray; 

    // Call the ReadFile function to read the file, store the 
    // words in the array and return the number of words read 
    // from the file. 
    int count = ReadFile (input, WordArray); 

    // Call the WordSort function to sort the words into 
    // alphabetical order. 
    WordSort (WordArray, count); 

    // Call the WriteReport function to write the data 
    // stored in the array in a formatted fashion. 
    WriteReport (cout, WordArray, count); 

    return 0; 
    } 

    worddata::worddata() 
    { 
    vowels = 0; 
    consonants = 0; 
    digits = 0; 
    specialchars = 0; 
    } 

int ReadFile (ifstream & input, worddata * & Words) 
{ 

    int count = 0; 
    string oneword; 
    // Read and count the words in the file 
    while (input >> oneword) 
    count++; 
    // Allocate space for the number of words counted by the 
    // previous loop 
    Words = new worddata [count]; 
    // Clear the fail flag 
    input.clear(); 
    // Reposition the file pointer to the beginning of the file 
    input.seekg (0, ios::beg); 
    count = 0; 

    // Read the words from the file into the array 
    while (input >> Words[count].word) 
    { 
     // Count the number of vowels, consonants, digits 
     // and special characters in the word and store them 
     // in the object Words [count] 

     string aword = Words[count].word; 

     // Number of letters in word 
     int l = 0; 
     while (l < aword.length()) 
     { 
     if (isvowel(aword[l])) 
      Words[count].vowels++; 
     else if (isalpha(aword[l])) 
      Words[count].consonants++; 
     else if (isdigit(aword[l])) 
      Words[count].digits++; 
     else 
      Words[count].specialchars++; 
     l++; 
     } 
     count++; 
    } 
    // Close the file 
    input.close(); 
    // Return the size of the Words array 
    return count; 
} 

void WriteReport (ostream & output, worddata Words [], int count) 
{ 
    worddata totals; 
    totals.vowels, totals.consonants = 0; 
    totals.digits, totals.specialchars = 0; 

    output << setw (14) << left << "Word"; 
    output << setw (8) << right << "Vowels"; 
    output << setw (8) << "Const."; 
    output << setw (8) << "Digits"; 

    output << setw (8) << "Special" << endl;; 

for(int i = 0; i < count; i++) 
{ 
     output << setw (14) << left << Words[i].word; 
     output << setw (8) << right << Words[i].vowels; 
     totals.vowels += Words[i].vowels; 
     output << setw (8) << Words[i].consonants; 
     totals.consonants += Words[i].consonants; 
     output << setw (8) << Words[i].digits; 
     totals.digits += Words[i].digits; 
     output << setw (8) << Words[i].specialchars << endl; 
     totals.specialchars += Words[i].specialchars; 
    } 
{ 
    output << setw (14) << left << " "; 
    output << setw (8) << right << "---"; 
    output << setw (8) << "---"; 
    output << setw (8) << "---"; 
    output << setw (8) << "---" << endl; 
    output << setw (14) << left << "Totals"; 
    output << setw (8) << right << totals.vowels; 
    output << setw (8) << totals.consonants; 
    output << setw (8) << totals.digits; 
    output << setw (8) << totals.specialchars << endl; 
    } 
} 

void Swap (worddata & a, worddata & b) 
{ 
    worddata t = a; 
    a = b; 
    b = t; 
} 

void WordSort (worddata W [], int N) 
{ 
    int i = 1; 
    while(i < N) 
    { 
     int j = i; 
     while(j > 0 && W[j].word < W[j-1].word) 
     { 
      Swap(W[j], W[j-1]); 
      j--; 
     } 
     i++; 
    } 
} 

// Returns true/false depeninding if a letter in a word is a vowel or not 
bool isvowel (char aletter) 
{ 
    char upcase = toupper (aletter); 
    if (upcase == 'A' || upcase == 'E' || upcase == 'I' || upcase == 'O' || upcase == 'U') 
    return true; 
    return false; 
} 

Project link

这是我已经成功地至今没有写迷路或将其打入遗忘

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream> 
#include <vector> 
#include <sstream> 
#include <cstdlib> 

using namespace std; 
// const int ListSize = 50; 

struct Assignment { 
    char atype; 
    string date; 
    float received; 
    int possible; 
    string title; 
}; 

// functions used by main 
int ReadFile(ifstream& input, Assignmnent list[], int listSize); 
void WriteReport(ostream & output, Assignment list [], int numRecords); 
void Swap (Assignment & a, Assignment & b); 
void CategorySort (Assignment C [], int N); 

int main() { 
    // Check to see if the user entered a file name 
    // Exit if no file name 
    if (argc < 2) 
    { 
    cout << "Usage: " << argv[0] << " <filename>\n"; 
    exit (1); 
    } 
    // Open the input file 
    ifstream input (argv[1]); 
    if (input.fail()) 
    { 
    cout << "File: " << argv[1] << " not found\n"; 
    exit (2); 
    } 


    int numRecords = ReadFile(input, Assignmnent list[], int listSize); 

    if(numRecords > ListSize+1) 
    { 
    cout << "Too Many Records for this program to read" << endl; 
    system("read"); 
    return -1; 
    } 

    // no records? 
    if(numRecords == 0) 
    { 
    cout << "Empty File" << endl; 
    system("read"); 
    return -1; 
    } 
} 

我也知道我可能会使用getline。就是这样。一旦我有一个关于如何处理ReadFile并声明其中一些范围的提纲,我就会有一种感觉,我会很好的。我真的很谨慎,不确定是否开始。另外,如果原型看起来很奇怪,我将它建立在实验室上,我从另一所学校的另一堂课看过,所以我不知道他们是否在这方面工作。

+1

你就要成功了,不是吗?要使用'getline'逐行读取输入,请参见[here](http://stackoverflow.com/questions/7868936/c-read-file-line-by-line)。在循环内部,可以使用'std :: string'的substr()来提取'struct'元素的各个值。 – jogojapan 2013-02-26 10:58:20

+1

您可以使用'std :: vector '来绕过所有与阵列相关的问题。 – juanchopanza 2013-02-26 10:59:59

+0

如果您计划在Windows平台上进行编译,请为“ReadFile”找到另一个名称。 – WhozCraig 2013-02-26 11:20:32

回答

1

由于这是一个任务,我不想只是放弃任何答案,但这里是应该帮助的东西。

我看完了你的任务,我没有看到动态使用的任何字。这意味着你可以创建大小为50的数组来启动。

// non dynamic array allocation: 
assignments assign[50]; 
// dynamic allocation containers 
vector<assignments> assign; // array list 
list<assignments> assign; // linked list 
queue<assignments> assign; // queue list, useful with insertion sort 

解析输入数据,我建议getlinestrtok打破行成正确的数据。

例子:

int linelength = 256; // don't forget the +1 for end of string terminator 
char *whitespaces = " \t" // \n and \0 will never be part of the token based on getline 
char line[256]; // or some other safe number length 
char *token; // no need to define size 

inputstream.getline(line, 256); 
token = strtok(line, whitespaces); 
// save token to correct location 
token = strtok(NULL, whitespaces); 
// save token to correct location 
token = strtok(NULL, whitespaces); 
// save token to correct location 
// ... 

只是不要指望后strtok s到使用line

至于你的ReadFile函数,我会给它输入流并让它返回一个动态容器。我尽量减少修改参数的频率。

vector<assignment> load(istream) { 
    // stuff like the second block of code goes here 
    return assign; 
} 

希望这会有所帮助。

感谢,