【发布时间】:2017-08-07 12:17:17
【问题描述】:
我们的公司办公室需要一个应用程序来维护钦奈的所有注册大学,并且该应用程序在搜索大学方面应该是用户友好的。使用以下属性创建一个名为“大学”的结构:名称、许可证号和区号。
要求:大学执照号码为6位,前2位为大写字母,后4位为数字。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct University
{
char name[100];
char license[10];
int area;
}u[10];
void main()
{
int i, n, r, k = 0, flag = 1, f2 = 1, j, search = 0;
char s[100];
printf("Enter the number of records\n");
scanf("%d", &n);
printf("Enter the details of %d universities\n", n);
for (i = 0; i<n; i++)
{
printf("Name of the University\n");
getchar();
scanf("%s", u[i].name);
j = strlen(u[i].name);
if (j <= 1)
{
f2 = 0;
break;
}
printf("License Number\n");
scanf("%s", u[i].license);
k = strlen(u[i].license);
if (k<1)
{
f2 = 0;
break;
}
if (k<6)
{
flag = 0;
}
else if ((u[i].license[0] >= 'A' && u[i].license[0] <= 'Z') && (u[i].license[1] >= 'A' && u[i].license[1] <= 'Z') && (u[i].license[2] >= '0' && u[i].license[2] <= '9') && (u[i].license[3] >= '0' && u[i].license[3] <= '9') && (u[i].license[4] >= '0' && u[i].license[4] <= '9') && (u[i].license[5] >= '0' && u[i].license[5] <= '9') && k == 6)
{
flag = 1;
}
else
{
flag = 0;
}
printf("Area Code\n");
scanf("%d", &u[i].area);
//printf("%d",u[i].area);
if (u[i].area <= 0)
{
f2 = 0;
}
}
if (flag == 0)
{
printf("Sorry! You have entered incorrect license number.");
}
else if (f2 == 0)
{
printf("Unable to continue");
}
else
{
printf("Enter the name of the University to be searched\n");
scanf("%s", s);
for (i = 0; i<n; i++)
{
if ((strcmp(u[i].name, s)) == 0)
{
search = 1;
}
}
if (search == 1)
{
printf("University is licensed one.");
}
else
{
printf("University is not found.");
}
}
}
当我给大学的编号为 3 时,它不接受第三大学的输入。
测试用例
输入 1
输入记录数
3
输入3所大学的详细信息
大学名称
SRM
许可证号
SR1234
区号
28
大学名称
马德拉斯大学
许可证号
SP0904
区号
18
大学名称
巴拉特大学
许可证号
BU0101
区号
35
输入要搜索的大学名称
SRM
输出 1
大学是获得许可的。
【问题讨论】:
-
请展示一些输入和预期/实际输出的示例。
-
整个程序逻辑错误,过于复杂。
-
使用
scanf读取用户输入的用途有限,因为它忽略了换行符。它需要更多的错误检查。特别是,如果下一个标记不是数字,%d格式将不会消耗流,而%s格式只会读取到下一个空格的字符串。如果您输入的是“New Delhi”、“AA9876”和“123”,则名称为“New”,许可证为“Delhi”,根本不会读取该区域。 -
丢掉所有的标志,改用正则表达式。
-
告诉我如何读取结构化数组中带空格的字符串,因为我多次遇到这个问题