佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1250|回复: 18

求助c++

[复制链接]
发表于 8-12-2016 12:02 PM | 显示全部楼层 |阅读模式
有谁会c++??

我是c++新手,就是 0 knowldge.需要高手帮忙解决。

http://melpon.org/wandbox/permlink/qLnE80Hs6FJWl9Fy

那个高手可以帮帮忙。。真的很急。

谢谢。
回复

使用道具 举报


ADVERTISEMENT

发表于 8-12-2016 04:09 PM | 显示全部楼层
你看看行不行.
243開始自己改吧

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include "Student.h"
#include "Module.h"
using namespace std;

void printMenu();
vector<Student> readFile();
void liststudentRecord(vector<Student>;
string trim(string);
bool isValidClasscode(string s);
bool isvalidStudentid(string s);
bool test1Ascending(Student s1, Student s2);
bool test1Desscending(Student s1, Student s2);
bool test1finalScoreAscending(Student s1, Student s2);
vector<Module> readFile2();
void listmoduleRecord(vector<Module>;
bool moduleName(Module m1, Module m2);
vector<Student> getStudentByModule(vector<Student>, string);
bool isDuplicateStudentRecord(vector<Student>, Student);

// gloval variable
vector<Student> duplicates;

int main() {

  int choice;
  vector<Student> students; // list of student
  vector<Module> modules;
  vector<Student> list;

  do {
    printMenu();
    cin >> choice;

    switch (choice) {
    case 1:
      cout << "Read Student file ... " << endl;
      students = readFile();
      cout << "Number of records read: " << students.size() << endl;
      break;
    case 2:
      liststudentRecord(students);
      break;
    case 3:
      cout << "Read Module file ... " << endl;
      modules = readFile2();
      cout << "Number of records read: " << modules.size() << endl;
      break;
    case 4:
      listmoduleRecord(modules);
      break;

    case 5:
      liststudentRecord(duplicates);
      break;
    case 6:
      cout << "Enter Module Code: ";
      {
        string code;
        cin >> code;
        list = getStudentByModule(students, code);
        for (unsigned long i = 0; i < list.size(); i++) {
          Student s = list.at(i);
          cout << s.toString() << endl;
        }
      }
      cout << "List Student by Module Code" << endl;
      break;
    case 7:
      // http://www.cplusplus.com/reference/algorithm/sort/
      // sort(students.begin(), students.end(), test1finalScoreAscending);
      sort(students.begin(), students.end(),
           test1finalScoreAscending); // finalScoreAcending
      break;
    case 8:
      break;
    case 9:
      cout << "Exiting program... " << endl;
      break;
    default:
      cout << "Invalid option specified. Please try again" << endl;
    }
  } while (choice != 9);

  return 0;
}

void printMenu() {
  cout << "Menu" << endl;
  cout << "----" << endl;
  cout << "[1] Read student file" << endl;
  cout << "[2] List student records" << endl;
  cout << "[3] Read module file" << endl;
  cout << "[4] List module records" << endl;
  cout << "[5] List Duplicate Record" << endl;
  cout << "[6] Display Student by Module" << endl;
  cout << "[7] Sort by Final Score" << endl;
  cout << "[8] Write to file" << endl;
  cout << "[9] Exit" << endl;
  cout << "Choice: ";
}

vector<Student> readFile() {
  vector<Student> students;
  string filename;
  ifstream inputFile;

  cout << "Enter filename: ";
  cin >> filename;

  inputFile.open(filename, ios::in); // open file, read only
  if (inputFile.is_open()) {
    while (inputFile.good()) {
      string line;
      getline(inputFile, line);

      Student s; // create a new student

      // extract id
      int i = line.find(",";
      string id = trim(line.substr(0, i)); // extract id
      s.setId(id);                         // set student id
      line = line.substr(i + 1);           // remove id form line
                                           /*if(isvalidStudentid(id)){
                                               string s = id.substr(0,1);
                                               const char* s = id.substr(1,7);
                                           }*/

      // extract name
      i = line.find(",";
      string name = trim(line.substr(0, i)); // extract name
      s.setName(name);                       // set student name
      line = line.substr(i + 1);             // remove name from line

      // extract classcode
      i = line.find(",";
      string classcode = trim(line.substr(0, i));
      s.setClasscode(classcode);
      line = line.substr(i + 1);
      if (isValidClasscode(classcode)) {
        string module = classcode.substr(0, 6);
        s.setModule(module);
        string status = classcode.substr(7, 9);
        const char *ft = "FT";
        if (status.compare(ft) == 0) {
          s.setFulltime(true);
        } else {
          s.setFulltime(false);
        }
        string classnumber = classcode.substr(9, 11);
        s.setClassnumber(classnumber);
      }

      // to use a for-loop for attendance
      for (int j = 0; j < 10; j++) {
        i = line.find(",";
        string attendance = trim(line.substr(0, i));
        s.setAttendance(j, attendance);
        line = line.substr(i + 1);
      }

      // to extreact test1
      i = line.find(",";
      int test1 = stoi(line.substr(0, i));
      s.setTest1(test1);
      line = line.substr(i + 1);

      // to extreact test2
      i = line.find(",";
      int test2 = stoi(line.substr(0, i));
      s.setTest2(test2);
      line = line.substr(i + 1);

      // to extreact tutorial
      i = line.find(",";
      int tutorial = stoi(line.substr(0, i));
      s.setTutorial(tutorial);
      line = line.substr(i + 1);

      // to extreact exam
      i = line.find(",";
      int exam = stoi(line.substr(0, i));
      s.setExam(exam);
      line = line.substr(i + 1);

      // need to do validationfirst
      // to only add the valid student

      if (isDuplicateStudentRecord(students, s) == false) {
        students.push_back(s); // add student to list
      } else {
        duplicates.push_back(s);
      }
      // cout<< line << endl;
    }
    inputFile.close();
  } else {
    cout << "Invalid file" << endl;
  }

  return students;
}

void liststudentRecord(vector<Student> list) {
  int numberStudents = list.size();

  if (numberStudents > 0) {
    for (int i = 0; i < numberStudents; i++) {
      Student s = list.at(i);
      cout << s.toString() << endl;
    }
  } else {
    cout << "Empty list" << endl;
  }
}

bool test1Ascending(Student s1, Student s2) {
  return s1.getTest1() < s2.getTest1();
}

bool test1Descending(Student s1, Student s2) {
  return s1.getTest1() < s2.getTest1();
}

bool finalScoreAscending(Student s1, Student s2) {
  return s1.getFinalScore() < s2.getFinalScore();
}

bool isvalidStudentid(string s) {
  int i;

  if (s.length() != 8) {
    return false;
  } // first check th length is 8

  if (s.at(i) != 'S') {
    return false;
  } // start with s

  for (i = 1; i < 8; i++) { // 7 digit
    if (isdigit(s.at(i)) == 0) {

      return false;
    }
  }

  return false;
}

bool isValidClasscode(string s) {
  int i;

  if (s.length() != 11) {
    return false;
  } // first check th length is 8

  for (i = 0; i < 3; i++) { // first 3 char are letters
    if (isalpha(s.at(i)) == 0) {
      return false;
    }
  }

  for (i = 3; i < 6; i++) { // next 3 char are number
    if (isdigit(s.at(i)) == 0) {
      return false;
    }
  }
  if (s.at(6) != '-') {
    return false;
  } // has a hyphen

  if (s.at(7) != 'F' && s.at(7) != 'P') {
    return false;
  }

  if (s.at(8) != 'T') {
    return false;
  } // T

  if (isdigit(s.at(9)) == 0 || isdigit(s.at(10)) == 0) {
    return false;
  }

  return false;
}

vector<Module> readFile2() {
  vector<Module> modules;
  string filename;
  ifstream inputFile;

  cout << "Enter filename: ";
  cin >> filename;

  inputFile.open(filename, ios::in); // open file, read only
  if (inputFile.is_open()) {
    while (inputFile.good()) {
      string line;
      getline(inputFile, line);

      Module m; // create a new student

      // extract name
      int i = line.find(",";
      string modulecode = trim(line.substr(0, i)); // extract id
      m.setModulecode(modulecode);                 // set student id
      // cout << id<< endl;
      line = line.substr(i + 1); // remove id form line
      // cout << line << endl;

      // extract id
      i = line.find(",";
      string modulename = trim(line.substr(0, i)); // extract name
      m.setModulename(modulename);                 // set student name
      line = line.substr(i + 1);                   // remove name from line

      // to extreact test1
      i = line.find(",";
      int test1 = stoi(line.substr(0, i));
      m.setTest1(test1);
      line = line.substr(i + 1);

      // to extreact test2
      i = line.find(",";
      int test2 = stoi(line.substr(0, i));
      m.setTest2(test2);
      line = line.substr(i + 1);

      // to extreact tutorial
      i = line.find(",";
      int tutorial = stoi(line.substr(0, i));
      m.setTutorial(tutorial);
      line = line.substr(i + 1);

      // to extreact exam
      i = line.find(",";
      int exam = stoi(line.substr(0, i));
      m.setExam(exam);
      line = line.substr(i + 1);

      modules.push_back(m); // add student to list

      // cout<< line << endl;
    }
    inputFile.close();
  } else {
    cout << "Invalid file" << endl;
  }

  return modules;
}

void listmoduleRecord(vector<Module> list) {
  int numberModules = list.size();

  if (numberModules > 0) {
    for (int i = 0; i < list.size(); i++) {
      Module m = list.at(i);
      cout << m.toString() << endl;
    }
  } else {
    cout << "Empty list" << endl;
  }
}

vector<Student> getStudentByModule(vector<Student> students, string code) {
  vector<Student> list;
  for (int i = 0; i < students.size(); i++) {
    Student s = students.at(i);
    if (code.compare(current_module) == 0) {
      list.push_back(s);
    }
  }
  return list;
}

bool isDuplicateStudentRecord(vector<Student> student, Student s) {
  string id = student.getId();
  string name = student.getName();
  string module = student.getModule();
  for (int i = 0; i < student.size(); i++) {
    Student s = student.at(i);
    string current_id = s.getId();
    string current_name = s.getName();
    string current_module = s.getModule();
    if (id.compare)
      (current_id) == 0 && name.compare(current_name) == 0 &&
          (module.compare(current_module) == 0);
    { return true; }
  }
  return false;
}

// http://stackoverflow.com/a/6500499/3839235
string trim(string s) {
  s.erase(0, s.find_first_not_of(' ')); // prefixing spaces
  s.erase(s.find_last_not_of(' ') + 1); // surfixing spaces
  return s;
}

回复

使用道具 举报

 楼主| 发表于 9-12-2016 09:38 AM | 显示全部楼层
yan13 发表于 8-12-2016 04:09 PM
你看看行不行.
243開始自己改吧

#include
#include
#include
#include
#include
#include
#include
#include
#include "Student.h"
#include "Module.h"
using namespace std;

void p ...

可以帮帮吗?
回复

使用道具 举报

发表于 9-12-2016 09:58 AM | 显示全部楼层

幫什麼?
回复

使用道具 举报

 楼主| 发表于 9-12-2016 10:10 AM | 显示全部楼层

就是帮我看看全部的code对吗?因为是assignment, 我是 0 knowledge c++.
如果可以的话,我email你全部的code。 ok吗?
回复

使用道具 举报

发表于 9-12-2016 04:46 PM | 显示全部楼层
cry8383 发表于 9-12-2016 10:10 AM
就是帮我看看全部的code对吗?因为是assignment, 我是 0 knowledge c++.
如果可以的话,我email你全部的code。 ok吗?

我想你應該知道的,不過我總是要意思意思的提醒一下.Assignment是要自己做的,不懂的我可以幫,但不能全部幫你做.好了.有什麼我們可以一起學習.code拿來吧,我工作累了就看看

回复

使用道具 举报

Follow Us
 楼主| 发表于 9-12-2016 05:06 PM | 显示全部楼层
yan13 发表于 9-12-2016 04:46 PM
我想你應該知道的,不過我總是要意思意思的提醒一下.Assignment是要自己做的,不懂的我可以幫,但不能全部幫你做.好了.有什麼我們可以一起學習.code拿來吧,我工作累了就看看

当然assignment,我今天也solved 了全部的error。 现在是complied那不到要的result。case: 6那一边出问题。 不知道哪里错,就是没有error。


https://www.dropbox.com/s/jsnidkhz9f1q8pw/partb.zip?dl=0


帮帮忙。。。
回复

使用道具 举报

发表于 9-12-2016 06:07 PM | 显示全部楼层
cry8383 发表于 9-12-2016 05:06 PM
当然assignment,我今天也solved 了全部的error。 现在是complied那不到要的result。case: 6那一边出问题。 不知道哪里错,就是没有error。


https://www.dropbox.com/s/jsnidkhz9f1q8pw/partb.zip?dl=0


...

你用什麼IDE的?
我用VC++
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 9-12-2016 07:22 PM | 显示全部楼层
yan13 发表于 9-12-2016 06:07 PM
你用什麼IDE的?
我用VC++

我也是用MS VC++
回复

使用道具 举报

发表于 9-12-2016 07:36 PM | 显示全部楼层

麻煩給我整個folder.謝謝
回复

使用道具 举报

 楼主| 发表于 10-12-2016 07:52 AM | 显示全部楼层
yan13 发表于 9-12-2016 07:36 PM
麻煩給我整個folder.謝謝

https://www.dropbox.com/s/tos6a5nsujwromi/PartD.zip?dl=0


这个是整个folder, 你是学java还是C#?

以前我学过java, java比较简单。
回复

使用道具 举报

发表于 14-12-2016 02:57 PM | 显示全部楼层
cry8383 发表于 10-12-2016 07:52 AM
https://www.dropbox.com/s/tos6a5nsujwromi/PartD.zip?dl=0


这个是整个folder, 你是学java还是C#?

以前我学过java, java比较简单。

Java還沒開始coding,不算學.C++類似Java,你上手會比較快.我下載了.等等看看


回复

使用道具 举报

发表于 18-12-2016 06:51 PM | 显示全部楼层
cry8383 发表于 10-12-2016 07:52 AM
https://www.dropbox.com/s/tos6a5nsujwromi/PartD.zip?dl=0


这个是整个folder, 你是学java还是C#?

以前我学过java, java比较简单。

vs2015 repaired 了也re installed了,還是開不到你的project
回复

使用道具 举报

 楼主| 发表于 6-1-2017 08:06 AM | 显示全部楼层
yan13 发表于 18-12-2016 06:51 PM
vs2015 repaired 了也re installed了,還是開不到你的project

哈哈。。功课已经交了。不过还是谢谢你啦。。
回复

使用道具 举报

发表于 6-1-2017 10:23 AM | 显示全部楼层
cry8383 发表于 6-1-2017 08:06 AM
哈哈。。功课已经交了。不过还是谢谢你啦。。

恭喜
回复

使用道具 举报

发表于 5-8-2017 11:20 AM | 显示全部楼层
为什么发代码不用 <> ? 这样比较整齐啊

  1. cout "Enter radius:";
复制代码
回复

使用道具 举报


ADVERTISEMENT

发表于 5-11-2018 04:33 PM | 显示全部楼层
yan13 发表于 8-12-2016 04:09 PM
你看看行不行.
243開始自己改吧

#include
#include
#include
#include
#include
#include
#include
#include
#include "Student.h"
#include "Module.h"
using namespace std;

void p ...

什么是vector?我之前学C++的时候曾经看过有个同学问Lecturer能不能用vector,但是最后lecturer不给啦,我也没有学过
回复

使用道具 举报

发表于 5-11-2018 06:16 PM | 显示全部楼层
KL_Seow 发表于 5-11-2018 04:33 PM
什么是vector?我之前学C++的时候曾经看过有个同学问Lecturer能不能用vector,但是最后lecturer不给啦,我也没有学过

當他是array好了.array, list之類的.詳細的就不懂了.
回复

使用道具 举报

发表于 6-11-2018 06:49 AM | 显示全部楼层
KL_Seow 发表于 5-11-2018 04:33 PM
什么是vector?我之前学C++的时候曾经看过有个同学问Lecturer能不能用vector,但是最后lecturer不给啦,我也没有学过

vector大致上和array是一样,只是在storage allocation上不同,如果你很频密insert大数据,vector有performance或dynamic size growing的优势

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 20-4-2024 02:01 AM , Processed in 0.074069 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表