0 Replies - 1123 Views - Last Post: 17 April 2013 - 08:00 PM

#1 kiknwing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 17-April 13

Memory development and interpretation for a text editor.

Posted 17 April 2013 - 08:00 PM

Intro
Feast your eyes on the latest undertaking of a diverse subject known as A.I. or Artificial Intelligence. This program was written to share and gain a wider perspective on how machines acquire knowledge. It's not just about me. Yeah I built a simple program, but I want others to also include their opinions on where this could go for them as well. If this helped you out in any way, feel free to discuss what intrigued you about it.

Errors
Vectors. What I know so far is that vectors will improve the programs I/O allocation. I am currently trying to include them in the program as you can see, but have quite a plate of spaghetti.

Time. I have implemented time as a part of the program. I am trying to code it to log dates into the DATALOG. Entries will be made each time it is accessed.

Pointers. I am going to use pointers to sort and locate data for the program.

v1.0
Code
/*
// Add these includes into stdafx.h
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <process.h>
#include <time.h>
#include <conio.h>
#include <sstream>
#include <vector>
using namespace std;
*/
#include "stdafx.h"
 
#define DEBUG
 
// Answer class
class CAnswers {
        public:
                vector<string> answer;
                CAnswers(void);
                CAnswers(string ans);
                ~CAnswers(void);
                int size(void);
} Answers;
CAnswers::CAnswers (void) {
}
CAnswers::CAnswers (string ans) {
        stringstream ss(ans);
        string temp_answer;
        while (getline(ss, temp_answer,','))
                 answer.push_back(temp_answer);
}
CAnswers::~CAnswers(void){
}
int CAnswers::size(void) {
        return answer.size();
}
 
// Question class
class CQuestion {
        string _question;
 
        public:
                CAnswers* answers;
                CQuestion(void);
                CQuestion(string q_a);
                ~CQuestion(void);
                string question(void);
} Question;
CQuestion::CQuestion (void) {
}
CQuestion::CQuestion (string q_a) {
        string ans;
 
        stringstream ss(q_a);
        getline(ss, _question,':');
        ss >> ans;
        answers = new CAnswers(ans);
}
string CQuestion::question (void) {
        return _question;
}
CQuestion::~CQuestion(void){
        delete answers;
}
 
//Editor class
class CEditor {
        vector<CQuestion> questions;
        ofstream myFile;
        int age, address;
        char selection, character[1];
        string name, response;
        HANDLE np;
        unsigned notepadID;
       
        public:
                CQuestion* q;
                CEditor(void);
                ~CEditor(void);
                int menu(void);
                void question(void);
                void reload(void);
                int time(void);
                static unsigned int __stdcall notepadThread(void* arg);
                void notepad(void);
  } Editor;
CEditor::~CEditor(void){
        delete q;
}
CEditor::CEditor (void) {
        np = 0;
}
unsigned int __stdcall CEditor::notepadThread(void* arg) {
        system("notepad.exe AI.txt");
        _endthreadex(0);
        return 0;
}
void CEditor::notepad(void) {
        _beginthreadex( NULL, 0, &CEditor::notepadThread, np, 0, &notepadID );
}
int CEditor::menu (void) {
        system ("CLS");
        cout<<"Menu:\n9 = Basic Information.\n8 = DATALOG\n7 = Reload \n6 = Time\n0 = Leave\n";
        cin>>selection;
 
        switch (selection)
        {
                // Open text in notepad
                case '8' : {
                        notepad();
                        break;
                }
                // Exit program
                case '0' : {
                        cout<<"- Please close Notepad to exit - ";
                        return 0;
                }
                // Ask questions and place answers into AI.txt
                case '9' : {
                        question();
                        break;
                }
                // Simulate reloading program,
                case '7' : {
                        reload();
                        break;
                }
                // Clock
                case '6' : {
                        do {
                                time();
                                cout<<"Press Enter to exit";
                                Sleep(750);
                        } while(_getch() == '\n');
                        break;
                }
                // Invalid response
                default : {
                        cout<<"Nope"<<endl;
                        break;
                }
        }
        return 1;
}
void CEditor::question (void) {
        // Open file and move to end of file.
        myFile.open("AI.txt",ios_base::app);
        myFile<<"\n----- new entry -----\n";
        // Clear console and ask questions.
        system ("CLS");
        cout<<"AI: What is your name?"<<endl;
        cin>>name;
        system ("CLS");
        myFile<<"Datalog"<<"\nName: "<<name<<endl;
 
        cout<<"AI: How are you feeling?"<<endl<<name<<": ";
        cin>>response;
        system ("CLS");
        if (response == character) {
                cout<<"Invalid answer";
        }
        myFile<<name<<" is feeling "<<response<<endl;
        cout<<"AI: What is your age "<<name<<"?"<<endl<<name<<": ";
        cin>>age;
        system ("CLS");
        myFile<<name<<"'s age is "<<age;
        myFile.close();
        if (! myFile)
        {
                cout<<"Error";
        }
}
void CEditor::reload (void) {
        system ("CLS");
        cout<<"Reloading";
        Sleep(1000);
        cout<<".";
        Sleep(1000);
        cout<<".";
        Sleep(1000);
        cout<<".";
}
int CEditor::time (void) {
                struct tm newtime;
        char am_pm[] = "AM";
        __time64_t long_time;
        char timebuf[26];
        errno_t err;
 
                // Clear the screen
                system("CLS");
        // Get time as 64-bit integer.
        _time64( &long_time );
        // Convert to local time.
        err = _localtime64_s( &newtime, &long_time );
        if (err)
        {
            printf("Invalid argument to _localtime64_s.");
            exit(1);
        }
        if( newtime.tm_hour > 12 )        // Set up extension.
                strcpy_s( am_pm, sizeof(am_pm), "PM" );
        if( newtime.tm_hour > 12 )        // Convert from 24-hour
                newtime.tm_hour -= 12;    // to 12-hour clock.
        if( newtime.tm_hour == 0 )        // Set hour to 12 if midnight.
                newtime.tm_hour = 12;
 
        // Convert to an ASCII representation.
        err = asctime_s(timebuf, 26, &newtime);
        if (err)
        {
           printf("Invalid argument to asctime_s.");
           exit(1);
        }
        printf( "%.19s %s\n", timebuf, am_pm );
        return 1;

}
 
int _tmain(int argc, _TCHAR* argv[])
{
        CEditor editor;
        editor.q = new CQuestion("How are you?:Good,Bad");
        cout<<editor.q->question()<<endl;
        for(int i=0;i<editor.q->answers->size();++i) {
                cout<<editor.q->answers->answer[i]<<endl;
        }
        system("pause");
		while(editor.menu());
        //while(editor.menu());
        return 0;
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1