commit c0cfaea285a885e4cc5547fab3076bcf6cc8452c Author: SomboChea Date: Tue Jan 10 00:22:00 2017 +0700 My Source test code in c and c++ diff --git a/callspend.cpp b/callspend.cpp new file mode 100644 index 0000000..8a7ee25 --- /dev/null +++ b/callspend.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +int main(){ + +//Start code; +int call; +float spent = 0; +cout<<"Enter n Call: ";cin>>call; + +if(call<=100 && call>0){ + spent = 8; +}else if(call>100 && call<=200){ + spent = 8 + (0.06*(call-100)); +}else if(call>200){ + spent = 8 + (0.06*100) + (0.04*(call-200)); +}else{ + spent = 0; +} +cout << "Amount of spent : $"< +#include +#include +#include + +using namespace std; + +void countStuff(istream& in, + int& chars, + int& words, + int& lines) { + + char cur = '\0'; + char last = '\0'; + chars = words = lines = 0; + + while (in.get(cur)) { + if (cur == '\n' || + (cur == '\f' && last == '\r')) + lines++; + else + chars++; + if (!std::isalnum(cur) && // This is the end of a + std::isalnum(last)) // word + words++; + last = cur; + } + if (chars > 0) { // Adjust word and line + if (std::isalnum(last)) // counts for special + words++; // case + lines++; + } +} + +int main(int argc, char** argv) { + + if (argc < 2) + return(EXIT_FAILURE); + + ifstream in(argv[1]); + + if (!in) + exit(EXIT_FAILURE); + + int c, w, l; + + countStuff(in, c, w, l); + cout << "chars: " << c << '\n'; + cout << "words: " << w << '\n'; + cout << "lines: " << l << '\n'; +} diff --git a/countnewline-1.cpp b/countnewline-1.cpp new file mode 100644 index 0000000..3a22e40 --- /dev/null +++ b/countnewline-1.cpp @@ -0,0 +1,75 @@ +// Keyboard keys \ | +#include +#include +#include +using std::cout; using std::endl; using std::cin; +using std::string; using std::vector; + +int main() +{ + string ch; // getting input + unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, blankCnt = 0, tabCnt = 0, newlineCnt = 0; // counters + cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)" << endl; // instructions + + while (getline(cin, ch)) + { + for (auto c : ch) + { + // if its a vowel, increment the appropriate counter + switch (c) + { + case 'a': + ++aCnt; + break; + case 'e': + ++eCnt; + break; + case 'i': + ++iCnt; + break; + case 'o': + ++oCnt; + break; + case 'u': + ++uCnt; + break; + // Capitals + case 'A': + ++aCnt; + break; + case 'E': + ++eCnt; + break; + case 'I': + ++iCnt; + break; + case 'O': + ++oCnt; + break; + case 'U': + ++uCnt; + break; + + case ' ': + ++blankCnt; + break; + case '\t': + ++tabCnt; + break; + case '\n': + ++newlineCnt; + break; + } + } + } + + cout << "Number if vowel a: \t" << aCnt << '\n' + << "Number of vowel e: \t" << eCnt << '\n' + << "Number of vowel i: \t" << iCnt << '\n' + << "Number of vowel o: \t" << oCnt << '\n' + << "Number of vowel u: \t" << uCnt << '\n' + << "Number of spaces: \t" << blankCnt << '\n' + << "Number of tabs: \t" << tabCnt << '\n' + << "Number of newlines \t" << newlineCnt << endl; + return 0; +} diff --git a/countnewline-2.cpp b/countnewline-2.cpp new file mode 100644 index 0000000..bf8f93c --- /dev/null +++ b/countnewline-2.cpp @@ -0,0 +1,33 @@ +#include +#include + +int main() +{ + std::cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)\n" ; + + constexpr char TAB = '\t' ; + constexpr char NEWLINE = '\n' ; + + std::size_t num_tabs = 0 ; + std::size_t num_newlines = 0 ; + std::size_t num_spaces = 0 ; + + char c ; + // http://en.cppreference.com/w/cpp/io/basic_istream/get + while( std::cin.get(c) ) + { + if( std::isspace(c) ) + { + switch(c) + { + case TAB: ++num_tabs ; break ; + case NEWLINE: ++num_newlines ; break ; + default: ++num_spaces ; + } + } + } + + std::cout << "#tabs: " << num_tabs << '\n' + << "#newlines: " << num_newlines << '\n' + << "#other space characters: " << num_spaces << '\n' ; +} diff --git a/countnewline.cpp b/countnewline.cpp new file mode 100644 index 0000000..2f202c8 --- /dev/null +++ b/countnewline.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +using std::cout; using std::endl; using std::cin; +using std::string; using std::vector; + +int main() +{ + char ch = 0; // getting input + unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, blankCnt = 0, tabCnt = 0, newlineCnt = 0; // counters + cout << "Type in letters, once finished press CTRL+D/Z (UNIX/WINDOWS)" << endl; // instructions + + while (cin >> ch) + { + // if its a vowel, increment the appropriate counter + switch (ch) + { + case 'a': + ++aCnt; + break; + case 'e': + ++eCnt; + break; + case 'i': + ++iCnt; + break; + case 'o': + ++oCnt; + break; + case 'u': + ++uCnt; + break; + // Capitals + case 'A': + ++aCnt; + break; + case 'E': + ++eCnt; + break; + case 'I': + ++iCnt; + break; + case 'O': + ++oCnt; + break; + case 'U': + ++uCnt; + break; + + case ' ': + ++blankCnt; + break; + case '\t': + ++tabCnt; + break; + case '\n': + ++newlineCnt; + break; + } + } + + cout << "Number if vowel a: \t" << aCnt << '\n' + << "Number of vowel e: \t" << eCnt << '\n' + << "Number of vowel i: \t" << iCnt << '\n' + << "Number of vowel o: \t" << oCnt << '\n' + << "Number of vowel u: \t" << uCnt << '\n' + << "Number of spaces: \t" << blankCnt << '\n' + << "Number of tabs: \t" << tabCnt << '\n' + << "Number of newlines \t" << newlineCnt << endl; + return 0; +} diff --git a/countword-macs.cpp b/countword-macs.cpp new file mode 100644 index 0000000..fcc27d6 --- /dev/null +++ b/countword-macs.cpp @@ -0,0 +1,59 @@ +// IEA November 96 +// Program reads a filename from cin and counts the +// number of lines in that file and the number of words. +// A word is considered to only consist of letters of +// the alphabet, any other character is treated as a +// separator or terminator (including newline) +// The words are printed as they are input. + +#include +#include +#include + +using namespace std; + +int main() +{ + char c; + char filename[20]; + int wordcount = 0; // word count + int linecount = 0; // line count + ifstream ins; + cout << "Enter name of input file: "; + cin >> filename; + ins.open(filename); + if (ins.fail()) + { + cout << "Failure on opening " << filename + << endl; + return 1; + } + ins.get(c); + while (!ins.eof()) + { + while (!isalpha(c) && !ins.eof()) + { + if (c == '\n') linecount++; + ins.get(c); + } + if (!ins.eof()) + { + while (isalpha(c) && !ins.eof()) + { + cout << c; + ins.get(c); + } + wordcount++; + cout << endl; + } + } + cout << endl << "The word count was " + << wordcount + << endl; + cout << endl << "The line count was " + << linecount + << endl; + ins.close(); + return 0; +} + diff --git a/countword.cpp b/countword.cpp new file mode 100644 index 0000000..4c7f4f1 --- /dev/null +++ b/countword.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + if (argc < 2) { + std::cerr << "Usage: count_words \n"; + return EXIT_FAILURE; + } + + std::ifstream infile(argv[1]); + + std::istream_iterator in{ infile }, end; + + std::cout << "Word count: " << std::distance(in, end); +} diff --git a/countwordwithout3.cpp b/countwordwithout3.cpp new file mode 100644 index 0000000..d7f6c31 --- /dev/null +++ b/countwordwithout3.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int main(){ + +//Start code; +int c,nc=0; +while((c=cin.get())!='0') +{ + if(c!='\n' && c!=' ' && c!='\t') + nc++; +} +cout << "Number of Characters : "< + +//using namespace std; + +struct People { +char* name; +int id; +}; + +void print(); +int Sum(int,int); +void printline(); + +int main(){ +int a,b; +std::cout<<"Enter A: ";std::cin>>a; +std::cout<<"Enter B: ";std::cin>>b; +std::cout<<"\n"; +printline(); +std::cout<<"Sum A + B : "<>p[1].name; +std::cout<<"Enter id: ";std::cin>>p[1].id; +return 0; +} + +int Sum(int a,int b){ + +return a+b; +} + +void printline(void){ +for(int i=0;i<=23;i++){ + std::cout<<"-"; +} +std::cout<<"\n"; + +} diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..558226e --- /dev/null +++ b/test.cpp @@ -0,0 +1,10 @@ +#include "iostream" + +using namespace std; + +int main(){ +int a; +cout<<"Enter a: ";cin>>a; +cout< +using namespace std; + +int main(){ +int IN, OUT=0; +int c,nl,nw,nc,state; +state =OUT; +nl = nw = nc = 0; +while((c=cin.get())!='0') +{ + nc++; + if(c=='\n') + nl++; + if(c==' ' || c=='\n' || c=='\t') + state=OUT; + else if(state=OUT&&c!='0') + { + state=IN; + nw++; + } +} +cout<<"Number of Line:"<