您的位置:首页 > 编程语言 > C语言/C++

C++学习记录 (20180208)

2018-02-28 11:18 197 查看
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

double tuition = 10000;
int year = 0;
for (year = 0; tuition < 20000; year++)
{
tuition *= 1.07;
}
cout << "Tuition will be doubled in " << year << "years" << endl;
cout << setprecision(2) << fixed << showpoint << "Tuition will be $" << tuition << " in " << year << " years " << endl;
        //精度设置
return 0;

精度设置: setprecision(x)<<fixed<<showpoint<<  x 为精度
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;

int main()
{
const int NUMBER_OF_TRIALS = 1000000;
int numberOfHits = 0;
srand(time(0));

for (int i=0; i < NUMBER_OF_TRIALS; i++)
{
double x = rand()*2.0 / RAND_MAX - 1;
double y = rand()*2.0 / RAND_MAX - 1;//RAND_MAX是rand()能够返回的最大值,所以rand()*2.0/RAND_MAX -1 为-1至1的随机数
if (x*x + y * y <= 1)
numberOfHits++;
}
double pi = 4.0*numberOfHits / NUMBER_OF_TRIALS;
cout << " pi is " <<setprecision(5)<<fixed<<showpoint<< pi << endl;

return 0;

}

实例二:十进制转十六进制#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string> //if u use string u must include this
using namespace std;

int main()
{
cout << "Enter a decimal number: ";
int decimal;
cin >> decimal;

string hex = "";

while (decimal != 0)
{
int hexValue = decimal % 16;

char hexChar = (hexValue <= 9 && hexValue >= 0) ?
static_cast<char>(hexValue + '0'):
static_cast<char>(hexValue - 10 + 'A');

hex = hexChar + hex;
decimal = decimal / 16;
}
cout << "The hex number is " << hex << endl;

return 0;
}

实例:检查回文#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string> //if u use string u must include this
using namespace std;

int main()
{
while (true)
{
cout << "Please input a string" << endl;
string s;
getline(cin, s);

int low = 0;
int high = s.length() - 1;
bool isPalindrome = true;

while (low < high)
{
if (s[low] != s[high])
{
isPalindrome = false;
break;
}
low++;
high--;

}
if (isPalindrome)
{
cout << s << " is a palindrome" << endl;

}

else
{
cout << s << "is not a palindrome" << endl;
}

}

return 0;
}
#include "stdafx.h"
#include <iostream>
#include <iomanip> //if u use string u must include this
using namespace std;

int main()
{
const int NUMBER_OF_PRIMES = 50;
const int NUMBER_OF_PRIMES_PER_LINE = 10;
int count = 0;
int number = 2;

cout << "The first 50 prime numbers are\n";

while (count < NUMBER_OF_PRIMES)
{
bool isPrime = true;
for (int divisor = 2; divisor <= number / 2 && isPrime; divisor++)
{
if (number % divisor == 0)
{
isPrime = false;

}
}
if (isPrime)
{
count++;

if (count %NUMBER_OF_PRIMES_PER_LINE == 0)
cout << setw(4) << number << endl;  //每打印十个数字,换一次行
else
cout << setw(4) << number;

}
number++;

}
return 0;
}

打印前50个素数
exercise1:
#include "stdafx.h"
#include <iostream>
#include <iomanip> //if u use string u must include this
using namespace std;

int main()
{
cout << "Enter an integer, the input ends if it is 0: ";
int posCount = 0;
int negCount = 0;
double total = 0;
double num=1;
double sum=0;
double actualTotal = 0;
while (num != 0)
{
cin >> num;

if (num > 0)
{
total++;
actualTotal++;
posCount++;
sum += num;
}
else if (num < 0)
{
total++;
actualTotal++;
negCount++;
sum += num;
}
else if (total==1 && num==0)
{
cout << "No numbers are entered except 0 ";
break;
}
else
{

total++;
}
}

double average = sum / actualTotal;
cout << "The number of positives is " << posCount << endl;
cout << "The number of negatives is " << negCount << endl;
cout << "The total is " << total << endl;
cout << "The average is " <<average << endl;

return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: