2021-09-07 照书码——_——

1.称水果。

#include<iostream>
using namespace std;

double capital=0.0;
double cal(int num,double quantity)
{
	switch(num)
	{
		case 0:return quantity*1.50;
		case 1:return quantity*1.70;
		case 2:return quantity*4.60;
		case 3:return quantity*0.90;
		case 4:return quantity*2.50;
		case 5:return quantity*3.70;
		case 6:return quantity*7.60;
		case 7:return quantity*3.90;
	}
 } 
 
int main()
{
	cout<<"Reminder:This shop provides eight kinds of fruits,as follows (the number and name of the fruit are on the left,and the unit price is on the right)."<<endl;
	cout<<"0 Watermelon 1.50 \t 1 Papaya 1.70  \t 2 Cantaloupe 4.60 \t 3 Apple 0.9  \t 4 Pear 2.50 \t 5 Grapefruit 3.70 \t 6 Kiwi 7.60 \t 7 Hawthorn 3.9 "<<endl;
	
	int num_choice=0;
	while(1)
	{
		int Y_N;
		cout<<"Please enter the quantity of the type you want to buy:";
		cin>>num_choice;
		
		if(num_choice==0)
		{
			cout<<"Are you sure you just have a look?Would you want to buy some?Please press 1 to continue,otherwise press 0 to exit directly."<<endl;
			cin>>Y_N;
			if(Y_N==0)
			{
				cout<<"Exit successfully. "<<endl;
				break;
			}
			else if(Y_N==1)
			{
			}
			else
				cout<<"Don't make trouble,lose according to the regulations,dear."<<endl;
		}
		else
		{
			cout<<"The category numbers are:";
			int *choice=new int[num_choice];
			double *quantity_choice=new double[num_choice];
			for(int i=0;i<num_choice;i++)
				cin>>choice[i];
			cout<<"Please enter the quantity corresponding to the purchase type:";
			for(int i=0;i<num_choice;i++)
				cin>>quantity_choice[i];
			
			for(int j=0;j<num_choice;j++)
			{
				capital+=cal(choice[j],quantity_choice[j]);
			}
			
			cout<<"Continue shopping?Please press 1 to continue,otherwise press 0,start settlement."<<endl;
			cin>>Y_N;
			if(Y_N==0)
			{
				cout<<"Start settlement,please pay "<<capital<<" yuan.";
				break;
			}
			else if(Y_N==1)
			{
			}
			else
				cout<<"Don't make trouble,lose according to the regulations,dear."<<endl;
			
			delete[] choice;
			choice=NULL;
			delete[] quantity_choice;
			quantity_choice=NULL;
		}
	}
	
	return 0;
}

2.Huffman编码

#include<iostream>
#include<cstring>
using namespace std;

struct huffTree
{
	int parent;
	int lchild;
	int rchild;
	int weight;
	string flag; 
};

struct Lowest_Node
{
	char ch;
	int ch_num;
};

void coding(int length,huffTree tree[],int n,int &a,int &b)
{
	int i;
	int r,s;
	r=s=length;
	for(i=0;i<n;i++)
	{
		if((tree[i].weight<r)&&(tree[i].parent==-1))
		{
			r=tree[i].weight;
			a=i;
		}
	}
	for(i=0;i<n;i++)
	{
		if((tree[i].weight<s)&&(i!=a)&&(tree[i].parent==-1))
		{
			s=tree[i].weight;
			b=i;
		}
	}
}
 
void frequency(string str)
{
	int length=str.length();
	Lowest_Node*node=new Lowest_Node[length];
	
	int i,j;
	for(i=0;i<length;i++)
		node[i].ch_num=0;
		
	int char_type_num=0;
	for(i=0;i<length;i++)
	{
		for(j=0;j<char_type_num;j++)
			if(str[i]==node[j].ch||(node[j].ch>='a'&&node[j].ch<='z'&&str[i]+32==node[j].ch))
				break;
				
		if(j<char_type_num)
			node[j].ch_num++;
		else
		{
			if(str[i]>='A'&&str[i]<='Z')
				node[j].ch=str[i]+32;
			else
				node[j].ch=str[i];
			node[j].ch_num++;
			char_type_num++;
		}
	}
	for(i=0;i<char_type_num;i++)
		{
		 	for(j=i;j<char_type_num;j++)
		 	{
		 		if(node[j].ch_num<node[j+1].ch_num)
		 		{
		 			int temp;
		 			char ch_temp;
		 			temp=node[j].ch_num;
		 			ch_temp=node[j].ch;
		 			node[j].ch_num=node[j+1].ch_num;
		 			node[j].ch=node[j+1].ch;
		 			node[j+1].ch_num=temp;
		 			node[j+1].ch=ch_temp;
				 }
			 }
		} 
	  	for(i=0;i<char_type_num;i++)
	  	cout<<"character"<<node[i].ch<<"apppeared"<<node[i].ch_num<<"times"<<endl; 	
	  	
	huffTree *huff=new huffTree[2*char_type_num-1];
	huffTree temp;
	string *code=new string[2*char_type_num-1];
	
	for(i=0;i<2*char_type_num-1;i++)
	{
		huff[i].lchild=-1;
		huff[i].parent=-1;
		huff[i].rchild=-1;
		huff[i].flag=-1;
	}
	for(j=0;j<char_type_num;j++)
	{
		huff[j].weight=node[j].ch_num;
	}
	int min1,min2;
	for(int k=char_type_num;k<2*char_type_num-1;k++)
	{
		coding(length,huff,k,min1,min2);
		huff[min1].parent=k;
		huff[min2].parent=k;
		huff[min1].flag="0";
		huff[min2].flag="1";
		huff[k].lchild=min1;
		huff[k].rchild=min2;
		huff[k].weight=huff[min1].weight+huff[min2].weight;
	}
	for(i=0;i<char_type_num;i++)
	{
		temp=huff[i];
		while(1)
		{
			code[i]=temp.flag+code[i];
			temp=huff[temp.parent];
			if(temp.parent==-1)
				break;
				
		}
	}
	cout<<"The Huffman encoding of each character of the string is:"<<endl;
	for(i=0;i<char_type_num;i++)
		cout<<node[i].ch<<" "<<code[i]<<endl;
	
	cout<<"The Huffman encoding of the entire string is:"<<endl;
	
	for(i=0;i<length;i++)
	{
		for(j=0;j<char_type_num;j++)
		{
			if(str[i]==node[j].ch)
				cout<<code[j];
		}
		
	 } 
	 
	 delete[] node;
	 node=NULL;
	 delete[] huff;
	 huff=NULL;
	 delete[] code;
	 code=NULL;
}
	 int main()
	 {
	 	int length=0;
	 	string str;
	 	cout<<"please enter a string:"<<endl;
		cin>>str;
		frequency(str);
		
		return 0; 
	 }


3.图书馆管理系统

#include<iostream>
#include<cstring>
using namespace std;

struct book
{
	char ch_num[3];
	char ch_name[100];
	char ch_author[10];
	char ch_publisher[50];
};

int main()
{
	book Book[100];
	int number=5;
	strcpy(Book[0].ch_num,"00");
	strcpy(Book[0].ch_name,"WeChat,make money by playing like this");
	strcpy(Book[0].ch_author,"wang_yi");
	strcpy(Book[0].ch_publisher,"Machinery industry Press");
	strcpy(Book[1].ch_num,"01");
	strcpy(Book[1].ch_name,"WeChat Marketing Confidential");
	strcpy(Book[1].ch_author,"cheng");
	strcpy(Book[1].ch_publisher,"Machinery industry Press");
	strcpy(Book[2].ch_num,"02");
	strcpy(Book[2].ch_name,"C++ Primer");
	strcpy(Book[2].ch_author,"li_pu");
	strcpy(Book[2].ch_publisher,"Electronic Industry Press");
	strcpy(Book[3].ch_num,"03");
	strcpy(Book[3].ch_name," Crazy Android handouts");
	strcpy(Book[3].ch_author,"li_gang");
	strcpy(Book[3].ch_publisher,"Electronic industry Press");
	strcpy(Book[4].ch_num,"04");
	strcpy(Book[4].ch_name,"Programing");
	strcpy(Book[4].ch_author,"tan_hao");
	strcpy(Book[4].ch_publisher,"Tsinghua University Press");
	cout<<"______________________________Library management system_______________________________________"<<endl;
	cout<<"The books in stock now hava numbers 00-04,and the book information has the following contents:"<<endl;
	cout<<"Book number__________book name_______book author________book publisher"<<endl;
	int choice;
	int del_num;
	int modify_num;
	int inquiry_num;
	bool cycle=true;
	while(cycle)
	{
		cout<<"Information of "<<number<<" books in current inerntory."<<endl;
		cout<<"Please select the function you want to operate,please correspond to the function selection button:"<<endl;
		cout<<"0-exit."<<endl;
		cout<<"1-add."<<endl;
		cout<<"2-delete."<<endl;
		cout<<"3-modify."<<endl;
		cout<<"4-inquiry."<<endl;
		
		cin>>choice;
		switch(choice)
		{
			case 0:cout<<"Exit successfully.";
				   cycle=false;
				   break;
			case 1:cout<<"please follow the above information to enter the information of the new book:"<<endl;
				   number++;
				   cin>>Book[number].ch_num>>Book[number].ch_name>>Book[number].ch_author>>Book[number].ch_publisher;
				   break;
			case 2:cout<<"Which message to delete";
				cin>>del_num;
				number--;
				if(del_num>number)
					cout<<"No such information."<<endl;
				else if(del_num==number)
				{
					strcpy(Book[del_num].ch_author,"");
					strcpy(Book[del_num].ch_name,"");
					strcpy(Book[del_num].ch_publisher,"");
					strcpy(Book[del_num].ch_num,"");
				}
				else
				{
					for(int i=del_num;i<number;i++)
					{
						strcpy(Book[i].ch_author,Book[i+1].ch_author);
						strcpy(Book[i].ch_name,Book[i+1].ch_name);
						strcpy(Book[i].ch_publisher,Book[i+1].ch_publisher);
						strcpy(Book[i].ch_num,Book[i+1].ch_num);
					}
				}
				break;
			case 3:cout<<"Which message to modify:";
				   cin>>modify_num;
				   if(modify_num>number)
				   		cout<<"No such information."<<endl;
				   else
				   {
				   		cout<<"Enter the new content in order,if part of it is modified,you can enter the original information as it is."<<endl;
				   		cin>>Book[modify_num-1].ch_num>>Book[modify_num-1].ch_name>>Book[modify_num-1].ch_author>>Book[modify_num-1].ch_publisher;
				   }
				   break;
			case 4:cout<<"Which message to inquiry:";
				   cin>>inquiry_num;
				   cout<<Book[inquiry_num].ch_num<<Book[inquiry_num].ch_name<<Book[inquiry_num].ch_author<<Book[inquiry_num].ch_publisher;
				   break;
			default:break;
		}
	}
	return 0;
}

4,算数优先级。

#include <iostream>
using namespace std;

int level(char ch)
{
	switch( ch)
	{
		case '!':return 2;break;
		case '*':return 3;break;
		case '/':return 3;break;
		case '%':return 3;break;
		case '+':return 4;break;
		case '-':return 4;break;
		case '=':return 5;break;
		default:return -1;break;
	}
}
int main()
{
	char symbol[3];
	int value[3];
	cout<<"From!、*、/、%、+、-、=choose three of the operators to participate in the operation."<<endl;
	cin>>symbol[0]>>symbol[1]>>symbol[2];
	
	value[0]=level(symbol[0]);
	value[1]=level(symbol[1]);
	value[2]=level(symbol[2]);
	
	if((value[0]==-1)||(value[1]==-1)||(value[2]==-1))
		cout<<"Operators that are not entered in the library."<<endl;
	else
	{
		for(int i=0;i<3-1;i++)
		{
			for(int j=i+1;j<3;j++)
			{
				if(value[i]>value[j])
				{
					char temp;
					temp=symbol[i];
					symbol[i]=symbol[j];
					symbol[j]=temp;
				}
			}
		}
		cout<<"The order of operations is:"<<endl;
		for(int k=0;k<3;k++)
			cout<<symbol[k]<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值