标签:
结构体是一个变量组,将一组变量放在一起,其实就是一个自定义的集合,里面可以包含各种类型的数据,用法和集合一样。
例如:结构体的应用,超市购物
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace 超市买东西
{
class Program
{
struct shop
{
public string shangpin;
public double danjia;
}
static void Main(string[] args)
{
double temp=0;
//显示超市欢迎您, 要购买的商品名称 单价 数量
//提示是否结账 否 循环 是结账
shop a1 = new shop();
shop a2 = new shop();
shop a3 = new shop();
shop a4 = new shop();
shop a = new shop();
a1.shangpin = "烟";
a1.danjia = 100;
a2.shangpin = "酒";
a2.danjia = 120;
a3.shangpin = "糖";
a3.danjia = 90;
a4.shangpin = "茶";
a4.danjia = 110;
ArrayList s = new ArrayList();
ArrayList zongjia = new ArrayList();
s.Add(a1);
s.Add(a2);
s.Add(a3);
s.Add(a4);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("全民超市欢迎您!本超市优惠大酬宾!\n满100元 九五折 满200元 九折 满300元 八五折 满400元 八折");
Console.ResetColor();
while (true)
{
Console.WriteLine("请输入您要购买的商品:1.烟(100) 2.酒(120) 3.糖(90) 4.茶(110)");
int s1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入您要购买的数量:");
int s2 = Convert.ToInt32(Console.ReadLine());
a = (shop)s[s1-1];//转换成shop类型结构体,赋值给a
double y = a.danjia * s2;
zongjia.Add(y);
Console.WriteLine("是否需要其他商品(Y/N)");
string s3 = Console.ReadLine();
if (s3.ToUpper()=="Y")
{
}
else
{
break;
}
}
for (int i = 0; i < zongjia.Count; i++)
{
temp = temp + Convert.ToDouble(zongjia[i].ToString());
}
Console.ForegroundColor = ConsoleColor.Green;
if (temp>=100&&temp<200)
{
temp = temp * 0.95;
Console.WriteLine("本次消费"+temp+" 享受九五折\n欢迎下次光临");
}
else if (temp>=200&&temp<300)
{
temp = temp * 0.9;
Console.WriteLine("本次消费" + temp + " 享受九折\n欢迎下次光临");
}
else if (temp>=300&&temp<400)
{
temp = temp * 0.85;
Console.WriteLine("本次消费" + temp + " 享受八五折\n欢迎下次光临");
}
else if (temp>=400)
{
temp = temp * 0.8;
Console.WriteLine("本次消费" + temp + " 享受八折\n欢迎下次光临");
}
Console.ReadLine();
}
}
}
标签:
原文地址:http://www.cnblogs.com/happinesshappy/p/4415649.html