所有变量在调用前必须被初始化。
对所有的用户输入,必须进行合法性检查。
不要比较浮点数的相等,如: 10.0 * 0.1 == 1.0 ,不可靠
程序与环境或状态发生关系时,必须主动去处理发生的意外事件,如文件能否逻辑锁定、打印机是否联机等。
单元测试也是编程的一部份,提交联调测试的程序必须通过单元测试。
对所有的用户输入,必须进行合法性检查。
不要比较浮点数的相等,如: 10.0 * 0.1 == 1.0 ,不可靠
程序与环境或状态发生关系时,必须主动去处理发生的意外事件,如文件能否逻辑锁定、打印机是否联机等。
单元测试也是编程的一部份,提交联调测试的程序必须通过单元测试。
所有程序在正常结束时都应该返回0。
函数不能嵌套定义。
唯一不需要原型声明的函数是main()。因为系统已经预定义了
函数不能嵌套定义。
唯一不需要原型声明的函数是main()。因为系统已经预定义了
getch( ); // #include <conio.h>
//求取质数2--1000
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=2; i<1000; i++)
{
for(j=2; j<=(i/j); j++)
if(!(i%j)) break;
if(j>(i/j))
cout<<i<<" is prime./n";
}
cout<<endl<<"======================="<<endl;
getchar();
return 0;
}
#include <iostream>
#include <cstdlib>
using namespace std;
#include <cstdlib>
using namespace std;
void play(int m);
int main()
{
int option;
int magic;
magic=rand();
cout<<"magic is :"<<magic<<endl;
{
int option;
int magic;
magic=rand();
cout<<"magic is :"<<magic<<endl;
do{
cout<<"1. Get a new magic number."<<endl;
cout<<"2. Play."<<endl;
cout<<"3. Quit."<<endl;
do{
cout<<"Enter your choice: "<<endl;
cin>>option;
}while(option<1 || option>3);
cout<<"1. Get a new magic number."<<endl;
cout<<"2. Play."<<endl;
cout<<"3. Quit."<<endl;
do{
cout<<"Enter your choice: "<<endl;
cin>>option;
}while(option<1 || option>3);
switch(option)
{
case 1: magic=rand(); break;
case 2: play(magic); break;
case 3: cout<<"Goodbye."<<endl; break;
}
}while(option!=3);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
{
case 1: magic=rand(); break;
case 2: play(magic); break;
case 3: cout<<"Goodbye."<<endl; break;
}
}while(option!=3);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
void play(int m)
{
int t,x;
for(t=0; t<100; t++)
{
cout<<"Guess the number: ";
cin>>x;
if(x==m)
{
cout<<"**Right**"<<endl;
return;
}
else
{
if(x<m) cout<<"Too low."<<endl;
else cout<<"Too high."<<endl;
}
}
cout<<"You've used up all your guesses. try again."<<endl;
}
{
int t,x;
for(t=0; t<100; t++)
{
cout<<"Guess the number: ";
cin>>x;
if(x==m)
{
cout<<"**Right**"<<endl;
return;
}
else
{
if(x<m) cout<<"Too low."<<endl;
else cout<<"Too high."<<endl;
}
}
cout<<"You've used up all your guesses. try again."<<endl;
}
#include <iostream>要在#include <conio.h>之前
//--程序#4 用冒泡排序法对数组排序
#include <iostream>
#include <cstdlib>
using namespace std;
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int nums[100];
int a,b,t;
int size;
size=100;
for(t=0; t<size; t++)
nums[t]=rand();
cout<<"Original array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
{
int nums[100];
int a,b,t;
int size;
size=100;
for(t=0; t<size; t++)
nums[t]=rand();
cout<<"Original array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
for(a=1; a<size; a++)//遍历size-1次
for(b=size-1; b>=a; b--)//每次找最小值的遍历次数
{
if(nums[b-1]>nums[b])
{
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
cout<<"Sorted array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
for(b=size-1; b>=a; b--)//每次找最小值的遍历次数
{
if(nums[b-1]>nums[b])
{
t=nums[b-1];
nums[b-1]=nums[b];
nums[b]=t;
}
}
cout<<"Sorted array is: /n";
for(t=0; t<size; t++)
cout<<nums[t]<<' ';
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//--程序#13 4个字符串处理函数
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
char s1[80],s2[80];
cout<<"Enter two strings: ";
gets(s1);
gets(s2);
{
char s1[80],s2[80];
cout<<"Enter two strings: ";
gets(s1);
gets(s2);
cout<<"lengths: "<<strlen(s1)<<' '<<strlen(s2)<<endl;
if(!strcmp(s1,s2))
cout<<"The strings are equal./n";
else
cout<<"not equal./n";
cout<<"The strings are equal./n";
else
cout<<"not equal./n";
strcat(s1,s2);
cout<<"s1: "<<s1<<' '<<"s2: "<<s2<<endl;
cout<<"s1: "<<s1<<' '<<"s2: "<<s2<<endl;
strcpy(s1,s2);
cout<<s1<<" and "<<s2<<' '<<"are now the same./n";
cout<<s1<<" and "<<s2<<' '<<"are now the same./n";
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//getchar();
return 0;
}
//--程序#19 一个简单的雇员数据库程序
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
char name[10][80];
char phone[10][20];
float hours[10];
float wage[10];
char phone[10][20];
float hours[10];
float wage[10];
int menu();
void enter(),report();//这么声明函数
void enter(),report();//这么声明函数
int main()
{
int choice;
do{
choice=menu();
switch(choice){
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default:cout<<"Try again./n/n";
}
}while(choice!=0);
{
int choice;
do{
choice=menu();
switch(choice){
case 0: break;
case 1: enter();
break;
case 2: report();
break;
default:cout<<"Try again./n/n";
}
}while(choice!=0);
cout<<endl<<"======================="<<endl;
//getchar();
return 0;
}
//getchar();
return 0;
}
int menu()
{
int choice;
cout<<"0. Quit./n";
cout<<"1. Enter information./n";
cout<<"2. Report information./n";
cout<<"/nChoose one: ";
cin>>choice;
return choice;
}
{
int choice;
cout<<"0. Quit./n";
cout<<"1. Enter information./n";
cout<<"2. Report information./n";
cout<<"/nChoose one: ";
cin>>choice;
return choice;
}
void enter()
{
int i;
for(i=0;i<10;i++){
cout<<"Enter last name: ";
cin>>name[i];
cout<<"Enter phone number: ";
cin>>phone[i];
cout<<"Enter number of hours worked: ";
cin>>hours[i];
cout<<"Enter wage: ";
cin>>wage[i];
}//for
}
{
int i;
for(i=0;i<10;i++){
cout<<"Enter last name: ";
cin>>name[i];
cout<<"Enter phone number: ";
cin>>phone[i];
cout<<"Enter number of hours worked: ";
cin>>hours[i];
cout<<"Enter wage: ";
cin>>wage[i];
}//for
}
void report()
{
int i;
for(i=0;i<10;i++){
cout<<name[i]<<' '<<phone[i]<<endl;
cout<<"Pay for the week: "<<wage[i]*hours[i]<<endl;
}//for
}
{
int i;
for(i=0;i<10;i++){
cout<<name[i]<<' '<<phone[i]<<endl;
cout<<"Pay for the week: "<<wage[i]*hours[i]<<endl;
}//for
}