C++ 实验一
2-28:
#include <iostream>
using namespace std;
int name()
{
char n;
cout << "Menu:A(dd) D(elete) S(ort) Q(uit),Select one:" << endl;
cin >> ;
while (n != 'Q')
{
switch (n)
{
case'A':
{
cout << "The date has been added" << endl; break;
}
case'D':
{
cout << "The data has been deleted" << endl; break;
}
case'S':
{
cout << "The data has been sorted" << endl; break;
}
}
cin >> n;
}
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char n;
cout << "Menu:A(dd) D(elete) S(ort) Q(uit),Selectone :" << endl;
cin >> n;
while (n != 'Q')
{
if (n == 'A')
{
cout << "The data has been added" << endl;
}
if (n == 'D')
{
cout << "The data has been deleted" << endl;
}
if (n == 'S')
{
cout << "The data has been sorted" << endl;
}
cin >> n;
}
system("pause");
return 0;
}

2-29:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i, j, k, flag;
i = 2;
while (i <= 100)
{
flag = 1;
int k = (int)sqrt((float)i);
j = 2;
while (j <= k)
{
if (i%j == 0)
{
flag = 0; break;
}
j++;
}
if (flag)
cout << i << "为质数。" << endl;
i++;
}
system("pause");
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i, j, k, flag;
i = 2;
do {
flag = 1;
int k = (int)sqrt((float)i);
j = 2;
do {
if (i%j == 0)
{
flag = 0; break;
}
j++;
} while (j <= k);
if (flag)
cout << i << "为质数。" << endl;
i++;
} while (i <= 100);
system("pause");
return 0;
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int i, j, k, flag;
for (i = 2; i <= 100; i++)
{
flag = 1;
int k = (int)sqrt((float)i);
for (j = 2; j <= k; j++)
{
if (i%j == 0)
{
flag = 0; break;
}
}
if (flag)
cout << i << "为质数。" << endl;
}
system("pause");
}

2-32:
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int x;
srand(time(0));
int number = rand() % 100 + 1;
cout << "猜这个数: ";
cin >> x;
while (1)
{
if (x != number)
{
if (x < number)
{
cout << "小" << endl;
}
else
{
cout << "大" << endl;
}
}
else
{
cout << "正确" << endl; break;
}
cin >> x;
}
system("pause");
return 0;
}

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int x;
srand(time(0));
int number = rand() % 100 + 1;
cout << "猜这个数: ";
cin >> x;
do
{
if (x != number)
{
if (x < number)
{
cout << "小" << endl;
}
else
{
cout << "大" << endl;
}
}
else
{
cout << "正确" << endl; break;
}
cin >> x;
} while (1);
system("pause");
return 0;
}

2-34:
#include<iostream>
#include<iomanip>
using namespace std;
void ball(int a)
{
switch (a)
{
case 1:cout << "red "; break;
case 2:cout << "yellow "; break;
case 3:cout << "blue "; break;
case 4:cout << "white "; break;
case 5:cout << "black "; break;
}
}
int main()
{
int i, j, k, n = 0;
for (i = 1; i <= 5; i++)
{
for (j = i + 1; j <= 5; j++)
{
for (k = j + 1; k <= 5; k++)
{
ball(i);
ball(j);
ball(k);
cout << endl;
n++;
}
}
}
cout << "number:" << n << endl;
system("pause");
return 0;
}


浙公网安备 33010602011771号