C++实验课总结 张涛 实验一:简单c++程序设计 一、 1、 猜价格游戏 编写 C++程序完成以下功能: (1) 假定有一件商品,程序用随机数指定该商品的价格(1-1000 的整数); (2) 提示用户猜价格,并输入:若用户猜的价格比商品价格高或低,对用户作出相应的提示; (3) 直到猜对为止,并给出提示。 #include #include using namespace std;//使用命名空间 std. int main() { srand(time(NULL));//将时钟数作为随机数种子 int price,guess; price = rand() % 1000 + 1;//取余,获得 1~1000 的随机数 cout << "please input the price:" << endl; do{ cin >> guess;//记录玩家所输入数值 if(guess > price) cout << "The price is lower than your guess,please input the price again:" << endl; if(guess < price) cout << "The price is higher than your guess,please input the price again:" << endl;//给予玩家比较大小的提示 } while (guess != price);//循环结构 cout << "The price you give is right.It cost " << price << "." < #include #include//两种输入流 using namespace std; int isprime(int a) {int b = 2; while(a % b!=0 && b < sqrt(a)) {b++; } if(a % b==0) return 0; if(a % b!=0) return 1; }//判断是否为素数的函数定义 int main() { int num,a; ofstream myFile("d:\\result.txt"); cout << "Please input the number:" << endl; cin >> num ; myFile << "2 "; for(a = 2;a <= num;a++)//例举所有数,并对其进行判断,是否为素数 {if(isprime(a)==1) myFile << a <<" "; }//向文件内输出数据 system("pause"); return 0; } 3、 袋中取球 编写C++程序完成以下功能(使用 enum): (1) 袋子中有 red, yellow, blue, white, black 五种颜色的球多个; (2) 一次从袋子里取出3 个颜色不同的球,有几种取法; (3) 将每种方法...