My Source test code in c and c++

This commit is contained in:
Sambo Chea 2017-01-10 00:22:00 +07:00
commit c0cfaea285
11 changed files with 424 additions and 0 deletions

24
callspend.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
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 : $"<<spent<<endl;
//End code;
return 0;
}

51
countNumberChar.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
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';
}

75
countnewline-1.cpp Normal file
View File

@ -0,0 +1,75 @@
// Keyboard keys \ |
#include <iostream>
#include <string>
#include <vector>
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;
}

33
countnewline-2.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <cctype>
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' ;
}

71
countnewline.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <iostream>
#include <string>
#include <vector>
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;
}

59
countword-macs.cpp Normal file
View File

@ -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 <iostream>
#include <fstream>
#include <ctype.h>
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;
}

19
countword.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <fstream>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <string>
#include <cstdlib>
int main(int argc, char **argv) {
if (argc < 2) {
std::cerr << "Usage: count_words <infile>\n";
return EXIT_FAILURE;
}
std::ifstream infile(argv[1]);
std::istream_iterator<std::string> in{ infile }, end;
std::cout << "Word count: " << std::distance(in, end);
}

16
countwordwithout3.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <iostream>
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 : "<<nc<<endl;
//End code;
}

40
myc.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
//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 : "<<Sum(a,b)<<std::endl;
printline();
struct People p[100];
std::cout<<"Enter name: ";std::cin>>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";
}

10
test.cpp Normal file
View File

@ -0,0 +1,10 @@
#include "iostream"
using namespace std;
int main(){
int a;
cout<<"Enter a: ";cin>>a;
cout<<a<<endl;
return 0;
}

26
whilewordcount.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
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:"<<nl<<endl;
cout<<"Number of Word:"<<nw<<endl;
cout<<"Number of Charactor:"<<nc<<endl;
return 0;
}