用C语言写一段统计点餐数量并排序的代码
时间: 2023-03-22 13:03:01 浏览: 60
很高兴能够回答您的问题。下面是一段用C语言统计点餐数量并排序的代码: #include<stdio.h>
int main()
{
int n,i,j,a[50],temp;
printf("Enter the number of orders: ");
scanf("%d",&n);
printf("Enter %d orders: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("The sorted order list is: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
相关问题
用c语言写一个电子点餐系统
好的,一个完整的电子点餐系统需要很多模块,包括前端界面、后端数据库、订单管理等等。但是在本次回答中,我将简单介绍如何使用 C 语言编写一个基本的电子点餐系统。
首先,我们需要定义菜单和订单结构体,如下所示:
```c
// 菜单结构体
struct menu {
char name[20]; // 菜名
int price; // 价格
};
// 订单结构体
struct order {
char name[20]; // 顾客姓名
int num; // 菜品数量
struct menu dish[20]; // 菜品信息
int total; // 订单总价
};
```
然后,我们需要编写菜单的选择和订单的生成及计算代码。下面是一个简单的示例代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
struct menu menu_list[5]; // 菜单列表
struct order customer_order; // 顾客订单
// 初始化菜单列表
strcpy(menu_list[0].name, "鱼香肉丝");
menu_list[0].price = 30;
strcpy(menu_list[1].name, "宫保鸡丁");
menu_list[1].price = 35;
strcpy(menu_list[2].name, "回锅肉");
menu_list[2].price = 40;
strcpy(menu_list[3].name, "水煮鱼");
menu_list[3].price = 50;
strcpy(menu_list[4].name, "麻婆豆腐");
menu_list[4].price = 25;
// 顾客点餐
printf("欢迎光临,请问你叫什么名字?\n");
scanf("%s", customer_order.name);
printf("请问你要点多少道菜?\n");
scanf("%d", &customer_order.num);
printf("请问你要点哪些菜?\n");
for (int i = 0; i < customer_order.num; i++) {
printf("请输入第%d道菜的编号:\n", i+1);
int choice;
scanf("%d", &choice);
customer_order.dish[i] = menu_list[choice-1];
customer_order.total += menu_list[choice-1].price;
}
// 打印订单
printf("\n顾客姓名:%s\n", customer_order.name);
printf("菜品数量:%d\n", customer_order.num);
printf("菜品列表:\n");
for (int i = 0; i < customer_order.num; i++) {
printf("%d. %s\t\t%d元\n", i+1, customer_order.dish[i].name, customer_order.dish[i].price);
}
printf("订单总价:%d元\n", customer_order.total);
return 0;
}
```
通过上面的代码,我们可以让顾客选择菜品,并且生成订单并计算总价。当然,这只是一个非常简单的示例代码,实际的电子点餐系统需要包括更多的功能和细节处理。
用c语言写一段冒泡排序代码
以下是用C语言实现的冒泡排序代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
在这段代码中,我们首先定义了一个 `bubbleSort` 函数,它接受一个整数数组和数组的大小作为参数。内部的两个嵌套循环用于比较和交换相邻的元素,以使整个数组按升序排序。接下来在主函数中声明一个整数数组并将其传递给 `bubbleSort` 函数进行排序。最后,我们使用简单的循环打印已排序的数组。