
例:
#include<stdio.h>/*standardinput&output*/
#include<stdlib.h>/*standardlibary*/
#include<string.h>/*string*/
#include<conio.h>/*ConsoleInput/Output*/
#include<time.h>
structstudentinfo/*学生信息的结构体*/
{
charsNo[5];/*学生编号*/
charsxueNo[14];/*学号*/
charsname[20];/*学生的姓名*/
}st[100];
charhash[100]={0};/*链表的数组*/
intmain()
{
inti=0,j=0,flag=0,RN,*a;
FILE*fp;
charch,filename[20]={0},line[100]={0};
printf("Pleaseinputfilename:");
//fflush(stdin);/*用来清空输入缓存,以便不影响后面输入的东西*/
gets(filename);/*键盘输入文件名*/
fp=fopen(filename,"r");/绝笑*openreadonly*/
printf("名单如下:\n");/*显示所有的学生信息*/
while(fgets(line,sizeof(line)-1,fp))
{
if(line[0]!='\n'&&line[0]!='')
{
sscanf(line,"%s%s%s\n",st[i].sNo,st[凳如i].sxueNo,st[i].sname);/*文件输入*/
printf("%s\n%s\n%s\n",st[i].sNo,枣宏启st[i].sxueNo,st[i].sname);/*打印出来*/
i++;/*统计人数*/
}
}
/*设置随机数种子*/
srand((unsigned)time(NULL));
/*sizeof(类型符)是计算类型所占字节数,sizeof(int)是int所占字节数,再乘以i,得到i个int型数据的总字节数。malloc函数用于动态开辟一块内存空间,参数为开辟的内存空间字节数,返回开辟的内存空间的首地址指针。*/
a=(int*)malloc(sizeof(int)*i);
memset(a,-1,sizeof(a));/*将已开辟内存空间a的第4个字节设置为-1*/
printf("按空格键点名,其他键退出:");
fflush(stdin);
while((ch=getch())=='')
/*while(!(ch=getch())==NULL)*/
{
if(flag==i)/*如果flag等于总人数*/
{
printf("%s\n","点名结束");
break;
}
RN=rand()%i;/*产生一个随机数*/
while(hash[RN]==1)/*判断有没有完成某个一个学生点名*/
RN=rand()%i;/*产生随机数*/
flag++;/*计数*/
printf("\n~~~~~\n%s\n%s\n%s\n------------\n",st[RN].sNo,st[RN].sxueNo,st[RN].sname);/*输出学生的信息*/
hash[RN]=1;
}
}
扩展资料:printf函数使用注意事项
1、域宽
%d:按整型数据的实际长度输出。
如果想输出指定宽度可以指定域宽,%md-->m域宽,打印出来以后,在控制台上,显示m位;
如果我们要打印的数的位数如果超过我们设定m则原样输出;
如果我们要打印的数的位数如果小于我们设定的位数,则补空白,具体如下:
如果m为正数,则左对齐(左侧补空白);
如果m为负数,则右对齐(右侧补空白)。
2、转义字符
如果想输出字符"%",则应该在“格式控制”字符串中用连续两个%表示。
如:printf("%f%%",1.0/3);输出结果:0.333333%。
Form1.Designer.cs的代码:namespace 随机点名
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing &&(components != null))
{
components.Dispose()
}
base.Dispose(disposing)
}
#region Windows 窗体设计器生成的代码汪烂
/// <summary>
/// 设计器绝伏支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的并陵携内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container()
this.button1 = new System.Windows.Forms.Button()
this.txt = new System.Windows.Forms.TextBox()
this.lbl = new System.Windows.Forms.Label()
this.lbl1 = new System.Windows.Forms.Label()
this.lbl2 = new System.Windows.Forms.Label()
this.button2 = new System.Windows.Forms.Button()
this.tm = new System.Windows.Forms.Timer(this.components)
this.richTextBox1 = new System.Windows.Forms.RichTextBox()
this.label1 = new System.Windows.Forms.Label()
this.SuspendLayout()
//
// button1
//
this.button1.Location = new System.Drawing.Point(171, 12)
this.button1.Name = "button1"
this.button1.Size = new System.Drawing.Size(75, 23)
this.button1.TabIndex = 0
this.button1.Text = "添加名单"
this.button1.UseVisualStyleBackColor = true
this.button1.Click += new System.EventHandler(this.button1_Click)
//
// txt
//
this.txt.Location = new System.Drawing.Point(52, 12)
this.txt.Name = "txt"
this.txt.Size = new System.Drawing.Size(93, 21)
this.txt.TabIndex = 1
//
// lbl
//
this.lbl.AutoSize = true
this.lbl.BackColor = System.Drawing.Color.Lime
this.lbl.Font = new System.Drawing.Font("楷体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)))
this.lbl.ForeColor = System.Drawing.Color.Red
this.lbl.Location = new System.Drawing.Point(234, 110)
this.lbl.Name = "lbl"
this.lbl.Size = new System.Drawing.Size(75, 20)
this.lbl.TabIndex = 2
this.lbl.Text = "OOOOOO"
//
// lbl1
//
this.lbl1.AutoSize = true
this.lbl1.BackColor = System.Drawing.SystemColors.Control
this.lbl1.Font = new System.Drawing.Font("楷体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)))
this.lbl1.Location = new System.Drawing.Point(234, 70)
this.lbl1.Name = "lbl1"
this.lbl1.Size = new System.Drawing.Size(75, 20)
this.lbl1.TabIndex = 3
this.lbl1.Text = "OOOOOO"
//
// lbl2
//
this.lbl2.AutoSize = true
this.lbl2.BackColor = System.Drawing.SystemColors.Control
this.lbl2.Font = new System.Drawing.Font("楷体", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)))
this.lbl2.Location = new System.Drawing.Point(234, 147)
this.lbl2.Name = "lbl2"
this.lbl2.Size = new System.Drawing.Size(75, 20)
this.lbl2.TabIndex = 4
this.lbl2.Text = "OOOOOO"
//
// button2
//
this.button2.Location = new System.Drawing.Point(300, 214)
this.button2.Name = "button2"
this.button2.Size = new System.Drawing.Size(75, 23)
this.button2.TabIndex = 5
this.button2.Text = "开始"
this.button2.UseVisualStyleBackColor = true
this.button2.Click += new System.EventHandler(this.button2_Click)
//
// tm
//
this.tm.Tick += new System.EventHandler(this.tm_Tick)
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(22, 55)
this.richTextBox1.Name = "richTextBox1"
this.richTextBox1.Size = new System.Drawing.Size(100, 140)
this.richTextBox1.TabIndex = 6
this.richTextBox1.Text = "名单"
//
// label1
//
this.label1.AutoSize = true
this.label1.Location = new System.Drawing.Point(187, 117)
this.label1.Name = "label1"
this.label1.Size = new System.Drawing.Size(41, 12)
this.label1.TabIndex = 7
this.label1.Text = "中奖者"
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F)
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
this.ClientSize = new System.Drawing.Size(410, 261)
this.Controls.Add(this.label1)
this.Controls.Add(this.richTextBox1)
this.Controls.Add(this.button2)
this.Controls.Add(this.lbl2)
this.Controls.Add(this.lbl1)
this.Controls.Add(this.lbl)
this.Controls.Add(this.txt)
this.Controls.Add(this.button1)
this.Name = "Form1"
this.Text = "Form1"
this.Load += new System.EventHandler(this.Form1_Load)
this.ResumeLayout(false)
this.PerformLayout()
}
#endregion
private System.Windows.Forms.Button button1
private System.Windows.Forms.TextBox txt
private System.Windows.Forms.Label lbl
private System.Windows.Forms.Label lbl1
private System.Windows.Forms.Label lbl2
private System.Windows.Forms.Button button2
private System.Windows.Forms.Timer tm
private System.Windows.Forms.RichTextBox richTextBox1
private System.Windows.Forms.Label label1
}
}
Form1.cs的代码:
using System
using System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Linq
using System.Text
using System.Threading.Tasks
using System.Windows.Forms
namespace 随机点名
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
}
String[] str=new String[100]
int n = 0
private void button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txt.Text)) {
MessageBox.Show("输入为空!")
}
else {
str[n] = txt.Text
richTextBox1.AppendText(str[n]+"\n")
MessageBox.Show("添加成功!")
n++
}
}
int l = 0
private void button2_Click(object sender, EventArgs e)
{
if (l == 0) {
tm.Start()
button2.Text = "停止"
l = 1
}
else if (l == 1) {
tm.Stop()
button2.Text="开始"
l = 0
}
}
static int k = 0
private void tm_Tick(object sender, EventArgs e)
{
if (n <3)
{ tm.Stop()
MessageBox.Show("名单太少,抽奖方式太浪费")
button2.Text = "开始"
}
else
{
if (k == 0) {
lbl1.Text = str[n-1]
lbl.Text = str[k]
lbl2.Text = str[k+1]
k++
}
else if (k >0&&k<n-1 )
{
lbl1.Text = str[k-1]
lbl.Text = str[k]
lbl2.Text = str[k+1 ]
k++
}
else {
lbl.Text = str[k]
lbl1.Text = str[n-2]
lbl2.Text = str[0]
k = 0
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.AppendText("\n")
}
}
}
Program.cs的代码:
using System
using System.Collections.Generic
using System.Linq
using System.Threading.Tasks
using System.Windows.Forms
namespace 随机点名
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
Application.Run(new Form1())
}
}
}
////给分吧,我的q2472591219,若需要加我,我将实例打包给你
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)